1 //===-- Single-precision sin function -------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "math_utils.h"
10 #include "sincosf_utils.h"
11 
12 #include "src/__support/common.h"
13 #include <math.h>
14 
15 #include <stdint.h>
16 
17 namespace __llvm_libc {
18 
19 // Fast sinf implementation. Worst-case ULP is 0.5607, maximum relative
20 // error is 0.5303 * 2^-23. A single-step range reduction is used for
21 // small values. Large inputs have their range reduced using fast integer
22 // arithmetic.
LLVM_LIBC_ENTRYPOINT(sinf)23 float LLVM_LIBC_ENTRYPOINT(sinf)(float y) {
24   double x = y;
25   double s;
26   int n;
27   const sincos_t *p = &__sincosf_table[0];
28 
29   if (abstop12(y) < abstop12(pio4)) {
30     s = x * x;
31 
32     if (unlikely(abstop12(y) < abstop12(as_float(0x39800000)))) {
33       if (unlikely(abstop12(y) < abstop12(as_float(0x800000))))
34         // Force underflow for tiny y.
35         force_eval<float>(s);
36       return y;
37     }
38 
39     return sinf_poly(x, s, p, 0);
40   } else if (likely(abstop12(y) < abstop12(120.0f))) {
41     x = reduce_fast(x, p, &n);
42 
43     // Setup the signs for sin and cos.
44     s = p->sign[n & 3];
45 
46     if (n & 2)
47       p = &__sincosf_table[1];
48 
49     return sinf_poly(x * s, x * x, p, n);
50   } else if (abstop12(y) < abstop12(INFINITY)) {
51     uint32_t xi = as_uint32_bits(y);
52     int sign = xi >> 31;
53 
54     x = reduce_large(xi, &n);
55 
56     // Setup signs for sin and cos - include original sign.
57     s = p->sign[(n + sign) & 3];
58 
59     if ((n + sign) & 2)
60       p = &__sincosf_table[1];
61 
62     return sinf_poly(x * s, x * x, p, n);
63   }
64 
65   return invalid(y);
66 }
67 
68 } // namespace __llvm_libc
69