| 1 | //===-- Single-precision sin function -------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_LIBC_SRC___SUPPORT_MATH_SINF_H |
| 10 | #define LLVM_LIBC_SRC___SUPPORT_MATH_SINF_H |
| 11 | |
| 12 | #include "src/__support/FPUtil/BasicOperations.h" |
| 13 | #include "src/__support/FPUtil/FEnvImpl.h" |
| 14 | #include "src/__support/FPUtil/FPBits.h" |
| 15 | #include "src/__support/FPUtil/PolyEval.h" |
| 16 | #include "src/__support/FPUtil/multiply_add.h" |
| 17 | #include "src/__support/FPUtil/rounding_mode.h" |
| 18 | #include "src/__support/macros/config.h" |
| 19 | #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY |
| 20 | #include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA |
| 21 | |
| 22 | #if defined(LIBC_MATH_HAS_SKIP_ACCURATE_PASS) && \ |
| 23 | defined(LIBC_MATH_HAS_INTERMEDIATE_COMP_IN_FLOAT) && \ |
| 24 | defined(LIBC_TARGET_CPU_HAS_FMA_FLOAT) |
| 25 | |
| 26 | #include "sincosf_float_eval.h" |
| 27 | |
| 28 | namespace LIBC_NAMESPACE_DECL { |
| 29 | |
| 30 | namespace math { |
| 31 | |
| 32 | LIBC_INLINE float sinf(float x) { |
| 33 | return math::sincosf_float_eval::sincosf_eval</*IS_SIN*/ true>(x); |
| 34 | } |
| 35 | |
| 36 | } // namespace math |
| 37 | |
| 38 | } // namespace LIBC_NAMESPACE_DECL |
| 39 | |
| 40 | #else // !LIBC_MATH_HAS_INTERMEDIATE_COMP_IN_FLOAT |
| 41 | |
| 42 | #include "src/__support/math/sincosf_utils.h" |
| 43 | |
| 44 | #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
| 45 | #include "src/__support/math/range_reduction_fma.h" |
| 46 | #else // !LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
| 47 | #include "src/__support/math/range_reduction.h" |
| 48 | #endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
| 49 | |
| 50 | namespace LIBC_NAMESPACE_DECL { |
| 51 | |
| 52 | namespace math { |
| 53 | |
| 54 | LIBC_INLINE float sinf(float x) { |
| 55 | using namespace sincosf_utils_internal; |
| 56 | using FPBits = typename fputil::FPBits<float>; |
| 57 | FPBits xbits(x); |
| 58 | |
| 59 | uint32_t x_u = xbits.uintval(); |
| 60 | uint32_t x_abs = x_u & 0x7fff'ffffU; |
| 61 | double xd = static_cast<double>(x); |
| 62 | |
| 63 | // Range reduction: |
| 64 | // For |x| > pi/32, we perform range reduction as follows: |
| 65 | // Find k and y such that: |
| 66 | // x = (k + y) * pi/32 |
| 67 | // k is an integer |
| 68 | // |y| < 0.5 |
| 69 | // For small range (|x| < 2^45 when FMA instructions are available, 2^22 |
| 70 | // otherwise), this is done by performing: |
| 71 | // k = round(x * 32/pi) |
| 72 | // y = x * 32/pi - k |
| 73 | // For large range, we will omit all the higher parts of 32/pi such that the |
| 74 | // least significant bits of their full products with x are larger than 63, |
| 75 | // since sin((k + y + 64*i) * pi/32) = sin(x + i * 2pi) = sin(x). |
| 76 | // |
| 77 | // When FMA instructions are not available, we store the digits of 32/pi in |
| 78 | // chunks of 28-bit precision. This will make sure that the products: |
| 79 | // x * THIRTYTWO_OVER_PI_28[i] are all exact. |
| 80 | // When FMA instructions are available, we simply store the digits of 32/pi in |
| 81 | // chunks of doubles (53-bit of precision). |
| 82 | // So when multiplying by the largest values of single precision, the |
| 83 | // resulting output should be correct up to 2^(-208 + 128) ~ 2^-80. By the |
| 84 | // worst-case analysis of range reduction, |y| >= 2^-38, so this should give |
| 85 | // us more than 40 bits of accuracy. For the worst-case estimation of range |
| 86 | // reduction, see for instances: |
| 87 | // Elementary Functions by J-M. Muller, Chapter 11, |
| 88 | // Handbook of Floating-Point Arithmetic by J-M. Muller et. al., |
| 89 | // Chapter 10.2. |
| 90 | // |
| 91 | // Once k and y are computed, we then deduce the answer by the sine of sum |
| 92 | // formula: |
| 93 | // sin(x) = sin((k + y)*pi/32) |
| 94 | // = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32) |
| 95 | // The values of sin(k*pi/32) and cos(k*pi/32) for k = 0..31 are precomputed |
| 96 | // and stored using a vector of 32 doubles. Sin(y*pi/32) and cos(y*pi/32) are |
| 97 | // computed using degree-7 and degree-6 minimax polynomials generated by |
| 98 | // Sollya respectively. |
| 99 | |
| 100 | // |x| <= pi/16 |
| 101 | if (LIBC_UNLIKELY(x_abs <= 0x3e49'0fdbU)) { |
| 102 | |
| 103 | // |x| < 0x1.d12ed2p-12f |
| 104 | if (LIBC_UNLIKELY(x_abs < 0x39e8'9769U)) { |
| 105 | if (LIBC_UNLIKELY(x_abs == 0U)) { |
| 106 | // For signed zeros. |
| 107 | return x; |
| 108 | } |
| 109 | // When |x| < 2^-12, the relative error of the approximation sin(x) ~ x |
| 110 | // is: |
| 111 | // |sin(x) - x| / |sin(x)| < |x^3| / (6|x|) |
| 112 | // = x^2 / 6 |
| 113 | // < 2^-25 |
| 114 | // < epsilon(1)/2. |
| 115 | // So the correctly rounded values of sin(x) are: |
| 116 | // = x - sign(x)*eps(x) if rounding mode = FE_TOWARDZERO, |
| 117 | // or (rounding mode = FE_UPWARD and x is |
| 118 | // negative), |
| 119 | // = x otherwise. |
| 120 | // To simplify the rounding decision and make it more efficient, we use |
| 121 | // fma(x, -2^-25, x) instead. |
| 122 | // An exhaustive test shows that this formula work correctly for all |
| 123 | // rounding modes up to |x| < 0x1.c555dep-11f. |
| 124 | // Note: to use the formula x - 2^-25*x to decide the correct rounding, we |
| 125 | // do need fma(x, -2^-25, x) to prevent underflow caused by -2^-25*x when |
| 126 | // |x| < 2^-125. For targets without FMA instructions, we simply use |
| 127 | // double for intermediate results as it is more efficient than using an |
| 128 | // emulated version of FMA. |
| 129 | #if defined(LIBC_TARGET_CPU_HAS_FMA_FLOAT) |
| 130 | return fputil::multiply_add(x, -0x1.0p-25f, x); |
| 131 | #else |
| 132 | return static_cast<float>(fputil::multiply_add(x: xd, y: -0x1.0p-25, z: xd)); |
| 133 | #endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT |
| 134 | } |
| 135 | |
| 136 | // |x| < pi/16. |
| 137 | double xsq = xd * xd; |
| 138 | |
| 139 | // Degree-9 polynomial approximation: |
| 140 | // sin(x) ~ x + a_3 x^3 + a_5 x^5 + a_7 x^7 + a_9 x^9 |
| 141 | // = x (1 + a_3 x^2 + ... + a_9 x^8) |
| 142 | // = x * P(x^2) |
| 143 | // generated by Sollya with the following commands: |
| 144 | // > display = hexadecimal; |
| 145 | // > Q = fpminimax(sin(x)/x, [|0, 2, 4, 6, 8|], [|1, D...|], [0, pi/16]); |
| 146 | double result = |
| 147 | fputil::polyeval(x: xsq, a0: 1.0, a: -0x1.55555555554c6p-3, a: 0x1.1111111085e65p-7, |
| 148 | a: -0x1.a019f70fb4d4fp-13, a: 0x1.718d179815e74p-19); |
| 149 | return static_cast<float>(xd * result); |
| 150 | } |
| 151 | |
| 152 | #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 153 | if (LIBC_UNLIKELY(x_abs == 0x4619'9998U)) { // x = 0x1.33333p13 |
| 154 | float r = -0x1.63f4bap-2f; |
| 155 | #ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY |
| 156 | int rounding = fputil::quick_get_round(); |
| 157 | if ((rounding == FE_DOWNWARD && xbits.is_pos()) || |
| 158 | (rounding == FE_UPWARD && xbits.is_neg())) |
| 159 | r = -0x1.63f4bcp-2f; |
| 160 | #endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY |
| 161 | return xbits.is_neg() ? -r : r; |
| 162 | } |
| 163 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 164 | |
| 165 | if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) { |
| 166 | if (xbits.is_signaling_nan()) { |
| 167 | fputil::raise_except_if_required(FE_INVALID); |
| 168 | return FPBits::quiet_nan().get_val(); |
| 169 | } |
| 170 | |
| 171 | if (x_abs == 0x7f80'0000U) { |
| 172 | fputil::set_errno_if_required(EDOM); |
| 173 | fputil::raise_except_if_required(FE_INVALID); |
| 174 | } |
| 175 | return x + FPBits::quiet_nan().get_val(); |
| 176 | } |
| 177 | |
| 178 | // Combine the results with the sine of sum formula: |
| 179 | // sin(x) = sin((k + y)*pi/32) |
| 180 | // = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32) |
| 181 | // = sin_y * cos_k + (1 + cosm1_y) * sin_k |
| 182 | // = sin_y * cos_k + (cosm1_y * sin_k + sin_k) |
| 183 | double sin_k, cos_k, sin_y, cosm1_y; |
| 184 | |
| 185 | sincosf_eval(xd, x_abs, sin_k, cos_k, sin_y, cosm1_y); |
| 186 | |
| 187 | return static_cast<float>(fputil::multiply_add( |
| 188 | x: sin_y, y: cos_k, z: fputil::multiply_add(x: cosm1_y, y: sin_k, z: sin_k))); |
| 189 | } |
| 190 | |
| 191 | } // namespace math |
| 192 | |
| 193 | } // namespace LIBC_NAMESPACE_DECL |
| 194 | |
| 195 | #endif // LIBC_MATH_HAS_INTERMEDIATE_COMP_IN_FLOAT |
| 196 | |
| 197 | #endif // LLVM_LIBC_SRC___SUPPORT_MATH_SINF_H |
| 198 | |