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