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