1 //===-- Single-precision e^x - 1 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/expm1f.h" 10 #include "common_constants.h" // Lookup tables EXP_M1 and EXP_M2. 11 #include "src/__support/FPUtil/BasicOperations.h" 12 #include "src/__support/FPUtil/FEnvImpl.h" 13 #include "src/__support/FPUtil/FMA.h" 14 #include "src/__support/FPUtil/FPBits.h" 15 #include "src/__support/FPUtil/PolyEval.h" 16 #include "src/__support/common.h" 17 18 #include <errno.h> 19 20 namespace __llvm_libc { 21 22 LLVM_LIBC_FUNCTION(float, expm1f, (float x)) { 23 using FPBits = typename fputil::FPBits<float>; 24 FPBits xbits(x); 25 26 uint32_t x_u = xbits.uintval(); 27 uint32_t x_abs = x_u & 0x7fff'ffffU; 28 29 // Exceptional value 30 if (unlikely(x_u == 0x3e35'bec5U)) { // x = 0x1.6b7d8ap-3f 31 int round_mode = fputil::get_round(); 32 if (round_mode == FE_TONEAREST || round_mode == FE_UPWARD) 33 return 0x1.8dbe64p-3f; 34 return 0x1.8dbe62p-3f; 35 } 36 37 // When |x| > 25*log(2), or nan 38 if (unlikely(x_abs >= 0x418a'a123U)) { 39 // x < log(2^-25) 40 if (xbits.get_sign()) { 41 // exp(-Inf) = 0 42 if (xbits.is_inf()) 43 return -1.0f; 44 // exp(nan) = nan 45 if (xbits.is_nan()) 46 return x; 47 int round_mode = fputil::get_round(); 48 if (round_mode == FE_UPWARD || round_mode == FE_TOWARDZERO) 49 return -0x1.ffff'fep-1f; // -1.0f + 0x1.0p-24f 50 return -1.0f; 51 } else { 52 // x >= 89 or nan 53 if (xbits.uintval() >= 0x42b2'0000) { 54 if (xbits.uintval() < 0x7f80'0000U) { 55 int rounding = fputil::get_round(); 56 if (rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO) 57 return static_cast<float>(FPBits(FPBits::MAX_NORMAL)); 58 59 errno = ERANGE; 60 } 61 return x + static_cast<float>(FPBits::inf()); 62 } 63 } 64 } 65 66 // |x| < 2^-4 67 if (x_abs < 0x3d80'0000U) { 68 // |x| < 2^-25 69 if (x_abs < 0x3300'0000U) { 70 // x = -0.0f 71 if (unlikely(xbits.uintval() == 0x8000'0000U)) 72 return x; 73 // When |x| < 2^-25, the relative error of the approximation e^x - 1 ~ x 74 // is: 75 // |(e^x - 1) - x| / |e^x - 1| < |x^2| / |x| 76 // = |x| 77 // < 2^-25 78 // < epsilon(1)/2. 79 // So the correctly rounded values of expm1(x) are: 80 // = x + eps(x) if rounding mode = FE_UPWARD, 81 // or (rounding mode = FE_TOWARDZERO and x is negative), 82 // = x otherwise. 83 // To simplify the rounding decision and make it more efficient, we use 84 // fma(x, x, x) ~ x + x^2 instead. 85 return fputil::multiply_add(x, x, x); 86 } 87 88 // 2^-25 <= |x| < 2^-4 89 double xd = static_cast<double>(x); 90 double xsq = xd * xd; 91 // Degree-8 minimax polynomial generated by Sollya with: 92 // > display = hexadecimal; 93 // > P = fpminimax((expm1(x) - x)/x^2, 6, [|D...|], [-2^-4, 2^-4]); 94 double r = 95 fputil::polyeval(xd, 0x1p-1, 0x1.55555555557ddp-3, 0x1.55555555552fap-5, 96 0x1.111110fcd58b7p-7, 0x1.6c16c1717660bp-10, 97 0x1.a0241f0006d62p-13, 0x1.a01e3f8d3c06p-16); 98 return static_cast<float>(fputil::multiply_add(r, xsq, xd)); 99 } 100 101 // For -18 < x < 89, to compute expm1(x), we perform the following range 102 // reduction: find hi, mid, lo such that: 103 // x = hi + mid + lo, in which 104 // hi is an integer, 105 // mid * 2^7 is an integer 106 // -2^(-8) <= lo < 2^-8. 107 // In particular, 108 // hi + mid = round(x * 2^7) * 2^(-7). 109 // Then, 110 // expm1(x) = exp(hi + mid + lo) - 1 = exp(hi) * exp(mid) * exp(lo) - 1. 111 // We store exp(hi) and exp(mid) in the lookup tables EXP_M1 and EXP_M2 112 // respectively. exp(lo) is computed using a degree-4 minimax polynomial 113 // generated by Sollya. 114 115 // x_hi = hi + mid. 116 int x_hi = static_cast<int>(x * 0x1.0p7f + (xbits.get_sign() ? -0.5f : 0.5f)); 117 // Subtract (hi + mid) from x to get lo. 118 x -= static_cast<float>(x_hi) * 0x1.0p-7f; 119 double xd = static_cast<double>(x); 120 x_hi += 104 << 7; 121 // hi = x_hi >> 7 122 double exp_hi = EXP_M1[x_hi >> 7]; 123 // lo = x_hi & 0x0000'007fU; 124 double exp_mid = EXP_M2[x_hi & 0x7f]; 125 double exp_hi_mid = exp_hi * exp_mid; 126 // Degree-4 minimax polynomial generated by Sollya with the following 127 // commands: 128 // > display = hexadecimal; 129 // > Q = fpminimax(expm1(x)/x, 3, [|D...|], [-2^-8, 2^-8]); 130 // > Q; 131 double exp_lo = 132 fputil::polyeval(xd, 0x1.0p0, 0x1.ffffffffff777p-1, 0x1.000000000071cp-1, 133 0x1.555566668e5e7p-3, 0x1.55555555ef243p-5); 134 return static_cast<float>(fputil::multiply_add(exp_hi_mid, exp_lo, -1.0)); 135 } 136 137 } // namespace __llvm_libc 138