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