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 using FPBits = fputil::FPBits<double>;
215
216 if (auto r = ziv_test_denorm(hi, mid: exp_mid.hi, lo, err: EXP10_ERR_D);
217 LIBC_LIKELY(r.has_value())) {
218 double res = r.value();
219 if (LIBC_UNLIKELY(FPBits(res).is_normal()))
220 return res;
221 fputil::set_errno_if_required(ERANGE);
222 fputil::raise_underflow_except_if_required<double>();
223 return res;
224 }
225
226 // Use double-double
227 DoubleDouble r_dd = exp10_double_double(x, kd, exp_mid);
228
229 if (auto r = ziv_test_denorm(hi, mid: r_dd.hi, lo: r_dd.lo, err: EXP10_ERR_DD);
230 LIBC_LIKELY(r.has_value())) {
231 double res = r.value();
232 if (LIBC_UNLIKELY(FPBits(res).is_normal()))
233 return res;
234 fputil::set_errno_if_required(ERANGE);
235 fputil::raise_underflow_except_if_required<double>();
236 return res;
237 }
238
239 // Use 128-bit precision
240 DFloat128 r_f128 = exp10_f128(x, kd, idx1, idx2);
241
242 return static_cast<double>(r_f128);
243#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
244}
245
246// Check for exceptional cases when:
247// * log10(1 - 2^-54) < x < log10(1 + 2^-53)
248// * x >= log10(2^1024)
249// * x <= log10(2^-1022)
250// * x is inf or nan
251LIBC_INLINE double exp10_set_exceptional(double x) {
252 using FPBits = typename fputil::FPBits<double>;
253 FPBits xbits(x);
254
255 uint64_t x_u = xbits.uintval();
256 uint64_t x_abs = xbits.abs().uintval();
257
258 // |x| < log10(1 + 2^-53)
259 if (x_abs <= 0x3c8bcb7b1526e50e) {
260 // 10^(x) ~ 1 + x/2
261 return fputil::multiply_add(x, y: 0.5, z: 1.0);
262 }
263
264 // x <= log10(2^-1022) || x >= log10(2^1024) or inf/nan.
265 if (x_u >= 0xc0733a7146f72a42) {
266 // x <= log10(2^-1075) or -inf/nan
267 if (x_u > 0xc07439b746e36b52) {
268 // exp(-Inf) = 0
269 if (xbits.is_inf())
270 return 0.0;
271
272 // exp(nan) = nan
273 if (xbits.is_nan())
274 return x + FPBits::inf().get_val();
275
276 fputil::set_errno_if_required(ERANGE);
277 fputil::raise_underflow_except_if_required<double>();
278#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
279 if (fputil::quick_get_round() == FE_UPWARD)
280 return FPBits::min_subnormal().get_val();
281#endif
282 return 0.0;
283 }
284
285 return exp10_denorm(x);
286 }
287
288 // x >= log10(2^1024) or +inf/nan
289 // x is finite
290 if (x_u < 0x7ff0'0000'0000'0000ULL) {
291 fputil::set_errno_if_required(ERANGE);
292 fputil::raise_overflow_except_if_required<double>();
293#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
294 int rounding = fputil::quick_get_round();
295 if (rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO)
296 return FPBits::max_normal().get_val();
297#endif
298 return FPBits::inf().get_val();
299 }
300 // x is +inf or nan
301 return x + FPBits::inf().get_val();
302}
303
304namespace math {
305
306LIBC_INLINE double exp10(double x) {
307 using FPBits = typename fputil::FPBits<double>;
308 FPBits xbits(x);
309
310 uint64_t x_u = xbits.uintval();
311
312 // x <= log10(2^-1022) or x >= log10(2^1024) or
313 // log10(1 - 2^-54) < x < log10(1 + 2^-53).
314 if (LIBC_UNLIKELY(x_u >= 0xc0733a7146f72a42 ||
315 (x_u <= 0xbc7bcb7b1526e50e && x_u >= 0x40734413509f79ff) ||
316 x_u < 0x3c8bcb7b1526e50e)) {
317 return exp10_set_exceptional(x);
318 }
319
320 // Now log10(2^-1075) < x <= log10(1 - 2^-54) or
321 // log10(1 + 2^-53) < x < log10(2^1024)
322
323 // Range reduction:
324 // Let x = log10(2) * (hi + mid1 + mid2) + lo
325 // in which:
326 // hi is an integer
327 // mid1 * 2^6 is an integer
328 // mid2 * 2^12 is an integer
329 // then:
330 // 10^(x) = 2^hi * 2^(mid1) * 2^(mid2) * 10^(lo).
331 // With this formula:
332 // - multiplying by 2^hi is exact and cheap, simply by adding the exponent
333 // field.
334 // - 2^(mid1) and 2^(mid2) are stored in 2 x 64-element tables.
335 // - 10^(lo) ~ 1 + a0*lo + a1 * lo^2 + ...
336 //
337 // We compute (hi + mid1 + mid2) together by perform the rounding on
338 // x * log2(10) * 2^12.
339 // Since |x| < |log10(2^-1075)| < 2^9,
340 // |x * 2^12| < 2^9 * 2^12 < 2^21,
341 // So we can fit the rounded result round(x * 2^12) 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).
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^23 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.
350 //
351 // One small problem with this approach is that the sum (x*2^12 + 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 + C,
367 // where C = 2^33 + 2^32 + 2^-1, then if
368 // k = int32_t(lower 51 bits of double(x * 2^12 + C) >> 19),
369 // the reduced argument:
370 // lo = x - log10(2) * 2^-12 * k is bounded by:
371 // |lo| = |x - log10(2) * 2^-12 * k|
372 // = log10(2) * 2^-12 * | x * log2(10) * 2^12 - k |
373 // <= log10(2) * 2^-12 * (2^-1 + 2^-19)
374 // < 1.5 * 2^-2 * (2^-13 + 2^-31)
375 // = 1.5 * (2^-15 * 2^-31)
376 //
377 // Finally, notice that k only uses the mantissa of x * 2^12, so the
378 // exponent 2^12 is not needed. So we can simply define
379 // C = 2^(33 - 12) + 2^(32 - 12) + 2^(-13 - 12), and
380 // k = int32_t(lower 51 bits of double(x + C) >> 19).
381
382 // Rounding errors <= 2^-31.
383 double tmp = fputil::multiply_add(x, y: LOG2_10, z: 0x1.8000'0000'4p21);
384 int k = static_cast<int>(cpp::bit_cast<uint64_t>(from: tmp) >> 19);
385 double kd = static_cast<double>(k);
386
387 uint32_t idx1 = (k >> 6) & 0x3f;
388 uint32_t idx2 = k & 0x3f;
389
390 int hi = k >> 12;
391
392 DoubleDouble exp_mid1{.lo: EXP2_MID1[idx1].mid, .hi: EXP2_MID1[idx1].hi};
393 DoubleDouble exp_mid2{.lo: EXP2_MID2[idx2].mid, .hi: EXP2_MID2[idx2].hi};
394 DoubleDouble exp_mid = fputil::quick_mult(a: exp_mid1, b: exp_mid2);
395
396 // |dx| < 1.5 * 2^-15 + 2^-31 < 2^-14
397 double lo_h = fputil::multiply_add(x: kd, y: MLOG10_2_EXP2_M12_HI, z: x); // exact
398 double dx = fputil::multiply_add(x: kd, y: MLOG10_2_EXP2_M12_MID, z: lo_h);
399
400 // We use the degree-4 polynomial to approximate 10^(lo):
401 // 10^(lo) ~ 1 + a0 * lo + a1 * lo^2 + a2 * lo^3 + a3 * lo^4
402 // = 1 + lo * P(lo)
403 // So that the errors are bounded by:
404 // |P(lo) - (10^lo - 1)/lo| < |lo|^4 / 64 < 2^(-13 * 4) / 64 = 2^-58
405 // Let P_ be an evaluation of P where all intermediate computations are in
406 // double precision. Using either Horner's or Estrin's schemes, the evaluated
407 // errors can be bounded by:
408 // |P_(lo) - P(lo)| < 2^-51
409 // => |lo * P_(lo) - (2^lo - 1) | < 2^-65
410 // => 2^(mid1 + mid2) * |lo * P_(lo) - expm1(lo)| < 2^-64.
411 // Since we approximate
412 // 2^(mid1 + mid2) ~ exp_mid.hi + exp_mid.lo,
413 // We use the expression:
414 // (exp_mid.hi + exp_mid.lo) * (1 + dx * P_(dx)) ~
415 // ~ exp_mid.hi + (exp_mid.hi * dx * P_(dx) + exp_mid.lo)
416 // with errors bounded by 2^-64.
417
418 double mid_lo = dx * exp_mid.hi;
419
420 // Approximate (10^dx - 1)/dx ~ 1 + a0*dx + a1*dx^2 + a2*dx^3 + a3*dx^4.
421 double p = exp10_poly_approx_d(dx);
422
423 double lo = fputil::multiply_add(x: p, y: mid_lo, z: exp_mid.lo);
424
425#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
426 int64_t exp_hi = static_cast<int64_t>(hi) * (1LL << FPBits::FRACTION_LEN);
427 double r =
428 cpp::bit_cast<double>(exp_hi + cpp::bit_cast<int64_t>(exp_mid.hi + lo));
429 return r;
430#else
431 double upper = exp_mid.hi + (lo + EXP10_ERR_D);
432 double lower = exp_mid.hi + (lo - EXP10_ERR_D);
433
434 if (LIBC_LIKELY(upper == lower)) {
435 // To multiply by 2^hi, a fast way is to simply add hi to the exponent
436 // field.
437 int64_t exp_hi = static_cast<int64_t>(hi) * (1LL << FPBits::FRACTION_LEN);
438 double r = cpp::bit_cast<double>(from: exp_hi + cpp::bit_cast<int64_t>(from: upper));
439 return r;
440 }
441
442 // Exact outputs when x = 1, 2, ..., 22 + hard to round with x = 23.
443 // Quick check mask: 0x800f'ffffU = ~(bits of 1.0 | ... | bits of 23.0)
444 if (LIBC_UNLIKELY((x_u & 0x8000'ffff'ffff'ffffULL) == 0ULL)) {
445 switch (x_u) {
446 case 0x3ff0000000000000: // x = 1.0
447 return 10.0;
448 case 0x4000000000000000: // x = 2.0
449 return 100.0;
450 case 0x4008000000000000: // x = 3.0
451 return 1'000.0;
452 case 0x4010000000000000: // x = 4.0
453 return 10'000.0;
454 case 0x4014000000000000: // x = 5.0
455 return 100'000.0;
456 case 0x4018000000000000: // x = 6.0
457 return 1'000'000.0;
458 case 0x401c000000000000: // x = 7.0
459 return 10'000'000.0;
460 case 0x4020000000000000: // x = 8.0
461 return 100'000'000.0;
462 case 0x4022000000000000: // x = 9.0
463 return 1'000'000'000.0;
464 case 0x4024000000000000: // x = 10.0
465 return 10'000'000'000.0;
466 case 0x4026000000000000: // x = 11.0
467 return 100'000'000'000.0;
468 case 0x4028000000000000: // x = 12.0
469 return 1'000'000'000'000.0;
470 case 0x402a000000000000: // x = 13.0
471 return 10'000'000'000'000.0;
472 case 0x402c000000000000: // x = 14.0
473 return 100'000'000'000'000.0;
474 case 0x402e000000000000: // x = 15.0
475 return 1'000'000'000'000'000.0;
476 case 0x4030000000000000: // x = 16.0
477 return 10'000'000'000'000'000.0;
478 case 0x4031000000000000: // x = 17.0
479 return 100'000'000'000'000'000.0;
480 case 0x4032000000000000: // x = 18.0
481 return 1'000'000'000'000'000'000.0;
482 case 0x4033000000000000: // x = 19.0
483 return 10'000'000'000'000'000'000.0;
484 case 0x4034000000000000: // x = 20.0
485 return 100'000'000'000'000'000'000.0;
486 case 0x4035000000000000: // x = 21.0
487 return 1'000'000'000'000'000'000'000.0;
488 case 0x4036000000000000: // x = 22.0
489 return 10'000'000'000'000'000'000'000.0;
490 case 0x4037000000000000: // x = 23.0
491 return 0x1.52d02c7e14af6p76 + x;
492 }
493 }
494
495 // Use double-double
496 DoubleDouble r_dd = exp10_double_double(x, kd, exp_mid);
497
498 double upper_dd = r_dd.hi + (r_dd.lo + EXP10_ERR_DD);
499 double lower_dd = r_dd.hi + (r_dd.lo - EXP10_ERR_DD);
500
501 if (LIBC_LIKELY(upper_dd == lower_dd)) {
502 // To multiply by 2^hi, a fast way is to simply add hi to the exponent
503 // field.
504 int64_t exp_hi = static_cast<int64_t>(hi) * (1LL << FPBits::FRACTION_LEN);
505 double r = cpp::bit_cast<double>(from: exp_hi + cpp::bit_cast<int64_t>(from: upper_dd));
506 return r;
507 }
508
509 // Use 128-bit precision
510 DFloat128 r_f128 = exp10_f128(x, kd, idx1, idx2);
511
512 return static_cast<double>(r_f128);
513#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
514}
515
516} // namespace math
517
518} // namespace LIBC_NAMESPACE_DECL
519
520#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXP10_H
521