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