| 1 | //===-- Implementation header for expf --------------------------*- 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_EXPF_H |
| 10 | #define LLVM_LIBC_SRC___SUPPORT_MATH_EXPF_H |
| 11 | |
| 12 | #include "exp_float_constants.h" // Lookup tables EXP_M1 and EXP_M2. |
| 13 | #include "src/__support/FPUtil/FEnvImpl.h" |
| 14 | #include "src/__support/FPUtil/FPBits.h" |
| 15 | #include "src/__support/FPUtil/PolyEval.h" |
| 16 | #include "src/__support/FPUtil/multiply_add.h" |
| 17 | #include "src/__support/FPUtil/nearest_integer.h" |
| 18 | #include "src/__support/FPUtil/rounding_mode.h" |
| 19 | #include "src/__support/common.h" |
| 20 | #include "src/__support/macros/config.h" |
| 21 | #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY |
| 22 | |
| 23 | namespace LIBC_NAMESPACE_DECL { |
| 24 | |
| 25 | namespace math { |
| 26 | |
| 27 | LIBC_INLINE float expf(float x) { |
| 28 | using FPBits = typename fputil::FPBits<float>; |
| 29 | FPBits xbits(x); |
| 30 | |
| 31 | uint32_t x_u = xbits.uintval(); |
| 32 | uint32_t x_abs = x_u & 0x7fff'ffffU; |
| 33 | |
| 34 | #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 35 | // Exceptional values |
| 36 | if (LIBC_UNLIKELY(x_u == 0xc236'bd8cU)) { // x = -0x1.6d7b18p+5f |
| 37 | return 0x1.108a58p-66f - x * 0x1.0p-95f; |
| 38 | } |
| 39 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 40 | |
| 41 | // When |x| >= 89, |x| < 2^-25, or x is nan |
| 42 | if (LIBC_UNLIKELY(x_abs >= 0x42b2'0000U || x_abs <= 0x3280'0000U)) { |
| 43 | // |x| < 2^-25 |
| 44 | if (xbits.get_biased_exponent() <= 101) { |
| 45 | return 1.0f + x; |
| 46 | } |
| 47 | |
| 48 | // When x < log(2^-150) or nan |
| 49 | if (xbits.uintval() >= 0xc2cf'f1b5U) { |
| 50 | // exp(-Inf) = 0 |
| 51 | if (xbits.is_inf()) |
| 52 | return 0.0f; |
| 53 | // exp(nan) = nan |
| 54 | if (xbits.is_nan()) |
| 55 | return x; |
| 56 | #ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY |
| 57 | if (fputil::fenv_is_round_up()) |
| 58 | return FPBits::min_subnormal().get_val(); |
| 59 | #endif |
| 60 | fputil::set_errno_if_required(ERANGE); |
| 61 | fputil::raise_except_if_required(FE_UNDERFLOW); |
| 62 | return 0.0f; |
| 63 | } |
| 64 | // x >= 89 or nan |
| 65 | if (xbits.is_pos() && (xbits.uintval() >= 0x42b2'0000)) { |
| 66 | // x is finite |
| 67 | if (xbits.uintval() < 0x7f80'0000U) { |
| 68 | #ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY |
| 69 | int rounding = fputil::quick_get_round(); |
| 70 | if (rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO) |
| 71 | return FPBits::max_normal().get_val(); |
| 72 | #endif |
| 73 | |
| 74 | fputil::set_errno_if_required(ERANGE); |
| 75 | fputil::raise_except_if_required(FE_OVERFLOW); |
| 76 | } |
| 77 | // x is +inf or nan |
| 78 | return x + FPBits::inf().get_val(); |
| 79 | } |
| 80 | } |
| 81 | // For -104 < x < 89, to compute exp(x), we perform the following range |
| 82 | // reduction: find hi, mid, lo such that: |
| 83 | // x = hi + mid + lo, in which |
| 84 | // hi is an integer, |
| 85 | // mid * 2^7 is an integer |
| 86 | // -2^(-8) <= lo < 2^-8. |
| 87 | // In particular, |
| 88 | // hi + mid = round(x * 2^7) * 2^(-7). |
| 89 | // Then, |
| 90 | // exp(x) = exp(hi + mid + lo) = exp(hi) * exp(mid) * exp(lo). |
| 91 | // We store exp(hi) and exp(mid) in the lookup tables EXP_M1 and EXP_M2 |
| 92 | // respectively. exp(lo) is computed using a degree-4 minimax polynomial |
| 93 | // generated by Sollya. |
| 94 | |
| 95 | // x_hi = (hi + mid) * 2^7 = round(x * 2^7). |
| 96 | float kf = fputil::nearest_integer(x: x * 0x1.0p7f); |
| 97 | // Subtract (hi + mid) from x to get lo. |
| 98 | double xd = static_cast<double>(fputil::multiply_add(x: kf, y: -0x1.0p-7f, z: x)); |
| 99 | int x_hi = static_cast<int>(kf); |
| 100 | x_hi += 104 << 7; |
| 101 | // hi = x_hi >> 7 |
| 102 | double exp_hi = EXP_M1[x_hi >> 7]; |
| 103 | // mid * 2^7 = x_hi & 0x0000'007fU; |
| 104 | double exp_mid = EXP_M2[x_hi & 0x7f]; |
| 105 | // Degree-4 minimax polynomial generated by Sollya with the following |
| 106 | // commands: |
| 107 | // > display = hexadecimal; |
| 108 | // > Q = fpminimax(expm1(x)/x, 3, [|D...|], [-2^-8, 2^-8]); |
| 109 | // > Q; |
| 110 | double exp_lo = |
| 111 | fputil::polyeval(x: xd, a0: 0x1p0, a: 0x1.ffffffffff777p-1, a: 0x1.000000000071cp-1, |
| 112 | a: 0x1.555566668e5e7p-3, a: 0x1.55555555ef243p-5); |
| 113 | return static_cast<float>(exp_hi * exp_mid * exp_lo); |
| 114 | } |
| 115 | |
| 116 | } // namespace math |
| 117 | |
| 118 | } // namespace LIBC_NAMESPACE_DECL |
| 119 | |
| 120 | #endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXPF_H |
| 121 | |