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

source code of libc/src/math/generic/expm1f.cpp