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

source code of libc/src/math/generic/exp10.cpp