1 /* 2 * Single-precision sin/cos function. 3 * 4 * Copyright (c) 2018-2024, Arm Limited. 5 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception 6 */ 7 8 #include <stdint.h> 9 #include <math.h> 10 #include "math_config.h" 11 #include "sincosf.h" 12 #include "test_defs.h" 13 14 /* Fast sincosf implementation. Worst-case ULP is 0.5607, maximum relative 15 error is 0.5303 * 2^-23. A single-step range reduction is used for 16 small values. Large inputs have their range reduced using fast integer 17 arithmetic. */ 18 void 19 sincosf (float y, float *sinp, float *cosp) 20 { 21 double x = y; 22 double s; 23 int n; 24 const sincos_t *p = &__sincosf_table[0]; 25 26 if (abstop12 (y) < abstop12 (pio4f)) 27 { 28 double x2 = x * x; 29 30 if (unlikely (abstop12 (y) < abstop12 (0x1p-12f))) 31 { 32 if (unlikely (abstop12 (y) < abstop12 (0x1p-126f))) 33 /* Force underflow for tiny y. */ 34 force_eval_float (x2); 35 *sinp = y; 36 *cosp = 1.0f; 37 return; 38 } 39 40 sincosf_poly (x, x2, p, 0, sinp, cosp); 41 } 42 else if (abstop12 (y) < abstop12 (120.0f)) 43 { 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 } 54 else if (likely (abstop12 (y) < abstop12 (INFINITY))) 55 { 56 uint32_t xi = asuint (y); 57 int sign = xi >> 31; 58 59 x = reduce_large (xi, &n); 60 61 /* Setup signs for sin and cos - include original sign. */ 62 s = p->sign[(n + sign) & 3]; 63 64 if ((n + sign) & 2) 65 p = &__sincosf_table[1]; 66 67 sincosf_poly (x * s, x * x, p, n, sinp, cosp); 68 } 69 else 70 { 71 /* Return NaN if Inf or NaN for both sin and cos. */ 72 *sinp = *cosp = y - y; 73 #if WANT_ERRNO 74 /* Needed to set errno for +-Inf, the add is a hack to work 75 around a gcc register allocation issue: just passing y 76 affects code generation in the fast path. */ 77 __math_invalidf (y + y); 78 #endif 79 } 80 } 81 82 TEST_ULP (sincosf_sinf, 0.06) 83 TEST_ULP (sincosf_cosf, 0.06) 84 TEST_ULP_NONNEAREST (sincosf_sinf, 0.5) 85 TEST_ULP_NONNEAREST (sincosf_cosf, 0.5) 86 TEST_INTERVAL (sincosf_sinf, 0, 0xffff0000, 10000) 87 TEST_SYM_INTERVAL (sincosf_sinf, 0x1p-14, 0x1p54, 50000) 88 TEST_INTERVAL (sincosf_cosf, 0, 0xffff0000, 10000) 89 TEST_SYM_INTERVAL (sincosf_cosf, 0x1p-14, 0x1p54, 50000) 90