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