| 1 | //===-- Double-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/sin.h" |
| 10 | #include "hdr/errno_macros.h" |
| 11 | #include "src/__support/FPUtil/FEnvImpl.h" |
| 12 | #include "src/__support/FPUtil/FPBits.h" |
| 13 | #include "src/__support/FPUtil/double_double.h" |
| 14 | #include "src/__support/FPUtil/dyadic_float.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 | #include "src/math/generic/range_reduction_double_common.h" |
| 22 | #include "src/math/generic/sincos_eval.h" |
| 23 | |
| 24 | #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
| 25 | #include "range_reduction_double_fma.h" |
| 26 | #else |
| 27 | #include "range_reduction_double_nofma.h" |
| 28 | #endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
| 29 | |
| 30 | namespace LIBC_NAMESPACE_DECL { |
| 31 | |
| 32 | using DoubleDouble = fputil::DoubleDouble; |
| 33 | using Float128 = typename fputil::DyadicFloat<128>; |
| 34 | |
| 35 | LLVM_LIBC_FUNCTION(double, sin, (double x)) { |
| 36 | using FPBits = typename fputil::FPBits<double>; |
| 37 | FPBits xbits(x); |
| 38 | |
| 39 | uint16_t x_e = xbits.get_biased_exponent(); |
| 40 | |
| 41 | DoubleDouble y; |
| 42 | unsigned k; |
| 43 | LargeRangeReduction range_reduction_large{}; |
| 44 | |
| 45 | // |x| < 2^16 |
| 46 | if (LIBC_LIKELY(x_e < FPBits::EXP_BIAS + FAST_PASS_EXPONENT)) { |
| 47 | // |x| < 2^-7 |
| 48 | if (LIBC_UNLIKELY(x_e < FPBits::EXP_BIAS - 7)) { |
| 49 | // |x| < 2^-26, |sin(x) - x| < ulp(x)/2. |
| 50 | if (LIBC_UNLIKELY(x_e < FPBits::EXP_BIAS - 26)) { |
| 51 | // Signed zeros. |
| 52 | if (LIBC_UNLIKELY(x == 0.0)) |
| 53 | return x + x; // Make sure it works with FTZ/DAZ. |
| 54 | |
| 55 | #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
| 56 | return fputil::multiply_add(x, -0x1.0p-54, x); |
| 57 | #else |
| 58 | if (LIBC_UNLIKELY(x_e < 4)) { |
| 59 | int rounding_mode = fputil::quick_get_round(); |
| 60 | if (rounding_mode == FE_TOWARDZERO || |
| 61 | (xbits.sign() == Sign::POS && rounding_mode == FE_DOWNWARD) || |
| 62 | (xbits.sign() == Sign::NEG && rounding_mode == FE_UPWARD)) |
| 63 | return FPBits(xbits.uintval() - 1).get_val(); |
| 64 | } |
| 65 | return fputil::multiply_add(x, -0x1.0p-54, x); |
| 66 | #endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
| 67 | } |
| 68 | // No range reduction needed. |
| 69 | k = 0; |
| 70 | y.lo = 0.0; |
| 71 | y.hi = x; |
| 72 | } else { |
| 73 | // Small range reduction. |
| 74 | k = range_reduction_small(x, y); |
| 75 | } |
| 76 | } else { |
| 77 | // Inf or NaN |
| 78 | if (LIBC_UNLIKELY(x_e > 2 * FPBits::EXP_BIAS)) { |
| 79 | // sin(+-Inf) = NaN |
| 80 | if (xbits.is_signaling_nan()) { |
| 81 | fputil::raise_except_if_required(FE_INVALID); |
| 82 | return FPBits::quiet_nan().get_val(); |
| 83 | } |
| 84 | |
| 85 | if (xbits.get_mantissa() == 0) { |
| 86 | fputil::set_errno_if_required(EDOM); |
| 87 | fputil::raise_except_if_required(FE_INVALID); |
| 88 | } |
| 89 | return x + FPBits::quiet_nan().get_val(); |
| 90 | } |
| 91 | |
| 92 | // Large range reduction. |
| 93 | k = range_reduction_large.fast(x, y); |
| 94 | } |
| 95 | |
| 96 | DoubleDouble sin_y, cos_y; |
| 97 | |
| 98 | [[maybe_unused]] double err = generic::sincos_eval(y, sin_y, cos_y); |
| 99 | |
| 100 | // Look up sin(k * pi/128) and cos(k * pi/128) |
| 101 | #ifdef LIBC_MATH_HAS_SMALL_TABLES |
| 102 | // Memory saving versions. Use 65-entry table. |
| 103 | auto get_idx_dd = [](unsigned kk) -> DoubleDouble { |
| 104 | unsigned idx = (kk & 64) ? 64 - (kk & 63) : (kk & 63); |
| 105 | DoubleDouble ans = SIN_K_PI_OVER_128[idx]; |
| 106 | if (kk & 128) { |
| 107 | ans.hi = -ans.hi; |
| 108 | ans.lo = -ans.lo; |
| 109 | } |
| 110 | return ans; |
| 111 | }; |
| 112 | DoubleDouble sin_k = get_idx_dd(k); |
| 113 | DoubleDouble cos_k = get_idx_dd(k + 64); |
| 114 | #else |
| 115 | // Fast look up version, but needs 256-entry table. |
| 116 | // cos(k * pi/128) = sin(k * pi/128 + pi/2) = sin((k + 64) * pi/128). |
| 117 | DoubleDouble sin_k = SIN_K_PI_OVER_128[k & 255]; |
| 118 | DoubleDouble cos_k = SIN_K_PI_OVER_128[(k + 64) & 255]; |
| 119 | #endif |
| 120 | |
| 121 | // After range reduction, k = round(x * 128 / pi) and y = x - k * (pi / 128). |
| 122 | // So k is an integer and -pi / 256 <= y <= pi / 256. |
| 123 | // Then sin(x) = sin((k * pi/128 + y) |
| 124 | // = sin(y) * cos(k*pi/128) + cos(y) * sin(k*pi/128) |
| 125 | DoubleDouble sin_k_cos_y = fputil::quick_mult(cos_y, sin_k); |
| 126 | DoubleDouble cos_k_sin_y = fputil::quick_mult(sin_y, cos_k); |
| 127 | |
| 128 | DoubleDouble rr = fputil::exact_add<false>(sin_k_cos_y.hi, cos_k_sin_y.hi); |
| 129 | rr.lo += sin_k_cos_y.lo + cos_k_sin_y.lo; |
| 130 | |
| 131 | #ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 132 | return rr.hi + rr.lo; |
| 133 | #else |
| 134 | // Accurate test and pass for correctly rounded implementation. |
| 135 | |
| 136 | double rlp = rr.lo + err; |
| 137 | double rlm = rr.lo - err; |
| 138 | |
| 139 | double r_upper = rr.hi + rlp; // (rr.lo + ERR); |
| 140 | double r_lower = rr.hi + rlm; // (rr.lo - ERR); |
| 141 | |
| 142 | // Ziv's rounding test. |
| 143 | if (LIBC_LIKELY(r_upper == r_lower)) |
| 144 | return r_upper; |
| 145 | |
| 146 | Float128 u_f128, sin_u, cos_u; |
| 147 | if (LIBC_LIKELY(x_e < FPBits::EXP_BIAS + FAST_PASS_EXPONENT)) |
| 148 | u_f128 = range_reduction_small_f128(x); |
| 149 | else |
| 150 | u_f128 = range_reduction_large.accurate(); |
| 151 | |
| 152 | generic::sincos_eval(u_f128, sin_u, cos_u); |
| 153 | |
| 154 | auto get_sin_k = [](unsigned kk) -> Float128 { |
| 155 | unsigned idx = (kk & 64) ? 64 - (kk & 63) : (kk & 63); |
| 156 | Float128 ans = SIN_K_PI_OVER_128_F128[idx]; |
| 157 | if (kk & 128) |
| 158 | ans.sign = Sign::NEG; |
| 159 | return ans; |
| 160 | }; |
| 161 | |
| 162 | // cos(k * pi/128) = sin(k * pi/128 + pi/2) = sin((k + 64) * pi/128). |
| 163 | Float128 sin_k_f128 = get_sin_k(k); |
| 164 | Float128 cos_k_f128 = get_sin_k(k + 64); |
| 165 | |
| 166 | // sin(x) = sin(k * pi/128 + u) |
| 167 | // = sin(u) * cos(k*pi/128) + cos(u) * sin(k*pi/128) |
| 168 | Float128 r = fputil::quick_add(fputil::quick_mul(sin_k_f128, cos_u), |
| 169 | fputil::quick_mul(cos_k_f128, sin_u)); |
| 170 | |
| 171 | // TODO: Add assertion if Ziv's accuracy tests fail in debug mode. |
| 172 | // https://github.com/llvm/llvm-project/issues/96452. |
| 173 | |
| 174 | return static_cast<double>(r); |
| 175 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 176 | } |
| 177 | |
| 178 | } // namespace LIBC_NAMESPACE_DECL |
| 179 | |