[Workflow] Roll back some settings since they caused more issues
[llvm-project.git] / libc / src / math / generic / exp10.cpp
blob4a43259b3307d31828286cf04393477779174fd2
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 //===----------------------------------------------------------------------===//
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/macros/optimization.h" // LIBC_UNLIKELY
26 #include <errno.h>
28 namespace __llvm_libc {
30 using fputil::DoubleDouble;
31 using fputil::TripleDouble;
32 using Float128 = typename fputil::DyadicFloat<128>;
34 // log2(10)
35 constexpr double LOG2_10 = 0x1.a934f0979a371p+1;
37 // -2^-12 * log10(2)
38 // > a = -2^-12 * log10(2);
39 // > b = round(a, 32, RN);
40 // > c = round(a - b, 32, RN);
41 // > d = round(a - b - c, D, RN);
42 // Errors < 1.5 * 2^-144
43 constexpr double MLOG10_2_EXP2_M12_HI = -0x1.3441350ap-14;
44 constexpr double MLOG10_2_EXP2_M12_MID = 0x1.0c0219dc1da99p-51;
45 constexpr double MLOG10_2_EXP2_M12_MID_32 = 0x1.0c0219dcp-51;
46 constexpr double MLOG10_2_EXP2_M12_LO = 0x1.da994fd20dba2p-87;
48 // Error bounds:
49 // Errors when using double precision.
50 constexpr double ERR_D = 0x1.8p-63;
52 // Errors when using double-double precision.
53 constexpr double ERR_DD = 0x1.8p-99;
55 // Polynomial approximations with double precision. Generated by Sollya with:
56 // > P = fpminimax((10^x - 1)/x, 3, [|D...|], [-2^-14, 2^-14]);
57 // > P;
58 // Error bounds:
59 // | output - (10^dx - 1) / dx | < 2^-52.
60 LIBC_INLINE double poly_approx_d(double dx) {
61 // dx^2
62 double dx2 = dx * dx;
63 double c0 =
64 fputil::multiply_add(dx, 0x1.53524c73cea6ap+1, 0x1.26bb1bbb55516p+1);
65 double c1 =
66 fputil::multiply_add(dx, 0x1.2bd75cc6afc65p+0, 0x1.0470587aa264cp+1);
67 double p = fputil::multiply_add(dx2, c1, c0);
68 return p;
71 // Polynomial approximation with double-double precision. Generated by Solya
72 // with:
73 // > P = fpminimax((10^x - 1)/x, 5, [|DD...|], [-2^-14, 2^-14]);
74 // Error bounds:
75 // | output - 10^(dx) | < 2^-101
76 DoubleDouble poly_approx_dd(const DoubleDouble &dx) {
77 // Taylor polynomial.
78 constexpr DoubleDouble COEFFS[] = {
79 {0, 0x1p0},
80 {-0x1.f48ad494e927bp-53, 0x1.26bb1bbb55516p1},
81 {-0x1.e2bfab3191cd2p-53, 0x1.53524c73cea69p1},
82 {0x1.80fb65ec3b503p-53, 0x1.0470591de2ca4p1},
83 {0x1.338fc05e21e55p-54, 0x1.2bd7609fd98c4p0},
84 {0x1.d4ea116818fbp-56, 0x1.1429ffd519865p-1},
85 {-0x1.872a8ff352077p-57, 0x1.a7ed70847c8b3p-3},
89 DoubleDouble p = fputil::polyeval(dx, COEFFS[0], COEFFS[1], COEFFS[2],
90 COEFFS[3], COEFFS[4], COEFFS[5], COEFFS[6]);
91 return p;
94 // Polynomial approximation with 128-bit precision:
95 // Return exp(dx) ~ 1 + a0 * dx + a1 * dx^2 + ... + a6 * dx^7
96 // For |dx| < 2^-14:
97 // | output - 10^dx | < 1.5 * 2^-124.
98 Float128 poly_approx_f128(const Float128 &dx) {
99 using MType = typename Float128::MantissaType;
101 constexpr Float128 COEFFS_128[]{
102 {false, -127, MType({0, 0x8000000000000000})}, // 1.0
103 {false, -126, MType({0xea56d62b82d30a2d, 0x935d8dddaaa8ac16})},
104 {false, -126, MType({0x80a99ce75f4d5bdb, 0xa9a92639e753443a})},
105 {false, -126, MType({0x6a4f9d7dbf6c9635, 0x82382c8ef1652304})},
106 {false, -124, MType({0x345787019216c7af, 0x12bd7609fd98c44c})},
107 {false, -127, MType({0xcc41ed7e0d27aee5, 0x450a7ff47535d889})},
108 {false, -130, MType({0x8326bb91a6e7601d, 0xd3f6b844702d636b})},
109 {false, -130, MType({0xfa7b46df314112a9, 0x45b937f0d05bb1cd})},
112 Float128 p = fputil::polyeval(dx, COEFFS_128[0], COEFFS_128[1], COEFFS_128[2],
113 COEFFS_128[3], COEFFS_128[4], COEFFS_128[5],
114 COEFFS_128[6], COEFFS_128[7]);
115 return p;
118 // Compute 10^(x) using 128-bit precision.
119 // TODO(lntue): investigate triple-double precision implementation for this
120 // step.
121 Float128 exp10_f128(double x, double kd, int idx1, int idx2) {
122 double t1 = fputil::multiply_add(kd, MLOG10_2_EXP2_M12_HI, x); // exact
123 double t2 = kd * MLOG10_2_EXP2_M12_MID_32; // exact
124 double t3 = kd * MLOG10_2_EXP2_M12_LO; // Error < 2^-144
126 Float128 dx = fputil::quick_add(
127 Float128(t1), fputil::quick_add(Float128(t2), Float128(t3)));
129 // TODO: Skip recalculating exp_mid1 and exp_mid2.
130 Float128 exp_mid1 =
131 fputil::quick_add(Float128(EXP2_MID1[idx1].hi),
132 fputil::quick_add(Float128(EXP2_MID1[idx1].mid),
133 Float128(EXP2_MID1[idx1].lo)));
135 Float128 exp_mid2 =
136 fputil::quick_add(Float128(EXP2_MID2[idx2].hi),
137 fputil::quick_add(Float128(EXP2_MID2[idx2].mid),
138 Float128(EXP2_MID2[idx2].lo)));
140 Float128 exp_mid = fputil::quick_mul(exp_mid1, exp_mid2);
142 Float128 p = poly_approx_f128(dx);
144 Float128 r = fputil::quick_mul(exp_mid, p);
146 r.exponent += static_cast<int>(kd) >> 12;
148 return r;
151 // Compute 10^x with double-double precision.
152 DoubleDouble exp10_double_double(double x, double kd,
153 const DoubleDouble &exp_mid) {
154 // Recalculate dx:
155 // dx = x - k * 2^-12 * log10(2)
156 double t1 = fputil::multiply_add(kd, MLOG10_2_EXP2_M12_HI, x); // exact
157 double t2 = kd * MLOG10_2_EXP2_M12_MID_32; // exact
158 double t3 = kd * MLOG10_2_EXP2_M12_LO; // Error < 2^-140
160 DoubleDouble dx = fputil::exact_add(t1, t2);
161 dx.lo += t3;
163 // Degree-6 polynomial approximation in double-double precision.
164 // | p - 10^x | < 2^-103.
165 DoubleDouble p = poly_approx_dd(dx);
167 // Error bounds: 2^-102.
168 DoubleDouble r = fputil::quick_mult(exp_mid, p);
170 return r;
173 // When output is denormal.
174 double exp10_denorm(double x) {
175 // Range reduction.
176 double tmp = fputil::multiply_add(x, LOG2_10, 0x1.8000'0000'4p21);
177 int k = static_cast<int>(cpp::bit_cast<uint64_t>(tmp) >> 19);
178 double kd = static_cast<double>(k);
180 uint32_t idx1 = (k >> 6) & 0x3f;
181 uint32_t idx2 = k & 0x3f;
183 int hi = k >> 12;
185 DoubleDouble exp_mid1{EXP2_MID1[idx1].mid, EXP2_MID1[idx1].hi};
186 DoubleDouble exp_mid2{EXP2_MID2[idx2].mid, EXP2_MID2[idx2].hi};
187 DoubleDouble exp_mid = fputil::quick_mult(exp_mid1, exp_mid2);
189 // |dx| < 1.5 * 2^-15 + 2^-31 < 2^-14
190 double lo_h = fputil::multiply_add(kd, MLOG10_2_EXP2_M12_HI, x); // exact
191 double dx = fputil::multiply_add(kd, MLOG10_2_EXP2_M12_MID, lo_h);
193 double mid_lo = dx * exp_mid.hi;
195 // Approximate (10^dx - 1)/dx ~ 1 + a0*dx + a1*dx^2 + a2*dx^3 + a3*dx^4.
196 double p = poly_approx_d(dx);
198 double lo = fputil::multiply_add(p, mid_lo, exp_mid.lo);
200 if (auto r = ziv_test_denorm(hi, exp_mid.hi, lo, ERR_D);
201 LIBC_LIKELY(r.has_value()))
202 return r.value();
204 // Use double-double
205 DoubleDouble r_dd = exp10_double_double(x, kd, exp_mid);
207 if (auto r = ziv_test_denorm(hi, r_dd.hi, r_dd.lo, ERR_DD);
208 LIBC_LIKELY(r.has_value()))
209 return r.value();
211 // Use 128-bit precision
212 Float128 r_f128 = exp10_f128(x, kd, idx1, idx2);
214 return static_cast<double>(r_f128);
217 // Check for exceptional cases when:
218 // * log10(1 - 2^-54) < x < log10(1 + 2^-53)
219 // * x >= log10(2^1024)
220 // * x <= log10(2^-1022)
221 // * x is inf or nan
222 double set_exceptional(double x) {
223 using FPBits = typename fputil::FPBits<double>;
224 using FloatProp = typename fputil::FloatProperties<double>;
225 FPBits xbits(x);
227 uint64_t x_u = xbits.uintval();
228 uint64_t x_abs = x_u & FloatProp::EXP_MANT_MASK;
230 // |x| < log10(1 + 2^-53)
231 if (x_abs <= 0x3c8bcb7b1526e50e) {
232 // 10^(x) ~ 1 + x/2
233 return fputil::multiply_add(x, 0.5, 1.0);
236 // x <= log10(2^-1022) || x >= log10(2^1024) or inf/nan.
237 if (x_u >= 0xc0733a7146f72a42) {
238 // x <= log10(2^-1075) or -inf/nan
239 if (x_u > 0xc07439b746e36b52) {
240 // exp(-Inf) = 0
241 if (xbits.is_inf())
242 return 0.0;
244 // exp(nan) = nan
245 if (xbits.is_nan())
246 return x;
248 if (fputil::quick_get_round() == FE_UPWARD)
249 return static_cast<double>(FPBits(FPBits::MIN_SUBNORMAL));
250 fputil::set_errno_if_required(ERANGE);
251 fputil::raise_except_if_required(FE_UNDERFLOW);
252 return 0.0;
255 return exp10_denorm(x);
258 // x >= log10(2^1024) or +inf/nan
259 // x is finite
260 if (x_u < 0x7ff0'0000'0000'0000ULL) {
261 int rounding = fputil::quick_get_round();
262 if (rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO)
263 return static_cast<double>(FPBits(FPBits::MAX_NORMAL));
265 fputil::set_errno_if_required(ERANGE);
266 fputil::raise_except_if_required(FE_OVERFLOW);
268 // x is +inf or nan
269 return x + static_cast<double>(FPBits::inf());
272 LLVM_LIBC_FUNCTION(double, exp10, (double x)) {
273 using FPBits = typename fputil::FPBits<double>;
274 using FloatProp = typename fputil::FloatProperties<double>;
275 FPBits xbits(x);
277 uint64_t x_u = xbits.uintval();
279 // x <= log10(2^-1022) or x >= log10(2^1024) or
280 // log10(1 - 2^-54) < x < log10(1 + 2^-53).
281 if (LIBC_UNLIKELY(x_u >= 0xc0733a7146f72a42 ||
282 (x_u <= 0xbc7bcb7b1526e50e && x_u >= 0x40734413509f79ff) ||
283 x_u < 0x3c8bcb7b1526e50e)) {
284 return set_exceptional(x);
287 // Now log10(2^-1075) < x <= log10(1 - 2^-54) or
288 // log10(1 + 2^-53) < x < log10(2^1024)
290 // Range reduction:
291 // Let x = log10(2) * (hi + mid1 + mid2) + lo
292 // in which:
293 // hi is an integer
294 // mid1 * 2^6 is an integer
295 // mid2 * 2^12 is an integer
296 // then:
297 // 10^(x) = 2^hi * 2^(mid1) * 2^(mid2) * 10^(lo).
298 // With this formula:
299 // - multiplying by 2^hi is exact and cheap, simply by adding the exponent
300 // field.
301 // - 2^(mid1) and 2^(mid2) are stored in 2 x 64-element tables.
302 // - 10^(lo) ~ 1 + a0*lo + a1 * lo^2 + ...
304 // We compute (hi + mid1 + mid2) together by perform the rounding on
305 // x * log2(10) * 2^12.
306 // Since |x| < |log10(2^-1075)| < 2^9,
307 // |x * 2^12| < 2^9 * 2^12 < 2^21,
308 // So we can fit the rounded result round(x * 2^12) in int32_t.
309 // Thus, the goal is to be able to use an additional addition and fixed width
310 // shift to get an int32_t representing round(x * 2^12).
312 // Assuming int32_t using 2-complement representation, since the mantissa part
313 // of a double precision is unsigned with the leading bit hidden, if we add an
314 // extra constant C = 2^e1 + 2^e2 with e1 > e2 >= 2^23 to the product, the
315 // part that are < 2^e2 in resulted mantissa of (x*2^12*L2E + C) can be
316 // considered as a proper 2-complement representations of x*2^12.
318 // One small problem with this approach is that the sum (x*2^12 + C) in
319 // double precision is rounded to the least significant bit of the dorminant
320 // factor C. In order to minimize the rounding errors from this addition, we
321 // want to minimize e1. Another constraint that we want is that after
322 // shifting the mantissa so that the least significant bit of int32_t
323 // corresponds to the unit bit of (x*2^12*L2E), the sign is correct without
324 // any adjustment. So combining these 2 requirements, we can choose
325 // C = 2^33 + 2^32, so that the sign bit corresponds to 2^31 bit, and hence
326 // after right shifting the mantissa, the resulting int32_t has correct sign.
327 // With this choice of C, the number of mantissa bits we need to shift to the
328 // right is: 52 - 33 = 19.
330 // Moreover, since the integer right shifts are equivalent to rounding down,
331 // we can add an extra 0.5 so that it will become round-to-nearest, tie-to-
332 // +infinity. So in particular, we can compute:
333 // hmm = x * 2^12 + C,
334 // where C = 2^33 + 2^32 + 2^-1, then if
335 // k = int32_t(lower 51 bits of double(x * 2^12 + C) >> 19),
336 // the reduced argument:
337 // lo = x - log10(2) * 2^-12 * k is bounded by:
338 // |lo| = |x - log10(2) * 2^-12 * k|
339 // = log10(2) * 2^-12 * | x * log2(10) * 2^12 - k |
340 // <= log10(2) * 2^-12 * (2^-1 + 2^-19)
341 // < 1.5 * 2^-2 * (2^-13 + 2^-31)
342 // = 1.5 * (2^-15 * 2^-31)
344 // Finally, notice that k only uses the mantissa of x * 2^12, so the
345 // exponent 2^12 is not needed. So we can simply define
346 // C = 2^(33 - 12) + 2^(32 - 12) + 2^(-13 - 12), and
347 // k = int32_t(lower 51 bits of double(x + C) >> 19).
349 // Rounding errors <= 2^-31.
350 double tmp = fputil::multiply_add(x, LOG2_10, 0x1.8000'0000'4p21);
351 int k = static_cast<int>(cpp::bit_cast<uint64_t>(tmp) >> 19);
352 double kd = static_cast<double>(k);
354 uint32_t idx1 = (k >> 6) & 0x3f;
355 uint32_t idx2 = k & 0x3f;
357 int hi = k >> 12;
359 DoubleDouble exp_mid1{EXP2_MID1[idx1].mid, EXP2_MID1[idx1].hi};
360 DoubleDouble exp_mid2{EXP2_MID2[idx2].mid, EXP2_MID2[idx2].hi};
361 DoubleDouble exp_mid = fputil::quick_mult(exp_mid1, exp_mid2);
363 // |dx| < 1.5 * 2^-15 + 2^-31 < 2^-14
364 double lo_h = fputil::multiply_add(kd, MLOG10_2_EXP2_M12_HI, x); // exact
365 double dx = fputil::multiply_add(kd, MLOG10_2_EXP2_M12_MID, lo_h);
367 // We use the degree-4 polynomial to approximate 10^(lo):
368 // 10^(lo) ~ 1 + a0 * lo + a1 * lo^2 + a2 * lo^3 + a3 * lo^4
369 // = 1 + lo * P(lo)
370 // So that the errors are bounded by:
371 // |P(lo) - (10^lo - 1)/lo| < |lo|^4 / 64 < 2^(-13 * 4) / 64 = 2^-58
372 // Let P_ be an evaluation of P where all intermediate computations are in
373 // double precision. Using either Horner's or Estrin's schemes, the evaluated
374 // errors can be bounded by:
375 // |P_(lo) - P(lo)| < 2^-51
376 // => |lo * P_(lo) - (2^lo - 1) | < 2^-65
377 // => 2^(mid1 + mid2) * |lo * P_(lo) - expm1(lo)| < 2^-64.
378 // Since we approximate
379 // 2^(mid1 + mid2) ~ exp_mid.hi + exp_mid.lo,
380 // We use the expression:
381 // (exp_mid.hi + exp_mid.lo) * (1 + dx * P_(dx)) ~
382 // ~ exp_mid.hi + (exp_mid.hi * dx * P_(dx) + exp_mid.lo)
383 // with errors bounded by 2^-64.
385 double mid_lo = dx * exp_mid.hi;
387 // Approximate (10^dx - 1)/dx ~ 1 + a0*dx + a1*dx^2 + a2*dx^3 + a3*dx^4.
388 double p = poly_approx_d(dx);
390 double lo = fputil::multiply_add(p, mid_lo, exp_mid.lo);
392 double upper = exp_mid.hi + (lo + ERR_D);
393 double lower = exp_mid.hi + (lo - ERR_D);
395 if (LIBC_LIKELY(upper == lower)) {
396 // To multiply by 2^hi, a fast way is to simply add hi to the exponent
397 // field.
398 int64_t exp_hi = static_cast<int64_t>(hi) << FloatProp::MANTISSA_WIDTH;
399 double r = cpp::bit_cast<double>(exp_hi + cpp::bit_cast<int64_t>(upper));
400 return r;
403 // Exact outputs when x = 1, 2, ..., 22 + hard to round with x = 23.
404 // Quick check mask: 0x800f'ffffU = ~(bits of 1.0 | ... | bits of 23.0)
405 if (LIBC_UNLIKELY((x_u & 0x8000'ffff'ffff'ffffULL) == 0ULL)) {
406 switch (x_u) {
407 case 0x3ff0000000000000: // x = 1.0
408 return 10.0;
409 case 0x4000000000000000: // x = 2.0
410 return 100.0;
411 case 0x4008000000000000: // x = 3.0
412 return 1'000.0;
413 case 0x4010000000000000: // x = 4.0
414 return 10'000.0;
415 case 0x4014000000000000: // x = 5.0
416 return 100'000.0;
417 case 0x4018000000000000: // x = 6.0
418 return 1'000'000.0;
419 case 0x401c000000000000: // x = 7.0
420 return 10'000'000.0;
421 case 0x4020000000000000: // x = 8.0
422 return 100'000'000.0;
423 case 0x4022000000000000: // x = 9.0
424 return 1'000'000'000.0;
425 case 0x4024000000000000: // x = 10.0
426 return 10'000'000'000.0;
427 case 0x4026000000000000: // x = 11.0
428 return 100'000'000'000.0;
429 case 0x4028000000000000: // x = 12.0
430 return 1'000'000'000'000.0;
431 case 0x402a000000000000: // x = 13.0
432 return 10'000'000'000'000.0;
433 case 0x402c000000000000: // x = 14.0
434 return 100'000'000'000'000.0;
435 case 0x402e000000000000: // x = 15.0
436 return 1'000'000'000'000'000.0;
437 case 0x4030000000000000: // x = 16.0
438 return 10'000'000'000'000'000.0;
439 case 0x4031000000000000: // x = 17.0
440 return 100'000'000'000'000'000.0;
441 case 0x4032000000000000: // x = 18.0
442 return 1'000'000'000'000'000'000.0;
443 case 0x4033000000000000: // x = 19.0
444 return 10'000'000'000'000'000'000.0;
445 case 0x4034000000000000: // x = 20.0
446 return 100'000'000'000'000'000'000.0;
447 case 0x4035000000000000: // x = 21.0
448 return 1'000'000'000'000'000'000'000.0;
449 case 0x4036000000000000: // x = 22.0
450 return 10'000'000'000'000'000'000'000.0;
451 case 0x4037000000000000: // x = 23.0
452 return 0x1.52d02c7e14af6p76 + x;
456 // Use double-double
457 DoubleDouble r_dd = exp10_double_double(x, kd, exp_mid);
459 double upper_dd = r_dd.hi + (r_dd.lo + ERR_DD);
460 double lower_dd = r_dd.hi + (r_dd.lo - ERR_DD);
462 if (LIBC_LIKELY(upper_dd == lower_dd)) {
463 // To multiply by 2^hi, a fast way is to simply add hi to the exponent
464 // field.
465 int64_t exp_hi = static_cast<int64_t>(hi) << FloatProp::MANTISSA_WIDTH;
466 double r = cpp::bit_cast<double>(exp_hi + cpp::bit_cast<int64_t>(upper_dd));
467 return r;
470 // Use 128-bit precision
471 Float128 r_f128 = exp10_f128(x, kd, idx1, idx2);
473 return static_cast<double>(r_f128);
476 } // namespace __llvm_libc