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