| //===----------------------------------------------------------------------===// | |
| // | |
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |
| // See https://llvm.org/LICENSE.txt for license information. | |
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |
| // | |
| //===----------------------------------------------------------------------===// | |
| /// | |
| /// \file | |
| /// Double-precision implementation of sinh. | |
| /// | |
| //===----------------------------------------------------------------------===// | |
| #ifndef LLVM_LIBC_SRC___SUPPORT_MATH_SINH_DOUBLE_EVAL_H | |
| #define LLVM_LIBC_SRC___SUPPORT_MATH_SINH_DOUBLE_EVAL_H | |
| #include "hdr/errno_macros.h" | |
| #include "src/__support/CPP/bit.h" | |
| #include "src/__support/FPUtil/FEnvImpl.h" | |
| #include "src/__support/FPUtil/FPBits.h" | |
| #include "src/__support/FPUtil/except_value_utils.h" | |
| #include "src/__support/FPUtil/fp_bitwise.h" | |
| #include "src/__support/FPUtil/multiply_add.h" | |
| #include "src/__support/macros/attributes.h" | |
| #include "src/__support/macros/config.h" | |
| #include "src/__support/macros/optimization.h" | |
| #include "src/__support/macros/properties/cpu_features.h" | |
| #include "src/__support/math/sinhcosh_utils.h" | |
| namespace LIBC_NAMESPACE_DECL { | |
| namespace math { | |
| namespace double_eval { | |
| #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS | |
| // Leading 11-bit split of c_0 ~ 1/6 for exact cubic reconstruction: | |
| // SINH_C0_HI_11 = round(1/6, 11, RD) = 0x1.554p-3 | |
| // 1/6 - SINH_C0_HI_11 = 0x1.5555555555555p-15. | |
| LIBC_INLINE_VAR constexpr double SINH_C0_HI_11 = 0x1.554p-3; | |
| // Polynomial approximation for ((sinh(x) - x) / x^3 - 0x1.554p-3) on | |
| // [-2^-2, 2^-2] as a degree-5 even polynomial in x^2: | |
| // sinh(x) - x ~ x^3 * (SINH_C0_HI_11 + P_rem(x^2)) | |
| // | |
| // Minimax polynomial coefficients generated by Sollya with: | |
| // > display = hexadecimal!; | |
| // > prec = 500; | |
| // > P = fpminimax((sinh(sqrt(x))/sqrt(x) - 1)/x - 0x1.554p-3, 5, [|D...|], | |
| // [2^(-100), 2^(-4)]); | |
| // > for i from 0 to 5 do print(coeff(P, i), ","); | |
| // > dirtyinfnorm(sinh(x) - (x + x^3 * (0x1.554p-3 + subst(P, x, x^2))), | |
| // [-2^(-2), 2^(-2)]); | |
| // 0x1.0315...p-75 < 2^-74. | |
| LIBC_INLINE_VAR constexpr double SINH_SMALL_P_COEFFS[6] = { | |
| 0x1.5555555555555p-15, // c_0 - 0x1.554p-3 ~ 1/6 - 0x1.554p-3 | |
| 0x1.1111111111111p-7, // c_1 ~ 1/120 | |
| 0x1.a01a01a01a38ap-13, // c_2 ~ 1/5040 | |
| 0x1.71de3a52826f3p-19, // c_3 ~ 1/362880 | |
| 0x1.ae6471bbce233p-26, // c_4 ~ 1/39916800 | |
| 0x1.60eca3b25ae29p-33, // c_5 ~ 1/6227020800 | |
| }; | |
| // Evaluate P_rem(x^2) ~ (sinh(x) - x) / x^3 - 0x1.554p-3 using Estrin's | |
| // scheme: | |
| LIBC_ALWAYS_INLINE double sinh_small_poly(double x2) { | |
| double x4 = x2 * x2; | |
| double x8 = x4 * x4; | |
| double c0 = | |
| fputil::multiply_add(x2, SINH_SMALL_P_COEFFS[1], SINH_SMALL_P_COEFFS[0]); | |
| double c1 = | |
| fputil::multiply_add(x2, SINH_SMALL_P_COEFFS[3], SINH_SMALL_P_COEFFS[2]); | |
| double c2 = | |
| fputil::multiply_add(x2, SINH_SMALL_P_COEFFS[5], SINH_SMALL_P_COEFFS[4]); | |
| double c01 = fputil::multiply_add(x4, c1, c0); | |
| return fputil::multiply_add(x8, c2, c01); | |
| } | |
| #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS | |
| // Evaluate sinh(x) for |x| < SMALL_BOUND (2^-26 + ulp(2^-26) when | |
| // LIBC_MATH_HAS_SKIP_ACCURATE_PASS is defined, or 2^-2 otherwise): | |
| LIBC_INLINE double sinh_small_eval(double x, uint64_t x_abs) { | |
| // Tiny and subnormal range: |x| <= 2^-26. | |
| // For 0 < |x| <= 2^-26: | |
| // 0 < (sinh(|x|) - |x|) / |x| ~ x^2 / 6 < 2^-54 < 2^-1 * ulp(x), | |
| // so sinh(x) rounds to: | |
| // - nextafter(x, +inf) when x > 0 and rounding mode is FE_UPWARD, | |
| // - nextafter(x, -inf) when x < 0 and rounding mode is FE_DOWNWARD, | |
| // - x otherwise, with FE_INEXACT raised for all x != 0 and FE_UNDERFLOW + | |
| // errno = ERANGE raised when |x| < 2^-1022. | |
| #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS | |
| if (LIBC_UNLIKELY(x_abs <= 0x3e50'0000'0000'0000ULL)) | |
| #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS | |
| { | |
| if (LIBC_UNLIKELY(x_abs == 0)) | |
| return x; | |
| if (LIBC_UNLIKELY(x_abs < 0x0010'0000'0000'0000ULL)) { | |
| fputil::set_errno_if_required(ERANGE); | |
| return fputil::multiply_add(x, 0x1.0p-55, x); | |
| } | |
| #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE | |
| return fputil::multiply_add(x, 0x1.0p-55, x); | |
| #else | |
| // Avoid spurious FE_UNDERFLOW on x * 2^-55 when 2^-1022 <= |x| < 2^-967: | |
| double x_scaled = fputil::multiply_add(x, 0x1.0p55, x); | |
| return x_scaled * 0x1.0p-55; | |
| #endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE | |
| } | |
| #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS | |
| // 2^-26 < |x| < 2^-2: | |
| // Split x = x_hi + x_lo into 14-bit x_hi and 39-bit x_lo so that: | |
| // x2_hi = x_hi * x_hi (28 bits) | |
| // x3_hi = x2_hi * x_hi (42 bits) | |
| // y_hi = x3_hi * SINH_C0_HI_11 (42 + 11 = 53 bits) | |
| // are all exact in double precision across all rounding modes (with or | |
| // without FMA). | |
| LIBC_BIT_CAST_CONSTEXPR_VAR double HI_MASK_14 = | |
| cpp::bit_cast<double>(0xffff'ff80'0000'0000ULL); | |
| double x_hi = fputil::fp_and(x, HI_MASK_14); | |
| double x_lo = x - x_hi; | |
| double x2 = x * x; | |
| double x2_hi = x_hi * x_hi; // exact (28 bits) | |
| double x3_hi = x2_hi * x_hi; // exact (42 bits) | |
| double y_hi = x3_hi * SINH_C0_HI_11; // exact (53 bits) | |
| // Cubic remainder: | |
| // x3_lo ~ x^3 - x_hi^3 = x_lo * (x^2 + x * x_hi + x_hi^2) | |
| double x2_mid = fputil::multiply_add(x, x_hi, x2); | |
| double x3_sum = x2_mid + x2_hi; | |
| double x3_lo = x_lo * x3_sum; | |
| // Polynomial remainder: | |
| // x^3 * (SINH_C0_HI_11 + P_rem(x^2)) = y_hi + y_lo | |
| // where: | |
| // y_lo ~ x_hi^3 * P_rem(x^2) + x3_lo * (SINH_C0_HI_11 + P_rem(x^2)) | |
| double p_rem = sinh_small_poly(x2); | |
| double p_full = SINH_C0_HI_11 + p_rem; | |
| double y_lo_t = x3_lo * p_full; | |
| double y_lo = fputil::multiply_add(x3_hi, p_rem, y_lo_t); | |
| // Leading sum r_hi + s_err = x + y_hi (exact FastTwoSum in all rounding modes | |
| // since |y_hi| < 2^-6 * |x|): | |
| double r_hi = x + y_hi; | |
| double r_t = x - r_hi; | |
| double s_err = r_t + y_hi; | |
| // Error bound for Ziv's rounding test: | |
| // The error of y_hi + y_lo is bounded by |y_hi| * 2^-57, and the rounding | |
| // error of s_err + y_lo is bounded by |x| * 2^-103. | |
| double x_err = x * 0x1.0p-103; | |
| double err = fputil::multiply_add(y_hi, 0x1.0p-57, x_err); | |
| // Low tail bounds lo_{u,l} ~ s_err + (y_lo +/- err): | |
| double y_lo_u = y_lo + err; | |
| double y_lo_l = y_lo - err; | |
| double lo_u = s_err + y_lo_u; | |
| double lo_l = s_err + y_lo_l; | |
| // Ziv's rounding test in the current rounding mode: | |
| double upper = r_hi + lo_u; | |
| double lower = r_hi + lo_l; | |
| if (LIBC_LIKELY(upper == lower)) | |
| return upper; | |
| return sinh_small_accurate_128(x); | |
| #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS | |
| } | |
| // Handle overflow, inf, and nan for sinh(x) when |x| >= 0x1.633ce8fb9f87ep+9: | |
| LIBC_NOINLINE LIBC_INLINE double sinh_exceptional(double x) { | |
| using FPBits = fputil::FPBits<double>; | |
| FPBits xbits(x); | |
| if (xbits.is_nan()) | |
| return x + 1.0; | |
| if (xbits.is_inf()) | |
| return x; | |
| return fputil::set_overflow<double>(xbits.sign()); | |
| } | |
| LIBC_ALWAYS_INLINE double sinh(double x) { | |
| using FPBits = fputil::FPBits<double>; | |
| FPBits xbits(x); | |
| uint64_t x_abs = xbits.abs().uintval(); | |
| // Bound constants (with biased exponent and mantissa bits): | |
| #ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS | |
| // - Small bound: 2^-26 + ulp(2^-26) (exp_pm_eval handles |x| > 2^-26 directly | |
| // with k = 0 and zero cancellation when |x| < log(2) / 2) | |
| constexpr uint64_t SMALL_BOUND = 0x3e50'0000'0000'0001ULL; | |
| #else | |
| // - Small bound: 2^-2 = 0x1.0p-2 | |
| constexpr uint64_t SMALL_BOUND = 0x3fd0'0000'0000'0000ULL; | |
| #endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS | |
| // - Overflow bound: | |
| // > round(asinh(2^1023 * (2 - 2^-52)), D, RU) = 0x1.633ce8fb9f87ep+9 | |
| // > round(asinh(2^1023 * (2 - 2^-52)), D, RD) = 0x1.633ce8fb9f87dp+9 | |
| // > round(sinh(0x1.633ce8fb9f87dp+9), D, RN) = 0x1.fffffffffffffp+1023 | |
| // > round(sinh(0x1.633ce8fb9f87ep+9), D, RN) = +inf | |
| constexpr uint64_t OVERFLOW_BOUND = 0x4086'33ce'8fb9'f87eULL; | |
| if (LIBC_UNLIKELY(x_abs < SMALL_BOUND || x_abs >= OVERFLOW_BOUND)) { | |
| if (x_abs < SMALL_BOUND) | |
| return sinh_small_eval(x, x_abs); | |
| return sinh_exceptional(x); | |
| } | |
| return exp_pm_eval(x, /*is_sinh=*/true); | |
| } | |
| } // namespace double_eval | |
| } // namespace math | |
| } // namespace LIBC_NAMESPACE_DECL | |
| #endif // LLVM_LIBC_SRC___SUPPORT_MATH_SINH_DOUBLE_EVAL_H |