1//===-- Implementation header for exp10 ------------------------*- 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_EXP10_H
10#define LLVM_LIBC_SRC___SUPPORT_MATH_EXP10_H
11
12#include "exp_constants.h" // Lookup tables EXP2_MID1 and EXP_M2.
13#include "exp_utils.h" // ziv_test_denorm.
14#include "src/__support/CPP/bit.h"
15#include "src/__support/CPP/optional.h"
16#include "src/__support/FPUtil/FEnvImpl.h"
17#include "src/__support/FPUtil/FPBits.h"
18#include "src/__support/FPUtil/PolyEval.h"
19#include "src/__support/FPUtil/double_double.h"
20#include "src/__support/FPUtil/dyadic_float.h"
21#include "src/__support/FPUtil/multiply_add.h"
22#include "src/__support/FPUtil/nearest_integer.h"
23#include "src/__support/FPUtil/rounding_mode.h"
24#include "src/__support/FPUtil/triple_double.h"
25#include "src/__support/common.h"
26#include "src/__support/integer_literals.h"
27#include "src/__support/macros/config.h"
28#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
29
30namespace LIBC_NAMESPACE_DECL {
31
32using fputil::DoubleDouble;
33using fputil::TripleDouble;
34using DFloat128 = typename fputil::DyadicFloat<128>;
35
36using LIBC_NAMESPACE::operator""_u128;
37
38// log2(10)
39LIBC_INLINE_VAR constexpr double LOG2_10 = 0x1.a934f0979a371p+1;
40
41// -2^-12 * log10(2)
42// > a = -2^-12 * log10(2);
43// > b = round(a, 32, RN);
44// > c = round(a - b, 32, RN);
45// > d = round(a - b - c, D, RN);
46// Errors < 1.5 * 2^-144
47LIBC_INLINE_VAR constexpr double MLOG10_2_EXP2_M12_HI = -0x1.3441350ap-14;
48LIBC_INLINE_VAR constexpr double MLOG10_2_EXP2_M12_MID = 0x1.0c0219dc1da99p-51;
49
50#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
51LIBC_INLINE_VAR constexpr double MLOG10_2_EXP2_M12_MID_32 = 0x1.0c0219dcp-51;
52LIBC_INLINE_VAR constexpr double MLOG10_2_EXP2_M12_LO = 0x1.da994fd20dba2p-87;
53#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
54
55// Error bounds:
56// Errors when using double precision.
57LIBC_INLINE_VAR constexpr double EXP10_ERR_D = 0x1.8p-63;
58
59#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
60// Errors when using double-double precision.
61LIBC_INLINE_VAR constexpr double EXP10_ERR_DD = 0x1.8p-99;
62#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
63
64// Polynomial approximations with double precision. Generated by Sollya with:
65// > P = fpminimax((10^x - 1)/x, 3, [|D...|], [-2^-14, 2^-14]);
66// > P;
67// Error bounds:
68// | output - (10^dx - 1) / dx | < 2^-52.
69LIBC_INLINE double exp10_poly_approx_d(double dx) {
70 // dx^2
71 double dx2 = dx * dx;
72 double c0 =
73 fputil::multiply_add(x: dx, y: 0x1.53524c73cea6ap+1, z: 0x1.26bb1bbb55516p+1);
74 double c1 =
75 fputil::multiply_add(x: dx, y: 0x1.2bd75cc6afc65p+0, z: 0x1.0470587aa264cp+1);
76 double p = fputil::multiply_add(x: dx2, y: c1, z: c0);
77 return p;
78}
79
80#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
81// Polynomial approximation with double-double precision. Generated by Solya
82// with:
83// > P = fpminimax((10^x - 1)/x, 5, [|DD...|], [-2^-14, 2^-14]);
84// Error bounds:
85// | output - 10^(dx) | < 2^-101
86LIBC_INLINE DoubleDouble exp10_poly_approx_dd(const DoubleDouble &dx) {
87 // Taylor polynomial.
88 constexpr DoubleDouble COEFFS[] = {
89 {.lo: 0, .hi: 0x1p0},
90 {.lo: -0x1.f48ad494e927bp-53, .hi: 0x1.26bb1bbb55516p1},
91 {.lo: -0x1.e2bfab3191cd2p-53, .hi: 0x1.53524c73cea69p1},
92 {.lo: 0x1.80fb65ec3b503p-53, .hi: 0x1.0470591de2ca4p1},
93 {.lo: 0x1.338fc05e21e55p-54, .hi: 0x1.2bd7609fd98c4p0},
94 {.lo: 0x1.d4ea116818fbp-56, .hi: 0x1.1429ffd519865p-1},
95 {.lo: -0x1.872a8ff352077p-57, .hi: 0x1.a7ed70847c8b3p-3},
96
97 };
98
99 DoubleDouble p = fputil::polyeval(x: dx, a0: COEFFS[0], a: COEFFS[1], a: COEFFS[2],
100 a: COEFFS[3], a: COEFFS[4], a: COEFFS[5], a: COEFFS[6]);
101 return p;
102}
103
104// Polynomial approximation with 128-bit precision:
105// Return exp(dx) ~ 1 + a0 * dx + a1 * dx^2 + ... + a6 * dx^7
106// For |dx| < 2^-14:
107// | output - 10^dx | < 1.5 * 2^-124.
108LIBC_INLINE DFloat128 exp10_poly_approx_f128(const DFloat128 &dx) {
109 constexpr DFloat128 COEFFS_128[]{
110 {Sign::POS, -127, 0x80000000'00000000'00000000'00000000_u128}, // 1.0
111 {Sign::POS, -126, 0x935d8ddd'aaa8ac16'ea56d62b'82d30a2d_u128},
112 {Sign::POS, -126, 0xa9a92639'e753443a'80a99ce7'5f4d5bdb_u128},
113 {Sign::POS, -126, 0x82382c8e'f1652304'6a4f9d7d'bf6c9635_u128},
114 {Sign::POS, -124, 0x12bd7609'fd98c44c'34578701'9216c7af_u128},
115 {Sign::POS, -127, 0x450a7ff4'7535d889'cc41ed7e'0d27aee5_u128},
116 {Sign::POS, -130, 0xd3f6b844'702d636b'8326bb91'a6e7601d_u128},
117 {Sign::POS, -130, 0x45b937f0'd05bb1cd'fa7b46df'314112a9_u128},
118 };
119
120 DFloat128 p = fputil::polyeval(x: dx, a0: COEFFS_128[0], a: COEFFS_128[1],
121 a: COEFFS_128[2], a: COEFFS_128[3], a: COEFFS_128[4],
122 a: COEFFS_128[5], a: COEFFS_128[6], a: COEFFS_128[7]);
123 return p;
124}
125
126// Compute 10^(x) using 128-bit precision.
127// TODO(lntue): investigate triple-double precision implementation for this
128// step.
129LIBC_INLINE DFloat128 exp10_f128(double x, double kd, int idx1, int idx2) {
130 double t1 = fputil::multiply_add(x: kd, y: MLOG10_2_EXP2_M12_HI, z: x); // exact
131 double t2 = kd * MLOG10_2_EXP2_M12_MID_32; // exact
132 double t3 = kd * MLOG10_2_EXP2_M12_LO; // Error < 2^-144
133
134 DFloat128 dx = fputil::quick_add(
135 a: DFloat128(t1), b: fputil::quick_add(a: DFloat128(t2), b: DFloat128(t3)));
136
137 // TODO: Skip recalculating exp_mid1 and exp_mid2.
138 DFloat128 exp_mid1 =
139 fputil::quick_add(a: DFloat128(EXP2_MID1[idx1].hi),
140 b: fputil::quick_add(a: DFloat128(EXP2_MID1[idx1].mid),
141 b: DFloat128(EXP2_MID1[idx1].lo)));
142
143 DFloat128 exp_mid2 =
144 fputil::quick_add(a: DFloat128(EXP2_MID2[idx2].hi),
145 b: fputil::quick_add(a: DFloat128(EXP2_MID2[idx2].mid),
146 b: DFloat128(EXP2_MID2[idx2].lo)));
147
148 DFloat128 exp_mid = fputil::quick_mul(a: exp_mid1, b: exp_mid2);
149
150 DFloat128 p = exp10_poly_approx_f128(dx);
151
152 DFloat128 r = fputil::quick_mul(a: exp_mid, b: p);
153
154 r.exponent += static_cast<int>(kd) >> 12;
155
156 return r;
157}
158
159// Compute 10^x with double-double precision.
160LIBC_INLINE DoubleDouble exp10_double_double(double x, double kd,
161 const DoubleDouble &exp_mid) {
162 // Recalculate dx:
163 // dx = x - k * 2^-12 * log10(2)
164 double t1 = fputil::multiply_add(x: kd, y: MLOG10_2_EXP2_M12_HI, z: x); // exact
165 double t2 = kd * MLOG10_2_EXP2_M12_MID_32; // exact
166 double t3 = kd * MLOG10_2_EXP2_M12_LO; // Error < 2^-140
167
168 DoubleDouble dx = fputil::exact_add(a: t1, b: t2);
169 dx.lo += t3;
170
171 // Degree-6 polynomial approximation in double-double precision.
172 // | p - 10^x | < 2^-103.
173 DoubleDouble p = exp10_poly_approx_dd(dx);
174
175 // Error bounds: 2^-102.
176 DoubleDouble r = fputil::quick_mult(a: exp_mid, b: p);
177
178 return r;
179}
180#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
181
182// When output is denormal.
183LIBC_INLINE double exp10_denorm(double x) {
184 // Range reduction.
185 double tmp = fputil::multiply_add(x, y: LOG2_10, z: 0x1.8000'0000'4p21);
186 int k = static_cast<int>(cpp::bit_cast<uint64_t>(from: tmp) >> 19);
187 double kd = static_cast<double>(k);
188
189 uint32_t idx1 = (k >> 6) & 0x3f;
190 uint32_t idx2 = k & 0x3f;
191
192 int hi = k >> 12;
193
194 DoubleDouble exp_mid1{.lo: EXP2_MID1[idx1].mid, .hi: EXP2_MID1[idx1].hi};
195 DoubleDouble exp_mid2{.lo: EXP2_MID2[idx2].mid, .hi: EXP2_MID2[idx2].hi};
196 DoubleDouble exp_mid = fputil::quick_mult(a: exp_mid1, b: exp_mid2);
197
198 // |dx| < 1.5 * 2^-15 + 2^-31 < 2^-14
199 double lo_h = fputil::multiply_add(x: kd, y: MLOG10_2_EXP2_M12_HI, z: x); // exact
200 double dx = fputil::multiply_add(x: kd, y: MLOG10_2_EXP2_M12_MID, z: lo_h);
201
202 double mid_lo = dx * exp_mid.hi;
203
204 // Approximate (10^dx - 1)/dx ~ 1 + a0*dx + a1*dx^2 + a2*dx^3 + a3*dx^4.
205 double p = exp10_poly_approx_d(dx);
206
207 double lo = fputil::multiply_add(x: p, y: mid_lo, z: exp_mid.lo);
208
209#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
210 return ziv_test_denorm</*SKIP_ZIV_TEST=*/true>(hi, exp_mid.hi, lo,
211 EXP10_ERR_D)
212 .value();
213#else
214 if (auto r = ziv_test_denorm(hi, mid: exp_mid.hi, lo, err: EXP10_ERR_D);
215 LIBC_LIKELY(r.has_value()))
216 return r.value();
217
218 // Use double-double
219 DoubleDouble r_dd = exp10_double_double(x, kd, exp_mid);
220
221 if (auto r = ziv_test_denorm(hi, mid: r_dd.hi, lo: r_dd.lo, err: EXP10_ERR_DD);
222 LIBC_LIKELY(r.has_value()))
223 return r.value();
224
225 // Use 128-bit precision
226 DFloat128 r_f128 = exp10_f128(x, kd, idx1, idx2);
227
228 return static_cast<double>(r_f128);
229#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
230}
231
232// Check for exceptional cases when:
233// * log10(1 - 2^-54) < x < log10(1 + 2^-53)
234// * x >= log10(2^1024)
235// * x <= log10(2^-1022)
236// * x is inf or nan
237LIBC_INLINE double exp10_set_exceptional(double x) {
238 using FPBits = typename fputil::FPBits<double>;
239 FPBits xbits(x);
240
241 uint64_t x_u = xbits.uintval();
242 uint64_t x_abs = xbits.abs().uintval();
243
244 // |x| < log10(1 + 2^-53)
245 if (x_abs <= 0x3c8bcb7b1526e50e) {
246 // 10^(x) ~ 1 + x/2
247 return fputil::multiply_add(x, y: 0.5, z: 1.0);
248 }
249
250 // x <= log10(2^-1022) || x >= log10(2^1024) or inf/nan.
251 if (x_u >= 0xc0733a7146f72a42) {
252 // x <= log10(2^-1075) or -inf/nan
253 if (x_u > 0xc07439b746e36b52) {
254 // exp(-Inf) = 0
255 if (xbits.is_inf())
256 return 0.0;
257
258 // exp(nan) = nan
259 if (xbits.is_nan())
260 return x;
261
262#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
263 if (fputil::quick_get_round() == FE_UPWARD)
264 return FPBits::min_subnormal().get_val();
265#endif
266 fputil::set_errno_if_required(ERANGE);
267 fputil::raise_except_if_required(FE_UNDERFLOW);
268 return 0.0;
269 }
270
271 return exp10_denorm(x);
272 }
273
274 // x >= log10(2^1024) or +inf/nan
275 // x is finite
276 if (x_u < 0x7ff0'0000'0000'0000ULL) {
277#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
278 int rounding = fputil::quick_get_round();
279 if (rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO)
280 return FPBits::max_normal().get_val();
281#endif
282
283 fputil::set_errno_if_required(ERANGE);
284 fputil::raise_except_if_required(FE_OVERFLOW);
285 }
286 // x is +inf or nan
287 return x + FPBits::inf().get_val();
288}
289
290namespace math {
291
292LIBC_INLINE double exp10(double x) {
293 using FPBits = typename fputil::FPBits<double>;
294 FPBits xbits(x);
295
296 uint64_t x_u = xbits.uintval();
297
298 // x <= log10(2^-1022) or x >= log10(2^1024) or
299 // log10(1 - 2^-54) < x < log10(1 + 2^-53).
300 if (LIBC_UNLIKELY(x_u >= 0xc0733a7146f72a42 ||
301 (x_u <= 0xbc7bcb7b1526e50e && x_u >= 0x40734413509f79ff) ||
302 x_u < 0x3c8bcb7b1526e50e)) {
303 return exp10_set_exceptional(x);
304 }
305
306 // Now log10(2^-1075) < x <= log10(1 - 2^-54) or
307 // log10(1 + 2^-53) < x < log10(2^1024)
308
309 // Range reduction:
310 // Let x = log10(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 // 10^(x) = 2^hi * 2^(mid1) * 2^(mid2) * 10^(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 // - 10^(lo) ~ 1 + a0*lo + a1 * lo^2 + ...
322 //
323 // We compute (hi + mid1 + mid2) together by perform the rounding on
324 // x * log2(10) * 2^12.
325 // Since |x| < |log10(2^-1075)| < 2^9,
326 // |x * 2^12| < 2^9 * 2^12 < 2^21,
327 // So we can fit the rounded result round(x * 2^12) in int32_t.
328 // Thus, the goal is to be able to use an additional addition and fixed width
329 // shift to get an int32_t representing round(x * 2^12).
330 //
331 // Assuming int32_t using 2-complement representation, since the mantissa part
332 // of a double precision is unsigned with the leading bit hidden, if we add an
333 // extra constant C = 2^e1 + 2^e2 with e1 > e2 >= 2^23 to the product, the
334 // part that are < 2^e2 in resulted mantissa of (x*2^12*L2E + C) can be
335 // considered as a proper 2-complement representations of x*2^12.
336 //
337 // One small problem with this approach is that the sum (x*2^12 + C) in
338 // double precision is rounded to the least significant bit of the dorminant
339 // factor C. In order to minimize the rounding errors from this addition, we
340 // want to minimize e1. Another constraint that we want is that after
341 // shifting the mantissa so that the least significant bit of int32_t
342 // corresponds to the unit bit of (x*2^12*L2E), the sign is correct without
343 // any adjustment. So combining these 2 requirements, we can choose
344 // C = 2^33 + 2^32, so that the sign bit corresponds to 2^31 bit, and hence
345 // after right shifting the mantissa, the resulting int32_t has correct sign.
346 // With this choice of C, the number of mantissa bits we need to shift to the
347 // right is: 52 - 33 = 19.
348 //
349 // Moreover, since the integer right shifts are equivalent to rounding down,
350 // we can add an extra 0.5 so that it will become round-to-nearest, tie-to-
351 // +infinity. So in particular, we can compute:
352 // hmm = x * 2^12 + C,
353 // where C = 2^33 + 2^32 + 2^-1, then if
354 // k = int32_t(lower 51 bits of double(x * 2^12 + C) >> 19),
355 // the reduced argument:
356 // lo = x - log10(2) * 2^-12 * k is bounded by:
357 // |lo| = |x - log10(2) * 2^-12 * k|
358 // = log10(2) * 2^-12 * | x * log2(10) * 2^12 - k |
359 // <= log10(2) * 2^-12 * (2^-1 + 2^-19)
360 // < 1.5 * 2^-2 * (2^-13 + 2^-31)
361 // = 1.5 * (2^-15 * 2^-31)
362 //
363 // Finally, notice that k only uses the mantissa of x * 2^12, so the
364 // exponent 2^12 is not needed. So we can simply define
365 // C = 2^(33 - 12) + 2^(32 - 12) + 2^(-13 - 12), and
366 // k = int32_t(lower 51 bits of double(x + C) >> 19).
367
368 // Rounding errors <= 2^-31.
369 double tmp = fputil::multiply_add(x, y: LOG2_10, z: 0x1.8000'0000'4p21);
370 int k = static_cast<int>(cpp::bit_cast<uint64_t>(from: tmp) >> 19);
371 double kd = static_cast<double>(k);
372
373 uint32_t idx1 = (k >> 6) & 0x3f;
374 uint32_t idx2 = k & 0x3f;
375
376 int hi = k >> 12;
377
378 DoubleDouble exp_mid1{.lo: EXP2_MID1[idx1].mid, .hi: EXP2_MID1[idx1].hi};
379 DoubleDouble exp_mid2{.lo: EXP2_MID2[idx2].mid, .hi: EXP2_MID2[idx2].hi};
380 DoubleDouble exp_mid = fputil::quick_mult(a: exp_mid1, b: exp_mid2);
381
382 // |dx| < 1.5 * 2^-15 + 2^-31 < 2^-14
383 double lo_h = fputil::multiply_add(x: kd, y: MLOG10_2_EXP2_M12_HI, z: x); // exact
384 double dx = fputil::multiply_add(x: kd, y: MLOG10_2_EXP2_M12_MID, z: lo_h);
385
386 // We use the degree-4 polynomial to approximate 10^(lo):
387 // 10^(lo) ~ 1 + a0 * lo + a1 * lo^2 + a2 * lo^3 + a3 * lo^4
388 // = 1 + lo * P(lo)
389 // So that the errors are bounded by:
390 // |P(lo) - (10^lo - 1)/lo| < |lo|^4 / 64 < 2^(-13 * 4) / 64 = 2^-58
391 // Let P_ be an evaluation of P where all intermediate computations are in
392 // double precision. Using either Horner's or Estrin's schemes, the evaluated
393 // errors can be bounded by:
394 // |P_(lo) - P(lo)| < 2^-51
395 // => |lo * P_(lo) - (2^lo - 1) | < 2^-65
396 // => 2^(mid1 + mid2) * |lo * P_(lo) - expm1(lo)| < 2^-64.
397 // Since we approximate
398 // 2^(mid1 + mid2) ~ exp_mid.hi + exp_mid.lo,
399 // We use the expression:
400 // (exp_mid.hi + exp_mid.lo) * (1 + dx * P_(dx)) ~
401 // ~ exp_mid.hi + (exp_mid.hi * dx * P_(dx) + exp_mid.lo)
402 // with errors bounded by 2^-64.
403
404 double mid_lo = dx * exp_mid.hi;
405
406 // Approximate (10^dx - 1)/dx ~ 1 + a0*dx + a1*dx^2 + a2*dx^3 + a3*dx^4.
407 double p = exp10_poly_approx_d(dx);
408
409 double lo = fputil::multiply_add(x: p, y: mid_lo, z: exp_mid.lo);
410
411#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
412 int64_t exp_hi = static_cast<int64_t>(hi) * (1LL << FPBits::FRACTION_LEN);
413 double r =
414 cpp::bit_cast<double>(exp_hi + cpp::bit_cast<int64_t>(exp_mid.hi + lo));
415 return r;
416#else
417 double upper = exp_mid.hi + (lo + EXP10_ERR_D);
418 double lower = exp_mid.hi + (lo - EXP10_ERR_D);
419
420 if (LIBC_LIKELY(upper == lower)) {
421 // To multiply by 2^hi, a fast way is to simply add hi to the exponent
422 // field.
423 int64_t exp_hi = static_cast<int64_t>(hi) * (1LL << FPBits::FRACTION_LEN);
424 double r = cpp::bit_cast<double>(from: exp_hi + cpp::bit_cast<int64_t>(from: upper));
425 return r;
426 }
427
428 // Exact outputs when x = 1, 2, ..., 22 + hard to round with x = 23.
429 // Quick check mask: 0x800f'ffffU = ~(bits of 1.0 | ... | bits of 23.0)
430 if (LIBC_UNLIKELY((x_u & 0x8000'ffff'ffff'ffffULL) == 0ULL)) {
431 switch (x_u) {
432 case 0x3ff0000000000000: // x = 1.0
433 return 10.0;
434 case 0x4000000000000000: // x = 2.0
435 return 100.0;
436 case 0x4008000000000000: // x = 3.0
437 return 1'000.0;
438 case 0x4010000000000000: // x = 4.0
439 return 10'000.0;
440 case 0x4014000000000000: // x = 5.0
441 return 100'000.0;
442 case 0x4018000000000000: // x = 6.0
443 return 1'000'000.0;
444 case 0x401c000000000000: // x = 7.0
445 return 10'000'000.0;
446 case 0x4020000000000000: // x = 8.0
447 return 100'000'000.0;
448 case 0x4022000000000000: // x = 9.0
449 return 1'000'000'000.0;
450 case 0x4024000000000000: // x = 10.0
451 return 10'000'000'000.0;
452 case 0x4026000000000000: // x = 11.0
453 return 100'000'000'000.0;
454 case 0x4028000000000000: // x = 12.0
455 return 1'000'000'000'000.0;
456 case 0x402a000000000000: // x = 13.0
457 return 10'000'000'000'000.0;
458 case 0x402c000000000000: // x = 14.0
459 return 100'000'000'000'000.0;
460 case 0x402e000000000000: // x = 15.0
461 return 1'000'000'000'000'000.0;
462 case 0x4030000000000000: // x = 16.0
463 return 10'000'000'000'000'000.0;
464 case 0x4031000000000000: // x = 17.0
465 return 100'000'000'000'000'000.0;
466 case 0x4032000000000000: // x = 18.0
467 return 1'000'000'000'000'000'000.0;
468 case 0x4033000000000000: // x = 19.0
469 return 10'000'000'000'000'000'000.0;
470 case 0x4034000000000000: // x = 20.0
471 return 100'000'000'000'000'000'000.0;
472 case 0x4035000000000000: // x = 21.0
473 return 1'000'000'000'000'000'000'000.0;
474 case 0x4036000000000000: // x = 22.0
475 return 10'000'000'000'000'000'000'000.0;
476 case 0x4037000000000000: // x = 23.0
477 return 0x1.52d02c7e14af6p76 + x;
478 }
479 }
480
481 // Use double-double
482 DoubleDouble r_dd = exp10_double_double(x, kd, exp_mid);
483
484 double upper_dd = r_dd.hi + (r_dd.lo + EXP10_ERR_DD);
485 double lower_dd = r_dd.hi + (r_dd.lo - EXP10_ERR_DD);
486
487 if (LIBC_LIKELY(upper_dd == lower_dd)) {
488 // To multiply by 2^hi, a fast way is to simply add hi to the exponent
489 // field.
490 int64_t exp_hi = static_cast<int64_t>(hi) * (1LL << FPBits::FRACTION_LEN);
491 double r = cpp::bit_cast<double>(from: exp_hi + cpp::bit_cast<int64_t>(from: upper_dd));
492 return r;
493 }
494
495 // Use 128-bit precision
496 DFloat128 r_f128 = exp10_f128(x, kd, idx1, idx2);
497
498 return static_cast<double>(r_f128);
499#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
500}
501
502} // namespace math
503
504} // namespace LIBC_NAMESPACE_DECL
505
506#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXP10_H
507