| //===----------------------------------------------------------------------===// | |
| // | |
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |
| // See https://llvm.org/LICENSE.txt for license information. | |
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |
| // | |
| //===----------------------------------------------------------------------===// | |
| /// | |
| /// \file | |
| /// Common utilities and tables for double-precision pow(x, y). | |
| /// | |
| //===----------------------------------------------------------------------===// | |
| #ifndef LLVM_LIBC_SRC___SUPPORT_MATH_POW_UTILS_H | |
| #define LLVM_LIBC_SRC___SUPPORT_MATH_POW_UTILS_H | |
| #include "hdr/errno_macros.h" | |
| #include "hdr/fenv_macros.h" | |
| #include "src/__support/CPP/bit.h" | |
| #include "src/__support/CPP/optional.h" | |
| #include "src/__support/FPUtil/FEnvImpl.h" | |
| #include "src/__support/FPUtil/FPBits.h" | |
| #include "src/__support/FPUtil/double_double.h" | |
| #include "src/__support/FPUtil/nearest_integer.h" | |
| #include "src/__support/FPUtil/rounding_mode.h" | |
| #include "src/__support/FPUtil/sqrt.h" | |
| #include "src/__support/common.h" | |
| #include "src/__support/macros/attributes.h" | |
| #include "src/__support/macros/config.h" | |
| #include "src/__support/macros/optimization.h" | |
| #include "src/__support/macros/properties/cpu_features.h" | |
| #include "src/__support/math/common_constants.h" | |
| #include "src/__support/math/exp10.h" | |
| #include "src/__support/math/exp2.h" | |
| namespace LIBC_NAMESPACE_DECL { | |
| namespace math { | |
| namespace pow_internal { | |
| using fputil::DoubleDouble; | |
| using namespace common_constants_internal; | |
| // Constants for log2(x) range reduction, generated by Sollya with: | |
| // > for i from 0 to 127 do { | |
| // r = 2^-8 * ceil( 2^8 * (1 - 2^(-8)) / (1 + i*2^-7) ); | |
| // b = nearestint(log2(r) * 2^41) * 2^-41; | |
| // c = round(log2(r) - b, D, RN); | |
| // print("{ ", -c, ", ", -b, " }, "); | |
| // }; | |
| // This is the same as -log2(RD[i]), with the least significant bits of the | |
| // high part set to be 2^-41, so that the sum of high parts + e_x is exact in | |
| // double precision. | |
| // We also replace the first and the last ones to be 0. | |
| LIBC_INLINE_VAR constexpr DoubleDouble LOG2_R_DD[128] = { | |
| {0.0, 0.0}, | |
| {-0x1.19b14945cf6bap-44, 0x1.72c7ba21p-7}, | |
| {-0x1.95539356f93dcp-43, 0x1.743ee862p-6}, | |
| {0x1.abe0a48f83604p-43, 0x1.184b8e4c5p-5}, | |
| {0x1.635577970e04p-43, 0x1.77394c9d9p-5}, | |
| {-0x1.401fbaaa67e3cp-45, 0x1.d6ebd1f2p-5}, | |
| {-0x1.5b1799ceaeb51p-43, 0x1.1bb32a6008p-4}, | |
| {0x1.7c407050799bfp-43, 0x1.4c560fe688p-4}, | |
| {0x1.da6339da288fcp-43, 0x1.7d60496cf8p-4}, | |
| {0x1.be4f6f22dbbadp-43, 0x1.960caf9ab8p-4}, | |
| {-0x1.c760bc9b188c4p-45, 0x1.c7b528b71p-4}, | |
| {0x1.164e932b2d51cp-44, 0x1.f9c95dc1dp-4}, | |
| {0x1.924ae921f7ecap-45, 0x1.097e38ce6p-3}, | |
| {-0x1.6d25a5b8a19b2p-44, 0x1.22dadc2ab4p-3}, | |
| {0x1.e50a1644ac794p-43, 0x1.3c6fb650ccp-3}, | |
| {0x1.f34baa74a7942p-43, 0x1.494f863b8cp-3}, | |
| {-0x1.8f7aac147fdc1p-46, 0x1.633a8bf438p-3}, | |
| {0x1.f84be19cb9578p-43, 0x1.7046031c78p-3}, | |
| {-0x1.66cccab240e9p-46, 0x1.8a8980abfcp-3}, | |
| {-0x1.3f7a55cd2af4cp-47, 0x1.97c1cb13c8p-3}, | |
| {0x1.3458cde69308cp-43, 0x1.b2602497d4p-3}, | |
| {-0x1.667f21fa8423fp-44, 0x1.bfc67a8p-3}, | |
| {0x1.d2fe4574e09b9p-47, 0x1.dac22d3e44p-3}, | |
| {0x1.367bde40c5e6dp-43, 0x1.e857d3d36p-3}, | |
| {0x1.d45da26510033p-46, 0x1.01d9bbcfa6p-2}, | |
| {-0x1.7204f55bbf90dp-44, 0x1.08bce0d96p-2}, | |
| {-0x1.d4f1b95e0ff45p-43, 0x1.169c05364p-2}, | |
| {0x1.c20d74c0211bfp-44, 0x1.1d982c9d52p-2}, | |
| {0x1.ad89a083e072ap-43, 0x1.249cd2b13cp-2}, | |
| {0x1.cd0cb4492f1bcp-43, 0x1.32bfee370ep-2}, | |
| {-0x1.2101a9685c779p-47, 0x1.39de8e155ap-2}, | |
| {0x1.9451cd394fe8dp-43, 0x1.4106017c3ep-2}, | |
| {0x1.661e393a16b95p-44, 0x1.4f6fbb2cecp-2}, | |
| {-0x1.c6d8d86531d56p-44, 0x1.56b22e6b58p-2}, | |
| {0x1.c1c885adb21d3p-43, 0x1.5dfdcf1eeap-2}, | |
| {0x1.3bb5921006679p-45, 0x1.6552b49986p-2}, | |
| {0x1.1d406db502403p-43, 0x1.6cb0f6865cp-2}, | |
| {0x1.55a63e278bad5p-43, 0x1.7b89f02cf2p-2}, | |
| {-0x1.66ae2a7ada553p-49, 0x1.8304d90c12p-2}, | |
| {-0x1.66cccab240e9p-45, 0x1.8a8980abfcp-2}, | |
| {-0x1.62404772a151dp-45, 0x1.921800924ep-2}, | |
| {0x1.ac9bca36fd02ep-44, 0x1.99b072a96cp-2}, | |
| {0x1.4bc302ffa76fbp-43, 0x1.a8ff97181p-2}, | |
| {0x1.01fea1ec47c71p-43, 0x1.b0b67f4f46p-2}, | |
| {-0x1.f20203b3186a6p-43, 0x1.b877c57b1cp-2}, | |
| {-0x1.2642415d47384p-45, 0x1.c043859e3p-2}, | |
| {-0x1.bc76a2753b99bp-50, 0x1.c819dc2d46p-2}, | |
| {-0x1.da93ae3a5f451p-43, 0x1.cffae611aep-2}, | |
| {-0x1.50e785694a8c6p-43, 0x1.d7e6c0abc4p-2}, | |
| {0x1.c56138c894641p-43, 0x1.dfdd89d586p-2}, | |
| {0x1.5669df6a2b592p-43, 0x1.e7df5fe538p-2}, | |
| {-0x1.ea92d9e0e8ac2p-48, 0x1.efec61b012p-2}, | |
| {0x1.a0331af2e6feap-43, 0x1.f804ae8d0cp-2}, | |
| {0x1.9518ce032f41dp-48, 0x1.0014332bep-1}, | |
| {-0x1.b3b3864c60011p-44, 0x1.042bd4b9a8p-1}, | |
| {-0x1.103e8f00d41c8p-45, 0x1.08494c66b9p-1}, | |
| {0x1.65be75cc3da17p-43, 0x1.0c6caaf0c5p-1}, | |
| {0x1.3676289cd3dd4p-43, 0x1.1096015deep-1}, | |
| {-0x1.41dfc7d7c3321p-43, 0x1.14c560fe69p-1}, | |
| {0x1.e0cda8bd74461p-44, 0x1.18fadb6e2dp-1}, | |
| {0x1.2a606046ad444p-44, 0x1.1d368296b5p-1}, | |
| {0x1.f9ea977a639cp-43, 0x1.217868b0c3p-1}, | |
| {-0x1.50520a377c7ecp-45, 0x1.25c0a0463cp-1}, | |
| {0x1.6e3cb71b554e7p-47, 0x1.2a0f3c3407p-1}, | |
| {-0x1.4275f1035e5e8p-48, 0x1.2e644fac05p-1}, | |
| {-0x1.4275f1035e5e8p-48, 0x1.2e644fac05p-1}, | |
| {-0x1.979a5db68721dp-45, 0x1.32bfee370fp-1}, | |
| {0x1.1ee969a95f529p-43, 0x1.37222bb707p-1}, | |
| {0x1.bb4b69336b66ep-43, 0x1.3b8b1c68fap-1}, | |
| {0x1.d5e6a8a4fb059p-45, 0x1.3ffad4e74fp-1}, | |
| {0x1.3106e404cabb7p-44, 0x1.44716a2c08p-1}, | |
| {0x1.3106e404cabb7p-44, 0x1.44716a2c08p-1}, | |
| {-0x1.9bcaf1aa4168ap-43, 0x1.48eef19318p-1}, | |
| {0x1.1646b761c48dep-44, 0x1.4d7380dcc4p-1}, | |
| {0x1.2f0c0bfe9dbecp-43, 0x1.51ff2e3021p-1}, | |
| {0x1.29904613e33cp-43, 0x1.5692101d9bp-1}, | |
| {0x1.1d406db502403p-44, 0x1.5b2c3da197p-1}, | |
| {0x1.1d406db502403p-44, 0x1.5b2c3da197p-1}, | |
| {-0x1.125d6cbcd1095p-44, 0x1.5fcdce2728p-1}, | |
| {-0x1.bd9b32266d92cp-43, 0x1.6476d98adap-1}, | |
| {0x1.54243b21709cep-44, 0x1.6927781d93p-1}, | |
| {0x1.54243b21709cep-44, 0x1.6927781d93p-1}, | |
| {-0x1.ce60916e52e91p-44, 0x1.6ddfc2a79p-1}, | |
| {0x1.f1f5ae718f241p-43, 0x1.729fd26b7p-1}, | |
| {-0x1.6eb9612e0b4f3p-43, 0x1.7767c12968p-1}, | |
| {-0x1.6eb9612e0b4f3p-43, 0x1.7767c12968p-1}, | |
| {0x1.fed21f9cb2cc5p-43, 0x1.7c37a9227ep-1}, | |
| {0x1.7f5dc57266758p-43, 0x1.810fa51bf6p-1}, | |
| {0x1.7f5dc57266758p-43, 0x1.810fa51bf6p-1}, | |
| {0x1.5b338360c2ae2p-43, 0x1.85efd062c6p-1}, | |
| {-0x1.96fc8f4b56502p-43, 0x1.8ad846cf37p-1}, | |
| {-0x1.96fc8f4b56502p-43, 0x1.8ad846cf37p-1}, | |
| {-0x1.bdc81c4db3134p-44, 0x1.8fc924c89bp-1}, | |
| {0x1.36c101ee1344p-43, 0x1.94c287492cp-1}, | |
| {0x1.36c101ee1344p-43, 0x1.94c287492cp-1}, | |
| {0x1.e41fa0a62e6aep-44, 0x1.99c48be206p-1}, | |
| {-0x1.d97ee9124773bp-46, 0x1.9ecf50bf44p-1}, | |
| {-0x1.d97ee9124773bp-46, 0x1.9ecf50bf44p-1}, | |
| {-0x1.3f94e00e7d6bcp-46, 0x1.a3e2f4ac44p-1}, | |
| {-0x1.6879fa00b120ap-43, 0x1.a8ff971811p-1}, | |
| {-0x1.6879fa00b120ap-43, 0x1.a8ff971811p-1}, | |
| {0x1.1659d8e2d7d38p-44, 0x1.ae255819fp-1}, | |
| {0x1.1e5e0ae0d3f8ap-43, 0x1.b35458761dp-1}, | |
| {0x1.1e5e0ae0d3f8ap-43, 0x1.b35458761dp-1}, | |
| {0x1.484a15babcf88p-43, 0x1.b88cb9a2abp-1}, | |
| {0x1.484a15babcf88p-43, 0x1.b88cb9a2abp-1}, | |
| {0x1.871a7610e40bdp-45, 0x1.bdce9dcc96p-1}, | |
| {-0x1.2d90e5edaeceep-43, 0x1.c31a27dd01p-1}, | |
| {-0x1.2d90e5edaeceep-43, 0x1.c31a27dd01p-1}, | |
| {-0x1.5dd31d962d373p-43, 0x1.c86f7b7ea5p-1}, | |
| {-0x1.5dd31d962d373p-43, 0x1.c86f7b7ea5p-1}, | |
| {-0x1.9ad57391924a7p-43, 0x1.cdcebd2374p-1}, | |
| {-0x1.3167ccc538261p-44, 0x1.d338120a6ep-1}, | |
| {-0x1.3167ccc538261p-44, 0x1.d338120a6ep-1}, | |
| {0x1.c7a4ff65ddbc9p-45, 0x1.d8aba045bp-1}, | |
| {0x1.c7a4ff65ddbc9p-45, 0x1.d8aba045bp-1}, | |
| {-0x1.f9ab3cf74babap-44, 0x1.de298ec0bbp-1}, | |
| {-0x1.f9ab3cf74babap-44, 0x1.de298ec0bbp-1}, | |
| {0x1.52842c1c1e586p-43, 0x1.e3b20546f5p-1}, | |
| {0x1.52842c1c1e586p-43, 0x1.e3b20546f5p-1}, | |
| {0x1.3c6764fc87b4ap-48, 0x1.e9452c8a71p-1}, | |
| {0x1.3c6764fc87b4ap-48, 0x1.e9452c8a71p-1}, | |
| {-0x1.a0976c0a2827dp-44, 0x1.eee32e2aedp-1}, | |
| {-0x1.a0976c0a2827dp-44, 0x1.eee32e2aedp-1}, | |
| {-0x1.a45314dc4fc42p-43, 0x1.f48c34bd1fp-1}, | |
| {-0x1.a45314dc4fc42p-43, 0x1.f48c34bd1fp-1}, | |
| {0x1.ef5d00e390ap-44, 0x1.fa406bd244p-1}, | |
| {0.0, 1.0}, | |
| }; | |
| // Check if x is an odd integer: the lowest set bit must be at the unit | |
| // position: | |
| // x_e + lsb == UNIT_EXPONENT. | |
| LIBC_INLINE bool is_odd_integer(double x) { | |
| using FPBits = fputil::FPBits<double>; | |
| FPBits xbits(x); | |
| uint64_t x_u = xbits.uintval(); | |
| unsigned x_e = static_cast<unsigned>(xbits.get_biased_exponent()); | |
| unsigned lsb = | |
| static_cast<unsigned>(cpp::countr_zero(x_u | FPBits::EXP_MASK)); | |
| constexpr unsigned UNIT_EXPONENT = | |
| static_cast<unsigned>(FPBits::EXP_BIAS + FPBits::FRACTION_LEN); | |
| return (x_e + lsb == UNIT_EXPONENT); | |
| } | |
| // Check if x is an integer: the lowest set bit must be at or above the unit | |
| // position: | |
| // x_e + lsb >= UNIT_EXPONENT. | |
| LIBC_INLINE bool is_integer(double x) { | |
| if (x == 0.0) | |
| return true; | |
| using FPBits = fputil::FPBits<double>; | |
| FPBits xbits(x); | |
| uint64_t x_u = xbits.uintval(); | |
| unsigned x_e = static_cast<unsigned>(xbits.get_biased_exponent()); | |
| unsigned lsb = | |
| static_cast<unsigned>(cpp::countr_zero(x_u | FPBits::EXP_MASK)); | |
| constexpr unsigned UNIT_EXPONENT = | |
| static_cast<unsigned>(FPBits::EXP_BIAS + FPBits::FRACTION_LEN); | |
| return (x_e + lsb >= UNIT_EXPONENT); | |
| } | |
| LIBC_INLINE double set_overflow(bool is_neg) { | |
| fputil::set_errno_if_required(ERANGE); | |
| fputil::raise_overflow_except_if_required<double>(); | |
| using FPBits = fputil::FPBits<double>; | |
| #ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY | |
| int rounding = fputil::quick_get_round(); | |
| if (rounding == FE_TOWARDZERO) | |
| return is_neg ? -FPBits::max_normal().get_val() | |
| : FPBits::max_normal().get_val(); | |
| if (rounding == FE_DOWNWARD) | |
| return is_neg ? -FPBits::inf().get_val() : FPBits::max_normal().get_val(); | |
| if (rounding == FE_UPWARD) | |
| return is_neg ? -FPBits::max_normal().get_val() : FPBits::inf().get_val(); | |
| #endif // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY | |
| return is_neg ? -FPBits::inf().get_val() : FPBits::inf().get_val(); | |
| } | |
| LIBC_INLINE double set_underflow(bool is_neg) { | |
| fputil::set_errno_if_required(ERANGE); | |
| fputil::raise_underflow_except_if_required<double>(); | |
| #ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY | |
| using FPBits = fputil::FPBits<double>; | |
| int rounding = fputil::quick_get_round(); | |
| if (rounding == FE_UPWARD && !is_neg) | |
| return FPBits::min_subnormal().get_val(); | |
| if (rounding == FE_DOWNWARD && is_neg) | |
| return -FPBits::min_subnormal().get_val(); | |
| #endif // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY | |
| return is_neg ? -0.0 : 0.0; | |
| } | |
| // Rounding tests for 2^hi * (mid + lo) when the output might be denormal. We | |
| // assume further that | |
| // 1 <= mid < 2, | |
| // mid + lo < 2, | |
| // and |lo| << mid. | |
| // | |
| // Notice that, if 0 < x < 2^-1022: | |
| // double( 2^-1022 + x ) - 2^-1022 = double(x), | |
| // and if -2^-1022 < x < 0: | |
| // double(-2^-1022 + x ) - -2^-1022 = double(x). | |
| // | |
| // So if we scale |x| up by 2^1022, we can use the "+1 trick" (or "-1 trick" | |
| // when x < 0): | |
| // double( 1.0 + 2^1022 * x) - 1.0, for x > 0, and | |
| // double(-1.0 + 2^1022 * x) - -1.0, for x < 0, | |
| // to test how x is rounded in the denormal range. By adding +-1.0, the | |
| // results will have similar rounding points as denormal outputs, aligning the | |
| // least significant bit of the mantissa with 2^-52 * 2^-1022 = 2^-1074. | |
| // | |
| // Finally, subtracting 1023 in the exponent field converts the rounded normal | |
| // value into the subnormal value. | |
| template <bool SKIP_ZIV_TEST = false> | |
| LIBC_INLINE constexpr cpp::optional<double> | |
| ziv_test_denorm(int hi, double mid, double lo, double err, bool is_neg) { | |
| using FPBits = typename fputil::FPBits<double>; | |
| uint64_t scale_u = static_cast<uint64_t>(hi + 2045) << FPBits::FRACTION_LEN; | |
| double exp_scale = cpp::bit_cast<double>(scale_u); | |
| double mid_hi = mid * exp_scale; | |
| double lo_scaled = lo * exp_scale; | |
| double extra_factor = 0.0; | |
| uint64_t scale_down = 0x3FE0'0000'0000'0000ULL; // 1022 in the exponent field. | |
| // Result is denormal if |mid_hi + lo_scaled| < 1.0. | |
| // In that case, add +-1.0 to mimic denormal rounding points. | |
| if (is_neg) { | |
| if ((-1.0 - mid_hi) < lo_scaled) { | |
| // Extra rounding step is needed, which adds more rounding errors. | |
| extra_factor = -1.0; | |
| scale_down = 0x3FF0'0000'0000'0000ULL; // 1023 in the exponent field. | |
| err += 0x1.0p-52; | |
| } | |
| } else { | |
| if ((1.0 - mid_hi) > lo_scaled) { | |
| // Extra rounding step is needed, which adds more rounding errors. | |
| extra_factor = 1.0; | |
| scale_down = 0x3FF0'0000'0000'0000ULL; // 1023 in the exponent field. | |
| err += 0x1.0p-52; | |
| } | |
| } | |
| // By adding +-1.0, the hardware adder performs rounding at the exact same | |
| // bit position as denormal rounding. | |
| if constexpr (SKIP_ZIV_TEST) { | |
| double r = extra_factor + (mid_hi + lo_scaled); | |
| return cpp::bit_cast<double>(cpp::bit_cast<uint64_t>(r) - scale_down); | |
| } else { | |
| double err_scaled = err * exp_scale; | |
| double lo_u = lo_scaled + err_scaled; | |
| double lo_l = lo_scaled - err_scaled; | |
| double upper = extra_factor + (mid_hi + lo_u); | |
| double lower = extra_factor + (mid_hi + lo_l); | |
| if (LIBC_LIKELY(upper == lower)) { | |
| return cpp::bit_cast<double>(cpp::bit_cast<uint64_t>(upper) - scale_down); | |
| } | |
| return cpp::nullopt; | |
| } | |
| } | |
| // Upper bound for y = 1075 / |log2(1 - 2^(-53)|, generated by Sollya: | |
| // > y = round(-1075 / log2(1 - 2^(-53)), D, RU); | |
| // > y; | |
| // 0x1.74910d52d3052p62 | |
| // > printdouble(y); | |
| // 0x43d74910d52d3052 | |
| constexpr uint64_t Y_UPPER_BOUND = 0x43d7'4910'd52d'3052; | |
| // Lower bound for y = 2^(-54) / 1074), generated by Sollya: | |
| // > y = round(2^(-54) / 1074), D, RD); | |
| // > y; | |
| // 0x1.e829f39aef509p-65 | |
| // > printdouble(y); | |
| // 0x3bee829f39ae'f509 | |
| constexpr uint64_t Y_LOWER_BOUND = 0x3bee'829f'39ae'f509; | |
| // Fast checks for special inputs: | |
| // x = 0, +-1, 2^k, 10, +- inf | |
| // y = 0, +-1, 2, 0.5 | |
| LIBC_ALWAYS_INLINE cpp::optional<double> check_special_inputs(double x, | |
| double y) { | |
| using FPBits = fputil::FPBits<double>; | |
| FPBits xbits(x), ybits(y); | |
| bool x_sign = xbits.sign() == Sign::NEG; | |
| bool y_sign = ybits.sign() == Sign::NEG; | |
| FPBits x_abs = xbits.abs(); | |
| FPBits y_abs = ybits.abs(); | |
| uint64_t x_u = xbits.uintval(); | |
| uint64_t y_u = ybits.uintval(); | |
| uint64_t y_a = y_abs.uintval(); | |
| if (LIBC_UNLIKELY((x_u & 0x0003'FFFF'FFFF'FFFF) == 0) || | |
| ((y_u & 0x000F'FFFF'FFFF'FFFF) == 0)) { | |
| // If x or y is signaling NaN | |
| if (x_abs.is_signaling_nan() || y_abs.is_signaling_nan()) { | |
| fputil::raise_except_if_required(FE_INVALID); | |
| return FPBits::quiet_nan().get_val(); | |
| } | |
| if (x == 1.0 || y == 0.0) | |
| return 1.0; | |
| if (x == 0.0) { | |
| if (y_abs.is_nan()) | |
| return y; | |
| if (y_abs.is_inf()) | |
| return y_sign ? FPBits::inf().get_val() : 0.0; | |
| bool out_is_neg = x_sign && is_odd_integer(y); | |
| if (y_sign) { | |
| // pow(0, negative number) = inf | |
| fputil::set_errno_if_required(EDOM); | |
| fputil::raise_except_if_required(FE_DIVBYZERO); | |
| return FPBits::inf(out_is_neg ? Sign::NEG : Sign::POS).get_val(); | |
| } | |
| // pow(0, positive number) = 0 | |
| return out_is_neg ? -0.0 : 0.0; | |
| } | |
| if (y == 1.0) | |
| return x; | |
| if (y == 2.0) | |
| return x * x; | |
| if (y == 0.5 && !x_sign) | |
| return fputil::sqrt<double>(x); | |
| // TODO: Add special case y = -0.5 when rsqrt is available. | |
| if (x == 2.0) | |
| return math::exp2(y); | |
| if (x == 10.0) | |
| return math::exp10(y); | |
| // For x = 2^(+- 2^n): | |
| // x^y = 2^(+- 2^n * y). | |
| if (!x_sign && x_abs.is_normal() && xbits.get_mantissa() == 0 && | |
| y_a > Y_LOWER_BOUND && y_a < Y_UPPER_BOUND) { | |
| int e_x = xbits.get_exponent(); | |
| uint32_t abs_e = static_cast<uint32_t>(e_x > 0 ? e_x : -e_x); | |
| if (cpp::has_single_bit(abs_e)) { | |
| double hi = static_cast<double>(e_x) * y; | |
| if (hi >= 1024.0) | |
| return set_overflow(false); | |
| if (hi < -1075.0) | |
| return set_underflow(false); | |
| if (hi >= -1022.0) | |
| return math::exp2(hi); | |
| } | |
| } | |
| } | |
| return cpp::nullopt; | |
| } | |
| // Filters out extreme input ranges, infinities, NaNs, normalizes denormal | |
| // inputs, and handles negative bases. Returns a value if the result is | |
| // determined, or nullopt if regular evaluation should proceed (in which case x, | |
| // y, e_x, x_mant, is_neg, sign_d may be updated). | |
| LIBC_ALWAYS_INLINE cpp::optional<double> | |
| check_exceptional_cases(double &x, double &y, double &e_x, uint64_t &x_mant, | |
| bool &is_neg, double &sign_d) { | |
| using FPBits = fputil::FPBits<double>; | |
| FPBits xbits(x), ybits(y); | |
| bool x_sign = xbits.sign() == Sign::NEG; | |
| bool y_sign = ybits.sign() == Sign::NEG; | |
| FPBits x_abs = xbits.abs(); | |
| FPBits y_abs = ybits.abs(); | |
| uint64_t x_u = xbits.uintval(); | |
| uint64_t y_mant = ybits.get_mantissa(); | |
| uint64_t x_a = x_abs.uintval(); | |
| uint64_t y_a = y_abs.uintval(); | |
| if (LIBC_UNLIKELY(y_a <= Y_LOWER_BOUND || y_a >= Y_UPPER_BOUND || | |
| x_u >= FPBits::inf().uintval() || | |
| x_u < FPBits::min_normal().uintval())) { | |
| // If x or y is signaling NaN | |
| if (x_abs.is_signaling_nan() || y_abs.is_signaling_nan()) { | |
| fputil::raise_except_if_required(FE_INVALID); | |
| return FPBits::quiet_nan().get_val(); | |
| } | |
| // |y| > |1075 / log2(1 - 2^-53)|. | |
| if (y_a > 0x43d7'4910'd52d'3052) { | |
| if (y_a >= 0x7ff0'0000'0000'0000) { | |
| // y is inf or nan | |
| if (y_mant != 0) { | |
| // y is NaN | |
| // pow(1, NaN) = 1 is already dealt with. | |
| // pow(x, NaN) = NaN | |
| return y; | |
| } | |
| // Now y is +-Inf | |
| if (x_abs.is_nan()) { | |
| // pow(NaN, +-Inf) = NaN | |
| return x; | |
| } | |
| if (x_a == 0x3ff0'0000'0000'0000) { | |
| // pow(+-1, +-Inf) = 1.0 | |
| return 1.0; | |
| } | |
| // pow (|x| < 1, -inf) = +inf | |
| // pow (|x| < 1, +inf) = 0.0 | |
| // pow (|x| > 1, -inf) = 0.0 | |
| // pow (|x| > 1, +inf) = +inf | |
| return ((x_a < FPBits::one().uintval()) == y_sign) | |
| ? FPBits::inf().get_val() | |
| : 0.0; | |
| } | |
| // x^y will overflow / underflow in double precision. Set y to a | |
| // large enough exponent but not too large, so that the computations | |
| // won't overflow in double precision. | |
| y = y_sign ? -0x1.0p100 : 0x1.0p100; | |
| } | |
| // y is finite and non-zero. | |
| if (x_a == FPBits::inf().uintval()) { | |
| bool out_is_neg = x_sign && is_odd_integer(y); | |
| Sign out_sign = out_is_neg ? Sign::NEG : Sign::POS; | |
| return y_sign ? FPBits::zero(out_sign).get_val() | |
| : FPBits::inf(out_sign).get_val(); | |
| } | |
| if (x_a > FPBits::inf().uintval()) { | |
| // x is NaN. | |
| // pow (aNaN, 0) is already taken care above. | |
| return x; | |
| } | |
| // Normalize denormal inputs. | |
| if (x_a < FPBits::min_normal().uintval()) { | |
| FPBits x_norm(x * 0x1.0p64); | |
| e_x = static_cast<double>(x_norm.get_exponent()) - 64.0; | |
| x_mant = x_norm.get_mantissa(); | |
| } | |
| // x is finite and negative, and y is a finite integer. | |
| if (x_sign) { | |
| if (is_integer(y)) { | |
| x = -x; | |
| if (is_odd_integer(y)) { | |
| is_neg = true; | |
| sign_d = -1.0; | |
| } | |
| } else { | |
| // pow( negative, non-integer ) = NaN | |
| fputil::set_errno_if_required(EDOM); | |
| fputil::raise_except_if_required(FE_INVALID); | |
| return FPBits::quiet_nan().get_val(); | |
| } | |
| } | |
| if (y_a <= Y_LOWER_BOUND) { | |
| volatile double one = 1.0; | |
| volatile double eps = ((x_a < FPBits::one().uintval()) == !y_sign) | |
| ? -0x1.0p-100 | |
| : 0x1.0p-100; | |
| return one + eps; | |
| } | |
| } | |
| return cpp::nullopt; | |
| } | |
| // Check if x^y is an exact rounding boundary case (a 54-bit dyadic float, | |
| // which is either an exact 53-bit float or an exact halfway midpoint). | |
| // | |
| // Reference: | |
| // Lauter, C. and Lefevre, V., "Rounding Boundary Cases for Values of the | |
| // Exponential and Power Functions," IEEE Trans. Comput. 58(8):1063-1074. | |
| // | |
| // For a normalized 128-bit dyadic float with MSB at bit 127, the top 54 bits | |
| // occupy bits [127:74]. For any 54-bit number, bits [73:0] are zero. | |
| // | |
| // If x^y is not in F_54: | |
| // |x^y - o_54(x^y)| / x^y >= 2^-114. | |
| // With an evaluation error < 2^-115, the distance from bits [73:0] to the | |
| // nearest 54-bit boundary must satisfy: | |
| // dist >= (2^-114 - 2^-115) * 2^127 = 2^12. | |
| // We use a threshold of 2^-114 (dist < 2^13) to capture all boundary | |
| // candidates: | |
| // | |
| // 1. x = 2^e: x^y = 2^(e * y) is in F_54 iff e * y is an integer. | |
| // 2. x = m * 2^e, y = n * 2^f with m, n odd integers: | |
| // x^y in F_54 implies: | |
| // - 0 <= y <= 35, n <= 35, and f >= -5. | |
| // - e * y is an integer. | |
| // - When f < 0 (i.e. y = n / 2^(-f)): m^(2^f) is an integer, verified | |
| // by taking repeated square roots -f times. | |
| // - The resulting significand (m^(2^f))^n <= 2^54. | |
| // | |
| // Returns true and sets exact_m and exact_exp if x^y is an exact boundary. | |
| LIBC_INLINE bool is_exact_rounding_boundary(double x, double y, | |
| uint64_t &exact_m, int &exact_exp) { | |
| using FPBits = fputil::FPBits<double>; | |
| FPBits xbits(x); | |
| int x_e = 0; | |
| uint64_t x_mant = xbits.get_mantissa(); | |
| if (LIBC_UNLIKELY(xbits.get_biased_exponent() == 0)) { | |
| if (x_mant != 0) { | |
| int shift = cpp::countl_zero(x_mant) - 11; | |
| x_mant = (x_mant << shift) & FPBits::FRACTION_MASK; | |
| x_e = -1022 - shift; | |
| } | |
| } else { | |
| x_e = xbits.get_exponent(); | |
| } | |
| // Case 1: x = 2^e. | |
| if (x_mant == 0) { | |
| double e = static_cast<double>(x_e); | |
| double ey = e * y; | |
| if (is_integer(ey)) { | |
| exact_m = 1; | |
| exact_exp = static_cast<int>(ey); | |
| return true; | |
| } | |
| return false; | |
| } | |
| // Case 2: x is not a power of 2. | |
| if (y < 0.0 || y > 35.0) | |
| return false; | |
| // Decompose y = n * 2^f with n odd. | |
| FPBits ybits(y); | |
| uint64_t y_mant = ybits.get_mantissa() | (1ULL << FPBits::FRACTION_LEN); | |
| int y_exp = ybits.get_exponent() - static_cast<int>(FPBits::FRACTION_LEN); | |
| int tz_y = cpp::countr_zero(y_mant); | |
| uint64_t n = y_mant >> tz_y; | |
| int f = y_exp + tz_y; | |
| if (n > 35 || f < -5) | |
| return false; | |
| // Decompose x = m * 2^e with m odd. | |
| uint64_t full_x_mant = x_mant | (1ULL << FPBits::FRACTION_LEN); | |
| int tz_x = cpp::countr_zero(full_x_mant); | |
| uint64_t m = full_x_mant >> tz_x; | |
| int e = x_e - static_cast<int>(FPBits::FRACTION_LEN) + tz_x; | |
| if (f < 0) { | |
| // Non-integer exponent: y = n / 2^(-f) with -f in {1, ..., 5}. | |
| double ey = static_cast<double>(e) * y; | |
| if (!is_integer(ey)) | |
| return false; | |
| // Check if m^(2^f) is an integer by taking repeated square roots -f times. | |
| int count = -f; | |
| uint64_t cur = m; | |
| for (int i = 0; i < count; ++i) { | |
| uint64_t s = | |
| static_cast<uint64_t>(fputil::sqrt<double>(static_cast<double>(cur))); | |
| if (s * s != cur) | |
| return false; | |
| cur = s; | |
| } | |
| // Compute res = cur^n and ensure res <= 2^54. | |
| uint64_t res = 1; | |
| for (uint64_t i = 0; i < n; ++i) { | |
| if (res > (1ULL << 54) / cur) | |
| return false; | |
| res *= cur; | |
| } | |
| exact_m = res; | |
| exact_exp = static_cast<int>(ey); | |
| return true; | |
| } else { | |
| // Integer exponent: f >= 0, so y is an integer. | |
| int y_int = static_cast<int>(y); | |
| uint64_t res = 1; | |
| for (int i = 0; i < y_int; ++i) { | |
| if (res > (1ULL << 54) / m) | |
| return false; | |
| res *= m; | |
| } | |
| exact_m = res; | |
| exact_exp = e * y_int; | |
| return true; | |
| } | |
| } | |
| } // namespace pow_internal | |
| } // namespace math | |
| } // namespace LIBC_NAMESPACE_DECL | |
| #endif // LLVM_LIBC_SRC___SUPPORT_MATH_POW_UTILS_H |