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