1 //===-- Double-precision 2^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/exp2.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 28 namespace LIBC_NAMESPACE_DECL { 29 30 using fputil::DoubleDouble; 31 using fputil::TripleDouble; 32 using Float128 = typename fputil::DyadicFloat<128>; 33 34 using LIBC_NAMESPACE::operator""_u128; 35 36 // Error bounds: 37 // Errors when using double precision. 38 #ifdef LIBC_TARGET_CPU_HAS_FMA 39 constexpr double ERR_D = 0x1.0p-63; 40 #else 41 constexpr double ERR_D = 0x1.8p-63; 42 #endif // LIBC_TARGET_CPU_HAS_FMA 43 44 // Errors when using double-double precision. 45 constexpr double ERR_DD = 0x1.0p-100; 46 47 namespace { 48 49 // Polynomial approximations with double precision. Generated by Sollya with: 50 // > P = fpminimax((2^x - 1)/x, 3, [|D...|], [-2^-13 - 2^-30, 2^-13 + 2^-30]); 51 // > P; 52 // Error bounds: 53 // | output - (2^dx - 1) / dx | < 1.5 * 2^-52. 54 LIBC_INLINE double poly_approx_d(double dx) { 55 // dx^2 56 double dx2 = dx * dx; 57 double c0 = 58 fputil::multiply_add(dx, 0x1.ebfbdff82c58ep-3, 0x1.62e42fefa39efp-1); 59 double c1 = 60 fputil::multiply_add(dx, 0x1.3b2aba7a95a89p-7, 0x1.c6b08e8fc0c0ep-5); 61 double p = fputil::multiply_add(dx2, c1, c0); 62 return p; 63 } 64 65 // Polynomial approximation with double-double precision. Generated by Solya 66 // with: 67 // > P = fpminimax((2^x - 1)/x, 5, [|DD...|], [-2^-13 - 2^-30, 2^-13 + 2^-30]); 68 // Error bounds: 69 // | output - 2^(dx) | < 2^-101 70 DoubleDouble poly_approx_dd(const DoubleDouble &dx) { 71 // Taylor polynomial. 72 constexpr DoubleDouble COEFFS[] = { 73 {0, 0x1p0}, 74 {0x1.abc9e3b39824p-56, 0x1.62e42fefa39efp-1}, 75 {-0x1.5e43a53e4527bp-57, 0x1.ebfbdff82c58fp-3}, 76 {-0x1.d37963a9444eep-59, 0x1.c6b08d704a0cp-5}, 77 {0x1.4eda1a81133dap-62, 0x1.3b2ab6fba4e77p-7}, 78 {-0x1.c53fd1ba85d14p-64, 0x1.5d87fe7a265a5p-10}, 79 {0x1.d89250b013eb8p-70, 0x1.430912f86cb8ep-13}, 80 }; 81 82 DoubleDouble p = fputil::polyeval(dx, COEFFS[0], COEFFS[1], COEFFS[2], 83 COEFFS[3], COEFFS[4], COEFFS[5], COEFFS[6]); 84 return p; 85 } 86 87 // Polynomial approximation with 128-bit precision: 88 // Return exp(dx) ~ 1 + a0 * dx + a1 * dx^2 + ... + a6 * dx^7 89 // For |dx| < 2^-13 + 2^-30: 90 // | output - exp(dx) | < 2^-126. 91 Float128 poly_approx_f128(const Float128 &dx) { 92 constexpr Float128 COEFFS_128[]{ 93 {Sign::POS, -127, 0x80000000'00000000'00000000'00000000_u128}, // 1.0 94 {Sign::POS, -128, 0xb17217f7'd1cf79ab'c9e3b398'03f2f6af_u128}, 95 {Sign::POS, -128, 0x3d7f7bff'058b1d50'de2d60dd'9c9a1d9f_u128}, 96 {Sign::POS, -132, 0xe35846b8'2505fc59'9d3b15d9'e7fb6897_u128}, 97 {Sign::POS, -134, 0x9d955b7d'd273b94e'184462f6'bcd2b9e7_u128}, 98 {Sign::POS, -137, 0xaec3ff3c'53398883'39ea1bb9'64c51a89_u128}, 99 {Sign::POS, -138, 0x2861225f'345c396a'842c5341'8fa8ae61_u128}, 100 {Sign::POS, -144, 0xffe5fe2d'109a319d'7abeb5ab'd5ad2079_u128}, 101 }; 102 103 Float128 p = fputil::polyeval(dx, COEFFS_128[0], COEFFS_128[1], COEFFS_128[2], 104 COEFFS_128[3], COEFFS_128[4], COEFFS_128[5], 105 COEFFS_128[6], COEFFS_128[7]); 106 return p; 107 } 108 109 // Compute 2^(x) using 128-bit precision. 110 // TODO(lntue): investigate triple-double precision implementation for this 111 // step. 112 Float128 exp2_f128(double x, int hi, int idx1, int idx2) { 113 Float128 dx = Float128(x); 114 115 // TODO: Skip recalculating exp_mid1 and exp_mid2. 116 Float128 exp_mid1 = 117 fputil::quick_add(Float128(EXP2_MID1[idx1].hi), 118 fputil::quick_add(Float128(EXP2_MID1[idx1].mid), 119 Float128(EXP2_MID1[idx1].lo))); 120 121 Float128 exp_mid2 = 122 fputil::quick_add(Float128(EXP2_MID2[idx2].hi), 123 fputil::quick_add(Float128(EXP2_MID2[idx2].mid), 124 Float128(EXP2_MID2[idx2].lo))); 125 126 Float128 exp_mid = fputil::quick_mul(exp_mid1, exp_mid2); 127 128 Float128 p = poly_approx_f128(dx); 129 130 Float128 r = fputil::quick_mul(exp_mid, p); 131 132 r.exponent += hi; 133 134 return r; 135 } 136 137 // Compute 2^x with double-double precision. 138 DoubleDouble exp2_double_double(double x, const DoubleDouble &exp_mid) { 139 DoubleDouble dx({0, x}); 140 141 // Degree-6 polynomial approximation in double-double precision. 142 // | p - 2^x | < 2^-103. 143 DoubleDouble p = poly_approx_dd(dx); 144 145 // Error bounds: 2^-102. 146 DoubleDouble r = fputil::quick_mult(exp_mid, p); 147 148 return r; 149 } 150 151 // When output is denormal. 152 double exp2_denorm(double x) { 153 // Range reduction. 154 int k = 155 static_cast<int>(cpp::bit_cast<uint64_t>(x + 0x1.8000'0000'4p21) >> 19); 156 double kd = static_cast<double>(k); 157 158 uint32_t idx1 = (k >> 6) & 0x3f; 159 uint32_t idx2 = k & 0x3f; 160 161 int hi = k >> 12; 162 163 DoubleDouble exp_mid1{EXP2_MID1[idx1].mid, EXP2_MID1[idx1].hi}; 164 DoubleDouble exp_mid2{EXP2_MID2[idx2].mid, EXP2_MID2[idx2].hi}; 165 DoubleDouble exp_mid = fputil::quick_mult(exp_mid1, exp_mid2); 166 167 // |dx| < 2^-13 + 2^-30. 168 double dx = fputil::multiply_add(kd, -0x1.0p-12, x); // exact 169 170 double mid_lo = dx * exp_mid.hi; 171 172 // Approximate (2^dx - 1)/dx ~ 1 + a0*dx + a1*dx^2 + a2*dx^3 + a3*dx^4. 173 double p = poly_approx_d(dx); 174 175 double lo = fputil::multiply_add(p, mid_lo, exp_mid.lo); 176 177 if (auto r = ziv_test_denorm(hi, exp_mid.hi, lo, ERR_D); 178 LIBC_LIKELY(r.has_value())) 179 return r.value(); 180 181 // Use double-double 182 DoubleDouble r_dd = exp2_double_double(dx, exp_mid); 183 184 if (auto r = ziv_test_denorm(hi, r_dd.hi, r_dd.lo, ERR_DD); 185 LIBC_LIKELY(r.has_value())) 186 return r.value(); 187 188 // Use 128-bit precision 189 Float128 r_f128 = exp2_f128(dx, hi, idx1, idx2); 190 191 return static_cast<double>(r_f128); 192 } 193 194 // Check for exceptional cases when: 195 // * log2(1 - 2^-54) < x < log2(1 + 2^-53) 196 // * x >= 1024 197 // * x <= -1022 198 // * x is inf or nan 199 double set_exceptional(double x) { 200 using FPBits = typename fputil::FPBits<double>; 201 FPBits xbits(x); 202 203 uint64_t x_u = xbits.uintval(); 204 uint64_t x_abs = xbits.abs().uintval(); 205 206 // |x| < log2(1 + 2^-53) 207 if (x_abs <= 0x3ca71547652b82fd) { 208 // 2^(x) ~ 1 + x/2 209 return fputil::multiply_add(x, 0.5, 1.0); 210 } 211 212 // x <= -1022 || x >= 1024 or inf/nan. 213 if (x_u > 0xc08ff00000000000) { 214 // x <= -1075 or -inf/nan 215 if (x_u >= 0xc090cc0000000000) { 216 // exp(-Inf) = 0 217 if (xbits.is_inf()) 218 return 0.0; 219 220 // exp(nan) = nan 221 if (xbits.is_nan()) 222 return x; 223 224 if (fputil::quick_get_round() == FE_UPWARD) 225 return FPBits::min_subnormal().get_val(); 226 fputil::set_errno_if_required(ERANGE); 227 fputil::raise_except_if_required(FE_UNDERFLOW); 228 return 0.0; 229 } 230 231 return exp2_denorm(x); 232 } 233 234 // x >= 1024 or +inf/nan 235 // x is finite 236 if (x_u < 0x7ff0'0000'0000'0000ULL) { 237 int rounding = fputil::quick_get_round(); 238 if (rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO) 239 return FPBits::max_normal().get_val(); 240 241 fputil::set_errno_if_required(ERANGE); 242 fputil::raise_except_if_required(FE_OVERFLOW); 243 } 244 // x is +inf or nan 245 return x + FPBits::inf().get_val(); 246 } 247 248 } // namespace 249 250 LLVM_LIBC_FUNCTION(double, exp2, (double x)) { 251 using FPBits = typename fputil::FPBits<double>; 252 FPBits xbits(x); 253 254 uint64_t x_u = xbits.uintval(); 255 256 // x < -1022 or x >= 1024 or log2(1 - 2^-54) < x < log2(1 + 2^-53). 257 if (LIBC_UNLIKELY(x_u > 0xc08ff00000000000 || 258 (x_u <= 0xbc971547652b82fe && x_u >= 0x4090000000000000) || 259 x_u <= 0x3ca71547652b82fd)) { 260 return set_exceptional(x); 261 } 262 263 // Now -1075 < x <= log2(1 - 2^-54) or log2(1 + 2^-53) < x < 1024 264 265 // Range reduction: 266 // Let x = (hi + mid1 + mid2) + lo 267 // in which: 268 // hi is an integer 269 // mid1 * 2^6 is an integer 270 // mid2 * 2^12 is an integer 271 // then: 272 // 2^(x) = 2^hi * 2^(mid1) * 2^(mid2) * 2^(lo). 273 // With this formula: 274 // - multiplying by 2^hi is exact and cheap, simply by adding the exponent 275 // field. 276 // - 2^(mid1) and 2^(mid2) are stored in 2 x 64-element tables. 277 // - 2^(lo) ~ 1 + a0*lo + a1 * lo^2 + ... 278 // 279 // We compute (hi + mid1 + mid2) together by perform the rounding on x * 2^12. 280 // Since |x| < |-1075)| < 2^11, 281 // |x * 2^12| < 2^11 * 2^12 < 2^23, 282 // So we can fit the rounded result round(x * 2^12) in int32_t. 283 // Thus, the goal is to be able to use an additional addition and fixed width 284 // shift to get an int32_t representing round(x * 2^12). 285 // 286 // Assuming int32_t using 2-complement representation, since the mantissa part 287 // of a double precision is unsigned with the leading bit hidden, if we add an 288 // extra constant C = 2^e1 + 2^e2 with e1 > e2 >= 2^25 to the product, the 289 // part that are < 2^e2 in resulted mantissa of (x*2^12*L2E + C) can be 290 // considered as a proper 2-complement representations of x*2^12. 291 // 292 // One small problem with this approach is that the sum (x*2^12 + C) in 293 // double precision is rounded to the least significant bit of the dorminant 294 // factor C. In order to minimize the rounding errors from this addition, we 295 // want to minimize e1. Another constraint that we want is that after 296 // shifting the mantissa so that the least significant bit of int32_t 297 // corresponds to the unit bit of (x*2^12*L2E), the sign is correct without 298 // any adjustment. So combining these 2 requirements, we can choose 299 // C = 2^33 + 2^32, so that the sign bit corresponds to 2^31 bit, and hence 300 // after right shifting the mantissa, the resulting int32_t has correct sign. 301 // With this choice of C, the number of mantissa bits we need to shift to the 302 // right is: 52 - 33 = 19. 303 // 304 // Moreover, since the integer right shifts are equivalent to rounding down, 305 // we can add an extra 0.5 so that it will become round-to-nearest, tie-to- 306 // +infinity. So in particular, we can compute: 307 // hmm = x * 2^12 + C, 308 // where C = 2^33 + 2^32 + 2^-1, then if 309 // k = int32_t(lower 51 bits of double(x * 2^12 + C) >> 19), 310 // the reduced argument: 311 // lo = x - 2^-12 * k is bounded by: 312 // |lo| <= 2^-13 + 2^-12*2^-19 313 // = 2^-13 + 2^-31. 314 // 315 // Finally, notice that k only uses the mantissa of x * 2^12, so the 316 // exponent 2^12 is not needed. So we can simply define 317 // C = 2^(33 - 12) + 2^(32 - 12) + 2^(-13 - 12), and 318 // k = int32_t(lower 51 bits of double(x + C) >> 19). 319 320 // Rounding errors <= 2^-31. 321 int k = 322 static_cast<int>(cpp::bit_cast<uint64_t>(x + 0x1.8000'0000'4p21) >> 19); 323 double kd = static_cast<double>(k); 324 325 uint32_t idx1 = (k >> 6) & 0x3f; 326 uint32_t idx2 = k & 0x3f; 327 328 int hi = k >> 12; 329 330 DoubleDouble exp_mid1{EXP2_MID1[idx1].mid, EXP2_MID1[idx1].hi}; 331 DoubleDouble exp_mid2{EXP2_MID2[idx2].mid, EXP2_MID2[idx2].hi}; 332 DoubleDouble exp_mid = fputil::quick_mult(exp_mid1, exp_mid2); 333 334 // |dx| < 2^-13 + 2^-30. 335 double dx = fputil::multiply_add(kd, -0x1.0p-12, x); // exact 336 337 // We use the degree-4 polynomial to approximate 2^(lo): 338 // 2^(lo) ~ 1 + a0 * lo + a1 * lo^2 + a2 * lo^3 + a3 * lo^4 = 1 + lo * P(lo) 339 // So that the errors are bounded by: 340 // |P(lo) - (2^lo - 1)/lo| < |lo|^4 / 64 < 2^(-13 * 4) / 64 = 2^-58 341 // Let P_ be an evaluation of P where all intermediate computations are in 342 // double precision. Using either Horner's or Estrin's schemes, the evaluated 343 // errors can be bounded by: 344 // |P_(lo) - P(lo)| < 2^-51 345 // => |lo * P_(lo) - (2^lo - 1) | < 2^-64 346 // => 2^(mid1 + mid2) * |lo * P_(lo) - expm1(lo)| < 2^-63. 347 // Since we approximate 348 // 2^(mid1 + mid2) ~ exp_mid.hi + exp_mid.lo, 349 // We use the expression: 350 // (exp_mid.hi + exp_mid.lo) * (1 + dx * P_(dx)) ~ 351 // ~ exp_mid.hi + (exp_mid.hi * dx * P_(dx) + exp_mid.lo) 352 // with errors bounded by 2^-63. 353 354 double mid_lo = dx * exp_mid.hi; 355 356 // Approximate (2^dx - 1)/dx ~ 1 + a0*dx + a1*dx^2 + a2*dx^3 + a3*dx^4. 357 double p = poly_approx_d(dx); 358 359 double lo = fputil::multiply_add(p, mid_lo, exp_mid.lo); 360 361 double upper = exp_mid.hi + (lo + ERR_D); 362 double lower = exp_mid.hi + (lo - ERR_D); 363 364 if (LIBC_LIKELY(upper == lower)) { 365 // To multiply by 2^hi, a fast way is to simply add hi to the exponent 366 // field. 367 int64_t exp_hi = static_cast<int64_t>(hi) << FPBits::FRACTION_LEN; 368 double r = cpp::bit_cast<double>(exp_hi + cpp::bit_cast<int64_t>(upper)); 369 return r; 370 } 371 372 // Use double-double 373 DoubleDouble r_dd = exp2_double_double(dx, exp_mid); 374 375 double upper_dd = r_dd.hi + (r_dd.lo + ERR_DD); 376 double lower_dd = r_dd.hi + (r_dd.lo - ERR_DD); 377 378 if (LIBC_LIKELY(upper_dd == lower_dd)) { 379 // To multiply by 2^hi, a fast way is to simply add hi to the exponent 380 // field. 381 int64_t exp_hi = static_cast<int64_t>(hi) << FPBits::FRACTION_LEN; 382 double r = cpp::bit_cast<double>(exp_hi + cpp::bit_cast<int64_t>(upper_dd)); 383 return r; 384 } 385 386 // Use 128-bit precision 387 Float128 r_f128 = exp2_f128(dx, hi, idx1, idx2); 388 389 return static_cast<double>(r_f128); 390 } 391 392 } // namespace LIBC_NAMESPACE_DECL 393