| |
| |
| |
| |
| |
| |
| |
|
|
| #include "src/math/exp.h" |
| #include "common_constants.h" |
| #include "explogxf.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/PolyEval.h" |
| #include "src/__support/FPUtil/double_double.h" |
| #include "src/__support/FPUtil/dyadic_float.h" |
| #include "src/__support/FPUtil/multiply_add.h" |
| #include "src/__support/FPUtil/nearest_integer.h" |
| #include "src/__support/FPUtil/rounding_mode.h" |
| #include "src/__support/FPUtil/triple_double.h" |
| #include "src/__support/common.h" |
| #include "src/__support/integer_literals.h" |
| #include "src/__support/macros/config.h" |
| #include "src/__support/macros/optimization.h" |
|
|
| namespace LIBC_NAMESPACE_DECL { |
|
|
| using fputil::DoubleDouble; |
| using fputil::TripleDouble; |
| using Float128 = typename fputil::DyadicFloat<128>; |
|
|
| using LIBC_NAMESPACE::operator""_u128; |
|
|
| |
| constexpr double LOG2_E = 0x1.71547652b82fep+0; |
|
|
| |
| |
| constexpr double ERR_D = 0x1.8p-63; |
|
|
| #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| |
| constexpr double ERR_DD = 0x1.0p-99; |
| #endif |
|
|
| |
| |
| |
| |
| |
| |
| constexpr double MLOG_2_EXP2_M12_HI = -0x1.62e42ffp-13; |
| constexpr double MLOG_2_EXP2_M12_MID = 0x1.718432a1b0e26p-47; |
|
|
| #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| constexpr double MLOG_2_EXP2_M12_MID_30 = 0x1.718432ap-47; |
| constexpr double MLOG_2_EXP2_M12_LO = 0x1.b0e2633fe0685p-79; |
| #endif |
|
|
| namespace { |
|
|
| |
| |
| |
| |
| LIBC_INLINE double poly_approx_d(double dx) { |
| |
| double dx2 = dx * dx; |
| |
| double c0 = fputil::multiply_add(dx, 0.5, 1.0); |
| |
| double c1 = |
| fputil::multiply_add(dx, 0x1.5555555555555p-5, 0x1.5555555555555p-3); |
| |
| double p = fputil::multiply_add(dx2, c1, c0); |
| return p; |
| } |
|
|
| #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| |
| |
| |
| |
| DoubleDouble poly_approx_dd(const DoubleDouble &dx) { |
| |
| constexpr DoubleDouble COEFFS[] = { |
| {0, 0x1p0}, |
| {0, 0x1p0}, |
| {0, 0x1p-1}, |
| {0x1.5555555555555p-57, 0x1.5555555555555p-3}, |
| {0x1.5555555555555p-59, 0x1.5555555555555p-5}, |
| {0x1.1111111111111p-63, 0x1.1111111111111p-7}, |
| {-0x1.f49f49f49f49fp-65, 0x1.6c16c16c16c17p-10}, |
| }; |
|
|
| DoubleDouble p = fputil::polyeval(dx, COEFFS[0], COEFFS[1], COEFFS[2], |
| COEFFS[3], COEFFS[4], COEFFS[5], COEFFS[6]); |
| return p; |
| } |
|
|
| |
| |
| |
| |
| Float128 poly_approx_f128(const Float128 &dx) { |
| constexpr Float128 COEFFS_128[]{ |
| {Sign::POS, -127, 0x80000000'00000000'00000000'00000000_u128}, |
| {Sign::POS, -127, 0x80000000'00000000'00000000'00000000_u128}, |
| {Sign::POS, -128, 0x80000000'00000000'00000000'00000000_u128}, |
| {Sign::POS, -130, 0xaaaaaaaa'aaaaaaaa'aaaaaaaa'aaaaaaab_u128}, |
| {Sign::POS, -132, 0xaaaaaaaa'aaaaaaaa'aaaaaaaa'aaaaaaab_u128}, |
| {Sign::POS, -134, 0x88888888'88888888'88888888'88888889_u128}, |
| {Sign::POS, -137, 0xb60b60b6'0b60b60b'60b60b60'b60b60b6_u128}, |
| {Sign::POS, -140, 0xd00d00d0'0d00d00d'00d00d00'd00d00d0_u128}, |
| }; |
|
|
| Float128 p = fputil::polyeval(dx, COEFFS_128[0], COEFFS_128[1], COEFFS_128[2], |
| COEFFS_128[3], COEFFS_128[4], COEFFS_128[5], |
| COEFFS_128[6], COEFFS_128[7]); |
| return p; |
| } |
|
|
| |
| |
| |
| Float128 exp_f128(double x, double kd, int idx1, int idx2) { |
| |
|
|
| double t1 = fputil::multiply_add(kd, MLOG_2_EXP2_M12_HI, x); |
| double t2 = kd * MLOG_2_EXP2_M12_MID_30; |
| double t3 = kd * MLOG_2_EXP2_M12_LO; |
|
|
| Float128 dx = fputil::quick_add( |
| Float128(t1), fputil::quick_add(Float128(t2), Float128(t3))); |
|
|
| |
| Float128 exp_mid1 = |
| fputil::quick_add(Float128(EXP2_MID1[idx1].hi), |
| fputil::quick_add(Float128(EXP2_MID1[idx1].mid), |
| Float128(EXP2_MID1[idx1].lo))); |
|
|
| Float128 exp_mid2 = |
| fputil::quick_add(Float128(EXP2_MID2[idx2].hi), |
| fputil::quick_add(Float128(EXP2_MID2[idx2].mid), |
| Float128(EXP2_MID2[idx2].lo))); |
|
|
| Float128 exp_mid = fputil::quick_mul(exp_mid1, exp_mid2); |
|
|
| Float128 p = poly_approx_f128(dx); |
|
|
| Float128 r = fputil::quick_mul(exp_mid, p); |
|
|
| r.exponent += static_cast<int>(kd) >> 12; |
|
|
| return r; |
| } |
|
|
| |
| DoubleDouble exp_double_double(double x, double kd, |
| const DoubleDouble &exp_mid) { |
| |
| |
| double t1 = fputil::multiply_add(kd, MLOG_2_EXP2_M12_HI, x); |
| double t2 = kd * MLOG_2_EXP2_M12_MID_30; |
| double t3 = kd * MLOG_2_EXP2_M12_LO; |
|
|
| DoubleDouble dx = fputil::exact_add(t1, t2); |
| dx.lo += t3; |
|
|
| |
| |
| DoubleDouble p = poly_approx_dd(dx); |
|
|
| |
| DoubleDouble r = fputil::quick_mult(exp_mid, p); |
|
|
| return r; |
| } |
| #endif |
|
|
| |
| |
| double set_exceptional(double x) { |
| using FPBits = typename fputil::FPBits<double>; |
| FPBits xbits(x); |
|
|
| uint64_t x_u = xbits.uintval(); |
| uint64_t x_abs = xbits.abs().uintval(); |
|
|
| |
| if (x_abs <= 0x3ca0'0000'0000'0000ULL) { |
| |
| return 1 + x; |
| } |
|
|
| |
|
|
| |
| if (x_u >= 0xc087'4910'd52d'3052ULL) { |
| |
| if (xbits.is_inf()) |
| return 0.0; |
|
|
| |
| if (xbits.is_nan()) |
| return x; |
|
|
| if (fputil::quick_get_round() == FE_UPWARD) |
| return FPBits::min_subnormal().get_val(); |
| fputil::set_errno_if_required(ERANGE); |
| fputil::raise_except_if_required(FE_UNDERFLOW); |
| return 0.0; |
| } |
|
|
| |
| |
| if (x_u < 0x7ff0'0000'0000'0000ULL) { |
| int rounding = fputil::quick_get_round(); |
| if (rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO) |
| return FPBits::max_normal().get_val(); |
|
|
| fputil::set_errno_if_required(ERANGE); |
| fputil::raise_except_if_required(FE_OVERFLOW); |
| } |
| |
| return x + FPBits::inf().get_val(); |
| } |
|
|
| } |
|
|
| LLVM_LIBC_FUNCTION(double, exp, (double x)) { |
| using FPBits = typename fputil::FPBits<double>; |
| FPBits xbits(x); |
|
|
| uint64_t x_u = xbits.uintval(); |
|
|
| |
| |
| |
| |
| |
|
|
| |
| |
|
|
| |
| |
|
|
| |
| if (LIBC_UNLIKELY(x_u >= 0xc0874910d52d3052 || |
| (x_u < 0xbca0000000000000 && x_u >= 0x40862e42fefa39f0) || |
| x_u < 0x3ca0000000000000)) { |
| return set_exceptional(x); |
| } |
|
|
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| double tmp = fputil::multiply_add(x, LOG2_E, 0x1.8000'0000'4p21); |
| int k = static_cast<int>(cpp::bit_cast<uint64_t>(tmp) >> 19); |
| double kd = static_cast<double>(k); |
|
|
| uint32_t idx1 = (k >> 6) & 0x3f; |
| uint32_t idx2 = k & 0x3f; |
| int hi = k >> 12; |
|
|
| bool denorm = (hi <= -1022); |
|
|
| DoubleDouble exp_mid1{EXP2_MID1[idx1].mid, EXP2_MID1[idx1].hi}; |
| DoubleDouble exp_mid2{EXP2_MID2[idx2].mid, EXP2_MID2[idx2].hi}; |
|
|
| DoubleDouble exp_mid = fputil::quick_mult(exp_mid1, exp_mid2); |
|
|
| |
| |
| |
| |
| double lo_h = fputil::multiply_add(kd, MLOG_2_EXP2_M12_HI, x); |
| double dx = fputil::multiply_add(kd, MLOG_2_EXP2_M12_MID, lo_h); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| double mid_lo = dx * exp_mid.hi; |
|
|
| |
| double p = poly_approx_d(dx); |
|
|
| double lo = fputil::multiply_add(p, mid_lo, exp_mid.lo); |
|
|
| #ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| if (LIBC_UNLIKELY(denorm)) { |
| return ziv_test_denorm<true>(hi, exp_mid.hi, lo, ERR_D) |
| .value(); |
| } else { |
| |
| |
| int64_t exp_hi = static_cast<int64_t>(hi) << FPBits::FRACTION_LEN; |
| double r = |
| cpp::bit_cast<double>(exp_hi + cpp::bit_cast<int64_t>(exp_mid.hi + lo)); |
| return r; |
| } |
| #else |
| if (LIBC_UNLIKELY(denorm)) { |
| if (auto r = ziv_test_denorm(hi, exp_mid.hi, lo, ERR_D); |
| LIBC_LIKELY(r.has_value())) |
| return r.value(); |
| } else { |
| double upper = exp_mid.hi + (lo + ERR_D); |
| double lower = exp_mid.hi + (lo - ERR_D); |
|
|
| if (LIBC_LIKELY(upper == lower)) { |
| |
| |
| int64_t exp_hi = static_cast<int64_t>(hi) << FPBits::FRACTION_LEN; |
| double r = cpp::bit_cast<double>(exp_hi + cpp::bit_cast<int64_t>(upper)); |
| return r; |
| } |
| } |
|
|
| |
| DoubleDouble r_dd = exp_double_double(x, kd, exp_mid); |
|
|
| if (LIBC_UNLIKELY(denorm)) { |
| if (auto r = ziv_test_denorm(hi, r_dd.hi, r_dd.lo, ERR_DD); |
| LIBC_LIKELY(r.has_value())) |
| return r.value(); |
| } else { |
| double upper_dd = r_dd.hi + (r_dd.lo + ERR_DD); |
| double lower_dd = r_dd.hi + (r_dd.lo - ERR_DD); |
|
|
| if (LIBC_LIKELY(upper_dd == lower_dd)) { |
| int64_t exp_hi = static_cast<int64_t>(hi) << FPBits::FRACTION_LEN; |
| double r = |
| cpp::bit_cast<double>(exp_hi + cpp::bit_cast<int64_t>(upper_dd)); |
| return r; |
| } |
| } |
|
|
| |
| Float128 r_f128 = exp_f128(x, kd, idx1, idx2); |
|
|
| return static_cast<double>(r_f128); |
| #endif |
| } |
|
|
| } |
|
|