1//===-- Implementation header for expm1 -------------------------*- 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_EXPM1_H
10#define LLVM_LIBC_SRC___SUPPORT_MATH_EXPM1_H
11
12#include "src/__support/CPP/bit.h"
13#include "src/__support/FPUtil/FPBits.h"
14#include "src/__support/FPUtil/double_double.h"
15#include "src/__support/FPUtil/dyadic_float.h"
16#include "src/__support/FPUtil/except_value_utils.h"
17#include "src/__support/FPUtil/multiply_add.h"
18#include "src/__support/FPUtil/polyeval.h"
19#include "src/__support/common.h"
20#include "src/__support/integer_literals.h"
21#include "src/__support/macros/config.h"
22#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
23#include "src/__support/math/exp_constants.h"
24
25namespace LIBC_NAMESPACE_DECL {
26
27namespace math {
28
29namespace expm1_internal {
30
31#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
32#define LIBC_MATH_EXPM1_SKIP_ACCURATE_PASS
33#endif
34
35using fputil::DoubleDouble;
36using DFloat128 = typename fputil::DyadicFloat<128>;
37
38using LIBC_NAMESPACE::operator""_u128;
39
40// log2(e)
41LIBC_INLINE_VAR constexpr double LOG2_E = 0x1.71547652b82fep+0;
42
43// Error bounds:
44// Errors when using double precision.
45// 0x1.8p-63;
46LIBC_INLINE_VAR constexpr uint64_t ERR_D = 0x3c08000000000000;
47// Errors when using double-double precision.
48// 0x1.0p-99
49[[maybe_unused]] LIBC_INLINE_VAR constexpr uint64_t ERR_DD = 0x39c0000000000000;
50
51// -2^-12 * log(2)
52// > a = -2^-12 * log(2);
53// > b = round(a, 30, RN);
54// > c = round(a - b, 30, RN);
55// > d = round(a - b - c, D, RN);
56// Errors < 1.5 * 2^-133
57LIBC_INLINE_VAR constexpr double MLOG_2_EXP2_M12_HI = -0x1.62e42ffp-13;
58LIBC_INLINE_VAR constexpr double MLOG_2_EXP2_M12_MID = 0x1.718432a1b0e26p-47;
59LIBC_INLINE_VAR constexpr double MLOG_2_EXP2_M12_MID_30 = 0x1.718432ap-47;
60LIBC_INLINE_VAR constexpr double MLOG_2_EXP2_M12_LO = 0x1.b0e2633fe0685p-79;
61
62// Polynomial approximations with double precision:
63// Return expm1(dx) / x ~ 1 + dx / 2 + dx^2 / 6 + dx^3 / 24.
64// For |dx| < 2^-13 + 2^-30:
65// | output - expm1(dx) / dx | < 2^-51.
66LIBC_INLINE double poly_approx_d(double dx) {
67 // dx^2
68 double dx2 = dx * dx;
69 // c0 = 1 + dx / 2
70 double c0 = fputil::multiply_add(x: dx, y: 0.5, z: 1.0);
71 // c1 = 1/6 + dx / 24
72 double c1 =
73 fputil::multiply_add(x: dx, y: 0x1.5555555555555p-5, z: 0x1.5555555555555p-3);
74 // p = dx^2 * c1 + c0 = 1 + dx / 2 + dx^2 / 6 + dx^3 / 24
75 double p = fputil::multiply_add(x: dx2, y: c1, z: c0);
76 return p;
77}
78
79// Polynomial approximation with double-double precision:
80// Return expm1(dx) / dx ~ 1 + dx / 2 + dx^2 / 6 + ... + dx^6 / 5040
81// For |dx| < 2^-13 + 2^-30:
82// | output - expm1(dx) | < 2^-101
83LIBC_INLINE DoubleDouble poly_approx_dd(const DoubleDouble &dx) {
84 // Taylor polynomial.
85 constexpr DoubleDouble COEFFS[] = {
86 {.lo: 0, .hi: 0x1p0}, // 1
87 {.lo: 0, .hi: 0x1p-1}, // 1/2
88 {.lo: 0x1.5555555555555p-57, .hi: 0x1.5555555555555p-3}, // 1/6
89 {.lo: 0x1.5555555555555p-59, .hi: 0x1.5555555555555p-5}, // 1/24
90 {.lo: 0x1.1111111111111p-63, .hi: 0x1.1111111111111p-7}, // 1/120
91 {.lo: -0x1.f49f49f49f49fp-65, .hi: 0x1.6c16c16c16c17p-10}, // 1/720
92 {.lo: 0x1.a01a01a01a01ap-73, .hi: 0x1.a01a01a01a01ap-13}, // 1/5040
93 };
94
95 DoubleDouble p = fputil::polyeval(x: dx, a0: COEFFS[0], a: COEFFS[1], a: COEFFS[2],
96 a: COEFFS[3], a: COEFFS[4], a: COEFFS[5], a: COEFFS[6]);
97 return p;
98}
99
100// Polynomial approximation with 128-bit precision:
101// Return (exp(dx) - 1)/dx ~ 1 + dx / 2 + dx^2 / 6 + ... + dx^6 / 5040
102// For |dx| < 2^-13 + 2^-30:
103// | output - exp(dx) | < 2^-126.
104[[maybe_unused]] LIBC_INLINE DFloat128 poly_approx_f128(const DFloat128 &dx) {
105 constexpr DFloat128 COEFFS_128[]{
106 {Sign::POS, -127, 0x80000000'00000000'00000000'00000000_u128}, // 1.0
107 {Sign::POS, -128, 0x80000000'00000000'00000000'00000000_u128}, // 0.5
108 {Sign::POS, -130, 0xaaaaaaaa'aaaaaaaa'aaaaaaaa'aaaaaaab_u128}, // 1/6
109 {Sign::POS, -132, 0xaaaaaaaa'aaaaaaaa'aaaaaaaa'aaaaaaab_u128}, // 1/24
110 {Sign::POS, -134, 0x88888888'88888888'88888888'88888889_u128}, // 1/120
111 {Sign::POS, -137, 0xb60b60b6'0b60b60b'60b60b60'b60b60b6_u128}, // 1/720
112 {Sign::POS, -140, 0xd00d00d0'0d00d00d'00d00d00'd00d00d0_u128}, // 1/5040
113 };
114
115 DFloat128 p = fputil::polyeval(x: dx, a0: COEFFS_128[0], a: COEFFS_128[1],
116 a: COEFFS_128[2], a: COEFFS_128[3], a: COEFFS_128[4],
117 a: COEFFS_128[5], a: COEFFS_128[6]);
118 return p;
119}
120
121#ifdef DEBUGDEBUG
122LIBC_INLINE std::ostream &operator<<(std::ostream &OS, const DFloat128 &r) {
123 OS << (r.sign == Sign::NEG ? "-(" : "(") << r.mantissa.val[0] << " + "
124 << r.mantissa.val[1] << " * 2^64) * 2^" << r.exponent << "\n";
125 return OS;
126}
127
128LIBC_INLINE std::ostream &operator<<(std::ostream &OS, const DoubleDouble &r) {
129 OS << std::hexfloat << "(" << r.hi << " + " << r.lo << ")"
130 << std::defaultfloat << "\n";
131 return OS;
132}
133#endif
134
135// Compute exp(x) - 1 using 128-bit precision.
136// TODO(lntue): investigate triple-double precision implementation for this
137// step.
138[[maybe_unused]] LIBC_INLINE DFloat128 expm1_f128(double x, double kd, int idx1,
139 int idx2) {
140 // Recalculate dx:
141
142 double t1 = fputil::multiply_add(x: kd, y: MLOG_2_EXP2_M12_HI, z: x); // exact
143 double t2 = kd * MLOG_2_EXP2_M12_MID_30; // exact
144 double t3 = kd * MLOG_2_EXP2_M12_LO; // Error < 2^-133
145
146 DFloat128 dx = fputil::quick_add(
147 a: DFloat128(t1), b: fputil::quick_add(a: DFloat128(t2), b: DFloat128(t3)));
148
149 // TODO: Skip recalculating exp_mid1 and exp_mid2.
150 DFloat128 exp_mid1 =
151 fputil::quick_add(a: DFloat128(EXP2_MID1[idx1].hi),
152 b: fputil::quick_add(a: DFloat128(EXP2_MID1[idx1].mid),
153 b: DFloat128(EXP2_MID1[idx1].lo)));
154
155 DFloat128 exp_mid2 =
156 fputil::quick_add(a: DFloat128(EXP2_MID2[idx2].hi),
157 b: fputil::quick_add(a: DFloat128(EXP2_MID2[idx2].mid),
158 b: DFloat128(EXP2_MID2[idx2].lo)));
159
160 DFloat128 exp_mid = fputil::quick_mul(a: exp_mid1, b: exp_mid2);
161
162 int hi = static_cast<int>(kd) >> 12;
163 DFloat128 minus_one{Sign::NEG, -127 - hi,
164 0x80000000'00000000'00000000'00000000_u128};
165
166 DFloat128 exp_mid_m1 = fputil::quick_add(a: exp_mid, b: minus_one);
167
168 DFloat128 p = poly_approx_f128(dx);
169
170 // r = exp_mid * (1 + dx * P) - 1
171 // = (exp_mid - 1) + (dx * exp_mid) * P
172 DFloat128 r =
173 fputil::multiply_add(a: fputil::quick_mul(a: exp_mid, b: dx), b: p, c: exp_mid_m1);
174
175 r.exponent += hi;
176
177#ifdef DEBUGDEBUG
178 std::cout << "=== VERY SLOW PASS ===\n"
179 << " kd: " << kd << "\n"
180 << " hi: " << hi << "\n"
181 << " minus_one: " << minus_one << " dx: " << dx
182 << "exp_mid_m1: " << exp_mid_m1 << " exp_mid: " << exp_mid
183 << " p: " << p << " r: " << r << std::endl;
184#endif
185
186 return r;
187}
188
189// Compute exp(x) - 1 with double-double precision.
190LIBC_INLINE DoubleDouble exp_double_double(double x, double kd,
191 const DoubleDouble &exp_mid,
192 const DoubleDouble &hi_part) {
193 // Recalculate dx:
194 // dx = x - k * 2^-12 * log(2)
195 double t1 = fputil::multiply_add(x: kd, y: MLOG_2_EXP2_M12_HI, z: x); // exact
196 double t2 = kd * MLOG_2_EXP2_M12_MID_30; // exact
197 double t3 = kd * MLOG_2_EXP2_M12_LO; // Error < 2^-130
198
199 DoubleDouble dx = fputil::exact_add(a: t1, b: t2);
200 dx.lo += t3;
201
202 // Degree-6 Taylor polynomial approximation in double-double precision.
203 // | p - exp(x) | < 2^-100.
204 DoubleDouble p = poly_approx_dd(dx);
205
206 // Error bounds: 2^-99.
207 DoubleDouble r =
208 fputil::multiply_add(a: fputil::quick_mult(a: exp_mid, b: dx), b: p, c: hi_part);
209
210#ifdef DEBUGDEBUG
211 std::cout << "=== SLOW PASS ===\n"
212 << " dx: " << dx << " p: " << p << " r: " << r << std::endl;
213#endif
214
215 return r;
216}
217
218// Check for exceptional cases when
219// |x| <= 2^-53 or x < log(2^-54) or x >= 0x1.6232bdd7abcd3p+9
220LIBC_INLINE double set_exceptional(double x) {
221 using FPBits = typename fputil::FPBits<double>;
222 FPBits xbits(x);
223
224 uint64_t x_u = xbits.uintval();
225 uint64_t x_abs = xbits.abs().uintval();
226
227 // |x| <= 2^-53.
228 if (x_abs <= 0x3ca0'0000'0000'0000ULL) {
229 // expm1(x) ~ x.
230
231 if (LIBC_UNLIKELY(x_abs <= 0x0370'0000'0000'0000ULL)) {
232 if (LIBC_UNLIKELY(x_abs == 0))
233 return x;
234 // |x| <= 2^-968, need to scale up a bit before rounding, then scale it
235 // back down.
236 return 0x1.0p-200 * fputil::multiply_add(x, y: 0x1.0p+200, z: 0x1.0p-1022);
237 }
238
239 // 2^-968 < |x| <= 2^-53.
240 return fputil::round_result_slightly_up(value_rn: x);
241 }
242
243 // x < log(2^-54) || x >= 0x1.6232bdd7abcd3p+9 or inf/nan.
244
245 // x < log(2^-54) or -inf/nan
246 if (x_u >= 0xc042'b708'8723'20e2ULL) {
247 // expm1(-Inf) = -1
248 if (xbits.is_inf())
249 return -1.0;
250
251 // exp(nan) = nan
252 if (xbits.is_nan())
253 return x;
254
255 return fputil::round_result_slightly_up(value_rn: -1.0);
256 }
257
258 // x >= round(log(MAX_NORMAL), D, RU) = 0x1.62e42fefa39fp+9 or +inf/nan
259 // x is finite
260 if (x_u < 0x7ff0'0000'0000'0000ULL)
261 return fputil::set_overflow<double>();
262 // x is +inf or nan
263 return x + FPBits::inf().get_val();
264}
265
266} // namespace expm1_internal
267
268LIBC_INLINE double expm1(double x) {
269 using namespace expm1_internal;
270
271 using FPBits = typename fputil::FPBits<double>;
272
273 FPBits xbits(x);
274
275 bool x_is_neg = xbits.is_neg();
276 uint64_t x_u = xbits.uintval();
277
278 // Upper bound: max normal number = 2^1023 * (2 - 2^-52)
279 // > round(log (2^1023 ( 2 - 2^-52 )), D, RU) = 0x1.62e42fefa39fp+9
280 // > round(log (2^1023 ( 2 - 2^-52 )), D, RD) = 0x1.62e42fefa39efp+9
281 // > round(log (2^1023 ( 2 - 2^-52 )), D, RN) = 0x1.62e42fefa39efp+9
282 // > round(exp(0x1.62e42fefa39fp+9), D, RN) = infty
283
284 // Lower bound: log(2^-54) = -0x1.2b708872320e2p5
285 // > round(log(2^-54), D, RN) = -0x1.2b708872320e2p5
286
287 // x < log(2^-54) or x >= 0x1.6232bdd7abcd3p+9 or |x| <= 2^-53.
288
289 if (LIBC_UNLIKELY(x_u >= 0xc042b708872320e2 ||
290 (x_u <= 0xbca0000000000000 && x_u >= 0x40862e42fefa39f0) ||
291 x_u <= 0x3ca0000000000000)) {
292 return set_exceptional(x);
293 }
294
295 // Now log(2^-54) <= x <= -2^-53 or 2^-53 <= x < log(2^1023 * (2 - 2^-52))
296
297 // Range reduction:
298 // Let x = log(2) * (hi + mid1 + mid2) + lo
299 // in which:
300 // hi is an integer
301 // mid1 * 2^6 is an integer
302 // mid2 * 2^12 is an integer
303 // then:
304 // exp(x) = 2^hi * 2^(mid1) * 2^(mid2) * exp(lo).
305 // With this formula:
306 // - multiplying by 2^hi is exact and cheap, simply by adding the exponent
307 // field.
308 // - 2^(mid1) and 2^(mid2) are stored in 2 x 64-element tables.
309 // - exp(lo) ~ 1 + lo + a0 * lo^2 + ...
310 //
311 // They can be defined by:
312 // hi + mid1 + mid2 = 2^(-12) * round(2^12 * log_2(e) * x)
313 // If we store L2E = round(log2(e), D, RN), then:
314 // log2(e) - L2E ~ 1.5 * 2^(-56)
315 // So the errors when computing in double precision is:
316 // | x * 2^12 * log_2(e) - D(x * 2^12 * L2E) | <=
317 // <= | x * 2^12 * log_2(e) - x * 2^12 * L2E | +
318 // + | x * 2^12 * L2E - D(x * 2^12 * L2E) |
319 // <= 2^12 * ( |x| * 1.5 * 2^-56 + eps(x)) for RN
320 // 2^12 * ( |x| * 1.5 * 2^-56 + 2*eps(x)) for other rounding modes.
321 // So if:
322 // hi + mid1 + mid2 = 2^(-12) * round(x * 2^12 * L2E) is computed entirely
323 // in double precision, the reduced argument:
324 // lo = x - log(2) * (hi + mid1 + mid2) is bounded by:
325 // |lo| <= 2^-13 + (|x| * 1.5 * 2^-56 + 2*eps(x))
326 // < 2^-13 + (1.5 * 2^9 * 1.5 * 2^-56 + 2*2^(9 - 52))
327 // < 2^-13 + 2^-41
328 //
329
330 // The following trick computes the round(x * L2E) more efficiently
331 // than using the rounding instructions, with the tradeoff for less accuracy,
332 // and hence a slightly larger range for the reduced argument `lo`.
333 //
334 // To be precise, since |x| < |log(2^-1075)| < 1.5 * 2^9,
335 // |x * 2^12 * L2E| < 1.5 * 2^9 * 1.5 < 2^23,
336 // So we can fit the rounded result round(x * 2^12 * L2E) in int32_t.
337 // Thus, the goal is to be able to use an additional addition and fixed width
338 // shift to get an int32_t representing round(x * 2^12 * L2E).
339 //
340 // Assuming int32_t using 2-complement representation, since the mantissa part
341 // of a double precision is unsigned with the leading bit hidden, if we add an
342 // extra constant C = 2^e1 + 2^e2 with e1 > e2 >= 2^25 to the product, the
343 // part that are < 2^e2 in resulted mantissa of (x*2^12*L2E + C) can be
344 // considered as a proper 2-complement representations of x*2^12*L2E.
345 //
346 // One small problem with this approach is that the sum (x*2^12*L2E + C) in
347 // double precision is rounded to the least significant bit of the dorminant
348 // factor C. In order to minimize the rounding errors from this addition, we
349 // want to minimize e1. Another constraint that we want is that after
350 // shifting the mantissa so that the least significant bit of int32_t
351 // corresponds to the unit bit of (x*2^12*L2E), the sign is correct without
352 // any adjustment. So combining these 2 requirements, we can choose
353 // C = 2^33 + 2^32, so that the sign bit corresponds to 2^31 bit, and hence
354 // after right shifting the mantissa, the resulting int32_t has correct sign.
355 // With this choice of C, the number of mantissa bits we need to shift to the
356 // right is: 52 - 33 = 19.
357 //
358 // Moreover, since the integer right shifts are equivalent to rounding down,
359 // we can add an extra 0.5 so that it will become round-to-nearest, tie-to-
360 // +infinity. So in particular, we can compute:
361 // hmm = x * 2^12 * L2E + C,
362 // where C = 2^33 + 2^32 + 2^-1, then if
363 // k = int32_t(lower 51 bits of double(x * 2^12 * L2E + C) >> 19),
364 // the reduced argument:
365 // lo = x - log(2) * 2^-12 * k is bounded by:
366 // |lo| <= 2^-13 + 2^-41 + 2^-12*2^-19
367 // = 2^-13 + 2^-31 + 2^-41.
368 //
369 // Finally, notice that k only uses the mantissa of x * 2^12 * L2E, so the
370 // exponent 2^12 is not needed. So we can simply define
371 // C = 2^(33 - 12) + 2^(32 - 12) + 2^(-13 - 12), and
372 // k = int32_t(lower 51 bits of double(x * L2E + C) >> 19).
373
374 // Rounding errors <= 2^-31 + 2^-41.
375 double tmp = fputil::multiply_add(x, y: LOG2_E, z: 0x1.8000'0000'4p21);
376 int k = static_cast<int>(cpp::bit_cast<uint64_t>(from: tmp) >> 19);
377 double kd = static_cast<double>(k);
378
379 uint32_t idx1 = (k >> 6) & 0x3f;
380 uint32_t idx2 = k & 0x3f;
381 int hi = k >> 12;
382
383 DoubleDouble exp_mid1{.lo: EXP2_MID1[idx1].mid, .hi: EXP2_MID1[idx1].hi};
384 DoubleDouble exp_mid2{.lo: EXP2_MID2[idx2].mid, .hi: EXP2_MID2[idx2].hi};
385
386 DoubleDouble exp_mid = fputil::quick_mult(a: exp_mid1, b: exp_mid2);
387
388 // -2^(-hi)
389 double one_scaled =
390 FPBits::create_value(sign: Sign::NEG, biased_exp: FPBits::EXP_BIAS - hi, mantissa: 0).get_val();
391
392 // 2^(mid1 + mid2) - 2^(-hi)
393 DoubleDouble hi_part = x_is_neg ? fputil::exact_add(a: one_scaled, b: exp_mid.hi)
394 : fputil::exact_add(a: exp_mid.hi, b: one_scaled);
395
396 hi_part.lo += exp_mid.lo;
397
398 // |x - (hi + mid1 + mid2) * log(2) - dx| < 2^11 * eps(M_LOG_2_EXP2_M12.lo)
399 // = 2^11 * 2^-13 * 2^-52
400 // = 2^-54.
401 // |dx| < 2^-13 + 2^-30.
402 double lo_h = fputil::multiply_add(x: kd, y: MLOG_2_EXP2_M12_HI, z: x); // exact
403 double dx = fputil::multiply_add(x: kd, y: MLOG_2_EXP2_M12_MID, z: lo_h);
404
405 // We use the degree-4 Taylor polynomial to approximate exp(lo):
406 // exp(lo) ~ 1 + lo + lo^2 / 2 + lo^3 / 6 + lo^4 / 24 = 1 + lo * P(lo)
407 // So that the errors are bounded by:
408 // |P(lo) - expm1(lo)/lo| < |lo|^4 / 64 < 2^(-13 * 4) / 64 = 2^-58
409 // Let P_ be an evaluation of P where all intermediate computations are in
410 // double precision. Using either Horner's or Estrin's schemes, the evaluated
411 // errors can be bounded by:
412 // |P_(dx) - P(dx)| < 2^-51
413 // => |dx * P_(dx) - expm1(lo) | < 1.5 * 2^-64
414 // => 2^(mid1 + mid2) * |dx * P_(dx) - expm1(lo)| < 1.5 * 2^-63.
415 // Since we approximate
416 // 2^(mid1 + mid2) ~ exp_mid.hi + exp_mid.lo,
417 // We use the expression:
418 // (exp_mid.hi + exp_mid.lo) * (1 + dx * P_(dx)) ~
419 // ~ exp_mid.hi + (exp_mid.hi * dx * P_(dx) + exp_mid.lo)
420 // with errors bounded by 1.5 * 2^-63.
421
422 // Finally, we have the following approximation formula:
423 // expm1(x) = 2^hi * 2^(mid1 + mid2) * exp(lo) - 1
424 // = 2^hi * ( 2^(mid1 + mid2) * exp(lo) - 2^(-hi) )
425 // ~ 2^hi * ( (exp_mid.hi - 2^-hi) +
426 // + (exp_mid.hi * dx * P_(dx) + exp_mid.lo))
427
428 double mid_lo = dx * exp_mid.hi;
429
430 // Approximate expm1(dx)/dx ~ 1 + dx / 2 + dx^2 / 6 + dx^3 / 24.
431 double p = poly_approx_d(dx);
432
433 double lo = fputil::multiply_add(x: p, y: mid_lo, z: hi_part.lo);
434
435 // TODO: The following line leaks encoding abstraction. Use FPBits methods
436 // instead.
437 uint64_t err = x_is_neg ? (static_cast<uint64_t>(-hi) << 52) : 0;
438
439 double err_d = cpp::bit_cast<double>(from: ERR_D + err);
440
441 double upper = hi_part.hi + (lo + err_d);
442 double lower = hi_part.hi + (lo - err_d);
443
444#ifdef DEBUGDEBUG
445 std::cout << "=== FAST PASS ===\n"
446 << " x: " << std::hexfloat << x << std::defaultfloat << "\n"
447 << " k: " << k << "\n"
448 << " idx1: " << idx1 << "\n"
449 << " idx2: " << idx2 << "\n"
450 << " hi: " << hi << "\n"
451 << " dx: " << std::hexfloat << dx << std::defaultfloat << "\n"
452 << "exp_mid: " << exp_mid << "hi_part: " << hi_part
453 << " mid_lo: " << std::hexfloat << mid_lo << std::defaultfloat
454 << "\n"
455 << " p: " << std::hexfloat << p << std::defaultfloat << "\n"
456 << " lo: " << std::hexfloat << lo << std::defaultfloat << "\n"
457 << " upper: " << std::hexfloat << upper << std::defaultfloat
458 << "\n"
459 << " lower: " << std::hexfloat << lower << std::defaultfloat
460 << "\n"
461 << std::endl;
462#endif
463
464 if (LIBC_LIKELY(upper == lower)) {
465 // to multiply by 2^hi, a fast way is to simply add hi to the exponent
466 // field.
467 int64_t exp_hi = static_cast<int64_t>(hi) * (1LL << FPBits::FRACTION_LEN);
468 double r = cpp::bit_cast<double>(from: exp_hi + cpp::bit_cast<int64_t>(from: upper));
469 return r;
470 }
471
472 // Use double-double
473 DoubleDouble r_dd = exp_double_double(x, kd, exp_mid, hi_part);
474
475#ifdef LIBC_MATH_EXPM1_SKIP_ACCURATE_PASS
476 int64_t exp_hi = static_cast<int64_t>(hi) << FPBits::FRACTION_LEN;
477 double r =
478 cpp::bit_cast<double>(exp_hi + cpp::bit_cast<int64_t>(r_dd.hi + r_dd.lo));
479 return r;
480#else
481 double err_dd = cpp::bit_cast<double>(from: ERR_DD + err);
482
483 double upper_dd = r_dd.hi + (r_dd.lo + err_dd);
484 double lower_dd = r_dd.hi + (r_dd.lo - err_dd);
485
486 if (LIBC_LIKELY(upper_dd == lower_dd)) {
487 int64_t exp_hi = static_cast<int64_t>(hi) * (1LL << FPBits::FRACTION_LEN);
488 double r = cpp::bit_cast<double>(from: exp_hi + cpp::bit_cast<int64_t>(from: upper_dd));
489 return r;
490 }
491
492 // Use 128-bit precision
493 DFloat128 r_f128 = expm1_f128(x, kd, idx1, idx2);
494
495 return static_cast<double>(r_f128);
496#endif // LIBC_MATH_EXPM1_SKIP_ACCURATE_PASS
497}
498
499} // namespace math
500
501} // namespace LIBC_NAMESPACE_DECL
502
503#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXPM1_H
504