|
| 1 | +//===-- Implementation header for expm1f ------------------------*- C++ -*-===// |
| 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_EXPM1F_H |
| 10 | +#define LLVM_LIBC_SRC___SUPPORT_MATH_EXPM1F_H |
| 11 | + |
| 12 | +#include "common_constants.h" // Lookup tables EXP_M1 and EXP_M2. |
| 13 | +#include "src/__support/FPUtil/BasicOperations.h" |
| 14 | +#include "src/__support/FPUtil/FEnvImpl.h" |
| 15 | +#include "src/__support/FPUtil/FMA.h" |
| 16 | +#include "src/__support/FPUtil/FPBits.h" |
| 17 | +#include "src/__support/FPUtil/PolyEval.h" |
| 18 | +#include "src/__support/FPUtil/multiply_add.h" |
| 19 | +#include "src/__support/FPUtil/nearest_integer.h" |
| 20 | +#include "src/__support/FPUtil/rounding_mode.h" |
| 21 | +#include "src/__support/common.h" |
| 22 | +#include "src/__support/macros/config.h" |
| 23 | +#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY |
| 24 | +#include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA |
| 25 | + |
| 26 | +namespace LIBC_NAMESPACE_DECL { |
| 27 | + |
| 28 | +namespace math { |
| 29 | + |
| 30 | +LIBC_INLINE static constexpr float expm1f(float x) { |
| 31 | + using namespace common_constants_internal; |
| 32 | + using FPBits = typename fputil::FPBits<float>; |
| 33 | + FPBits xbits(x); |
| 34 | + |
| 35 | + uint32_t x_u = xbits.uintval(); |
| 36 | + uint32_t x_abs = x_u & 0x7fff'ffffU; |
| 37 | + |
| 38 | +#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 39 | + // Exceptional value |
| 40 | + if (LIBC_UNLIKELY(x_u == 0x3e35'bec5U)) { // x = 0x1.6b7d8ap-3f |
| 41 | + int round_mode = fputil::quick_get_round(); |
| 42 | + if (round_mode == FE_TONEAREST || round_mode == FE_UPWARD) |
| 43 | + return 0x1.8dbe64p-3f; |
| 44 | + return 0x1.8dbe62p-3f; |
| 45 | + } |
| 46 | +#if !defined(LIBC_TARGET_CPU_HAS_FMA_DOUBLE) |
| 47 | + if (LIBC_UNLIKELY(x_u == 0xbdc1'c6cbU)) { // x = -0x1.838d96p-4f |
| 48 | + int round_mode = fputil::quick_get_round(); |
| 49 | + if (round_mode == FE_TONEAREST || round_mode == FE_DOWNWARD) |
| 50 | + return -0x1.71c884p-4f; |
| 51 | + return -0x1.71c882p-4f; |
| 52 | + } |
| 53 | +#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
| 54 | +#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 55 | + |
| 56 | + // When |x| > 25*log(2), or nan |
| 57 | + if (LIBC_UNLIKELY(x_abs >= 0x418a'a123U)) { |
| 58 | + // x < log(2^-25) |
| 59 | + if (xbits.is_neg()) { |
| 60 | + // exp(-Inf) = 0 |
| 61 | + if (xbits.is_inf()) |
| 62 | + return -1.0f; |
| 63 | + // exp(nan) = nan |
| 64 | + if (xbits.is_nan()) |
| 65 | + return x; |
| 66 | + int round_mode = fputil::quick_get_round(); |
| 67 | + if (round_mode == FE_UPWARD || round_mode == FE_TOWARDZERO) |
| 68 | + return -0x1.ffff'fep-1f; // -1.0f + 0x1.0p-24f |
| 69 | + return -1.0f; |
| 70 | + } else { |
| 71 | + // x >= 89 or nan |
| 72 | + if (xbits.uintval() >= 0x42b2'0000) { |
| 73 | + if (xbits.uintval() < 0x7f80'0000U) { |
| 74 | + int rounding = fputil::quick_get_round(); |
| 75 | + if (rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO) |
| 76 | + return FPBits::max_normal().get_val(); |
| 77 | + |
| 78 | + fputil::set_errno_if_required(ERANGE); |
| 79 | + fputil::raise_except_if_required(FE_OVERFLOW); |
| 80 | + } |
| 81 | + return x + FPBits::inf().get_val(); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // |x| < 2^-4 |
| 87 | + if (x_abs < 0x3d80'0000U) { |
| 88 | + // |x| < 2^-25 |
| 89 | + if (x_abs < 0x3300'0000U) { |
| 90 | + // x = -0.0f |
| 91 | + if (LIBC_UNLIKELY(xbits.uintval() == 0x8000'0000U)) |
| 92 | + return x; |
| 93 | + // When |x| < 2^-25, the relative error of the approximation e^x - 1 ~ x |
| 94 | + // is: |
| 95 | + // |(e^x - 1) - x| / |e^x - 1| < |x^2| / |x| |
| 96 | + // = |x| |
| 97 | + // < 2^-25 |
| 98 | + // < epsilon(1)/2. |
| 99 | + // So the correctly rounded values of expm1(x) are: |
| 100 | + // = x + eps(x) if rounding mode = FE_UPWARD, |
| 101 | + // or (rounding mode = FE_TOWARDZERO and x is |
| 102 | + // negative), |
| 103 | + // = x otherwise. |
| 104 | + // To simplify the rounding decision and make it more efficient, we use |
| 105 | + // fma(x, x, x) ~ x + x^2 instead. |
| 106 | + // Note: to use the formula x + x^2 to decide the correct rounding, we |
| 107 | + // do need fma(x, x, x) to prevent underflow caused by x*x when |x| < |
| 108 | + // 2^-76. For targets without FMA instructions, we simply use double for |
| 109 | + // intermediate results as it is more efficient than using an emulated |
| 110 | + // version of FMA. |
| 111 | +#if defined(LIBC_TARGET_CPU_HAS_FMA_FLOAT) |
| 112 | + return fputil::multiply_add(x, x, x); |
| 113 | +#else |
| 114 | + double xd = x; |
| 115 | + return static_cast<float>(fputil::multiply_add(xd, xd, xd)); |
| 116 | +#endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT |
| 117 | + } |
| 118 | + |
| 119 | + constexpr double COEFFS[] = {0x1p-1, |
| 120 | + 0x1.55555555557ddp-3, |
| 121 | + 0x1.55555555552fap-5, |
| 122 | + 0x1.111110fcd58b7p-7, |
| 123 | + 0x1.6c16c1717660bp-10, |
| 124 | + 0x1.a0241f0006d62p-13, |
| 125 | + 0x1.a01e3f8d3c06p-16}; |
| 126 | + |
| 127 | + // 2^-25 <= |x| < 2^-4 |
| 128 | + double xd = static_cast<double>(x); |
| 129 | + double xsq = xd * xd; |
| 130 | + // Degree-8 minimax polynomial generated by Sollya with: |
| 131 | + // > display = hexadecimal; |
| 132 | + // > P = fpminimax((expm1(x) - x)/x^2, 6, [|D...|], [-2^-4, 2^-4]); |
| 133 | + |
| 134 | + double c0 = fputil::multiply_add(xd, COEFFS[1], COEFFS[0]); |
| 135 | + double c1 = fputil::multiply_add(xd, COEFFS[3], COEFFS[2]); |
| 136 | + double c2 = fputil::multiply_add(xd, COEFFS[5], COEFFS[4]); |
| 137 | + |
| 138 | + double r = fputil::polyeval(xsq, c0, c1, c2, COEFFS[6]); |
| 139 | + return static_cast<float>(fputil::multiply_add(r, xsq, xd)); |
| 140 | + } |
| 141 | + |
| 142 | + // For -18 < x < 89, to compute expm1(x), we perform the following range |
| 143 | + // reduction: find hi, mid, lo such that: |
| 144 | + // x = hi + mid + lo, in which |
| 145 | + // hi is an integer, |
| 146 | + // mid * 2^7 is an integer |
| 147 | + // -2^(-8) <= lo < 2^-8. |
| 148 | + // In particular, |
| 149 | + // hi + mid = round(x * 2^7) * 2^(-7). |
| 150 | + // Then, |
| 151 | + // expm1(x) = exp(hi + mid + lo) - 1 = exp(hi) * exp(mid) * exp(lo) - 1. |
| 152 | + // We store exp(hi) and exp(mid) in the lookup tables EXP_M1 and EXP_M2 |
| 153 | + // respectively. exp(lo) is computed using a degree-4 minimax polynomial |
| 154 | + // generated by Sollya. |
| 155 | + |
| 156 | + // x_hi = hi + mid. |
| 157 | + float kf = fputil::nearest_integer(x * 0x1.0p7f); |
| 158 | + int x_hi = static_cast<int>(kf); |
| 159 | + // Subtract (hi + mid) from x to get lo. |
| 160 | + double xd = static_cast<double>(fputil::multiply_add(kf, -0x1.0p-7f, x)); |
| 161 | + x_hi += 104 << 7; |
| 162 | + // hi = x_hi >> 7 |
| 163 | + double exp_hi = EXP_M1[x_hi >> 7]; |
| 164 | + // lo = x_hi & 0x0000'007fU; |
| 165 | + double exp_mid = EXP_M2[x_hi & 0x7f]; |
| 166 | + double exp_hi_mid = exp_hi * exp_mid; |
| 167 | + // Degree-4 minimax polynomial generated by Sollya with the following |
| 168 | + // commands: |
| 169 | + // > display = hexadecimal; |
| 170 | + // > Q = fpminimax(expm1(x)/x, 3, [|D...|], [-2^-8, 2^-8]); |
| 171 | + // > Q; |
| 172 | + double exp_lo = |
| 173 | + fputil::polyeval(xd, 0x1.0p0, 0x1.ffffffffff777p-1, 0x1.000000000071cp-1, |
| 174 | + 0x1.555566668e5e7p-3, 0x1.55555555ef243p-5); |
| 175 | + return static_cast<float>(fputil::multiply_add(exp_hi_mid, exp_lo, -1.0)); |
| 176 | +} |
| 177 | + |
| 178 | +} // namespace math |
| 179 | + |
| 180 | +} // namespace LIBC_NAMESPACE_DECL |
| 181 | + |
| 182 | +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXPM1F_H |
0 commit comments