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