| 1 | //===----------------------------------------------------------------------===// |
| 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 | /// \file |
| 10 | /// Double-precision implementation of sinh. |
| 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_LIBC_SRC___SUPPORT_MATH_SINH_DOUBLE_EVAL_H |
| 15 | #define LLVM_LIBC_SRC___SUPPORT_MATH_SINH_DOUBLE_EVAL_H |
| 16 | |
| 17 | #include "hdr/errno_macros.h" |
| 18 | #include "src/__support/CPP/bit.h" |
| 19 | #include "src/__support/FPUtil/FEnvImpl.h" |
| 20 | #include "src/__support/FPUtil/FPBits.h" |
| 21 | #include "src/__support/FPUtil/except_value_utils.h" |
| 22 | #include "src/__support/FPUtil/fp_bitwise.h" |
| 23 | #include "src/__support/FPUtil/multiply_add.h" |
| 24 | #include "src/__support/FPUtil/polyeval.h" |
| 25 | #include "src/__support/macros/attributes.h" |
| 26 | #include "src/__support/macros/config.h" |
| 27 | #include "src/__support/macros/optimization.h" |
| 28 | #include "src/__support/macros/properties/cpu_features.h" |
| 29 | #include "src/__support/math/sinhcosh_utils.h" |
| 30 | |
| 31 | namespace LIBC_NAMESPACE_DECL { |
| 32 | namespace math { |
| 33 | namespace double_eval { |
| 34 | |
| 35 | #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 36 | |
| 37 | // Leading 11-bit split of c_0 ~ 1/6 for exact cubic reconstruction: |
| 38 | // SINH_C0_HI_11 = round(1/6, 11, RD) = 0x1.554p-3 |
| 39 | // 1/6 - SINH_C0_HI_11 = 0x1.5555555555555p-15. |
| 40 | LIBC_INLINE_VAR constexpr double SINH_C0_HI_11 = 0x1.554p-3; |
| 41 | |
| 42 | // Polynomial approximation for ((sinh(x) - x) / x^3 - 0x1.554p-3) on |
| 43 | // [-2^-2, 2^-2] as a degree-5 even polynomial in x^2: |
| 44 | // sinh(x) - x ~ x^3 * (SINH_C0_HI_11 + P_rem(x^2)) |
| 45 | // |
| 46 | // Minimax polynomial coefficients generated by Sollya with: |
| 47 | // > display = hexadecimal!; |
| 48 | // > prec = 500; |
| 49 | // > P = fpminimax((sinh(sqrt(x))/sqrt(x) - 1)/x - 0x1.554p-3, 5, [|D...|], |
| 50 | // [2^(-100), 2^(-4)]); |
| 51 | // > for i from 0 to 5 do print(coeff(P, i), ","); |
| 52 | // > dirtyinfnorm(sinh(x) - (x + x^3 * (0x1.554p-3 + subst(P, x, x^2))), |
| 53 | // [-2^(-2), 2^(-2)]); |
| 54 | // 0x1.0315...p-75 < 2^-74. |
| 55 | LIBC_INLINE_VAR constexpr double SINH_SMALL_P_COEFFS[6] = { |
| 56 | 0x1.5555555555555p-15, // c_0 - 0x1.554p-3 ~ 1/6 - 0x1.554p-3 |
| 57 | 0x1.1111111111111p-7, // c_1 ~ 1/120 |
| 58 | 0x1.a01a01a01a38ap-13, // c_2 ~ 1/5040 |
| 59 | 0x1.71de3a52826f3p-19, // c_3 ~ 1/362880 |
| 60 | 0x1.ae6471bbce233p-26, // c_4 ~ 1/39916800 |
| 61 | 0x1.60eca3b25ae29p-33, // c_5 ~ 1/6227020800 |
| 62 | }; |
| 63 | |
| 64 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 65 | |
| 66 | // Evaluate sinh(x) for |x| < SMALL_BOUND (2^-26 + ulp(2^-26) when |
| 67 | // LIBC_MATH_HAS_SKIP_ACCURATE_PASS is defined, or 2^-2 otherwise): |
| 68 | LIBC_INLINE double sinh_small_eval(double x, uint64_t x_abs) { |
| 69 | // Tiny and subnormal range: |x| <= 2^-26. |
| 70 | // For 0 < |x| <= 2^-26: |
| 71 | // 0 < (sinh(|x|) - |x|) / |x| ~ x^2 / 6 < 2^-54 < 2^-1 * ulp(x), |
| 72 | // so sinh(x) rounds to: |
| 73 | // - nextafter(x, +inf) when x > 0 and rounding mode is FE_UPWARD, |
| 74 | // - nextafter(x, -inf) when x < 0 and rounding mode is FE_DOWNWARD, |
| 75 | // - x otherwise, with FE_INEXACT raised for all x != 0 and FE_UNDERFLOW + |
| 76 | // errno = ERANGE raised when |x| < 2^-1022. |
| 77 | #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 78 | if (LIBC_UNLIKELY(x_abs <= 0x3e50'0000'0000'0000ULL)) |
| 79 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 80 | { |
| 81 | if (LIBC_UNLIKELY(x_abs == 0)) |
| 82 | return x; |
| 83 | if (LIBC_UNLIKELY(x_abs < 0x0010'0000'0000'0000ULL)) { |
| 84 | fputil::set_errno_if_required(ERANGE); |
| 85 | return fputil::multiply_add(x, y: 0x1.0p-55, z: x); |
| 86 | } |
| 87 | #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
| 88 | return fputil::multiply_add(x, 0x1.0p-55, x); |
| 89 | #else |
| 90 | // Avoid spurious FE_UNDERFLOW on x * 2^-55 when 2^-1022 <= |x| < 2^-967: |
| 91 | double x_scaled = fputil::multiply_add(x, y: 0x1.0p55, z: x); |
| 92 | return x_scaled * 0x1.0p-55; |
| 93 | #endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
| 94 | } |
| 95 | |
| 96 | #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 97 | // 2^-26 < |x| < 2^-2: |
| 98 | // Split x = x_hi + x_lo into 14-bit x_hi and 39-bit x_lo so that: |
| 99 | // x2_hi = x_hi * x_hi (28 bits) |
| 100 | // x3_hi = x2_hi * x_hi (42 bits) |
| 101 | // y_hi = x3_hi * SINH_C0_HI_11 (42 + 11 = 53 bits) |
| 102 | // are all exact in double precision across all rounding modes (with or |
| 103 | // without FMA). |
| 104 | LIBC_BIT_CAST_CONSTEXPR_VAR double HI_MASK_14 = |
| 105 | cpp::bit_cast<double>(from: 0xffff'ff80'0000'0000ULL); |
| 106 | double x_hi = fputil::fp_and(a: x, b: HI_MASK_14); |
| 107 | double x_lo = x - x_hi; |
| 108 | double x2 = x * x; |
| 109 | double x2_hi = x_hi * x_hi; // exact (28 bits) |
| 110 | double x3_hi = x2_hi * x_hi; // exact (42 bits) |
| 111 | double y_hi = x3_hi * SINH_C0_HI_11; // exact (53 bits) |
| 112 | |
| 113 | // Cubic remainder: |
| 114 | // x3_lo ~ x^3 - x_hi^3 = x_lo * (x^2 + x * x_hi + x_hi^2) |
| 115 | double x2_mid = fputil::multiply_add(x, y: x_hi, z: x2); |
| 116 | double x3_sum = x2_mid + x2_hi; |
| 117 | double x3_lo = x_lo * x3_sum; |
| 118 | |
| 119 | // Polynomial remainder: |
| 120 | // x^3 * (SINH_C0_HI_11 + P_rem(x^2)) = y_hi + y_lo |
| 121 | // where: |
| 122 | // y_lo ~ x_hi^3 * P_rem(x^2) + x3_lo * (SINH_C0_HI_11 + P_rem(x^2)) |
| 123 | double p_rem = fputil::estrin_polyeval(x: x2, a: SINH_SMALL_P_COEFFS); |
| 124 | double p_full = SINH_C0_HI_11 + p_rem; |
| 125 | double y_lo_t = x3_lo * p_full; |
| 126 | double y_lo = fputil::multiply_add(x: x3_hi, y: p_rem, z: y_lo_t); |
| 127 | |
| 128 | // Leading sum r_hi + s_err = x + y_hi (exact FastTwoSum in all rounding modes |
| 129 | // since |y_hi| < 2^-6 * |x|): |
| 130 | double r_hi = x + y_hi; |
| 131 | double r_t = x - r_hi; |
| 132 | double s_err = r_t + y_hi; |
| 133 | |
| 134 | // Error bound for Ziv's rounding test: |
| 135 | // The error of y_hi + y_lo is bounded by |y_hi| * 2^-57, and the rounding |
| 136 | // error of s_err + y_lo is bounded by |x| * 2^-103. |
| 137 | double x_err = x * 0x1.0p-103; |
| 138 | double err = fputil::multiply_add(x: y_hi, y: 0x1.0p-57, z: x_err); |
| 139 | |
| 140 | // Low tail bounds lo_{u,l} ~ s_err + (y_lo +/- err): |
| 141 | double y_lo_u = y_lo + err; |
| 142 | double y_lo_l = y_lo - err; |
| 143 | double lo_u = s_err + y_lo_u; |
| 144 | double lo_l = s_err + y_lo_l; |
| 145 | |
| 146 | // Ziv's rounding test in the current rounding mode: |
| 147 | double upper = r_hi + lo_u; |
| 148 | double lower = r_hi + lo_l; |
| 149 | |
| 150 | if (LIBC_LIKELY(upper == lower)) |
| 151 | return upper; |
| 152 | |
| 153 | return sinh_small_accurate_128(x); |
| 154 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 155 | } |
| 156 | |
| 157 | // Handle overflow, inf, and nan for sinh(x) when |x| >= 0x1.633ce8fb9f87ep+9: |
| 158 | LIBC_NOINLINE LIBC_INLINE double sinh_exceptional(double x) { |
| 159 | using FPBits = fputil::FPBits<double>; |
| 160 | FPBits xbits(x); |
| 161 | |
| 162 | if (xbits.is_nan()) |
| 163 | return x + 1.0; |
| 164 | |
| 165 | if (xbits.is_inf()) |
| 166 | return x; |
| 167 | |
| 168 | return fputil::set_overflow<double>(xbits.sign()); |
| 169 | } |
| 170 | |
| 171 | LIBC_ALWAYS_INLINE double sinh(double x) { |
| 172 | using FPBits = fputil::FPBits<double>; |
| 173 | FPBits xbits(x); |
| 174 | |
| 175 | uint64_t x_abs = xbits.abs().uintval(); |
| 176 | |
| 177 | // Bound constants (with biased exponent and mantissa bits): |
| 178 | #ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 179 | // - Small bound: 2^-26 + ulp(2^-26) (exp_pm_eval handles |x| > 2^-26 directly |
| 180 | // with k = 0 and zero cancellation when |x| < log(2) / 2) |
| 181 | constexpr uint64_t SMALL_BOUND = 0x3e50'0000'0000'0001ULL; |
| 182 | #else |
| 183 | // - Small bound: 2^-2 = 0x1.0p-2 |
| 184 | constexpr uint64_t SMALL_BOUND = 0x3fd0'0000'0000'0000ULL; |
| 185 | #endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 186 | // - Overflow bound: |
| 187 | // > round(asinh(2^1023 * (2 - 2^-52)), D, RU) = 0x1.633ce8fb9f87ep+9 |
| 188 | // > round(asinh(2^1023 * (2 - 2^-52)), D, RD) = 0x1.633ce8fb9f87dp+9 |
| 189 | // > round(sinh(0x1.633ce8fb9f87dp+9), D, RN) = 0x1.fffffffffffffp+1023 |
| 190 | // > round(sinh(0x1.633ce8fb9f87ep+9), D, RN) = +inf |
| 191 | constexpr uint64_t OVERFLOW_BOUND = 0x4086'33ce'8fb9'f87eULL; |
| 192 | |
| 193 | if (LIBC_UNLIKELY(x_abs < SMALL_BOUND || x_abs >= OVERFLOW_BOUND)) { |
| 194 | if (x_abs < SMALL_BOUND) |
| 195 | return sinh_small_eval(x, x_abs); |
| 196 | |
| 197 | return sinh_exceptional(x); |
| 198 | } |
| 199 | |
| 200 | return exp_pm_eval(x, /*is_sinh=*/is_sinh: true); |
| 201 | } |
| 202 | |
| 203 | } // namespace double_eval |
| 204 | } // namespace math |
| 205 | } // namespace LIBC_NAMESPACE_DECL |
| 206 | |
| 207 | #endif // LLVM_LIBC_SRC___SUPPORT_MATH_SINH_DOUBLE_EVAL_H |
| 208 | |