506 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXP10_H10#define LLVM_LIBC_SRC___SUPPORT_MATH_EXP10_H11 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_UNLIKELY29 30namespace LIBC_NAMESPACE_DECL {31 32using fputil::DoubleDouble;33using fputil::TripleDouble;34using Float128 = typename fputil::DyadicFloat<128>;35 36using LIBC_NAMESPACE::operator""_u128;37 38// log2(10)39static 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^-14447static constexpr double MLOG10_2_EXP2_M12_HI = -0x1.3441350ap-14;48static constexpr double MLOG10_2_EXP2_M12_MID = 0x1.0c0219dc1da99p-51;49 50#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS51static constexpr double MLOG10_2_EXP2_M12_MID_32 = 0x1.0c0219dcp-51;52static constexpr double MLOG10_2_EXP2_M12_LO = 0x1.da994fd20dba2p-87;53#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS54 55// Error bounds:56// Errors when using double precision.57constexpr double EXP10_ERR_D = 0x1.8p-63;58 59#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS60// Errors when using double-double precision.61static constexpr double EXP10_ERR_DD = 0x1.8p-99;62#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS63 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 static double exp10_poly_approx_d(double dx) {70 // dx^271 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_PASS81// Polynomial approximation with double-double precision. Generated by Solya82// with:83// > P = fpminimax((10^x - 1)/x, 5, [|DD...|], [-2^-14, 2^-14]);84// Error bounds:85// | output - 10^(dx) | < 2^-10186LIBC_INLINE static constexpr DoubleDouble87exp10_poly_approx_dd(const DoubleDouble &dx) {88 // Taylor polynomial.89 constexpr DoubleDouble COEFFS[] = {90 {0, 0x1p0},91 {-0x1.f48ad494e927bp-53, 0x1.26bb1bbb55516p1},92 {-0x1.e2bfab3191cd2p-53, 0x1.53524c73cea69p1},93 {0x1.80fb65ec3b503p-53, 0x1.0470591de2ca4p1},94 {0x1.338fc05e21e55p-54, 0x1.2bd7609fd98c4p0},95 {0x1.d4ea116818fbp-56, 0x1.1429ffd519865p-1},96 {-0x1.872a8ff352077p-57, 0x1.a7ed70847c8b3p-3},97 98 };99 100 DoubleDouble p = fputil::polyeval(dx, COEFFS[0], COEFFS[1], COEFFS[2],101 COEFFS[3], COEFFS[4], COEFFS[5], COEFFS[6]);102 return p;103}104 105// Polynomial approximation with 128-bit precision:106// Return exp(dx) ~ 1 + a0 * dx + a1 * dx^2 + ... + a6 * dx^7107// For |dx| < 2^-14:108// | output - 10^dx | < 1.5 * 2^-124.109LIBC_INLINE static constexpr Float128110exp10_poly_approx_f128(const Float128 &dx) {111 constexpr Float128 COEFFS_128[]{112 {Sign::POS, -127, 0x80000000'00000000'00000000'00000000_u128}, // 1.0113 {Sign::POS, -126, 0x935d8ddd'aaa8ac16'ea56d62b'82d30a2d_u128},114 {Sign::POS, -126, 0xa9a92639'e753443a'80a99ce7'5f4d5bdb_u128},115 {Sign::POS, -126, 0x82382c8e'f1652304'6a4f9d7d'bf6c9635_u128},116 {Sign::POS, -124, 0x12bd7609'fd98c44c'34578701'9216c7af_u128},117 {Sign::POS, -127, 0x450a7ff4'7535d889'cc41ed7e'0d27aee5_u128},118 {Sign::POS, -130, 0xd3f6b844'702d636b'8326bb91'a6e7601d_u128},119 {Sign::POS, -130, 0x45b937f0'd05bb1cd'fa7b46df'314112a9_u128},120 };121 122 Float128 p = fputil::polyeval(dx, COEFFS_128[0], COEFFS_128[1], COEFFS_128[2],123 COEFFS_128[3], COEFFS_128[4], COEFFS_128[5],124 COEFFS_128[6], COEFFS_128[7]);125 return p;126}127 128// Compute 10^(x) using 128-bit precision.129// TODO(lntue): investigate triple-double precision implementation for this130// step.131LIBC_INLINE static Float128 exp10_f128(double x, double kd, int idx1,132 int idx2) {133 double t1 = fputil::multiply_add(kd, MLOG10_2_EXP2_M12_HI, x); // exact134 double t2 = kd * MLOG10_2_EXP2_M12_MID_32; // exact135 double t3 = kd * MLOG10_2_EXP2_M12_LO; // Error < 2^-144136 137 Float128 dx = fputil::quick_add(138 Float128(t1), fputil::quick_add(Float128(t2), Float128(t3)));139 140 // TODO: Skip recalculating exp_mid1 and exp_mid2.141 Float128 exp_mid1 =142 fputil::quick_add(Float128(EXP2_MID1[idx1].hi),143 fputil::quick_add(Float128(EXP2_MID1[idx1].mid),144 Float128(EXP2_MID1[idx1].lo)));145 146 Float128 exp_mid2 =147 fputil::quick_add(Float128(EXP2_MID2[idx2].hi),148 fputil::quick_add(Float128(EXP2_MID2[idx2].mid),149 Float128(EXP2_MID2[idx2].lo)));150 151 Float128 exp_mid = fputil::quick_mul(exp_mid1, exp_mid2);152 153 Float128 p = exp10_poly_approx_f128(dx);154 155 Float128 r = fputil::quick_mul(exp_mid, p);156 157 r.exponent += static_cast<int>(kd) >> 12;158 159 return r;160}161 162// Compute 10^x with double-double precision.163LIBC_INLINE static DoubleDouble164exp10_double_double(double x, double kd, const DoubleDouble &exp_mid) {165 // Recalculate dx:166 // dx = x - k * 2^-12 * log10(2)167 double t1 = fputil::multiply_add(kd, MLOG10_2_EXP2_M12_HI, x); // exact168 double t2 = kd * MLOG10_2_EXP2_M12_MID_32; // exact169 double t3 = kd * MLOG10_2_EXP2_M12_LO; // Error < 2^-140170 171 DoubleDouble dx = fputil::exact_add(t1, t2);172 dx.lo += t3;173 174 // Degree-6 polynomial approximation in double-double precision.175 // | p - 10^x | < 2^-103.176 DoubleDouble p = exp10_poly_approx_dd(dx);177 178 // Error bounds: 2^-102.179 DoubleDouble r = fputil::quick_mult(exp_mid, p);180 181 return r;182}183#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS184 185// When output is denormal.186LIBC_INLINE static double exp10_denorm(double x) {187 // Range reduction.188 double tmp = fputil::multiply_add(x, LOG2_10, 0x1.8000'0000'4p21);189 int k = static_cast<int>(cpp::bit_cast<uint64_t>(tmp) >> 19);190 double kd = static_cast<double>(k);191 192 uint32_t idx1 = (k >> 6) & 0x3f;193 uint32_t idx2 = k & 0x3f;194 195 int hi = k >> 12;196 197 DoubleDouble exp_mid1{EXP2_MID1[idx1].mid, EXP2_MID1[idx1].hi};198 DoubleDouble exp_mid2{EXP2_MID2[idx2].mid, EXP2_MID2[idx2].hi};199 DoubleDouble exp_mid = fputil::quick_mult(exp_mid1, exp_mid2);200 201 // |dx| < 1.5 * 2^-15 + 2^-31 < 2^-14202 double lo_h = fputil::multiply_add(kd, MLOG10_2_EXP2_M12_HI, x); // exact203 double dx = fputil::multiply_add(kd, MLOG10_2_EXP2_M12_MID, lo_h);204 205 double mid_lo = dx * exp_mid.hi;206 207 // Approximate (10^dx - 1)/dx ~ 1 + a0*dx + a1*dx^2 + a2*dx^3 + a3*dx^4.208 double p = exp10_poly_approx_d(dx);209 210 double lo = fputil::multiply_add(p, mid_lo, exp_mid.lo);211 212#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS213 return ziv_test_denorm</*SKIP_ZIV_TEST=*/true>(hi, exp_mid.hi, lo,214 EXP10_ERR_D)215 .value();216#else217 if (auto r = ziv_test_denorm(hi, exp_mid.hi, lo, EXP10_ERR_D);218 LIBC_LIKELY(r.has_value()))219 return r.value();220 221 // Use double-double222 DoubleDouble r_dd = exp10_double_double(x, kd, exp_mid);223 224 if (auto r = ziv_test_denorm(hi, r_dd.hi, r_dd.lo, EXP10_ERR_DD);225 LIBC_LIKELY(r.has_value()))226 return r.value();227 228 // Use 128-bit precision229 Float128 r_f128 = exp10_f128(x, kd, idx1, idx2);230 231 return static_cast<double>(r_f128);232#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS233}234 235// Check for exceptional cases when:236// * log10(1 - 2^-54) < x < log10(1 + 2^-53)237// * x >= log10(2^1024)238// * x <= log10(2^-1022)239// * x is inf or nan240LIBC_INLINE static constexpr double exp10_set_exceptional(double x) {241 using FPBits = typename fputil::FPBits<double>;242 FPBits xbits(x);243 244 uint64_t x_u = xbits.uintval();245 uint64_t x_abs = xbits.abs().uintval();246 247 // |x| < log10(1 + 2^-53)248 if (x_abs <= 0x3c8bcb7b1526e50e) {249 // 10^(x) ~ 1 + x/2250 return fputil::multiply_add(x, 0.5, 1.0);251 }252 253 // x <= log10(2^-1022) || x >= log10(2^1024) or inf/nan.254 if (x_u >= 0xc0733a7146f72a42) {255 // x <= log10(2^-1075) or -inf/nan256 if (x_u > 0xc07439b746e36b52) {257 // exp(-Inf) = 0258 if (xbits.is_inf())259 return 0.0;260 261 // exp(nan) = nan262 if (xbits.is_nan())263 return x;264 265 if (fputil::quick_get_round() == FE_UPWARD)266 return FPBits::min_subnormal().get_val();267 fputil::set_errno_if_required(ERANGE);268 fputil::raise_except_if_required(FE_UNDERFLOW);269 return 0.0;270 }271 272 return exp10_denorm(x);273 }274 275 // x >= log10(2^1024) or +inf/nan276 // x is finite277 if (x_u < 0x7ff0'0000'0000'0000ULL) {278 int rounding = fputil::quick_get_round();279 if (rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO)280 return FPBits::max_normal().get_val();281 282 fputil::set_errno_if_required(ERANGE);283 fputil::raise_except_if_required(FE_OVERFLOW);284 }285 // x is +inf or nan286 return x + FPBits::inf().get_val();287}288 289namespace math {290 291LIBC_INLINE static constexpr double exp10(double x) {292 using FPBits = typename fputil::FPBits<double>;293 FPBits xbits(x);294 295 uint64_t x_u = xbits.uintval();296 297 // x <= log10(2^-1022) or x >= log10(2^1024) or298 // log10(1 - 2^-54) < x < log10(1 + 2^-53).299 if (LIBC_UNLIKELY(x_u >= 0xc0733a7146f72a42 ||300 (x_u <= 0xbc7bcb7b1526e50e && x_u >= 0x40734413509f79ff) ||301 x_u < 0x3c8bcb7b1526e50e)) {302 return exp10_set_exceptional(x);303 }304 305 // Now log10(2^-1075) < x <= log10(1 - 2^-54) or306 // log10(1 + 2^-53) < x < log10(2^1024)307 308 // Range reduction:309 // Let x = log10(2) * (hi + mid1 + mid2) + lo310 // in which:311 // hi is an integer312 // mid1 * 2^6 is an integer313 // mid2 * 2^12 is an integer314 // then:315 // 10^(x) = 2^hi * 2^(mid1) * 2^(mid2) * 10^(lo).316 // With this formula:317 // - multiplying by 2^hi is exact and cheap, simply by adding the exponent318 // field.319 // - 2^(mid1) and 2^(mid2) are stored in 2 x 64-element tables.320 // - 10^(lo) ~ 1 + a0*lo + a1 * lo^2 + ...321 //322 // We compute (hi + mid1 + mid2) together by perform the rounding on323 // x * log2(10) * 2^12.324 // Since |x| < |log10(2^-1075)| < 2^9,325 // |x * 2^12| < 2^9 * 2^12 < 2^21,326 // So we can fit the rounded result round(x * 2^12) in int32_t.327 // Thus, the goal is to be able to use an additional addition and fixed width328 // shift to get an int32_t representing round(x * 2^12).329 //330 // Assuming int32_t using 2-complement representation, since the mantissa part331 // of a double precision is unsigned with the leading bit hidden, if we add an332 // extra constant C = 2^e1 + 2^e2 with e1 > e2 >= 2^23 to the product, the333 // part that are < 2^e2 in resulted mantissa of (x*2^12*L2E + C) can be334 // considered as a proper 2-complement representations of x*2^12.335 //336 // One small problem with this approach is that the sum (x*2^12 + C) in337 // double precision is rounded to the least significant bit of the dorminant338 // factor C. In order to minimize the rounding errors from this addition, we339 // want to minimize e1. Another constraint that we want is that after340 // shifting the mantissa so that the least significant bit of int32_t341 // corresponds to the unit bit of (x*2^12*L2E), the sign is correct without342 // any adjustment. So combining these 2 requirements, we can choose343 // C = 2^33 + 2^32, so that the sign bit corresponds to 2^31 bit, and hence344 // after right shifting the mantissa, the resulting int32_t has correct sign.345 // With this choice of C, the number of mantissa bits we need to shift to the346 // right is: 52 - 33 = 19.347 //348 // Moreover, since the integer right shifts are equivalent to rounding down,349 // we can add an extra 0.5 so that it will become round-to-nearest, tie-to-350 // +infinity. So in particular, we can compute:351 // hmm = x * 2^12 + C,352 // where C = 2^33 + 2^32 + 2^-1, then if353 // k = int32_t(lower 51 bits of double(x * 2^12 + C) >> 19),354 // the reduced argument:355 // lo = x - log10(2) * 2^-12 * k is bounded by:356 // |lo| = |x - log10(2) * 2^-12 * k|357 // = log10(2) * 2^-12 * | x * log2(10) * 2^12 - k |358 // <= log10(2) * 2^-12 * (2^-1 + 2^-19)359 // < 1.5 * 2^-2 * (2^-13 + 2^-31)360 // = 1.5 * (2^-15 * 2^-31)361 //362 // Finally, notice that k only uses the mantissa of x * 2^12, so the363 // exponent 2^12 is not needed. So we can simply define364 // C = 2^(33 - 12) + 2^(32 - 12) + 2^(-13 - 12), and365 // k = int32_t(lower 51 bits of double(x + C) >> 19).366 367 // Rounding errors <= 2^-31.368 double tmp = fputil::multiply_add(x, LOG2_10, 0x1.8000'0000'4p21);369 int k = static_cast<int>(cpp::bit_cast<uint64_t>(tmp) >> 19);370 double kd = static_cast<double>(k);371 372 uint32_t idx1 = (k >> 6) & 0x3f;373 uint32_t idx2 = k & 0x3f;374 375 int hi = k >> 12;376 377 DoubleDouble exp_mid1{EXP2_MID1[idx1].mid, EXP2_MID1[idx1].hi};378 DoubleDouble exp_mid2{EXP2_MID2[idx2].mid, EXP2_MID2[idx2].hi};379 DoubleDouble exp_mid = fputil::quick_mult(exp_mid1, exp_mid2);380 381 // |dx| < 1.5 * 2^-15 + 2^-31 < 2^-14382 double lo_h = fputil::multiply_add(kd, MLOG10_2_EXP2_M12_HI, x); // exact383 double dx = fputil::multiply_add(kd, MLOG10_2_EXP2_M12_MID, lo_h);384 385 // We use the degree-4 polynomial to approximate 10^(lo):386 // 10^(lo) ~ 1 + a0 * lo + a1 * lo^2 + a2 * lo^3 + a3 * lo^4387 // = 1 + lo * P(lo)388 // So that the errors are bounded by:389 // |P(lo) - (10^lo - 1)/lo| < |lo|^4 / 64 < 2^(-13 * 4) / 64 = 2^-58390 // Let P_ be an evaluation of P where all intermediate computations are in391 // double precision. Using either Horner's or Estrin's schemes, the evaluated392 // errors can be bounded by:393 // |P_(lo) - P(lo)| < 2^-51394 // => |lo * P_(lo) - (2^lo - 1) | < 2^-65395 // => 2^(mid1 + mid2) * |lo * P_(lo) - expm1(lo)| < 2^-64.396 // Since we approximate397 // 2^(mid1 + mid2) ~ exp_mid.hi + exp_mid.lo,398 // We use the expression:399 // (exp_mid.hi + exp_mid.lo) * (1 + dx * P_(dx)) ~400 // ~ exp_mid.hi + (exp_mid.hi * dx * P_(dx) + exp_mid.lo)401 // with errors bounded by 2^-64.402 403 double mid_lo = dx * exp_mid.hi;404 405 // Approximate (10^dx - 1)/dx ~ 1 + a0*dx + a1*dx^2 + a2*dx^3 + a3*dx^4.406 double p = exp10_poly_approx_d(dx);407 408 double lo = fputil::multiply_add(p, mid_lo, exp_mid.lo);409 410#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS411 int64_t exp_hi = static_cast<int64_t>(hi) << FPBits::FRACTION_LEN;412 double r =413 cpp::bit_cast<double>(exp_hi + cpp::bit_cast<int64_t>(exp_mid.hi + lo));414 return r;415#else416 double upper = exp_mid.hi + (lo + EXP10_ERR_D);417 double lower = exp_mid.hi + (lo - EXP10_ERR_D);418 419 if (LIBC_LIKELY(upper == lower)) {420 // To multiply by 2^hi, a fast way is to simply add hi to the exponent421 // field.422 int64_t exp_hi = static_cast<int64_t>(hi) << FPBits::FRACTION_LEN;423 double r = cpp::bit_cast<double>(exp_hi + cpp::bit_cast<int64_t>(upper));424 return r;425 }426 427 // Exact outputs when x = 1, 2, ..., 22 + hard to round with x = 23.428 // Quick check mask: 0x800f'ffffU = ~(bits of 1.0 | ... | bits of 23.0)429 if (LIBC_UNLIKELY((x_u & 0x8000'ffff'ffff'ffffULL) == 0ULL)) {430 switch (x_u) {431 case 0x3ff0000000000000: // x = 1.0432 return 10.0;433 case 0x4000000000000000: // x = 2.0434 return 100.0;435 case 0x4008000000000000: // x = 3.0436 return 1'000.0;437 case 0x4010000000000000: // x = 4.0438 return 10'000.0;439 case 0x4014000000000000: // x = 5.0440 return 100'000.0;441 case 0x4018000000000000: // x = 6.0442 return 1'000'000.0;443 case 0x401c000000000000: // x = 7.0444 return 10'000'000.0;445 case 0x4020000000000000: // x = 8.0446 return 100'000'000.0;447 case 0x4022000000000000: // x = 9.0448 return 1'000'000'000.0;449 case 0x4024000000000000: // x = 10.0450 return 10'000'000'000.0;451 case 0x4026000000000000: // x = 11.0452 return 100'000'000'000.0;453 case 0x4028000000000000: // x = 12.0454 return 1'000'000'000'000.0;455 case 0x402a000000000000: // x = 13.0456 return 10'000'000'000'000.0;457 case 0x402c000000000000: // x = 14.0458 return 100'000'000'000'000.0;459 case 0x402e000000000000: // x = 15.0460 return 1'000'000'000'000'000.0;461 case 0x4030000000000000: // x = 16.0462 return 10'000'000'000'000'000.0;463 case 0x4031000000000000: // x = 17.0464 return 100'000'000'000'000'000.0;465 case 0x4032000000000000: // x = 18.0466 return 1'000'000'000'000'000'000.0;467 case 0x4033000000000000: // x = 19.0468 return 10'000'000'000'000'000'000.0;469 case 0x4034000000000000: // x = 20.0470 return 100'000'000'000'000'000'000.0;471 case 0x4035000000000000: // x = 21.0472 return 1'000'000'000'000'000'000'000.0;473 case 0x4036000000000000: // x = 22.0474 return 10'000'000'000'000'000'000'000.0;475 case 0x4037000000000000: // x = 23.0476 return 0x1.52d02c7e14af6p76 + x;477 }478 }479 480 // Use double-double481 DoubleDouble r_dd = exp10_double_double(x, kd, exp_mid);482 483 double upper_dd = r_dd.hi + (r_dd.lo + EXP10_ERR_DD);484 double lower_dd = r_dd.hi + (r_dd.lo - EXP10_ERR_DD);485 486 if (LIBC_LIKELY(upper_dd == lower_dd)) {487 // To multiply by 2^hi, a fast way is to simply add hi to the exponent488 // field.489 int64_t exp_hi = static_cast<int64_t>(hi) << FPBits::FRACTION_LEN;490 double r = cpp::bit_cast<double>(exp_hi + cpp::bit_cast<int64_t>(upper_dd));491 return r;492 }493 494 // Use 128-bit precision495 Float128 r_f128 = exp10_f128(x, kd, idx1, idx2);496 497 return static_cast<double>(r_f128);498#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS499}500 501} // namespace math502 503} // namespace LIBC_NAMESPACE_DECL504 505#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXP10_H506