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