1 //===-- Single-precision sincos 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/sincosf.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 sincosf 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(void, sincosf, (float y, float *sinp, float *cosp)) {
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     double x2 = 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>(x2);
37       *sinp = y;
38       *cosp = 1.0f;
39       return;
40     }
41 
42     sincosf_poly(x, x2, p, 0, sinp, cosp);
43   } else if (abstop12(y) < abstop12(120.0f)) {
44     x = reduce_fast(x, p, &n);
45 
46     // Setup the signs for sin and cos.
47     s = p->sign[n & 3];
48 
49     if (n & 2)
50       p = &__sincosf_table[1];
51 
52     sincosf_poly(x * s, x * x, p, n, sinp, cosp);
53   } else if (likely(abstop12(y) < abstop12(INFINITY))) {
54     uint32_t xi = as_uint32_bits(y);
55     int sign = xi >> 31;
56 
57     x = reduce_large(xi, &n);
58 
59     // Setup signs for sin and cos - include original sign.
60     s = p->sign[(n + sign) & 3];
61 
62     if ((n + sign) & 2)
63       p = &__sincosf_table[1];
64 
65     sincosf_poly(x * s, x * x, p, n, sinp, cosp);
66   } else {
67     // Return NaN if Inf or NaN for both sin and cos.
68     *sinp = *cosp = y - y;
69 
70     // Needed to set errno for +-Inf, the add is a hack to work
71     // around a gcc register allocation issue: just passing y
72     // affects code generation in the fast path.
73     invalid(y + y);
74   }
75 }
76 
77 } // namespace __llvm_libc
78