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