xref: /freebsd/contrib/arm-optimized-routines/math/sincosf.h (revision f3087bef11543b42e0d69b708f367097a4118d24)
131914882SAlex Richardson /*
231914882SAlex Richardson  * Header for sinf, cosf and sincosf.
331914882SAlex Richardson  *
4*f3087befSAndrew Turner  * Copyright (c) 2018-2024, Arm Limited.
5072a4ba8SAndrew Turner  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
631914882SAlex Richardson  */
731914882SAlex Richardson 
831914882SAlex Richardson #include <stdint.h>
931914882SAlex Richardson #include <math.h>
1031914882SAlex Richardson #include "math_config.h"
1131914882SAlex Richardson 
1231914882SAlex Richardson /* 2PI * 2^-64.  */
1331914882SAlex Richardson static const double pi63 = 0x1.921FB54442D18p-62;
1431914882SAlex Richardson /* PI / 4.  */
15d49ad206SAndrew Turner static const float pio4f = 0x1.921FB6p-1f;
1631914882SAlex Richardson 
1731914882SAlex Richardson /* The constants and polynomials for sine and cosine.  */
1831914882SAlex Richardson typedef struct
1931914882SAlex Richardson {
2031914882SAlex Richardson   double sign[4];		/* Sign of sine in quadrants 0..3.  */
2131914882SAlex Richardson   double hpi_inv;		/* 2 / PI ( * 2^24 if !TOINT_INTRINSICS).  */
2231914882SAlex Richardson   double hpi;			/* PI / 2.  */
2331914882SAlex Richardson   double c0, c1, c2, c3, c4;	/* Cosine polynomial.  */
2431914882SAlex Richardson   double s1, s2, s3;		/* Sine polynomial.  */
2531914882SAlex Richardson } sincos_t;
2631914882SAlex Richardson 
2731914882SAlex Richardson /* Polynomial data (the cosine polynomial is negated in the 2nd entry).  */
2831914882SAlex Richardson extern const sincos_t __sincosf_table[2] HIDDEN;
2931914882SAlex Richardson 
3031914882SAlex Richardson /* Top 12 bits of the float representation with the sign bit cleared.  */
3131914882SAlex Richardson static inline uint32_t
abstop12(float x)3231914882SAlex Richardson abstop12 (float x)
3331914882SAlex Richardson {
3431914882SAlex Richardson   return (asuint (x) >> 20) & 0x7ff;
3531914882SAlex Richardson }
3631914882SAlex Richardson 
3731914882SAlex Richardson /* Compute the sine and cosine of inputs X and X2 (X squared), using the
3831914882SAlex Richardson    polynomial P and store the results in SINP and COSP.  N is the quadrant,
3931914882SAlex Richardson    if odd the cosine and sine polynomials are swapped.  */
4031914882SAlex Richardson static inline void
sincosf_poly(double x,double x2,const sincos_t * p,int n,float * sinp,float * cosp)4131914882SAlex Richardson sincosf_poly (double x, double x2, const sincos_t *p, int n, float *sinp,
4231914882SAlex Richardson 	      float *cosp)
4331914882SAlex Richardson {
4431914882SAlex Richardson   double x3, x4, x5, x6, s, c, c1, c2, s1;
4531914882SAlex Richardson 
4631914882SAlex Richardson   x4 = x2 * x2;
4731914882SAlex Richardson   x3 = x2 * x;
4831914882SAlex Richardson   c2 = p->c3 + x2 * p->c4;
4931914882SAlex Richardson   s1 = p->s2 + x2 * p->s3;
5031914882SAlex Richardson 
5131914882SAlex Richardson   /* Swap sin/cos result based on quadrant.  */
5231914882SAlex Richardson   float *tmp = (n & 1 ? cosp : sinp);
5331914882SAlex Richardson   cosp = (n & 1 ? sinp : cosp);
5431914882SAlex Richardson   sinp = tmp;
5531914882SAlex Richardson 
5631914882SAlex Richardson   c1 = p->c0 + x2 * p->c1;
5731914882SAlex Richardson   x5 = x3 * x2;
5831914882SAlex Richardson   x6 = x4 * x2;
5931914882SAlex Richardson 
6031914882SAlex Richardson   s = x + x3 * p->s1;
6131914882SAlex Richardson   c = c1 + x4 * p->c2;
6231914882SAlex Richardson 
6331914882SAlex Richardson   *sinp = s + x5 * s1;
6431914882SAlex Richardson   *cosp = c + x6 * c2;
6531914882SAlex Richardson }
6631914882SAlex Richardson 
6731914882SAlex Richardson /* Return the sine of inputs X and X2 (X squared) using the polynomial P.
6831914882SAlex Richardson    N is the quadrant, and if odd the cosine polynomial is used.  */
6931914882SAlex Richardson static inline float
sinf_poly(double x,double x2,const sincos_t * p,int n)7031914882SAlex Richardson sinf_poly (double x, double x2, const sincos_t *p, int n)
7131914882SAlex Richardson {
7231914882SAlex Richardson   double x3, x4, x6, x7, s, c, c1, c2, s1;
7331914882SAlex Richardson 
7431914882SAlex Richardson   if ((n & 1) == 0)
7531914882SAlex Richardson     {
7631914882SAlex Richardson       x3 = x * x2;
7731914882SAlex Richardson       s1 = p->s2 + x2 * p->s3;
7831914882SAlex Richardson 
7931914882SAlex Richardson       x7 = x3 * x2;
8031914882SAlex Richardson       s = x + x3 * p->s1;
8131914882SAlex Richardson 
8231914882SAlex Richardson       return s + x7 * s1;
8331914882SAlex Richardson     }
8431914882SAlex Richardson   else
8531914882SAlex Richardson     {
8631914882SAlex Richardson       x4 = x2 * x2;
8731914882SAlex Richardson       c2 = p->c3 + x2 * p->c4;
8831914882SAlex Richardson       c1 = p->c0 + x2 * p->c1;
8931914882SAlex Richardson 
9031914882SAlex Richardson       x6 = x4 * x2;
9131914882SAlex Richardson       c = c1 + x4 * p->c2;
9231914882SAlex Richardson 
9331914882SAlex Richardson       return c + x6 * c2;
9431914882SAlex Richardson     }
9531914882SAlex Richardson }
9631914882SAlex Richardson 
9731914882SAlex Richardson /* Fast range reduction using single multiply-subtract.  Return the modulo of
9831914882SAlex Richardson    X as a value between -PI/4 and PI/4 and store the quadrant in NP.
9931914882SAlex Richardson    The values for PI/2 and 2/PI are accessed via P.  Since PI/2 as a double
10031914882SAlex Richardson    is accurate to 55 bits and the worst-case cancellation happens at 6 * PI/4,
10131914882SAlex Richardson    the result is accurate for |X| <= 120.0.  */
10231914882SAlex Richardson static inline double
reduce_fast(double x,const sincos_t * p,int * np)10331914882SAlex Richardson reduce_fast (double x, const sincos_t *p, int *np)
10431914882SAlex Richardson {
10531914882SAlex Richardson   double r;
10631914882SAlex Richardson #if TOINT_INTRINSICS
10731914882SAlex Richardson   /* Use fast round and lround instructions when available.  */
10831914882SAlex Richardson   r = x * p->hpi_inv;
10931914882SAlex Richardson   *np = converttoint (r);
11031914882SAlex Richardson   return x - roundtoint (r) * p->hpi;
11131914882SAlex Richardson #else
11231914882SAlex Richardson   /* Use scaled float to int conversion with explicit rounding.
11331914882SAlex Richardson      hpi_inv is prescaled by 2^24 so the quadrant ends up in bits 24..31.
11431914882SAlex Richardson      This avoids inaccuracies introduced by truncating negative values.  */
11531914882SAlex Richardson   r = x * p->hpi_inv;
11631914882SAlex Richardson   int n = ((int32_t)r + 0x800000) >> 24;
11731914882SAlex Richardson   *np = n;
11831914882SAlex Richardson   return x - n * p->hpi;
11931914882SAlex Richardson #endif
12031914882SAlex Richardson }
12131914882SAlex Richardson 
12231914882SAlex Richardson /* Reduce the range of XI to a multiple of PI/2 using fast integer arithmetic.
12331914882SAlex Richardson    XI is a reinterpreted float and must be >= 2.0f (the sign bit is ignored).
12431914882SAlex Richardson    Return the modulo between -PI/4 and PI/4 and store the quadrant in NP.
12531914882SAlex Richardson    Reduction uses a table of 4/PI with 192 bits of precision.  A 32x96->128 bit
12631914882SAlex Richardson    multiply computes the exact 2.62-bit fixed-point modulo.  Since the result
12731914882SAlex Richardson    can have at most 29 leading zeros after the binary point, the double
12831914882SAlex Richardson    precision result is accurate to 33 bits.  */
12931914882SAlex Richardson static inline double
reduce_large(uint32_t xi,int * np)13031914882SAlex Richardson reduce_large (uint32_t xi, int *np)
13131914882SAlex Richardson {
13231914882SAlex Richardson   const uint32_t *arr = &__inv_pio4[(xi >> 26) & 15];
13331914882SAlex Richardson   int shift = (xi >> 23) & 7;
13431914882SAlex Richardson   uint64_t n, res0, res1, res2;
13531914882SAlex Richardson 
13631914882SAlex Richardson   xi = (xi & 0xffffff) | 0x800000;
13731914882SAlex Richardson   xi <<= shift;
13831914882SAlex Richardson 
13931914882SAlex Richardson   res0 = xi * arr[0];
14031914882SAlex Richardson   res1 = (uint64_t)xi * arr[4];
14131914882SAlex Richardson   res2 = (uint64_t)xi * arr[8];
14231914882SAlex Richardson   res0 = (res2 >> 32) | (res0 << 32);
14331914882SAlex Richardson   res0 += res1;
14431914882SAlex Richardson 
14531914882SAlex Richardson   n = (res0 + (1ULL << 61)) >> 62;
14631914882SAlex Richardson   res0 -= n << 62;
14731914882SAlex Richardson   double x = (int64_t)res0;
14831914882SAlex Richardson   *np = n;
14931914882SAlex Richardson   return x * pi63;
15031914882SAlex Richardson }
151