1*412f47f9SXin Li /*
2*412f47f9SXin Li * Header for sinf, cosf and sincosf.
3*412f47f9SXin Li *
4*412f47f9SXin Li * Copyright (c) 2018-2021, Arm Limited.
5*412f47f9SXin Li * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*412f47f9SXin Li */
7*412f47f9SXin Li
8*412f47f9SXin Li #include <stdint.h>
9*412f47f9SXin Li #include <math.h>
10*412f47f9SXin Li #include "math_config.h"
11*412f47f9SXin Li
12*412f47f9SXin Li /* 2PI * 2^-64. */
13*412f47f9SXin Li static const double pi63 = 0x1.921FB54442D18p-62;
14*412f47f9SXin Li /* PI / 4. */
15*412f47f9SXin Li static const float pio4f = 0x1.921FB6p-1f;
16*412f47f9SXin Li
17*412f47f9SXin Li /* The constants and polynomials for sine and cosine. */
18*412f47f9SXin Li typedef struct
19*412f47f9SXin Li {
20*412f47f9SXin Li double sign[4]; /* Sign of sine in quadrants 0..3. */
21*412f47f9SXin Li double hpi_inv; /* 2 / PI ( * 2^24 if !TOINT_INTRINSICS). */
22*412f47f9SXin Li double hpi; /* PI / 2. */
23*412f47f9SXin Li double c0, c1, c2, c3, c4; /* Cosine polynomial. */
24*412f47f9SXin Li double s1, s2, s3; /* Sine polynomial. */
25*412f47f9SXin Li } sincos_t;
26*412f47f9SXin Li
27*412f47f9SXin Li /* Polynomial data (the cosine polynomial is negated in the 2nd entry). */
28*412f47f9SXin Li extern const sincos_t __sincosf_table[2] HIDDEN;
29*412f47f9SXin Li
30*412f47f9SXin Li /* Table with 4/PI to 192 bit precision. */
31*412f47f9SXin Li extern const uint32_t __inv_pio4[] HIDDEN;
32*412f47f9SXin Li
33*412f47f9SXin Li /* Top 12 bits of the float representation with the sign bit cleared. */
34*412f47f9SXin Li static inline uint32_t
abstop12(float x)35*412f47f9SXin Li abstop12 (float x)
36*412f47f9SXin Li {
37*412f47f9SXin Li return (asuint (x) >> 20) & 0x7ff;
38*412f47f9SXin Li }
39*412f47f9SXin Li
40*412f47f9SXin Li /* Compute the sine and cosine of inputs X and X2 (X squared), using the
41*412f47f9SXin Li polynomial P and store the results in SINP and COSP. N is the quadrant,
42*412f47f9SXin Li if odd the cosine and sine polynomials are swapped. */
43*412f47f9SXin Li static inline void
sincosf_poly(double x,double x2,const sincos_t * p,int n,float * sinp,float * cosp)44*412f47f9SXin Li sincosf_poly (double x, double x2, const sincos_t *p, int n, float *sinp,
45*412f47f9SXin Li float *cosp)
46*412f47f9SXin Li {
47*412f47f9SXin Li double x3, x4, x5, x6, s, c, c1, c2, s1;
48*412f47f9SXin Li
49*412f47f9SXin Li x4 = x2 * x2;
50*412f47f9SXin Li x3 = x2 * x;
51*412f47f9SXin Li c2 = p->c3 + x2 * p->c4;
52*412f47f9SXin Li s1 = p->s2 + x2 * p->s3;
53*412f47f9SXin Li
54*412f47f9SXin Li /* Swap sin/cos result based on quadrant. */
55*412f47f9SXin Li float *tmp = (n & 1 ? cosp : sinp);
56*412f47f9SXin Li cosp = (n & 1 ? sinp : cosp);
57*412f47f9SXin Li sinp = tmp;
58*412f47f9SXin Li
59*412f47f9SXin Li c1 = p->c0 + x2 * p->c1;
60*412f47f9SXin Li x5 = x3 * x2;
61*412f47f9SXin Li x6 = x4 * x2;
62*412f47f9SXin Li
63*412f47f9SXin Li s = x + x3 * p->s1;
64*412f47f9SXin Li c = c1 + x4 * p->c2;
65*412f47f9SXin Li
66*412f47f9SXin Li *sinp = s + x5 * s1;
67*412f47f9SXin Li *cosp = c + x6 * c2;
68*412f47f9SXin Li }
69*412f47f9SXin Li
70*412f47f9SXin Li /* Return the sine of inputs X and X2 (X squared) using the polynomial P.
71*412f47f9SXin Li N is the quadrant, and if odd the cosine polynomial is used. */
72*412f47f9SXin Li static inline float
sinf_poly(double x,double x2,const sincos_t * p,int n)73*412f47f9SXin Li sinf_poly (double x, double x2, const sincos_t *p, int n)
74*412f47f9SXin Li {
75*412f47f9SXin Li double x3, x4, x6, x7, s, c, c1, c2, s1;
76*412f47f9SXin Li
77*412f47f9SXin Li if ((n & 1) == 0)
78*412f47f9SXin Li {
79*412f47f9SXin Li x3 = x * x2;
80*412f47f9SXin Li s1 = p->s2 + x2 * p->s3;
81*412f47f9SXin Li
82*412f47f9SXin Li x7 = x3 * x2;
83*412f47f9SXin Li s = x + x3 * p->s1;
84*412f47f9SXin Li
85*412f47f9SXin Li return s + x7 * s1;
86*412f47f9SXin Li }
87*412f47f9SXin Li else
88*412f47f9SXin Li {
89*412f47f9SXin Li x4 = x2 * x2;
90*412f47f9SXin Li c2 = p->c3 + x2 * p->c4;
91*412f47f9SXin Li c1 = p->c0 + x2 * p->c1;
92*412f47f9SXin Li
93*412f47f9SXin Li x6 = x4 * x2;
94*412f47f9SXin Li c = c1 + x4 * p->c2;
95*412f47f9SXin Li
96*412f47f9SXin Li return c + x6 * c2;
97*412f47f9SXin Li }
98*412f47f9SXin Li }
99*412f47f9SXin Li
100*412f47f9SXin Li /* Fast range reduction using single multiply-subtract. Return the modulo of
101*412f47f9SXin Li X as a value between -PI/4 and PI/4 and store the quadrant in NP.
102*412f47f9SXin Li The values for PI/2 and 2/PI are accessed via P. Since PI/2 as a double
103*412f47f9SXin Li is accurate to 55 bits and the worst-case cancellation happens at 6 * PI/4,
104*412f47f9SXin Li the result is accurate for |X| <= 120.0. */
105*412f47f9SXin Li static inline double
reduce_fast(double x,const sincos_t * p,int * np)106*412f47f9SXin Li reduce_fast (double x, const sincos_t *p, int *np)
107*412f47f9SXin Li {
108*412f47f9SXin Li double r;
109*412f47f9SXin Li #if TOINT_INTRINSICS
110*412f47f9SXin Li /* Use fast round and lround instructions when available. */
111*412f47f9SXin Li r = x * p->hpi_inv;
112*412f47f9SXin Li *np = converttoint (r);
113*412f47f9SXin Li return x - roundtoint (r) * p->hpi;
114*412f47f9SXin Li #else
115*412f47f9SXin Li /* Use scaled float to int conversion with explicit rounding.
116*412f47f9SXin Li hpi_inv is prescaled by 2^24 so the quadrant ends up in bits 24..31.
117*412f47f9SXin Li This avoids inaccuracies introduced by truncating negative values. */
118*412f47f9SXin Li r = x * p->hpi_inv;
119*412f47f9SXin Li int n = ((int32_t)r + 0x800000) >> 24;
120*412f47f9SXin Li *np = n;
121*412f47f9SXin Li return x - n * p->hpi;
122*412f47f9SXin Li #endif
123*412f47f9SXin Li }
124*412f47f9SXin Li
125*412f47f9SXin Li /* Reduce the range of XI to a multiple of PI/2 using fast integer arithmetic.
126*412f47f9SXin Li XI is a reinterpreted float and must be >= 2.0f (the sign bit is ignored).
127*412f47f9SXin Li Return the modulo between -PI/4 and PI/4 and store the quadrant in NP.
128*412f47f9SXin Li Reduction uses a table of 4/PI with 192 bits of precision. A 32x96->128 bit
129*412f47f9SXin Li multiply computes the exact 2.62-bit fixed-point modulo. Since the result
130*412f47f9SXin Li can have at most 29 leading zeros after the binary point, the double
131*412f47f9SXin Li precision result is accurate to 33 bits. */
132*412f47f9SXin Li static inline double
reduce_large(uint32_t xi,int * np)133*412f47f9SXin Li reduce_large (uint32_t xi, int *np)
134*412f47f9SXin Li {
135*412f47f9SXin Li const uint32_t *arr = &__inv_pio4[(xi >> 26) & 15];
136*412f47f9SXin Li int shift = (xi >> 23) & 7;
137*412f47f9SXin Li uint64_t n, res0, res1, res2;
138*412f47f9SXin Li
139*412f47f9SXin Li xi = (xi & 0xffffff) | 0x800000;
140*412f47f9SXin Li xi <<= shift;
141*412f47f9SXin Li
142*412f47f9SXin Li res0 = xi * arr[0];
143*412f47f9SXin Li res1 = (uint64_t)xi * arr[4];
144*412f47f9SXin Li res2 = (uint64_t)xi * arr[8];
145*412f47f9SXin Li res0 = (res2 >> 32) | (res0 << 32);
146*412f47f9SXin Li res0 += res1;
147*412f47f9SXin Li
148*412f47f9SXin Li n = (res0 + (1ULL << 61)) >> 62;
149*412f47f9SXin Li res0 -= n << 62;
150*412f47f9SXin Li double x = (int64_t)res0;
151*412f47f9SXin Li *np = n;
152*412f47f9SXin Li return x * pi63;
153*412f47f9SXin Li }
154