diff options
| author | Peter Collingbourne <peter@pcc.me.uk> | 2025-07-18 13:26:00 -0700 |
|---|---|---|
| committer | Peter Collingbourne <peter@pcc.me.uk> | 2025-07-18 13:26:00 -0700 |
| commit | 9bf3524731070cadc6175707314f3b6ca37190d5 (patch) | |
| tree | 86dcab7604336b01ae938fe81062c29ff69efba8 /libc/src | |
| parent | 3a84c15cc13b6daf8e812592898ab6c7f19091a9 (diff) | |
| parent | 4f43f0606c3d7e1ce6d069583b5e59f036e112ce (diff) | |
Created using spr 1.3.6-beta.1
Diffstat (limited to 'libc/src')
72 files changed, 3005 insertions, 1797 deletions
diff --git a/libc/src/CMakeLists.txt b/libc/src/CMakeLists.txt index a665253c4cc0..d7a1e1f49e6f 100644 --- a/libc/src/CMakeLists.txt +++ b/libc/src/CMakeLists.txt @@ -17,6 +17,7 @@ add_subdirectory(strings) add_subdirectory(time) add_subdirectory(unistd) add_subdirectory(wchar) +add_subdirectory(wctype) if(${LIBC_TARGET_OS} STREQUAL "linux") add_subdirectory(dirent) diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt index 294d68474bd5..37a27cc9b700 100644 --- a/libc/src/__support/CMakeLists.txt +++ b/libc/src/__support/CMakeLists.txt @@ -406,7 +406,7 @@ add_subdirectory(time) # Requires access to uchar header which is not on macos # Therefore, cannot currently build this on macos in overlay mode -if(NOT(LIBC_TARGET_OS_IS_DARWIN)) +if(NOT (LIBC_TARGET_OS_IS_DARWIN)) add_subdirectory(wchar) endif() diff --git a/libc/src/__support/FPUtil/ManipulationFunctions.h b/libc/src/__support/FPUtil/ManipulationFunctions.h index 9c10011ccd20..ea9ee5a57c36 100644 --- a/libc/src/__support/FPUtil/ManipulationFunctions.h +++ b/libc/src/__support/FPUtil/ManipulationFunctions.h @@ -29,7 +29,7 @@ namespace LIBC_NAMESPACE_DECL { namespace fputil { template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0> -LIBC_INLINE T frexp(T x, int &exp) { +LIBC_INLINE constexpr T frexp(T x, int &exp) { FPBits<T> bits(x); if (bits.is_inf_or_nan()) { #ifdef LIBC_FREXP_INF_NAN_EXPONENT diff --git a/libc/src/__support/FPUtil/PolyEval.h b/libc/src/__support/FPUtil/PolyEval.h index 41104620ed61..7bec4e30a996 100644 --- a/libc/src/__support/FPUtil/PolyEval.h +++ b/libc/src/__support/FPUtil/PolyEval.h @@ -37,7 +37,7 @@ LIBC_INLINE cpp::enable_if_t<(sizeof(T) <= sizeof(void *)), T> polyeval(T, } template <typename T, typename... Ts> -LIBC_INLINE cpp::enable_if_t<(sizeof(T) > sizeof(void *)), T> +LIBC_INLINE static constexpr cpp::enable_if_t<(sizeof(T) > sizeof(void *)), T> polyeval(const T &x, const T &a0, const Ts &...a) { return multiply_add(x, polyeval(x, a...), a0); } diff --git a/libc/src/__support/FPUtil/double_double.h b/libc/src/__support/FPUtil/double_double.h index c27885aadc02..8e54e845de49 100644 --- a/libc/src/__support/FPUtil/double_double.h +++ b/libc/src/__support/FPUtil/double_double.h @@ -151,8 +151,8 @@ LIBC_INLINE DoubleDouble quick_mult(double a, const DoubleDouble &b) { } template <size_t SPLIT_B = 27> -LIBC_INLINE DoubleDouble quick_mult(const DoubleDouble &a, - const DoubleDouble &b) { +LIBC_INLINE constexpr DoubleDouble quick_mult(const DoubleDouble &a, + const DoubleDouble &b) { DoubleDouble r = exact_mult<double, SPLIT_B>(a.hi, b.hi); double t1 = multiply_add(a.hi, b.lo, r.lo); double t2 = multiply_add(a.lo, b.hi, t1); diff --git a/libc/src/__support/FPUtil/multiply_add.h b/libc/src/__support/FPUtil/multiply_add.h index 8260702e2c9f..1469326c9ba3 100644 --- a/libc/src/__support/FPUtil/multiply_add.h +++ b/libc/src/__support/FPUtil/multiply_add.h @@ -29,7 +29,7 @@ multiply_add(const T &x, const T &y, const T &z) { } template <typename T> -LIBC_INLINE cpp::enable_if_t<(sizeof(T) <= sizeof(void *)), T> +LIBC_INLINE static constexpr cpp::enable_if_t<(sizeof(T) <= sizeof(void *)), T> multiply_add(T x, T y, T z) { return x * y + z; } diff --git a/libc/src/__support/FPUtil/nearest_integer.h b/libc/src/__support/FPUtil/nearest_integer.h index 768f13414bd9..fabd309bd352 100644 --- a/libc/src/__support/FPUtil/nearest_integer.h +++ b/libc/src/__support/FPUtil/nearest_integer.h @@ -40,7 +40,7 @@ namespace fputil { // Notice that for AARCH64 and x86-64 with SSE4.2 support, we will use their // corresponding rounding instruction instead. And in those cases, the results // are rounded to the nearest integer, tie-to-even. -LIBC_INLINE float nearest_integer(float x) { +LIBC_INLINE static constexpr float nearest_integer(float x) { if (x < 0x1p24f && x > -0x1p24f) { float r = x < 0 ? (x - 0x1.0p23f) + 0x1.0p23f : (x + 0x1.0p23f) - 0x1.0p23f; float diff = x - r; @@ -56,7 +56,7 @@ LIBC_INLINE float nearest_integer(float x) { return x; } -LIBC_INLINE double nearest_integer(double x) { +LIBC_INLINE static constexpr double nearest_integer(double x) { if (x < 0x1p53 && x > -0x1p53) { double r = x < 0 ? (x - 0x1.0p52) + 0x1.0p52 : (x + 0x1.0p52) - 0x1.0p52; double diff = x - r; diff --git a/libc/src/__support/macros/properties/architectures.h b/libc/src/__support/macros/properties/architectures.h index c88956ff4114..ecc93196be28 100644 --- a/libc/src/__support/macros/properties/architectures.h +++ b/libc/src/__support/macros/properties/architectures.h @@ -21,7 +21,7 @@ #define LIBC_TARGET_ARCH_IS_GPU #endif -#if defined(__pnacl__) || defined(__CLR_VER) || defined(LIBC_TARGET_ARCH_IS_GPU) +#if defined(__CLR_VER) || defined(LIBC_TARGET_ARCH_IS_GPU) #define LIBC_TARGET_ARCH_IS_VM #endif diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt index 4c73fba6613f..77a47c65489d 100644 --- a/libc/src/__support/math/CMakeLists.txt +++ b/libc/src/__support/math/CMakeLists.txt @@ -55,3 +55,180 @@ add_header_library( libc.src.__support.macros.optimization libc.include.llvm-libc-macros.float16_macros ) + +add_header_library( + frexpf128 + HDRS + frexpf128.h + DEPENDS + libc.src.__support.macros.properties.types + libc.src.__support.FPUtil.manipulation_functions +) + +add_header_library( + frexpf16 + HDRS + frexpf16.h + DEPENDS + libc.src.__support.macros.config + libc.src.__support.macros.properties.types + libc.src.__support.FPUtil.manipulation_functions +) + +add_header_library( + frexpf + HDRS + frexpf.h + DEPENDS + libc.src.__support.FPUtil.manipulation_functions +) + +add_header_library( + ldexpf128 + HDRS + ldexpf128.h + DEPENDS + libc.src.__support.macros.properties.types + libc.src.__support.FPUtil.manipulation_functions + libc.include.llvm-libc-types.float128 +) + +add_header_library( + ldexpf16 + HDRS + ldexpf16.h + DEPENDS + libc.src.__support.macros.properties.types + libc.src.__support.FPUtil.manipulation_functions + libc.include.llvm-libc-macros.float16_macros +) + +add_header_library( + ldexpf + HDRS + ldexpf.h + DEPENDS + libc.src.__support.FPUtil.manipulation_functions +) + +add_header_library( + exp_constants + HDRS + exp_constants.h + DEPENDS + libc.src.__support.FPUtil.triple_double +) + +add_header_library( + exp_utils + HDRS + exp_utils.h + DEPENDS + libc.src.__support.CPP.optional + libc.src.__support.CPP.bit + libc.src.__support.FPUtil.fp_bits +) + +add_header_library( + exp + HDRS + exp.h + DEPENDS + .exp_constants + .exp_utils + libc.src.__support.CPP.bit + libc.src.__support.CPP.optional + libc.src.__support.FPUtil.dyadic_float + libc.src.__support.FPUtil.fenv_impl + libc.src.__support.FPUtil.fp_bits + libc.src.__support.FPUtil.multiply_add + libc.src.__support.FPUtil.nearest_integer + libc.src.__support.FPUtil.polyeval + libc.src.__support.FPUtil.rounding_mode + libc.src.__support.FPUtil.triple_double + libc.src.__support.integer_literals + libc.src.__support.macros.optimization +) + +add_header_library( + exp10 + HDRS + exp10.h + DEPENDS + .exp_constants + .exp_utils + libc.src.__support.CPP.bit + libc.src.__support.CPP.optional + libc.src.__support.FPUtil.dyadic_float + libc.src.__support.FPUtil.fenv_impl + libc.src.__support.FPUtil.fp_bits + libc.src.__support.FPUtil.multiply_add + libc.src.__support.FPUtil.nearest_integer + libc.src.__support.FPUtil.polyeval + libc.src.__support.FPUtil.rounding_mode + libc.src.__support.FPUtil.triple_double + libc.src.__support.integer_literals + libc.src.__support.macros.optimization +) + +add_header_library( + exp10f_utils + HDRS + exp10f_utils.h + DEPENDS + libc.src.__support.FPUtil.basic_operations + libc.src.__support.FPUtil.fenv_impl + libc.src.__support.FPUtil.multiply_add + libc.src.__support.FPUtil.nearest_integer + libc.src.__support.FPUtil.polyeval + libc.src.__support.common + libc.src.__support.math.exp_utils +) + +add_header_library( + exp10f + HDRS + exp10f.h + DEPENDS + .exp10f_utils + libc.src.__support.macros.config + libc.src.__support.FPUtil.fenv_impl + libc.src.__support.FPUtil.fp_bits + libc.src.__support.FPUtil.multiply_add + libc.src.__support.FPUtil.rounding_mode + libc.src.__support.macros.optimization +) + +add_header_library( + exp10_float16_constants + HDRS + exp10_float16_constants.h + DEPENDS + libc.src.__support.CPP.array +) + +add_header_library( + exp10f16_utils + HDRS + exp10f16_utils.h + DEPENDS + .expf16_utils + .exp10_float16_constants + libc.src.__support.FPUtil.fp_bits +) + +add_header_library( + exp10f16 + HDRS + exp10f16.h + DEPENDS + .exp10f16_utils + libc.src.__support.FPUtil.fp_bits + src.__support.FPUtil.FEnvImpl + src.__support.FPUtil.FPBits + src.__support.FPUtil.cast + src.__support.FPUtil.rounding_mode + src.__support.FPUtil.except_value_utils + src.__support.macros.optimization + src.__support.macros.properties.cpu_features +) diff --git a/libc/src/__support/math/exp.h b/libc/src/__support/math/exp.h new file mode 100644 index 000000000000..a538df1e825d --- /dev/null +++ b/libc/src/__support/math/exp.h @@ -0,0 +1,448 @@ +//===-- Implementation header for exp ---------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXP_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_EXP_H + +#include "exp_constants.h" +#include "exp_utils.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" // LIBC_UNLIKELY + +namespace LIBC_NAMESPACE_DECL { + +using fputil::DoubleDouble; +using fputil::TripleDouble; +using Float128 = typename fputil::DyadicFloat<128>; + +using LIBC_NAMESPACE::operator""_u128; + +// log2(e) +static constexpr double LOG2_E = 0x1.71547652b82fep+0; + +// Error bounds: +// Errors when using double precision. +static constexpr double ERR_D = 0x1.8p-63; + +#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS +// Errors when using double-double precision. +static constexpr double ERR_DD = 0x1.0p-99; +#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS + +// -2^-12 * log(2) +// > a = -2^-12 * log(2); +// > b = round(a, 30, RN); +// > c = round(a - b, 30, RN); +// > d = round(a - b - c, D, RN); +// Errors < 1.5 * 2^-133 +static constexpr double MLOG_2_EXP2_M12_HI = -0x1.62e42ffp-13; +static constexpr double MLOG_2_EXP2_M12_MID = 0x1.718432a1b0e26p-47; + +#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS +static constexpr double MLOG_2_EXP2_M12_MID_30 = 0x1.718432ap-47; +static constexpr double MLOG_2_EXP2_M12_LO = 0x1.b0e2633fe0685p-79; +#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS + +namespace { + +// Polynomial approximations with double precision: +// Return expm1(dx) / x ~ 1 + dx / 2 + dx^2 / 6 + dx^3 / 24. +// For |dx| < 2^-13 + 2^-30: +// | output - expm1(dx) / dx | < 2^-51. +static double poly_approx_d(double dx) { + // dx^2 + double dx2 = dx * dx; + // c0 = 1 + dx / 2 + double c0 = fputil::multiply_add(dx, 0.5, 1.0); + // c1 = 1/6 + dx / 24 + double c1 = + fputil::multiply_add(dx, 0x1.5555555555555p-5, 0x1.5555555555555p-3); + // p = dx^2 * c1 + c0 = 1 + dx / 2 + dx^2 / 6 + dx^3 / 24 + double p = fputil::multiply_add(dx2, c1, c0); + return p; +} + +#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS +// Polynomial approximation with double-double precision: +// Return exp(dx) ~ 1 + dx + dx^2 / 2 + ... + dx^6 / 720 +// For |dx| < 2^-13 + 2^-30: +// | output - exp(dx) | < 2^-101 +static DoubleDouble poly_approx_dd(const DoubleDouble &dx) { + // Taylor polynomial. + constexpr DoubleDouble COEFFS[] = { + {0, 0x1p0}, // 1 + {0, 0x1p0}, // 1 + {0, 0x1p-1}, // 1/2 + {0x1.5555555555555p-57, 0x1.5555555555555p-3}, // 1/6 + {0x1.5555555555555p-59, 0x1.5555555555555p-5}, // 1/24 + {0x1.1111111111111p-63, 0x1.1111111111111p-7}, // 1/120 + {-0x1.f49f49f49f49fp-65, 0x1.6c16c16c16c17p-10}, // 1/720 + }; + + DoubleDouble p = fputil::polyeval(dx, COEFFS[0], COEFFS[1], COEFFS[2], + COEFFS[3], COEFFS[4], COEFFS[5], COEFFS[6]); + return p; +} + +// Polynomial approximation with 128-bit precision: +// Return exp(dx) ~ 1 + dx + dx^2 / 2 + ... + dx^7 / 5040 +// For |dx| < 2^-13 + 2^-30: +// | output - exp(dx) | < 2^-126. +static Float128 poly_approx_f128(const Float128 &dx) { + constexpr Float128 COEFFS_128[]{ + {Sign::POS, -127, 0x80000000'00000000'00000000'00000000_u128}, // 1.0 + {Sign::POS, -127, 0x80000000'00000000'00000000'00000000_u128}, // 1.0 + {Sign::POS, -128, 0x80000000'00000000'00000000'00000000_u128}, // 0.5 + {Sign::POS, -130, 0xaaaaaaaa'aaaaaaaa'aaaaaaaa'aaaaaaab_u128}, // 1/6 + {Sign::POS, -132, 0xaaaaaaaa'aaaaaaaa'aaaaaaaa'aaaaaaab_u128}, // 1/24 + {Sign::POS, -134, 0x88888888'88888888'88888888'88888889_u128}, // 1/120 + {Sign::POS, -137, 0xb60b60b6'0b60b60b'60b60b60'b60b60b6_u128}, // 1/720 + {Sign::POS, -140, 0xd00d00d0'0d00d00d'00d00d00'd00d00d0_u128}, // 1/5040 + }; + + 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; +} + +// Compute exp(x) using 128-bit precision. +// TODO(lntue): investigate triple-double precision implementation for this +// step. +static Float128 exp_f128(double x, double kd, int idx1, int idx2) { + // Recalculate dx: + + double t1 = fputil::multiply_add(kd, MLOG_2_EXP2_M12_HI, x); // exact + double t2 = kd * MLOG_2_EXP2_M12_MID_30; // exact + double t3 = kd * MLOG_2_EXP2_M12_LO; // Error < 2^-133 + + Float128 dx = fputil::quick_add( + Float128(t1), fputil::quick_add(Float128(t2), Float128(t3))); + + // TODO: Skip recalculating exp_mid1 and exp_mid2. + 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; +} + +// Compute exp(x) with double-double precision. +static DoubleDouble exp_double_double(double x, double kd, + const DoubleDouble &exp_mid) { + // Recalculate dx: + // dx = x - k * 2^-12 * log(2) + double t1 = fputil::multiply_add(kd, MLOG_2_EXP2_M12_HI, x); // exact + double t2 = kd * MLOG_2_EXP2_M12_MID_30; // exact + double t3 = kd * MLOG_2_EXP2_M12_LO; // Error < 2^-130 + + DoubleDouble dx = fputil::exact_add(t1, t2); + dx.lo += t3; + + // Degree-6 Taylor polynomial approximation in double-double precision. + // | p - exp(x) | < 2^-100. + DoubleDouble p = poly_approx_dd(dx); + + // Error bounds: 2^-99. + DoubleDouble r = fputil::quick_mult(exp_mid, p); + + return r; +} +#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS + +// Check for exceptional cases when +// |x| <= 2^-53 or x < log(2^-1075) or x >= 0x1.6232bdd7abcd3p+9 +static 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(); + + // |x| <= 2^-53 + if (x_abs <= 0x3ca0'0000'0000'0000ULL) { + // exp(x) ~ 1 + x + return 1 + x; + } + + // x <= log(2^-1075) || x >= 0x1.6232bdd7abcd3p+9 or inf/nan. + + // x <= log(2^-1075) or -inf/nan + if (x_u >= 0xc087'4910'd52d'3052ULL) { + // exp(-Inf) = 0 + if (xbits.is_inf()) + return 0.0; + + // exp(nan) = nan + 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; + } + + // x >= round(log(MAX_NORMAL), D, RU) = 0x1.62e42fefa39fp+9 or +inf/nan + // x is finite + 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); + } + // x is +inf or nan + return x + FPBits::inf().get_val(); +} + +} // namespace + +namespace math { + +static double exp(double x) { + using FPBits = typename fputil::FPBits<double>; + FPBits xbits(x); + + uint64_t x_u = xbits.uintval(); + + // Upper bound: max normal number = 2^1023 * (2 - 2^-52) + // > round(log (2^1023 ( 2 - 2^-52 )), D, RU) = 0x1.62e42fefa39fp+9 + // > round(log (2^1023 ( 2 - 2^-52 )), D, RD) = 0x1.62e42fefa39efp+9 + // > round(log (2^1023 ( 2 - 2^-52 )), D, RN) = 0x1.62e42fefa39efp+9 + // > round(exp(0x1.62e42fefa39fp+9), D, RN) = infty + + // Lower bound: min denormal number / 2 = 2^-1075 + // > round(log(2^-1075), D, RN) = -0x1.74910d52d3052p9 + + // Another lower bound: min normal number = 2^-1022 + // > round(log(2^-1022), D, RN) = -0x1.6232bdd7abcd2p9 + + // x < log(2^-1075) or x >= 0x1.6232bdd7abcd3p+9 or |x| < 2^-53. + if (LIBC_UNLIKELY(x_u >= 0xc0874910d52d3052 || + (x_u < 0xbca0000000000000 && x_u >= 0x40862e42fefa39f0) || + x_u < 0x3ca0000000000000)) { + return set_exceptional(x); + } + + // Now log(2^-1075) <= x <= -2^-53 or 2^-53 <= x < log(2^1023 * (2 - 2^-52)) + + // Range reduction: + // Let x = log(2) * (hi + mid1 + mid2) + lo + // in which: + // hi is an integer + // mid1 * 2^6 is an integer + // mid2 * 2^12 is an integer + // then: + // exp(x) = 2^hi * 2^(mid1) * 2^(mid2) * exp(lo). + // With this formula: + // - multiplying by 2^hi is exact and cheap, simply by adding the exponent + // field. + // - 2^(mid1) and 2^(mid2) are stored in 2 x 64-element tables. + // - exp(lo) ~ 1 + lo + a0 * lo^2 + ... + // + // They can be defined by: + // hi + mid1 + mid2 = 2^(-12) * round(2^12 * log_2(e) * x) + // If we store L2E = round(log2(e), D, RN), then: + // log2(e) - L2E ~ 1.5 * 2^(-56) + // So the errors when computing in double precision is: + // | x * 2^12 * log_2(e) - D(x * 2^12 * L2E) | <= + // <= | x * 2^12 * log_2(e) - x * 2^12 * L2E | + + // + | x * 2^12 * L2E - D(x * 2^12 * L2E) | + // <= 2^12 * ( |x| * 1.5 * 2^-56 + eps(x)) for RN + // 2^12 * ( |x| * 1.5 * 2^-56 + 2*eps(x)) for other rounding modes. + // So if: + // hi + mid1 + mid2 = 2^(-12) * round(x * 2^12 * L2E) is computed entirely + // in double precision, the reduced argument: + // lo = x - log(2) * (hi + mid1 + mid2) is bounded by: + // |lo| <= 2^-13 + (|x| * 1.5 * 2^-56 + 2*eps(x)) + // < 2^-13 + (1.5 * 2^9 * 1.5 * 2^-56 + 2*2^(9 - 52)) + // < 2^-13 + 2^-41 + // + + // The following trick computes the round(x * L2E) more efficiently + // than using the rounding instructions, with the tradeoff for less accuracy, + // and hence a slightly larger range for the reduced argument `lo`. + // + // To be precise, since |x| < |log(2^-1075)| < 1.5 * 2^9, + // |x * 2^12 * L2E| < 1.5 * 2^9 * 1.5 < 2^23, + // So we can fit the rounded result round(x * 2^12 * L2E) in int32_t. + // Thus, the goal is to be able to use an additional addition and fixed width + // shift to get an int32_t representing round(x * 2^12 * L2E). + // + // Assuming int32_t using 2-complement representation, since the mantissa part + // of a double precision is unsigned with the leading bit hidden, if we add an + // extra constant C = 2^e1 + 2^e2 with e1 > e2 >= 2^25 to the product, the + // part that are < 2^e2 in resulted mantissa of (x*2^12*L2E + C) can be + // considered as a proper 2-complement representations of x*2^12*L2E. + // + // One small problem with this approach is that the sum (x*2^12*L2E + C) in + // double precision is rounded to the least significant bit of the dorminant + // factor C. In order to minimize the rounding errors from this addition, we + // want to minimize e1. Another constraint that we want is that after + // shifting the mantissa so that the least significant bit of int32_t + // corresponds to the unit bit of (x*2^12*L2E), the sign is correct without + // any adjustment. So combining these 2 requirements, we can choose + // C = 2^33 + 2^32, so that the sign bit corresponds to 2^31 bit, and hence + // after right shifting the mantissa, the resulting int32_t has correct sign. + // With this choice of C, the number of mantissa bits we need to shift to the + // right is: 52 - 33 = 19. + // + // Moreover, since the integer right shifts are equivalent to rounding down, + // we can add an extra 0.5 so that it will become round-to-nearest, tie-to- + // +infinity. So in particular, we can compute: + // hmm = x * 2^12 * L2E + C, + // where C = 2^33 + 2^32 + 2^-1, then if + // k = int32_t(lower 51 bits of double(x * 2^12 * L2E + C) >> 19), + // the reduced argument: + // lo = x - log(2) * 2^-12 * k is bounded by: + // |lo| <= 2^-13 + 2^-41 + 2^-12*2^-19 + // = 2^-13 + 2^-31 + 2^-41. + // + // Finally, notice that k only uses the mantissa of x * 2^12 * L2E, so the + // exponent 2^12 is not needed. So we can simply define + // C = 2^(33 - 12) + 2^(32 - 12) + 2^(-13 - 12), and + // k = int32_t(lower 51 bits of double(x * L2E + C) >> 19). + + // Rounding errors <= 2^-31 + 2^-41. + 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); + + // |x - (hi + mid1 + mid2) * log(2) - dx| < 2^11 * eps(M_LOG_2_EXP2_M12.lo) + // = 2^11 * 2^-13 * 2^-52 + // = 2^-54. + // |dx| < 2^-13 + 2^-30. + double lo_h = fputil::multiply_add(kd, MLOG_2_EXP2_M12_HI, x); // exact + double dx = fputil::multiply_add(kd, MLOG_2_EXP2_M12_MID, lo_h); + + // We use the degree-4 Taylor polynomial to approximate exp(lo): + // exp(lo) ~ 1 + lo + lo^2 / 2 + lo^3 / 6 + lo^4 / 24 = 1 + lo * P(lo) + // So that the errors are bounded by: + // |P(lo) - expm1(lo)/lo| < |lo|^4 / 64 < 2^(-13 * 4) / 64 = 2^-58 + // Let P_ be an evaluation of P where all intermediate computations are in + // double precision. Using either Horner's or Estrin's schemes, the evaluated + // errors can be bounded by: + // |P_(dx) - P(dx)| < 2^-51 + // => |dx * P_(dx) - expm1(lo) | < 1.5 * 2^-64 + // => 2^(mid1 + mid2) * |dx * P_(dx) - expm1(lo)| < 1.5 * 2^-63. + // Since we approximate + // 2^(mid1 + mid2) ~ exp_mid.hi + exp_mid.lo, + // We use the expression: + // (exp_mid.hi + exp_mid.lo) * (1 + dx * P_(dx)) ~ + // ~ exp_mid.hi + (exp_mid.hi * dx * P_(dx) + exp_mid.lo) + // with errors bounded by 1.5 * 2^-63. + + double mid_lo = dx * exp_mid.hi; + + // Approximate expm1(dx)/dx ~ 1 + dx / 2 + dx^2 / 6 + dx^3 / 24. + 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</*SKIP_ZIV_TEST=*/true>(hi, exp_mid.hi, lo, ERR_D) + .value(); + } else { + // to multiply by 2^hi, a fast way is to simply add hi to the exponent + // field. + 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)) { + // to multiply by 2^hi, a fast way is to simply add hi to the exponent + // field. + 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; + } + } + + // Use double-double + 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; + } + } + + // Use 128-bit precision + Float128 r_f128 = exp_f128(x, kd, idx1, idx2); + + return static_cast<double>(r_f128); +#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS +} + +} // namespace math + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXP_H diff --git a/libc/src/__support/math/exp10.h b/libc/src/__support/math/exp10.h new file mode 100644 index 000000000000..88748523deb3 --- /dev/null +++ b/libc/src/__support/math/exp10.h @@ -0,0 +1,501 @@ +//===-- Implementation header for exp10 ------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXP10_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_EXP10_H + +#include "exp_constants.h" // Lookup tables EXP2_MID1 and EXP_M2. +#include "exp_utils.h" // ziv_test_denorm. +#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" // LIBC_UNLIKELY + +namespace LIBC_NAMESPACE_DECL { + +using fputil::DoubleDouble; +using fputil::TripleDouble; +using Float128 = typename fputil::DyadicFloat<128>; + +using LIBC_NAMESPACE::operator""_u128; + +// log2(10) +static constexpr double LOG2_10 = 0x1.a934f0979a371p+1; + +// -2^-12 * log10(2) +// > a = -2^-12 * log10(2); +// > b = round(a, 32, RN); +// > c = round(a - b, 32, RN); +// > d = round(a - b - c, D, RN); +// Errors < 1.5 * 2^-144 +static constexpr double MLOG10_2_EXP2_M12_HI = -0x1.3441350ap-14; +static constexpr double MLOG10_2_EXP2_M12_MID = 0x1.0c0219dc1da99p-51; + +#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS +static constexpr double MLOG10_2_EXP2_M12_MID_32 = 0x1.0c0219dcp-51; +static constexpr double MLOG10_2_EXP2_M12_LO = 0x1.da994fd20dba2p-87; +#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS + +// Error bounds: +// Errors when using double precision. +constexpr double ERR_D = 0x1.8p-63; + +#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS +// Errors when using double-double precision. +static constexpr double ERR_DD = 0x1.8p-99; +#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS + +// Polynomial approximations with double precision. Generated by Sollya with: +// > P = fpminimax((10^x - 1)/x, 3, [|D...|], [-2^-14, 2^-14]); +// > P; +// Error bounds: +// | output - (10^dx - 1) / dx | < 2^-52. +LIBC_INLINE static double exp10_poly_approx_d(double dx) { + // dx^2 + double dx2 = dx * dx; + double c0 = + fputil::multiply_add(dx, 0x1.53524c73cea6ap+1, 0x1.26bb1bbb55516p+1); + double c1 = + fputil::multiply_add(dx, 0x1.2bd75cc6afc65p+0, 0x1.0470587aa264cp+1); + double p = fputil::multiply_add(dx2, c1, c0); + return p; +} + +#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS +// Polynomial approximation with double-double precision. Generated by Solya +// with: +// > P = fpminimax((10^x - 1)/x, 5, [|DD...|], [-2^-14, 2^-14]); +// Error bounds: +// | output - 10^(dx) | < 2^-101 +static constexpr DoubleDouble exp10_poly_approx_dd(const DoubleDouble &dx) { + // Taylor polynomial. + constexpr DoubleDouble COEFFS[] = { + {0, 0x1p0}, + {-0x1.f48ad494e927bp-53, 0x1.26bb1bbb55516p1}, + {-0x1.e2bfab3191cd2p-53, 0x1.53524c73cea69p1}, + {0x1.80fb65ec3b503p-53, 0x1.0470591de2ca4p1}, + {0x1.338fc05e21e55p-54, 0x1.2bd7609fd98c4p0}, + {0x1.d4ea116818fbp-56, 0x1.1429ffd519865p-1}, + {-0x1.872a8ff352077p-57, 0x1.a7ed70847c8b3p-3}, + + }; + + DoubleDouble p = fputil::polyeval(dx, COEFFS[0], COEFFS[1], COEFFS[2], + COEFFS[3], COEFFS[4], COEFFS[5], COEFFS[6]); + return p; +} + +// Polynomial approximation with 128-bit precision: +// Return exp(dx) ~ 1 + a0 * dx + a1 * dx^2 + ... + a6 * dx^7 +// For |dx| < 2^-14: +// | output - 10^dx | < 1.5 * 2^-124. +static constexpr Float128 exp10_poly_approx_f128(const Float128 &dx) { + constexpr Float128 COEFFS_128[]{ + {Sign::POS, -127, 0x80000000'00000000'00000000'00000000_u128}, // 1.0 + {Sign::POS, -126, 0x935d8ddd'aaa8ac16'ea56d62b'82d30a2d_u128}, + {Sign::POS, -126, 0xa9a92639'e753443a'80a99ce7'5f4d5bdb_u128}, + {Sign::POS, -126, 0x82382c8e'f1652304'6a4f9d7d'bf6c9635_u128}, + {Sign::POS, -124, 0x12bd7609'fd98c44c'34578701'9216c7af_u128}, + {Sign::POS, -127, 0x450a7ff4'7535d889'cc41ed7e'0d27aee5_u128}, + {Sign::POS, -130, 0xd3f6b844'702d636b'8326bb91'a6e7601d_u128}, + {Sign::POS, -130, 0x45b937f0'd05bb1cd'fa7b46df'314112a9_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; +} + +// Compute 10^(x) using 128-bit precision. +// TODO(lntue): investigate triple-double precision implementation for this +// step. +static Float128 exp10_f128(double x, double kd, int idx1, int idx2) { + double t1 = fputil::multiply_add(kd, MLOG10_2_EXP2_M12_HI, x); // exact + double t2 = kd * MLOG10_2_EXP2_M12_MID_32; // exact + double t3 = kd * MLOG10_2_EXP2_M12_LO; // Error < 2^-144 + + Float128 dx = fputil::quick_add( + Float128(t1), fputil::quick_add(Float128(t2), Float128(t3))); + + // TODO: Skip recalculating exp_mid1 and exp_mid2. + 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 = exp10_poly_approx_f128(dx); + + Float128 r = fputil::quick_mul(exp_mid, p); + + r.exponent += static_cast<int>(kd) >> 12; + + return r; +} + +// Compute 10^x with double-double precision. +static DoubleDouble exp10_double_double(double x, double kd, + const DoubleDouble &exp_mid) { + // Recalculate dx: + // dx = x - k * 2^-12 * log10(2) + double t1 = fputil::multiply_add(kd, MLOG10_2_EXP2_M12_HI, x); // exact + double t2 = kd * MLOG10_2_EXP2_M12_MID_32; // exact + double t3 = kd * MLOG10_2_EXP2_M12_LO; // Error < 2^-140 + + DoubleDouble dx = fputil::exact_add(t1, t2); + dx.lo += t3; + + // Degree-6 polynomial approximation in double-double precision. + // | p - 10^x | < 2^-103. + DoubleDouble p = exp10_poly_approx_dd(dx); + + // Error bounds: 2^-102. + DoubleDouble r = fputil::quick_mult(exp_mid, p); + + return r; +} +#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS + +// When output is denormal. +static double exp10_denorm(double x) { + // Range reduction. + double tmp = fputil::multiply_add(x, LOG2_10, 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; + + 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); + + // |dx| < 1.5 * 2^-15 + 2^-31 < 2^-14 + double lo_h = fputil::multiply_add(kd, MLOG10_2_EXP2_M12_HI, x); // exact + double dx = fputil::multiply_add(kd, MLOG10_2_EXP2_M12_MID, lo_h); + + double mid_lo = dx * exp_mid.hi; + + // Approximate (10^dx - 1)/dx ~ 1 + a0*dx + a1*dx^2 + a2*dx^3 + a3*dx^4. + double p = exp10_poly_approx_d(dx); + + double lo = fputil::multiply_add(p, mid_lo, exp_mid.lo); + +#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS + return ziv_test_denorm</*SKIP_ZIV_TEST=*/true>(hi, exp_mid.hi, lo, ERR_D) + .value(); +#else + if (auto r = ziv_test_denorm(hi, exp_mid.hi, lo, ERR_D); + LIBC_LIKELY(r.has_value())) + return r.value(); + + // Use double-double + DoubleDouble r_dd = exp10_double_double(x, kd, exp_mid); + + if (auto r = ziv_test_denorm(hi, r_dd.hi, r_dd.lo, ERR_DD); + LIBC_LIKELY(r.has_value())) + return r.value(); + + // Use 128-bit precision + Float128 r_f128 = exp10_f128(x, kd, idx1, idx2); + + return static_cast<double>(r_f128); +#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS +} + +// Check for exceptional cases when: +// * log10(1 - 2^-54) < x < log10(1 + 2^-53) +// * x >= log10(2^1024) +// * x <= log10(2^-1022) +// * x is inf or nan +static constexpr double exp10_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(); + + // |x| < log10(1 + 2^-53) + if (x_abs <= 0x3c8bcb7b1526e50e) { + // 10^(x) ~ 1 + x/2 + return fputil::multiply_add(x, 0.5, 1.0); + } + + // x <= log10(2^-1022) || x >= log10(2^1024) or inf/nan. + if (x_u >= 0xc0733a7146f72a42) { + // x <= log10(2^-1075) or -inf/nan + if (x_u > 0xc07439b746e36b52) { + // exp(-Inf) = 0 + if (xbits.is_inf()) + return 0.0; + + // exp(nan) = nan + 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; + } + + return exp10_denorm(x); + } + + // x >= log10(2^1024) or +inf/nan + // x is finite + 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); + } + // x is +inf or nan + return x + FPBits::inf().get_val(); +} + +namespace math { + +static constexpr double exp10(double x) { + using FPBits = typename fputil::FPBits<double>; + FPBits xbits(x); + + uint64_t x_u = xbits.uintval(); + + // x <= log10(2^-1022) or x >= log10(2^1024) or + // log10(1 - 2^-54) < x < log10(1 + 2^-53). + if (LIBC_UNLIKELY(x_u >= 0xc0733a7146f72a42 || + (x_u <= 0xbc7bcb7b1526e50e && x_u >= 0x40734413509f79ff) || + x_u < 0x3c8bcb7b1526e50e)) { + return exp10_set_exceptional(x); + } + + // Now log10(2^-1075) < x <= log10(1 - 2^-54) or + // log10(1 + 2^-53) < x < log10(2^1024) + + // Range reduction: + // Let x = log10(2) * (hi + mid1 + mid2) + lo + // in which: + // hi is an integer + // mid1 * 2^6 is an integer + // mid2 * 2^12 is an integer + // then: + // 10^(x) = 2^hi * 2^(mid1) * 2^(mid2) * 10^(lo). + // With this formula: + // - multiplying by 2^hi is exact and cheap, simply by adding the exponent + // field. + // - 2^(mid1) and 2^(mid2) are stored in 2 x 64-element tables. + // - 10^(lo) ~ 1 + a0*lo + a1 * lo^2 + ... + // + // We compute (hi + mid1 + mid2) together by perform the rounding on + // x * log2(10) * 2^12. + // Since |x| < |log10(2^-1075)| < 2^9, + // |x * 2^12| < 2^9 * 2^12 < 2^21, + // So we can fit the rounded result round(x * 2^12) in int32_t. + // Thus, the goal is to be able to use an additional addition and fixed width + // shift to get an int32_t representing round(x * 2^12). + // + // Assuming int32_t using 2-complement representation, since the mantissa part + // of a double precision is unsigned with the leading bit hidden, if we add an + // extra constant C = 2^e1 + 2^e2 with e1 > e2 >= 2^23 to the product, the + // part that are < 2^e2 in resulted mantissa of (x*2^12*L2E + C) can be + // considered as a proper 2-complement representations of x*2^12. + // + // One small problem with this approach is that the sum (x*2^12 + C) in + // double precision is rounded to the least significant bit of the dorminant + // factor C. In order to minimize the rounding errors from this addition, we + // want to minimize e1. Another constraint that we want is that after + // shifting the mantissa so that the least significant bit of int32_t + // corresponds to the unit bit of (x*2^12*L2E), the sign is correct without + // any adjustment. So combining these 2 requirements, we can choose + // C = 2^33 + 2^32, so that the sign bit corresponds to 2^31 bit, and hence + // after right shifting the mantissa, the resulting int32_t has correct sign. + // With this choice of C, the number of mantissa bits we need to shift to the + // right is: 52 - 33 = 19. + // + // Moreover, since the integer right shifts are equivalent to rounding down, + // we can add an extra 0.5 so that it will become round-to-nearest, tie-to- + // +infinity. So in particular, we can compute: + // hmm = x * 2^12 + C, + // where C = 2^33 + 2^32 + 2^-1, then if + // k = int32_t(lower 51 bits of double(x * 2^12 + C) >> 19), + // the reduced argument: + // lo = x - log10(2) * 2^-12 * k is bounded by: + // |lo| = |x - log10(2) * 2^-12 * k| + // = log10(2) * 2^-12 * | x * log2(10) * 2^12 - k | + // <= log10(2) * 2^-12 * (2^-1 + 2^-19) + // < 1.5 * 2^-2 * (2^-13 + 2^-31) + // = 1.5 * (2^-15 * 2^-31) + // + // Finally, notice that k only uses the mantissa of x * 2^12, so the + // exponent 2^12 is not needed. So we can simply define + // C = 2^(33 - 12) + 2^(32 - 12) + 2^(-13 - 12), and + // k = int32_t(lower 51 bits of double(x + C) >> 19). + + // Rounding errors <= 2^-31. + double tmp = fputil::multiply_add(x, LOG2_10, 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; + + 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); + + // |dx| < 1.5 * 2^-15 + 2^-31 < 2^-14 + double lo_h = fputil::multiply_add(kd, MLOG10_2_EXP2_M12_HI, x); // exact + double dx = fputil::multiply_add(kd, MLOG10_2_EXP2_M12_MID, lo_h); + + // We use the degree-4 polynomial to approximate 10^(lo): + // 10^(lo) ~ 1 + a0 * lo + a1 * lo^2 + a2 * lo^3 + a3 * lo^4 + // = 1 + lo * P(lo) + // So that the errors are bounded by: + // |P(lo) - (10^lo - 1)/lo| < |lo|^4 / 64 < 2^(-13 * 4) / 64 = 2^-58 + // Let P_ be an evaluation of P where all intermediate computations are in + // double precision. Using either Horner's or Estrin's schemes, the evaluated + // errors can be bounded by: + // |P_(lo) - P(lo)| < 2^-51 + // => |lo * P_(lo) - (2^lo - 1) | < 2^-65 + // => 2^(mid1 + mid2) * |lo * P_(lo) - expm1(lo)| < 2^-64. + // Since we approximate + // 2^(mid1 + mid2) ~ exp_mid.hi + exp_mid.lo, + // We use the expression: + // (exp_mid.hi + exp_mid.lo) * (1 + dx * P_(dx)) ~ + // ~ exp_mid.hi + (exp_mid.hi * dx * P_(dx) + exp_mid.lo) + // with errors bounded by 2^-64. + + double mid_lo = dx * exp_mid.hi; + + // Approximate (10^dx - 1)/dx ~ 1 + a0*dx + a1*dx^2 + a2*dx^3 + a3*dx^4. + double p = exp10_poly_approx_d(dx); + + double lo = fputil::multiply_add(p, mid_lo, exp_mid.lo); + +#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS + 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 + double upper = exp_mid.hi + (lo + ERR_D); + double lower = exp_mid.hi + (lo - ERR_D); + + if (LIBC_LIKELY(upper == lower)) { + // To multiply by 2^hi, a fast way is to simply add hi to the exponent + // field. + 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; + } + + // Exact outputs when x = 1, 2, ..., 22 + hard to round with x = 23. + // Quick check mask: 0x800f'ffffU = ~(bits of 1.0 | ... | bits of 23.0) + if (LIBC_UNLIKELY((x_u & 0x8000'ffff'ffff'ffffULL) == 0ULL)) { + switch (x_u) { + case 0x3ff0000000000000: // x = 1.0 + return 10.0; + case 0x4000000000000000: // x = 2.0 + return 100.0; + case 0x4008000000000000: // x = 3.0 + return 1'000.0; + case 0x4010000000000000: // x = 4.0 + return 10'000.0; + case 0x4014000000000000: // x = 5.0 + return 100'000.0; + case 0x4018000000000000: // x = 6.0 + return 1'000'000.0; + case 0x401c000000000000: // x = 7.0 + return 10'000'000.0; + case 0x4020000000000000: // x = 8.0 + return 100'000'000.0; + case 0x4022000000000000: // x = 9.0 + return 1'000'000'000.0; + case 0x4024000000000000: // x = 10.0 + return 10'000'000'000.0; + case 0x4026000000000000: // x = 11.0 + return 100'000'000'000.0; + case 0x4028000000000000: // x = 12.0 + return 1'000'000'000'000.0; + case 0x402a000000000000: // x = 13.0 + return 10'000'000'000'000.0; + case 0x402c000000000000: // x = 14.0 + return 100'000'000'000'000.0; + case 0x402e000000000000: // x = 15.0 + return 1'000'000'000'000'000.0; + case 0x4030000000000000: // x = 16.0 + return 10'000'000'000'000'000.0; + case 0x4031000000000000: // x = 17.0 + return 100'000'000'000'000'000.0; + case 0x4032000000000000: // x = 18.0 + return 1'000'000'000'000'000'000.0; + case 0x4033000000000000: // x = 19.0 + return 10'000'000'000'000'000'000.0; + case 0x4034000000000000: // x = 20.0 + return 100'000'000'000'000'000'000.0; + case 0x4035000000000000: // x = 21.0 + return 1'000'000'000'000'000'000'000.0; + case 0x4036000000000000: // x = 22.0 + return 10'000'000'000'000'000'000'000.0; + case 0x4037000000000000: // x = 23.0 + return 0x1.52d02c7e14af6p76 + x; + } + } + + // Use double-double + DoubleDouble r_dd = exp10_double_double(x, kd, exp_mid); + + 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)) { + // To multiply by 2^hi, a fast way is to simply add hi to the exponent + // field. + 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; + } + + // Use 128-bit precision + Float128 r_f128 = exp10_f128(x, kd, idx1, idx2); + + return static_cast<double>(r_f128); +#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS +} + +} // namespace math + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXP10_H diff --git a/libc/src/__support/math/exp10_float16_constants.h b/libc/src/__support/math/exp10_float16_constants.h new file mode 100644 index 000000000000..f5928db740ee --- /dev/null +++ b/libc/src/__support/math/exp10_float16_constants.h @@ -0,0 +1,43 @@ +//===-- Constants for exp10f16 function -------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXP10_FLOAT16_CONSTANTS_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_EXP10_FLOAT16_CONSTANTS_H + +#include "include/llvm-libc-macros/float16-macros.h" +#include <stdint.h> + +#ifdef LIBC_TYPES_HAS_FLOAT16 + +#include "src/__support/CPP/array.h" + +namespace LIBC_NAMESPACE_DECL { + +// Generated by Sollya with the following commands: +// > display = hexadecimal; +// > for i from 0 to 7 do printsingle(round(2^(i * 2^-3), SG, RN)); +static constexpr cpp::array<uint32_t, 8> EXP2_MID_BITS = { + 0x3f80'0000U, 0x3f8b'95c2U, 0x3f98'37f0U, 0x3fa5'fed7U, + 0x3fb5'04f3U, 0x3fc5'672aU, 0x3fd7'44fdU, 0x3fea'c0c7U, +}; + +// Generated by Sollya with the following commands: +// > display = hexadecimal; +// > round(log2(10), SG, RN); +static constexpr float LOG2F_10 = 0x1.a934fp+1f; + +// Generated by Sollya with the following commands: +// > display = hexadecimal; +// > round(log10(2), SG, RN); +static constexpr float LOG10F_2 = 0x1.344136p-2f; + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT16 + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXP10F16_H diff --git a/libc/src/math/generic/exp10f_impl.h b/libc/src/__support/math/exp10f.h index 975fd01a0a25..807b4f0d6c10 100644 --- a/libc/src/math/generic/exp10f_impl.h +++ b/libc/src/__support/math/exp10f.h @@ -1,4 +1,4 @@ -//===-- Single-precision 10^x function ------------------------------------===// +//===-- Implementation header for exp10f ------------------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,22 +6,21 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_LIBC_SRC_MATH_GENERIC_EXP10F_IMPL_H -#define LLVM_LIBC_SRC_MATH_GENERIC_EXP10F_IMPL_H +#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXP10F_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_EXP10F_H -#include "explogxf.h" +#include "exp10f_utils.h" #include "src/__support/FPUtil/FEnvImpl.h" #include "src/__support/FPUtil/FPBits.h" #include "src/__support/FPUtil/multiply_add.h" #include "src/__support/FPUtil/rounding_mode.h" -#include "src/__support/common.h" #include "src/__support/macros/config.h" #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY namespace LIBC_NAMESPACE_DECL { -namespace generic { +namespace math { -LIBC_INLINE float exp10f(float x) { +static constexpr float exp10f(float x) { using FPBits = typename fputil::FPBits<float>; FPBits xbits(x); @@ -132,7 +131,7 @@ LIBC_INLINE float exp10f(float x) { return static_cast<float>(multiply_add(p, lo2 * rr.mh, c0 * rr.mh)); } -} // namespace generic +} // namespace math } // namespace LIBC_NAMESPACE_DECL -#endif // LLVM_LIBC_SRC_MATH_GENERIC_EXP10F_IMPL_H +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXP10F_H diff --git a/libc/src/__support/math/exp10f16.h b/libc/src/__support/math/exp10f16.h new file mode 100644 index 000000000000..0d8b12534884 --- /dev/null +++ b/libc/src/__support/math/exp10f16.h @@ -0,0 +1,141 @@ +//===-- Implementation header for exp10f16 ----------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXP10F16_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_EXP10F16_H + +#include "include/llvm-libc-macros/float16-macros.h" + +#ifdef LIBC_TYPES_HAS_FLOAT16 + +#include "exp10f16_utils.h" +#include "src/__support/FPUtil/FEnvImpl.h" +#include "src/__support/FPUtil/FPBits.h" +#include "src/__support/FPUtil/cast.h" +#include "src/__support/FPUtil/except_value_utils.h" +#include "src/__support/FPUtil/rounding_mode.h" +#include "src/__support/macros/config.h" +#include "src/__support/macros/optimization.h" +#include "src/__support/macros/properties/cpu_features.h" + +namespace LIBC_NAMESPACE_DECL { + +namespace math { + +#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS +#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT +static constexpr size_t N_EXP10F16_EXCEPTS = 5; +#else +static constexpr size_t N_EXP10F16_EXCEPTS = 8; +#endif + +static constexpr fputil::ExceptValues<float16, N_EXP10F16_EXCEPTS> + EXP10F16_EXCEPTS = {{ + // x = 0x1.8f4p-2, exp10f16(x) = 0x1.3ap+1 (RZ) + {0x363dU, 0x40e8U, 1U, 0U, 1U}, + // x = 0x1.95cp-2, exp10f16(x) = 0x1.3ecp+1 (RZ) + {0x3657U, 0x40fbU, 1U, 0U, 0U}, + // x = -0x1.018p-4, exp10f16(x) = 0x1.bbp-1 (RZ) + {0xac06U, 0x3aecU, 1U, 0U, 0U}, + // x = -0x1.c28p+0, exp10f16(x) = 0x1.1ccp-6 (RZ) + {0xbf0aU, 0x2473U, 1U, 0U, 0U}, + // x = -0x1.e1cp+1, exp10f16(x) = 0x1.694p-13 (RZ) + {0xc387U, 0x09a5U, 1U, 0U, 0U}, +#ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT + // x = 0x1.0cp+1, exp10f16(x) = 0x1.f04p+6 (RZ) + {0x4030U, 0x57c1U, 1U, 0U, 1U}, + // x = 0x1.1b8p+1, exp10f16(x) = 0x1.47cp+7 (RZ) + {0x406eU, 0x591fU, 1U, 0U, 1U}, + // x = 0x1.1b8p+2, exp10f16(x) = 0x1.a4p+14 (RZ) + {0x446eU, 0x7690U, 1U, 0U, 1U}, +#endif + }}; +#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS + +static constexpr float16 exp10f16(float16 x) { + using FPBits = fputil::FPBits<float16>; + FPBits x_bits(x); + + uint16_t x_u = x_bits.uintval(); + uint16_t x_abs = x_u & 0x7fffU; + + // When |x| >= 5, or x is NaN. + if (LIBC_UNLIKELY(x_abs >= 0x4500U)) { + // exp10(NaN) = NaN + if (x_bits.is_nan()) { + if (x_bits.is_signaling_nan()) { + fputil::raise_except_if_required(FE_INVALID); + return FPBits::quiet_nan().get_val(); + } + + return x; + } + + // When x >= 5. + if (x_bits.is_pos()) { + // exp10(+inf) = +inf + if (x_bits.is_inf()) + return FPBits::inf().get_val(); + + switch (fputil::quick_get_round()) { + case FE_TONEAREST: + case FE_UPWARD: + fputil::set_errno_if_required(ERANGE); + fputil::raise_except_if_required(FE_OVERFLOW); + return FPBits::inf().get_val(); + default: + return FPBits::max_normal().get_val(); + } + } + + // When x <= -8. + if (x_u >= 0xc800U) { + // exp10(-inf) = +0 + if (x_bits.is_inf()) + return FPBits::zero().get_val(); + + fputil::set_errno_if_required(ERANGE); + fputil::raise_except_if_required(FE_UNDERFLOW | FE_INEXACT); + + if (fputil::fenv_is_round_up()) + return FPBits::min_subnormal().get_val(); + return FPBits::zero().get_val(); + } + } + + // When x is 1, 2, 3, or 4. These are hard-to-round cases with exact results. + if (LIBC_UNLIKELY((x_u & ~(0x3c00U | 0x4000U | 0x4200U | 0x4400U)) == 0)) { + switch (x_u) { + case 0x3c00U: // x = 1.0f16 + return fputil::cast<float16>(10.0); + case 0x4000U: // x = 2.0f16 + return fputil::cast<float16>(100.0); + case 0x4200U: // x = 3.0f16 + return fputil::cast<float16>(1'000.0); + case 0x4400U: // x = 4.0f16 + return fputil::cast<float16>(10'000.0); + } + } + +#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS + if (auto r = EXP10F16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value())) + return r.value(); +#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS + + // 10^x = 2^((hi + mid) * log2(10)) * 10^lo + auto [exp2_hi_mid, exp10_lo] = exp10_range_reduction(x); + return fputil::cast<float16>(exp2_hi_mid * exp10_lo); +} + +} // namespace math + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT16 + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXP10F16_H diff --git a/libc/src/__support/math/exp10f16_utils.h b/libc/src/__support/math/exp10f16_utils.h new file mode 100644 index 000000000000..bffb81ba606b --- /dev/null +++ b/libc/src/__support/math/exp10f16_utils.h @@ -0,0 +1,64 @@ +//===-- Common utils for exp10f16 -------------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXP10F16_UTILS_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_EXP10F16_UTILS_H + +#include "include/llvm-libc-macros/float16-macros.h" + +#ifdef LIBC_TYPES_HAS_FLOAT16 + +#include "exp10_float16_constants.h" +#include "expf16_utils.h" +#include "src/__support/FPUtil/FPBits.h" + +namespace LIBC_NAMESPACE_DECL { + +LIBC_INLINE static constexpr ExpRangeReduction +exp10_range_reduction(float16 x) { + // For -8 < x < 5, to compute 10^x, we perform the following range reduction: + // find hi, mid, lo, such that: + // x = (hi + mid) * log2(10) + lo, in which + // hi is an integer, + // mid * 2^3 is an integer, + // -2^(-4) <= lo < 2^(-4). + // In particular, + // hi + mid = round(x * 2^3) * 2^(-3). + // Then, + // 10^x = 10^(hi + mid + lo) = 2^((hi + mid) * log2(10)) + 10^lo + // We store 2^mid in the lookup table EXP2_MID_BITS, and compute 2^hi * 2^mid + // by adding hi to the exponent field of 2^mid. 10^lo is computed using a + // degree-4 minimax polynomial generated by Sollya. + + float xf = x; + float kf = fputil::nearest_integer(xf * (LOG2F_10 * 0x1.0p+3f)); + int x_hi_mid = static_cast<int>(kf); + unsigned x_hi = static_cast<unsigned>(x_hi_mid) >> 3; + unsigned x_mid = static_cast<unsigned>(x_hi_mid) & 0x7; + // lo = x - (hi + mid) = round(x * 2^3 * log2(10)) * log10(2) * (-2^(-3)) + x + float lo = fputil::multiply_add(kf, LOG10F_2 * -0x1.0p-3f, xf); + + uint32_t exp2_hi_mid_bits = + EXP2_MID_BITS[x_mid] + + static_cast<uint32_t>(x_hi << fputil::FPBits<float>::FRACTION_LEN); + float exp2_hi_mid = fputil::FPBits<float>(exp2_hi_mid_bits).get_val(); + // Degree-4 minimax polynomial generated by Sollya with the following + // commands: + // > display = hexadecimal; + // > P = fpminimax((10^x - 1)/x, 3, [|SG...|], [-2^-4, 2^-4]); + // > 1 + x * P; + float exp10_lo = fputil::polyeval(lo, 0x1p+0f, 0x1.26bb14p+1f, 0x1.53526p+1f, + 0x1.04b434p+1f, 0x1.2bcf9ep+0f); + return {exp2_hi_mid, exp10_lo}; +} + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT16 + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXP10F16_UTILS_H diff --git a/libc/src/__support/math/exp10f_utils.h b/libc/src/__support/math/exp10f_utils.h new file mode 100644 index 000000000000..0493e1b993e0 --- /dev/null +++ b/libc/src/__support/math/exp10f_utils.h @@ -0,0 +1,157 @@ +//===-- Common utils for exp10f ---------------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXP_FLOAT_CONSTANTS_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_EXP_FLOAT_CONSTANTS_H + +#include "src/__support/FPUtil/FPBits.h" +#include "src/__support/FPUtil/PolyEval.h" +#include "src/__support/FPUtil/nearest_integer.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +struct ExpBase { + // Base = e + static constexpr int MID_BITS = 5; + static constexpr int MID_MASK = (1 << MID_BITS) - 1; + // log2(e) * 2^5 + static constexpr double LOG2_B = 0x1.71547652b82fep+0 * (1 << MID_BITS); + // High and low parts of -log(2) * 2^(-5) + static constexpr double M_LOGB_2_HI = -0x1.62e42fefa0000p-1 / (1 << MID_BITS); + static constexpr double M_LOGB_2_LO = + -0x1.cf79abc9e3b3ap-40 / (1 << MID_BITS); + // Look up table for bit fields of 2^(i/32) for i = 0..31, generated by Sollya + // with: + // > for i from 0 to 31 do printdouble(round(2^(i/32), D, RN)); + static constexpr int64_t EXP_2_MID[1 << MID_BITS] = { + 0x3ff0000000000000, 0x3ff059b0d3158574, 0x3ff0b5586cf9890f, + 0x3ff11301d0125b51, 0x3ff172b83c7d517b, 0x3ff1d4873168b9aa, + 0x3ff2387a6e756238, 0x3ff29e9df51fdee1, 0x3ff306fe0a31b715, + 0x3ff371a7373aa9cb, 0x3ff3dea64c123422, 0x3ff44e086061892d, + 0x3ff4bfdad5362a27, 0x3ff5342b569d4f82, 0x3ff5ab07dd485429, + 0x3ff6247eb03a5585, 0x3ff6a09e667f3bcd, 0x3ff71f75e8ec5f74, + 0x3ff7a11473eb0187, 0x3ff82589994cce13, 0x3ff8ace5422aa0db, + 0x3ff93737b0cdc5e5, 0x3ff9c49182a3f090, 0x3ffa5503b23e255d, + 0x3ffae89f995ad3ad, 0x3ffb7f76f2fb5e47, 0x3ffc199bdd85529c, + 0x3ffcb720dcef9069, 0x3ffd5818dcfba487, 0x3ffdfc97337b9b5f, + 0x3ffea4afa2a490da, 0x3fff50765b6e4540, + }; + + // Approximating e^dx with degree-5 minimax polynomial generated by Sollya: + // > Q = fpminimax(expm1(x)/x, 4, [|1, D...|], [-log(2)/64, log(2)/64]); + // Then: + // e^dx ~ P(dx) = 1 + dx + COEFFS[0] * dx^2 + ... + COEFFS[3] * dx^5. + static constexpr double COEFFS[4] = { + 0x1.ffffffffe5bc8p-2, 0x1.555555555cd67p-3, 0x1.5555c2a9b48b4p-5, + 0x1.11112a0e34bdbp-7}; + + LIBC_INLINE static double powb_lo(double dx) { + using fputil::multiply_add; + double dx2 = dx * dx; + double c0 = 1.0 + dx; + // c1 = COEFFS[0] + COEFFS[1] * dx + double c1 = multiply_add(dx, ExpBase::COEFFS[1], ExpBase::COEFFS[0]); + // c2 = COEFFS[2] + COEFFS[3] * dx + double c2 = multiply_add(dx, ExpBase::COEFFS[3], ExpBase::COEFFS[2]); + // r = c4 + c5 * dx^4 + // = 1 + dx + COEFFS[0] * dx^2 + ... + COEFFS[5] * dx^7 + return fputil::polyeval(dx2, c0, c1, c2); + } +}; + +struct Exp10Base : public ExpBase { + // log2(10) * 2^5 + static constexpr double LOG2_B = 0x1.a934f0979a371p1 * (1 << MID_BITS); + // High and low parts of -log10(2) * 2^(-5). + // Notice that since |x * log2(10)| < 150: + // |k| = |round(x * log2(10) * 2^5)| < 2^8 * 2^5 = 2^13 + // So when the FMA instructions are not available, in order for the product + // k * M_LOGB_2_HI + // to be exact, we only store the high part of log10(2) up to 38 bits + // (= 53 - 15) of precision. + // It is generated by Sollya with: + // > round(log10(2), 44, RN); + static constexpr double M_LOGB_2_HI = -0x1.34413509f8p-2 / (1 << MID_BITS); + // > round(log10(2) - 0x1.34413509f8p-2, D, RN); + static constexpr double M_LOGB_2_LO = 0x1.80433b83b532ap-44 / (1 << MID_BITS); + + // Approximating 10^dx with degree-5 minimax polynomial generated by Sollya: + // > Q = fpminimax((10^x - 1)/x, 4, [|D...|], [-log10(2)/2^6, log10(2)/2^6]); + // Then: + // 10^dx ~ P(dx) = 1 + COEFFS[0] * dx + ... + COEFFS[4] * dx^5. + static constexpr double COEFFS[5] = {0x1.26bb1bbb55515p1, 0x1.53524c73bd3eap1, + 0x1.0470591dff149p1, 0x1.2bd7c0a9fbc4dp0, + 0x1.1429e74a98f43p-1}; + + static double powb_lo(double dx) { + using fputil::multiply_add; + double dx2 = dx * dx; + // c0 = 1 + COEFFS[0] * dx + double c0 = multiply_add(dx, Exp10Base::COEFFS[0], 1.0); + // c1 = COEFFS[1] + COEFFS[2] * dx + double c1 = multiply_add(dx, Exp10Base::COEFFS[2], Exp10Base::COEFFS[1]); + // c2 = COEFFS[3] + COEFFS[4] * dx + double c2 = multiply_add(dx, Exp10Base::COEFFS[4], Exp10Base::COEFFS[3]); + // r = c0 + dx^2 * (c1 + c2 * dx^2) + // = c0 + c1 * dx^2 + c2 * dx^4 + // = 1 + COEFFS[0] * dx + ... + COEFFS[4] * dx^5. + return fputil::polyeval(dx2, c0, c1, c2); + } +}; + +// Output of range reduction for exp_b: (2^(mid + hi), lo) +// where: +// b^x = 2^(mid + hi) * b^lo +struct exp_b_reduc_t { + double mh; // 2^(mid + hi) + double lo; +}; + +// The function correctly calculates b^x value with at least float precision +// in a limited range. +// Range reduction: +// b^x = 2^(hi + mid) * b^lo +// where: +// x = (hi + mid) * log_b(2) + lo +// hi is an integer, +// 0 <= mid * 2^MID_BITS < 2^MID_BITS is an integer +// -2^(-MID_BITS - 1) <= lo * log2(b) <= 2^(-MID_BITS - 1) +// Base class needs to provide the following constants: +// - MID_BITS : number of bits after decimal points used for mid +// - MID_MASK : 2^MID_BITS - 1, mask to extract mid bits +// - LOG2_B : log2(b) * 2^MID_BITS for scaling +// - M_LOGB_2_HI : high part of -log_b(2) * 2^(-MID_BITS) +// - M_LOGB_2_LO : low part of -log_b(2) * 2^(-MID_BITS) +// - EXP_2_MID : look up table for bit fields of 2^mid +// Return: +// { 2^(hi + mid), lo } +template <class Base> +LIBC_INLINE static constexpr exp_b_reduc_t exp_b_range_reduc(float x) { + double xd = static_cast<double>(x); + // kd = round((hi + mid) * log2(b) * 2^MID_BITS) + double kd = fputil::nearest_integer(Base::LOG2_B * xd); + // k = round((hi + mid) * log2(b) * 2^MID_BITS) + int k = static_cast<int>(kd); + // hi = floor(kd * 2^(-MID_BITS)) + // exp_hi = shift hi to the exponent field of double precision. + uint64_t exp_hi = static_cast<uint64_t>(k >> Base::MID_BITS) + << fputil::FPBits<double>::FRACTION_LEN; + // mh = 2^hi * 2^mid + // mh_bits = bit field of mh + uint64_t mh_bits = Base::EXP_2_MID[k & Base::MID_MASK] + exp_hi; + double mh = fputil::FPBits<double>(mh_bits).get_val(); + // dx = lo = x - (hi + mid) * log(2) + double dx = fputil::multiply_add( + kd, Base::M_LOGB_2_LO, fputil::multiply_add(kd, Base::M_LOGB_2_HI, xd)); + return {mh, dx}; +} + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXP_FLOAT_CONSTANTS_H diff --git a/libc/src/__support/math/exp_constants.h b/libc/src/__support/math/exp_constants.h new file mode 100644 index 000000000000..1abb4479e084 --- /dev/null +++ b/libc/src/__support/math/exp_constants.h @@ -0,0 +1,174 @@ +//===-- Constants for exp function ------------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXP_CONSTANTS_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_EXP_CONSTANTS_H + +#include "src/__support/FPUtil/triple_double.h" + +namespace LIBC_NAMESPACE_DECL { + +// Lookup table for 2^(k * 2^-6) with k = 0..63. +// Generated by Sollya with: +// > display=hexadecimal; +// > prec = 500; +// > for i from 0 to 63 do { +// a = 2^(i * 2^-6); +// b = round(a, D, RN); +// c = round(a - b, D, RN); +// d = round(a - b - c, D, RN); +// print("{", d, ",", c, ",", b, "},"); +// }; +static constexpr fputil::TripleDouble EXP2_MID1[64] = { + {0, 0, 0x1p0}, + {-0x1.9085b0a3d74d5p-110, -0x1.19083535b085dp-56, 0x1.02c9a3e778061p0}, + {0x1.05ff94f8d257ep-110, 0x1.d73e2a475b465p-55, 0x1.059b0d3158574p0}, + {0x1.15820d96b414fp-111, 0x1.186be4bb284ffp-57, 0x1.0874518759bc8p0}, + {-0x1.67c9bd6ebf74cp-108, 0x1.8a62e4adc610bp-54, 0x1.0b5586cf9890fp0}, + {-0x1.5aa76994e9ddbp-113, 0x1.03a1727c57b53p-59, 0x1.0e3ec32d3d1a2p0}, + {0x1.9d58b988f562dp-109, -0x1.6c51039449b3ap-54, 0x1.11301d0125b51p0}, + {-0x1.2fe7bb4c76416p-108, -0x1.32fbf9af1369ep-54, 0x1.1429aaea92dep0}, + {0x1.4f2406aa13ffp-109, -0x1.19041b9d78a76p-55, 0x1.172b83c7d517bp0}, + {0x1.ad36183926ae8p-111, 0x1.e5b4c7b4968e4p-55, 0x1.1a35beb6fcb75p0}, + {0x1.ea62d0881b918p-110, 0x1.e016e00a2643cp-54, 0x1.1d4873168b9aap0}, + {-0x1.781dbc16f1ea4p-111, 0x1.dc775814a8495p-55, 0x1.2063b88628cd6p0}, + {-0x1.4d89f9af532ep-109, 0x1.9b07eb6c70573p-54, 0x1.2387a6e756238p0}, + {0x1.277393a461b77p-110, 0x1.2bd339940e9d9p-55, 0x1.26b4565e27cddp0}, + {0x1.de5448560469p-111, 0x1.612e8afad1255p-55, 0x1.29e9df51fdee1p0}, + {-0x1.ee9d8f8cb9307p-110, 0x1.0024754db41d5p-54, 0x1.2d285a6e4030bp0}, + {0x1.7b7b2f09cd0d9p-110, 0x1.6f46ad23182e4p-55, 0x1.306fe0a31b715p0}, + {-0x1.406a2ea6cfc6bp-108, 0x1.32721843659a6p-54, 0x1.33c08b26416ffp0}, + {0x1.87e3e12516bfap-108, -0x1.63aeabf42eae2p-54, 0x1.371a7373aa9cbp0}, + {0x1.9b0b1ff17c296p-111, -0x1.5e436d661f5e3p-56, 0x1.3a7db34e59ff7p0}, + {-0x1.808ba68fa8fb7p-109, 0x1.ada0911f09ebcp-55, 0x1.3dea64c123422p0}, + {-0x1.32b43eafc6518p-114, -0x1.ef3691c309278p-58, 0x1.4160a21f72e2ap0}, + {-0x1.0ac312de3d922p-114, 0x1.89b7a04ef80dp-59, 0x1.44e086061892dp0}, + {0x1.e1eebae743acp-111, 0x1.3c1a3b69062fp-56, 0x1.486a2b5c13cdp0}, + {0x1.c06c7745c2b39p-113, 0x1.d4397afec42e2p-56, 0x1.4bfdad5362a27p0}, + {-0x1.1aa1fd7b685cdp-112, -0x1.4b309d25957e3p-54, 0x1.4f9b2769d2ca7p0}, + {0x1.fa733951f214cp-111, -0x1.07abe1db13cadp-55, 0x1.5342b569d4f82p0}, + {-0x1.ff86852a613ffp-111, 0x1.9bb2c011d93adp-54, 0x1.56f4736b527dap0}, + {-0x1.744ee506fdafep-109, 0x1.6324c054647adp-54, 0x1.5ab07dd485429p0}, + {-0x1.95f9ab75fa7d6p-108, 0x1.ba6f93080e65ep-54, 0x1.5e76f15ad2148p0}, + {0x1.5d8e757cfb991p-111, -0x1.383c17e40b497p-54, 0x1.6247eb03a5585p0}, + {0x1.4a337f4dc0a3bp-108, -0x1.bb60987591c34p-54, 0x1.6623882552225p0}, + {0x1.57d3e3adec175p-108, -0x1.bdd3413b26456p-54, 0x1.6a09e667f3bcdp0}, + {0x1.a59f88abbe778p-115, -0x1.bbe3a683c88abp-57, 0x1.6dfb23c651a2fp0}, + {-0x1.269796953a4c3p-109, -0x1.16e4786887a99p-55, 0x1.71f75e8ec5f74p0}, + {-0x1.8f8e7fa19e5e8p-108, -0x1.0245957316dd3p-54, 0x1.75feb564267c9p0}, + {-0x1.4217a932d10d4p-113, -0x1.41577ee04992fp-55, 0x1.7a11473eb0187p0}, + {0x1.70a1427f8fcdfp-112, 0x1.05d02ba15797ep-56, 0x1.7e2f336cf4e62p0}, + {0x1.0f6ad65cbbac1p-112, -0x1.d4c1dd41532d8p-54, 0x1.82589994cce13p0}, + {-0x1.f16f65181d921p-109, -0x1.fc6f89bd4f6bap-54, 0x1.868d99b4492edp0}, + {-0x1.30644a7836333p-110, 0x1.6e9f156864b27p-54, 0x1.8ace5422aa0dbp0}, + {0x1.3bf26d2b85163p-114, 0x1.5cc13a2e3976cp-55, 0x1.8f1ae99157736p0}, + {0x1.697e257ac0db2p-111, -0x1.75fc781b57ebcp-57, 0x1.93737b0cdc5e5p0}, + {0x1.7edb9d7144b6fp-108, -0x1.d185b7c1b85d1p-54, 0x1.97d829fde4e5p0}, + {0x1.6376b7943085cp-110, 0x1.c7c46b071f2bep-56, 0x1.9c49182a3f09p0}, + {0x1.354084551b4fbp-109, -0x1.359495d1cd533p-54, 0x1.a0c667b5de565p0}, + {-0x1.bfd7adfd63f48p-111, -0x1.d2f6edb8d41e1p-54, 0x1.a5503b23e255dp0}, + {0x1.8b16ae39e8cb9p-109, 0x1.0fac90ef7fd31p-54, 0x1.a9e6b5579fdbfp0}, + {0x1.a7fbc3ae675eap-108, 0x1.7a1cd345dcc81p-54, 0x1.ae89f995ad3adp0}, + {0x1.2babc0edda4d9p-111, -0x1.2805e3084d708p-57, 0x1.b33a2b84f15fbp0}, + {0x1.aa64481e1ab72p-111, -0x1.5584f7e54ac3bp-56, 0x1.b7f76f2fb5e47p0}, + {0x1.9a164050e1258p-109, 0x1.23dd07a2d9e84p-55, 0x1.bcc1e904bc1d2p0}, + {0x1.99e51125928dap-110, 0x1.11065895048ddp-55, 0x1.c199bdd85529cp0}, + {-0x1.fc44c329d5cb2p-109, 0x1.2884dff483cadp-54, 0x1.c67f12e57d14bp0}, + {0x1.d8765566b032ep-110, 0x1.503cbd1e949dbp-56, 0x1.cb720dcef9069p0}, + {-0x1.e7044039da0f6p-108, -0x1.cbc3743797a9cp-54, 0x1.d072d4a07897cp0}, + {-0x1.ab053b05531fcp-111, 0x1.2ed02d75b3707p-55, 0x1.d5818dcfba487p0}, + {0x1.7f6246f0ec615p-108, 0x1.c2300696db532p-54, 0x1.da9e603db3285p0}, + {0x1.b7225a944efd6p-108, -0x1.1a5cd4f184b5cp-54, 0x1.dfc97337b9b5fp0}, + {0x1.1e92cb3c2d278p-109, 0x1.39e8980a9cc8fp-55, 0x1.e502ee78b3ff6p0}, + {-0x1.fc0f242bbf3dep-109, -0x1.e9c23179c2893p-54, 0x1.ea4afa2a490dap0}, + {0x1.f6dd5d229ff69p-108, 0x1.dc7f486a4b6bp-54, 0x1.efa1bee615a27p0}, + {-0x1.4019bffc80ef3p-110, 0x1.9d3e12dd8a18bp-54, 0x1.f50765b6e454p0}, + {0x1.dc060c36f7651p-112, 0x1.74853f3a5931ep-55, 0x1.fa7c1819e90d8p0}, +}; + +// Lookup table for 2^(k * 2^-12) with k = 0..63. +// Generated by Sollya with: +// > display=hexadecimal; +// > prec = 500; +// > for i from 0 to 63 do { +// a = 2^(i * 2^-12); +// b = round(a, D, RN); +// c = round(a - b, D, RN); +// d = round(a - b - c, D, RN); +// print("{", d, ",", c, ",", b, "},"); +// }; +static constexpr fputil::TripleDouble EXP2_MID2[64] = { + {0, 0, 0x1p0}, + {0x1.39726694630e3p-108, 0x1.ae8e38c59c72ap-54, 0x1.000b175effdc7p0}, + {0x1.e5e06ddd31156p-112, -0x1.7b5d0d58ea8f4p-58, 0x1.00162f3904052p0}, + {0x1.5a0768b51f609p-111, 0x1.4115cb6b16a8ep-54, 0x1.0021478e11ce6p0}, + {0x1.d008403605217p-111, -0x1.d7c96f201bb2fp-55, 0x1.002c605e2e8cfp0}, + {0x1.89bc16f765708p-109, 0x1.84711d4c35e9fp-54, 0x1.003779a95f959p0}, + {-0x1.4535b7f8c1e2dp-109, -0x1.0484245243777p-55, 0x1.0042936faa3d8p0}, + {-0x1.8ba92f6b25456p-108, -0x1.4b237da2025f9p-54, 0x1.004dadb113dap0}, + {-0x1.30c72e81f4294p-113, -0x1.5e00e62d6b30dp-56, 0x1.0058c86da1c0ap0}, + {-0x1.34a5384e6f0b9p-110, 0x1.a1d6cedbb9481p-54, 0x1.0063e3a559473p0}, + {0x1.f8d0580865d2ep-108, -0x1.4acf197a00142p-54, 0x1.006eff583fc3dp0}, + {-0x1.002bcb3ae9a99p-111, -0x1.eaf2ea42391a5p-57, 0x1.007a1b865a8cap0}, + {0x1.c3c5aedee9851p-111, 0x1.da93f90835f75p-56, 0x1.0085382faef83p0}, + {0x1.7217851d1ec6ep-109, -0x1.6a79084ab093cp-55, 0x1.00905554425d4p0}, + {-0x1.80cbca335a7c3p-110, 0x1.86364f8fbe8f8p-54, 0x1.009b72f41a12bp0}, + {-0x1.706bd4eb22595p-110, -0x1.82e8e14e3110ep-55, 0x1.00a6910f3b6fdp0}, + {-0x1.b55dd523f3c08p-111, -0x1.4f6b2a7609f71p-55, 0x1.00b1afa5abcbfp0}, + {0x1.90a1e207cced1p-110, -0x1.e1a258ea8f71bp-56, 0x1.00bcceb7707ecp0}, + {0x1.78d0472db37c5p-110, 0x1.4362ca5bc26f1p-56, 0x1.00c7ee448ee02p0}, + {-0x1.bcd4db3cb52fep-109, 0x1.095a56c919d02p-54, 0x1.00d30e4d0c483p0}, + {-0x1.cf1b131575ec2p-112, -0x1.406ac4e81a645p-57, 0x1.00de2ed0ee0f5p0}, + {-0x1.6aaa1fa7ff913p-112, 0x1.b5a6902767e09p-54, 0x1.00e94fd0398ep0}, + {0x1.68f236dff3218p-110, -0x1.91b2060859321p-54, 0x1.00f4714af41d3p0}, + {-0x1.e8bb58067e60ap-109, 0x1.427068ab22306p-55, 0x1.00ff93412315cp0}, + {0x1.d4cd5e1d71fdfp-108, 0x1.c1d0660524e08p-54, 0x1.010ab5b2cbd11p0}, + {0x1.e4ecf350ebe88p-108, -0x1.e7bdfb3204be8p-54, 0x1.0115d89ff3a8bp0}, + {0x1.6a2aa2c89c4f8p-109, 0x1.843aa8b9cbbc6p-55, 0x1.0120fc089ff63p0}, + {0x1.1ca368a20ed05p-110, -0x1.34104ee7edae9p-56, 0x1.012c1fecd613bp0}, + {0x1.edb1095d925cfp-114, -0x1.2b6aeb6176892p-56, 0x1.0137444c9b5b5p0}, + {-0x1.488c78eded75fp-111, 0x1.a8cd33b8a1bb3p-56, 0x1.01426927f5278p0}, + {-0x1.7480f5ea1b3c9p-113, 0x1.2edc08e5da99ap-56, 0x1.014d8e7ee8d2fp0}, + {-0x1.ae45989a04dd5p-111, 0x1.57ba2dc7e0c73p-55, 0x1.0158b4517bb88p0}, + {0x1.bf48007d80987p-109, 0x1.b61299ab8cdb7p-54, 0x1.0163da9fb3335p0}, + {0x1.1aa91a059292cp-109, -0x1.90565902c5f44p-54, 0x1.016f0169949edp0}, + {0x1.b6663292855f5p-110, 0x1.70fc41c5c2d53p-55, 0x1.017a28af25567p0}, + {0x1.e7fbca6793d94p-108, 0x1.4b9a6e145d76cp-54, 0x1.018550706ab62p0}, + {-0x1.5b9f5c7de3b93p-110, -0x1.008eff5142bf9p-56, 0x1.019078ad6a19fp0}, + {0x1.4638bf2f6acabp-110, -0x1.77669f033c7dep-54, 0x1.019ba16628de2p0}, + {-0x1.ab237b9a069c5p-109, -0x1.09bb78eeead0ap-54, 0x1.01a6ca9aac5f3p0}, + {0x1.3ab358be97cefp-108, 0x1.371231477ece5p-54, 0x1.01b1f44af9f9ep0}, + {-0x1.4027b2294bb64p-110, 0x1.5e7626621eb5bp-56, 0x1.01bd1e77170b4p0}, + {0x1.656394426c99p-111, -0x1.bc72b100828a5p-54, 0x1.01c8491f08f08p0}, + {0x1.bf9785189bdd8p-111, -0x1.ce39cbbab8bbep-57, 0x1.01d37442d507p0}, + {0x1.7c12f86114fe3p-109, 0x1.16996709da2e2p-55, 0x1.01de9fe280ac8p0}, + {-0x1.653d5d24b5d28p-109, -0x1.c11f5239bf535p-55, 0x1.01e9cbfe113efp0}, + {0x1.04a0cdc1d86d7p-109, 0x1.e1d4eb5edc6b3p-55, 0x1.01f4f8958c1c6p0}, + {0x1.c678c46149782p-109, -0x1.afb99946ee3fp-54, 0x1.020025a8f6a35p0}, + {0x1.48524e1e9df7p-108, -0x1.8f06d8a148a32p-54, 0x1.020b533856324p0}, + {0x1.9953ea727ff0bp-109, -0x1.2bf310fc54eb6p-55, 0x1.02168143b0281p0}, + {-0x1.ccfbbec22d28ep-108, -0x1.c95a035eb4175p-54, 0x1.0221afcb09e3ep0}, + {0x1.9e2bb6e181de1p-108, -0x1.491793e46834dp-54, 0x1.022cdece68c4fp0}, + {0x1.f17609ae29308p-110, -0x1.3e8d0d9c49091p-56, 0x1.02380e4dd22adp0}, + {-0x1.c7dc2c476bfb8p-110, -0x1.314aa16278aa3p-54, 0x1.02433e494b755p0}, + {-0x1.fab994971d4a3p-109, 0x1.48daf888e9651p-55, 0x1.024e6ec0da046p0}, + {0x1.848b62cbdd0afp-109, 0x1.56dc8046821f4p-55, 0x1.02599fb483385p0}, + {-0x1.bf603ba715d0cp-109, 0x1.45b42356b9d47p-54, 0x1.0264d1244c719p0}, + {0x1.89434e751e1aap-110, -0x1.082ef51b61d7ep-56, 0x1.027003103b10ep0}, + {-0x1.03b54fd64e8acp-110, 0x1.2106ed0920a34p-56, 0x1.027b357854772p0}, + {0x1.7785ea0acc486p-109, -0x1.fd4cf26ea5d0fp-54, 0x1.0286685c9e059p0}, + {-0x1.ce447fdb35ff9p-109, -0x1.09f8775e78084p-54, 0x1.02919bbd1d1d8p0}, + {0x1.5b884aab5642ap-112, 0x1.64cbba902ca27p-58, 0x1.029ccf99d720ap0}, + {-0x1.cfb3e46d7c1cp-108, 0x1.4383ef231d207p-54, 0x1.02a803f2d170dp0}, + {-0x1.0d40cee4b81afp-112, 0x1.4a47a505b3a47p-54, 0x1.02b338c811703p0}, + {0x1.6ae7d36d7c1f7p-109, 0x1.e47120223467fp-54, 0x1.02be6e199c811p0}, +}; + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXP_CONSTANTS_H diff --git a/libc/src/__support/math/exp_utils.h b/libc/src/__support/math/exp_utils.h new file mode 100644 index 000000000000..fc9ab10d76cc --- /dev/null +++ b/libc/src/__support/math/exp_utils.h @@ -0,0 +1,72 @@ +//===-- Common utils for exp function ---------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXP_UTILS_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_EXP_UTILS_H + +#include "src/__support/CPP/bit.h" +#include "src/__support/CPP/optional.h" +#include "src/__support/FPUtil/FPBits.h" + +namespace LIBC_NAMESPACE_DECL { + +// 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). +// So if we scale x up by 2^1022, we can use +// double(1.0 + 2^1022 * x) - 1.0 to test how x is rounded in denormal range. +template <bool SKIP_ZIV_TEST = false> +static constexpr cpp::optional<double> ziv_test_denorm(int hi, double mid, + double lo, double err) { + using FPBits = typename fputil::FPBits<double>; + + // Scaling factor = 1/(min normal number) = 2^1022 + int64_t exp_hi = static_cast<int64_t>(hi + 1022) << FPBits::FRACTION_LEN; + double mid_hi = cpp::bit_cast<double>(exp_hi + cpp::bit_cast<int64_t>(mid)); + double lo_scaled = + (lo != 0.0) ? cpp::bit_cast<double>(exp_hi + cpp::bit_cast<int64_t>(lo)) + : 0.0; + + double extra_factor = 0.0; + uint64_t scale_down = 0x3FE0'0000'0000'0000; // 1022 in the exponent field. + + // Result is denormal if (mid_hi + lo_scale < 1.0). + if ((1.0 - mid_hi) > lo_scaled) { + // Extra rounding step is needed, which adds more rounding errors. + err += 0x1.0p-52; + extra_factor = 1.0; + scale_down = 0x3FF0'0000'0000'0000; // 1023 in the exponent field. + } + + // By adding 1.0, the results will have similar rounding points as denormal + // outputs. + 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 = + cpp::bit_cast<double>(exp_hi + cpp::bit_cast<int64_t>(err)); + + 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; + } +} + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXP_UTILS_H diff --git a/libc/src/__support/math/expf16_utils.h b/libc/src/__support/math/expf16_utils.h index bebb72b09b88..8a2fc9415ab8 100644 --- a/libc/src/__support/math/expf16_utils.h +++ b/libc/src/__support/math/expf16_utils.h @@ -47,7 +47,7 @@ struct ExpRangeReduction { float exp_lo; }; -static constexpr ExpRangeReduction exp_range_reduction(float16 x) { +[[maybe_unused]] static ExpRangeReduction exp_range_reduction(float16 x) { // For -18 < x < 12, to compute exp(x), we perform the following range // reduction: find hi, mid, lo, such that: // x = hi + mid + lo, in which diff --git a/libc/src/__support/math/frexpf.h b/libc/src/__support/math/frexpf.h new file mode 100644 index 000000000000..4d2f4948f2af --- /dev/null +++ b/libc/src/__support/math/frexpf.h @@ -0,0 +1,28 @@ +//===-- Implementation header for frexpf ------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_FREXPF_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_FREXPF_H + +#include "src/__support/FPUtil/ManipulationFunctions.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +namespace math { + +static constexpr float frexpf(float x, int *exp) { + return fputil::frexp(x, *exp); +} + +} // namespace math + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FREXPF_H diff --git a/libc/src/__support/math/frexpf128.h b/libc/src/__support/math/frexpf128.h new file mode 100644 index 000000000000..2fd5bc4318e2 --- /dev/null +++ b/libc/src/__support/math/frexpf128.h @@ -0,0 +1,34 @@ +//===-- Implementation header for expf --------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_FREXPF128_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_FREXPF128_H + +#include "include/llvm-libc-types/float128.h" + +#ifdef LIBC_TYPES_HAS_FLOAT128 + +#include "src/__support/FPUtil/ManipulationFunctions.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +namespace math { + +static constexpr float128 frexpf128(float128 x, int *exp) { + return fputil::frexp(x, *exp); +} + +} // namespace math + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FREXPF128_H diff --git a/libc/src/__support/math/frexpf16.h b/libc/src/__support/math/frexpf16.h new file mode 100644 index 000000000000..8deeba0f43e3 --- /dev/null +++ b/libc/src/__support/math/frexpf16.h @@ -0,0 +1,34 @@ +//===-- Implementation header for frexpf16 ----------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_FREXPF16_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_FREXPF16_H + +#include "include/llvm-libc-macros/float16-macros.h" + +#ifdef LIBC_TYPES_HAS_FLOAT16 + +#include "src/__support/FPUtil/ManipulationFunctions.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +namespace math { + +static constexpr float16 frexpf16(float16 x, int *exp) { + return fputil::frexp(x, *exp); +} + +} // namespace math + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT16 + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FREXPF16_H diff --git a/libc/src/__support/math/ldexpf.h b/libc/src/__support/math/ldexpf.h new file mode 100644 index 000000000000..3a5ec1d47133 --- /dev/null +++ b/libc/src/__support/math/ldexpf.h @@ -0,0 +1,28 @@ +//===-- Implementation header for ldexpf ------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_LDEXPF_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_LDEXPF_H + +#include "src/__support/FPUtil/ManipulationFunctions.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +namespace math { + +static constexpr float ldexpf(float x, int exp) { + return fputil::ldexp(x, exp); +} + +} // namespace math + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_LDEXPF_H diff --git a/libc/src/__support/math/ldexpf128.h b/libc/src/__support/math/ldexpf128.h new file mode 100644 index 000000000000..362583093b2f --- /dev/null +++ b/libc/src/__support/math/ldexpf128.h @@ -0,0 +1,34 @@ +//===-- Implementation header for ldexpf ------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_LDEXPF128_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_LDEXPF128_H + +#include "include/llvm-libc-types/float128.h" + +#ifdef LIBC_TYPES_HAS_FLOAT128 + +#include "src/__support/FPUtil/ManipulationFunctions.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +namespace math { + +static constexpr float128 ldexpf128(float128 x, int exp) { + return fputil::ldexp(x, exp); +} + +} // namespace math + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_LDEXPF128_H diff --git a/libc/src/__support/math/ldexpf16.h b/libc/src/__support/math/ldexpf16.h new file mode 100644 index 000000000000..fbead87d909a --- /dev/null +++ b/libc/src/__support/math/ldexpf16.h @@ -0,0 +1,34 @@ +//===-- Implementation header for ldexpf16 ----------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_LDEXPF16_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_LDEXPF16_H + +#include "include/llvm-libc-macros/float16-macros.h" + +#ifdef LIBC_TYPES_HAS_FLOAT16 + +#include "src/__support/FPUtil/ManipulationFunctions.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +namespace math { + +static constexpr float16 ldexpf16(float16 x, int exp) { + return fputil::ldexp(x, exp); +} + +} // namespace math + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT16 + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_LDEXPF16_H diff --git a/libc/src/__support/wchar/CMakeLists.txt b/libc/src/__support/wchar/CMakeLists.txt index d3fb58ed0c71..802441d37fe9 100644 --- a/libc/src/__support/wchar/CMakeLists.txt +++ b/libc/src/__support/wchar/CMakeLists.txt @@ -6,6 +6,19 @@ add_header_library( libc.hdr.types.char32_t ) +add_header_library( + string_converter + HDRS + string_converter.h + DEPENDS + libc.hdr.types.char8_t + libc.hdr.types.char32_t + libc.hdr.types.size_t + libc.src.__support.error_or + .mbstate + .character_converter +) + add_object_library( character_converter HDRS @@ -16,6 +29,7 @@ add_object_library( libc.hdr.errno_macros libc.hdr.types.char8_t libc.hdr.types.char32_t + libc.hdr.types.size_t libc.src.__support.error_or libc.src.__support.math_extras .mbstate diff --git a/libc/src/__support/wchar/character_converter.cpp b/libc/src/__support/wchar/character_converter.cpp index 3cacfa5689e4..15d0f478a18a 100644 --- a/libc/src/__support/wchar/character_converter.cpp +++ b/libc/src/__support/wchar/character_converter.cpp @@ -9,6 +9,7 @@ #include "hdr/errno_macros.h" #include "hdr/types/char32_t.h" #include "hdr/types/char8_t.h" +#include "hdr/types/size_t.h" #include "src/__support/CPP/bit.h" #include "src/__support/common.h" #include "src/__support/error_or.h" @@ -92,6 +93,7 @@ int CharacterConverter::push(char8_t utf8_byte) { state->bytes_stored++; return 0; } + // Invalid byte -> reset the state clear(); return EILSEQ; @@ -130,6 +132,12 @@ ErrorOr<char32_t> CharacterConverter::pop_utf32() { return utf32; } +size_t CharacterConverter::sizeAsUTF32() { + return 1; // a single utf-32 value can fit an entire character +} + +size_t CharacterConverter::sizeAsUTF8() { return state->total_bytes; } + ErrorOr<char8_t> CharacterConverter::pop_utf8() { if (isEmpty()) return Error(-1); @@ -156,6 +164,9 @@ ErrorOr<char8_t> CharacterConverter::pop_utf8() { } state->bytes_stored--; + if (state->bytes_stored == 0) + clear(); + return static_cast<char8_t>(output); } diff --git a/libc/src/__support/wchar/character_converter.h b/libc/src/__support/wchar/character_converter.h index d9a63fdc0522..b6d918f2d2ed 100644 --- a/libc/src/__support/wchar/character_converter.h +++ b/libc/src/__support/wchar/character_converter.h @@ -11,6 +11,7 @@ #include "hdr/types/char32_t.h" #include "hdr/types/char8_t.h" +#include "hdr/types/size_t.h" #include "src/__support/common.h" #include "src/__support/error_or.h" #include "src/__support/wchar/mbstate.h" @@ -30,6 +31,9 @@ public: bool isEmpty(); bool isValidState(); + size_t sizeAsUTF32(); + size_t sizeAsUTF8(); + int push(char8_t utf8_byte); int push(char32_t utf32); diff --git a/libc/src/__support/wchar/string_converter.h b/libc/src/__support/wchar/string_converter.h new file mode 100644 index 000000000000..869ebdfc8b39 --- /dev/null +++ b/libc/src/__support/wchar/string_converter.h @@ -0,0 +1,116 @@ +//===-- Definition of a class for mbstate_t and conversion -----*-- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_STRING_CONVERTER_H +#define LLVM_LIBC_SRC___SUPPORT_STRING_CONVERTER_H + +#include "hdr/types/char32_t.h" +#include "hdr/types/char8_t.h" +#include "hdr/types/size_t.h" +#include "src/__support/common.h" +#include "src/__support/error_or.h" +#include "src/__support/wchar/character_converter.h" +#include "src/__support/wchar/mbstate.h" + +namespace LIBC_NAMESPACE_DECL { +namespace internal { + +template <typename T> class StringConverter { +private: + CharacterConverter cr; + const T *src; + size_t src_len; + size_t src_idx; + + // # of pops we are allowed to perform (essentially size of the dest buffer) + size_t num_to_write; + + ErrorOr<size_t> pushFullCharacter() { + size_t num_pushed; + for (num_pushed = 0; !cr.isFull() && src_idx + num_pushed < src_len; + ++num_pushed) { + int err = cr.push(src[src_idx + num_pushed]); + if (err != 0) + return Error(err); + } + + // if we aren't able to read a full character from the source string + if (src_idx + num_pushed == src_len && !cr.isFull()) { + src_idx += num_pushed; + return Error(-1); + } + + return num_pushed; + } + +public: + StringConverter(const T *s, mbstate *ps, size_t dstlen, + size_t srclen = SIZE_MAX) + : cr(ps), src(s), src_len(srclen), src_idx(0), num_to_write(dstlen) {} + + // TODO: following functions are almost identical + // look into templating CharacterConverter pop functions + ErrorOr<char32_t> popUTF32() { + if (num_to_write == 0) + return Error(-1); + + if (cr.isEmpty() || src_idx == 0) { + auto src_elements_read = pushFullCharacter(); + if (!src_elements_read.has_value()) + return Error(src_elements_read.error()); + + if (cr.sizeAsUTF32() > num_to_write) { + cr.clear(); + return Error(-1); + } + + src_idx += src_elements_read.value(); + } + + auto out = cr.pop_utf32(); + if (out.has_value() && out.value() == L'\0') + src_len = src_idx; + + num_to_write--; + + return out; + } + + ErrorOr<char8_t> popUTF8() { + if (num_to_write == 0) + return Error(-1); + + if (cr.isEmpty() || src_idx == 0) { + auto src_elements_read = pushFullCharacter(); + if (!src_elements_read.has_value()) + return Error(src_elements_read.error()); + + if (cr.sizeAsUTF8() > num_to_write) { + cr.clear(); + return Error(-1); + } + + src_idx += src_elements_read.value(); + } + + auto out = cr.pop_utf8(); + if (out.has_value() && out.value() == '\0') + src_len = src_idx; + + num_to_write--; + + return out; + } + + size_t getSourceIndex() { return src_idx; } +}; + +} // namespace internal +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_STRING_CONVERTER_H diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt index b27b0d2b523f..455ad3456573 100644 --- a/libc/src/math/CMakeLists.txt +++ b/libc/src/math/CMakeLists.txt @@ -519,6 +519,8 @@ add_math_entrypoint_object(tanf16) add_math_entrypoint_object(tanh) add_math_entrypoint_object(tanhf) add_math_entrypoint_object(tanhf16) + +add_math_entrypoint_object(tanpif) add_math_entrypoint_object(tanpif16) add_math_entrypoint_object(tgamma) diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index 6c3f28f423c7..fb253a450270 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -358,7 +358,6 @@ add_entrypoint_object( libc.src.__support.FPUtil.fp_bits libc.src.__support.FPUtil.except_value_utils libc.src.__support.FPUtil.fma - libc.src.__support.FPUtil.multiply_add libc.src.__support.FPUtil.polyeval libc.src.__support.macros.optimization ) @@ -448,7 +447,6 @@ add_entrypoint_object( libc.src.__support.FPUtil.fenv_impl libc.src.__support.FPUtil.fp_bits libc.src.__support.FPUtil.fma - libc.src.__support.FPUtil.multiply_add libc.src.__support.FPUtil.polyeval libc.src.__support.FPUtil.rounding_mode libc.src.__support.macros.optimization @@ -607,6 +605,21 @@ add_entrypoint_object( ) add_entrypoint_object( + tanpif + SRCS + tanpif.cpp + HDRS + ../tanpif.h + DEPENDS + .sincosf_utils + libc.src.__support.FPUtil.except_value_utils + libc.src.__support.FPUtil.fenv_impl + libc.src.__support.FPUtil.fp_bits + libc.src.__support.FPUtil.multiply_add + libc.src.__support.macros.optimization +) + +add_entrypoint_object( tanpif16 SRCS tanpif16.cpp @@ -1297,20 +1310,7 @@ add_entrypoint_object( HDRS ../exp.h DEPENDS - .common_constants - .explogxf - libc.src.__support.CPP.bit - libc.src.__support.CPP.optional - libc.src.__support.FPUtil.dyadic_float - libc.src.__support.FPUtil.fenv_impl - libc.src.__support.FPUtil.fp_bits - libc.src.__support.FPUtil.multiply_add - libc.src.__support.FPUtil.nearest_integer - libc.src.__support.FPUtil.polyeval - libc.src.__support.FPUtil.rounding_mode - libc.src.__support.FPUtil.triple_double - libc.src.__support.integer_literals - libc.src.__support.macros.optimization + libc.src.__support.math.exp libc.src.errno.errno ) @@ -1455,35 +1455,7 @@ add_entrypoint_object( HDRS ../exp10.h DEPENDS - .common_constants - .explogxf - libc.src.__support.CPP.bit - libc.src.__support.CPP.optional - libc.src.__support.FPUtil.dyadic_float - libc.src.__support.FPUtil.fenv_impl - libc.src.__support.FPUtil.fp_bits - libc.src.__support.FPUtil.multiply_add - libc.src.__support.FPUtil.nearest_integer - libc.src.__support.FPUtil.polyeval - libc.src.__support.FPUtil.rounding_mode - libc.src.__support.FPUtil.triple_double - libc.src.__support.integer_literals - libc.src.__support.macros.optimization - libc.src.errno.errno -) - -add_header_library( - exp10f_impl - HDRS - exp10f_impl.h - DEPENDS - .explogxf - libc.src.__support.FPUtil.fenv_impl - libc.src.__support.FPUtil.fp_bits - libc.src.__support.FPUtil.multiply_add - libc.src.__support.FPUtil.rounding_mode - libc.src.__support.macros.optimization - libc.src.__support.common + libc.src.__support.math.exp10 libc.src.errno.errno ) @@ -1494,7 +1466,8 @@ add_entrypoint_object( HDRS ../exp10f.h DEPENDS - .exp10f_impl + libc.src.__support.math.exp10f + libc.src.errno.errno ) add_entrypoint_object( @@ -1504,20 +1477,8 @@ add_entrypoint_object( HDRS ../exp10f16.h DEPENDS - .expxf16 - libc.hdr.errno_macros - libc.hdr.fenv_macros - libc.src.__support.CPP.array - libc.src.__support.FPUtil.cast - libc.src.__support.FPUtil.except_value_utils - libc.src.__support.FPUtil.fenv_impl - libc.src.__support.FPUtil.fp_bits - libc.src.__support.FPUtil.multiply_add - libc.src.__support.FPUtil.nearest_integer - libc.src.__support.FPUtil.polyeval - libc.src.__support.FPUtil.rounding_mode - libc.src.__support.macros.optimization - libc.src.__support.macros.properties.cpu_features + libc.src.__support.math.exp10f16 + libc.src.errno.errno ) add_entrypoint_object( @@ -1546,7 +1507,6 @@ add_entrypoint_object( HDRS ../exp10m1f16.h DEPENDS - .expxf16 libc.hdr.errno_macros libc.hdr.fenv_macros libc.src.__support.FPUtil.cast @@ -1558,6 +1518,7 @@ add_entrypoint_object( libc.src.__support.FPUtil.rounding_mode libc.src.__support.macros.optimization libc.src.__support.macros.properties.cpu_features + libc.src.__support.math.exp10f16_utils ) add_entrypoint_object( @@ -1631,17 +1592,15 @@ add_entrypoint_object( ../powf.h DEPENDS .common_constants - .exp10f_impl .exp2f_impl .explogxf + libc.src.__support.math.exp10f libc.src.__support.CPP.bit - libc.src.__support.CPP.optional libc.src.__support.FPUtil.fenv_impl libc.src.__support.FPUtil.fp_bits libc.src.__support.FPUtil.multiply_add libc.src.__support.FPUtil.nearest_integer libc.src.__support.FPUtil.polyeval - libc.src.__support.FPUtil.rounding_mode libc.src.__support.FPUtil.sqrt libc.src.__support.FPUtil.triple_double libc.src.__support.macros.optimization @@ -1744,7 +1703,7 @@ add_entrypoint_object( HDRS ../frexpf.h DEPENDS - libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.math.frexpf ) add_entrypoint_object( @@ -1764,8 +1723,7 @@ add_entrypoint_object( HDRS ../frexpf16.h DEPENDS - libc.src.__support.macros.properties.types - libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.math.frexpf16 ) add_entrypoint_object( @@ -1775,8 +1733,7 @@ add_entrypoint_object( HDRS ../frexpf128.h DEPENDS - libc.src.__support.macros.properties.types - libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.math.frexpf128 ) add_entrypoint_object( @@ -1900,7 +1857,7 @@ add_entrypoint_object( HDRS ../ldexpf.h DEPENDS - libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.math.ldexpf ) add_entrypoint_object( @@ -1920,8 +1877,7 @@ add_entrypoint_object( HDRS ../ldexpf16.h DEPENDS - libc.src.__support.macros.properties.types - libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.math.ldexpf16 ) add_entrypoint_object( @@ -1931,8 +1887,7 @@ add_entrypoint_object( HDRS ../ldexpf128.h DEPENDS - libc.src.__support.macros.properties.types - libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.math.ldexpf128 ) add_object_library( @@ -1942,8 +1897,8 @@ add_object_library( SRCS common_constants.cpp DEPENDS + libc.src.__support.math.exp_constants libc.src.__support.number_pair - libc.src.__support.FPUtil.triple_double ) add_header_library( @@ -3799,24 +3754,15 @@ add_entrypoint_object( ) #TODO: Add errno include to the hyperbolic functions. -add_object_library( +add_header_library( explogxf HDRS explogxf.h - SRCS - explogxf.cpp DEPENDS .common_constants - libc.src.__support.CPP.bit - libc.src.__support.CPP.optional - libc.src.__support.FPUtil.basic_operations - libc.src.__support.FPUtil.basic_operations - libc.src.__support.FPUtil.fenv_impl - libc.src.__support.FPUtil.fp_bits - libc.src.__support.FPUtil.multiply_add - libc.src.__support.FPUtil.nearest_integer - libc.src.__support.FPUtil.polyeval - libc.src.__support.common + libc.src.__support.math.exp_utils + libc.src.__support.math.exp10f_utils + libc.src.__support.macros.properties.cpu_features libc.src.errno.errno ) @@ -3999,6 +3945,7 @@ add_entrypoint_object( DEPENDS .explogxf libc.src.__support.FPUtil.fp_bits + libc.src.__support.FPUtil.fenv_impl libc.src.__support.macros.optimization ) @@ -5064,10 +5011,11 @@ add_header_library( HDRS expxf16.h DEPENDS - libc.src.__support.FPUtil.cast libc.src.__support.FPUtil.fp_bits + libc.src.__support.FPUtil.cast libc.src.__support.FPUtil.multiply_add libc.src.__support.FPUtil.nearest_integer libc.src.__support.macros.attributes libc.src.__support.math.expf16_utils + libc.src.__support.math.exp10_float16_constants ) diff --git a/libc/src/math/generic/atanhf.cpp b/libc/src/math/generic/atanhf.cpp index 2149314d2f67..f6fde766ef78 100644 --- a/libc/src/math/generic/atanhf.cpp +++ b/libc/src/math/generic/atanhf.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "src/math/atanhf.h" +#include "src/__support/FPUtil/FEnvImpl.h" #include "src/__support/FPUtil/FPBits.h" #include "src/__support/macros/config.h" #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY diff --git a/libc/src/math/generic/common_constants.cpp b/libc/src/math/generic/common_constants.cpp index b2c1293c6326..4dcf84d00ad5 100644 --- a/libc/src/math/generic/common_constants.cpp +++ b/libc/src/math/generic/common_constants.cpp @@ -7,7 +7,6 @@ //===----------------------------------------------------------------------===// #include "common_constants.h" -#include "src/__support/FPUtil/triple_double.h" #include "src/__support/macros/config.h" #include "src/__support/number_pair.h" @@ -728,160 +727,4 @@ const double EXP_M2[128] = { 0x1.568bb722dd593p1, 0x1.593b7d72305bbp1, }; -// Lookup table for 2^(k * 2^-6) with k = 0..63. -// Generated by Sollya with: -// > display=hexadecimal; -// > prec = 500; -// > for i from 0 to 63 do { -// a = 2^(i * 2^-6); -// b = round(a, D, RN); -// c = round(a - b, D, RN); -// d = round(a - b - c, D, RN); -// print("{", d, ",", c, ",", b, "},"); -// }; -alignas(16) const fputil::TripleDouble EXP2_MID1[64] = { - {0, 0, 0x1p0}, - {-0x1.9085b0a3d74d5p-110, -0x1.19083535b085dp-56, 0x1.02c9a3e778061p0}, - {0x1.05ff94f8d257ep-110, 0x1.d73e2a475b465p-55, 0x1.059b0d3158574p0}, - {0x1.15820d96b414fp-111, 0x1.186be4bb284ffp-57, 0x1.0874518759bc8p0}, - {-0x1.67c9bd6ebf74cp-108, 0x1.8a62e4adc610bp-54, 0x1.0b5586cf9890fp0}, - {-0x1.5aa76994e9ddbp-113, 0x1.03a1727c57b53p-59, 0x1.0e3ec32d3d1a2p0}, - {0x1.9d58b988f562dp-109, -0x1.6c51039449b3ap-54, 0x1.11301d0125b51p0}, - {-0x1.2fe7bb4c76416p-108, -0x1.32fbf9af1369ep-54, 0x1.1429aaea92dep0}, - {0x1.4f2406aa13ffp-109, -0x1.19041b9d78a76p-55, 0x1.172b83c7d517bp0}, - {0x1.ad36183926ae8p-111, 0x1.e5b4c7b4968e4p-55, 0x1.1a35beb6fcb75p0}, - {0x1.ea62d0881b918p-110, 0x1.e016e00a2643cp-54, 0x1.1d4873168b9aap0}, - {-0x1.781dbc16f1ea4p-111, 0x1.dc775814a8495p-55, 0x1.2063b88628cd6p0}, - {-0x1.4d89f9af532ep-109, 0x1.9b07eb6c70573p-54, 0x1.2387a6e756238p0}, - {0x1.277393a461b77p-110, 0x1.2bd339940e9d9p-55, 0x1.26b4565e27cddp0}, - {0x1.de5448560469p-111, 0x1.612e8afad1255p-55, 0x1.29e9df51fdee1p0}, - {-0x1.ee9d8f8cb9307p-110, 0x1.0024754db41d5p-54, 0x1.2d285a6e4030bp0}, - {0x1.7b7b2f09cd0d9p-110, 0x1.6f46ad23182e4p-55, 0x1.306fe0a31b715p0}, - {-0x1.406a2ea6cfc6bp-108, 0x1.32721843659a6p-54, 0x1.33c08b26416ffp0}, - {0x1.87e3e12516bfap-108, -0x1.63aeabf42eae2p-54, 0x1.371a7373aa9cbp0}, - {0x1.9b0b1ff17c296p-111, -0x1.5e436d661f5e3p-56, 0x1.3a7db34e59ff7p0}, - {-0x1.808ba68fa8fb7p-109, 0x1.ada0911f09ebcp-55, 0x1.3dea64c123422p0}, - {-0x1.32b43eafc6518p-114, -0x1.ef3691c309278p-58, 0x1.4160a21f72e2ap0}, - {-0x1.0ac312de3d922p-114, 0x1.89b7a04ef80dp-59, 0x1.44e086061892dp0}, - {0x1.e1eebae743acp-111, 0x1.3c1a3b69062fp-56, 0x1.486a2b5c13cdp0}, - {0x1.c06c7745c2b39p-113, 0x1.d4397afec42e2p-56, 0x1.4bfdad5362a27p0}, - {-0x1.1aa1fd7b685cdp-112, -0x1.4b309d25957e3p-54, 0x1.4f9b2769d2ca7p0}, - {0x1.fa733951f214cp-111, -0x1.07abe1db13cadp-55, 0x1.5342b569d4f82p0}, - {-0x1.ff86852a613ffp-111, 0x1.9bb2c011d93adp-54, 0x1.56f4736b527dap0}, - {-0x1.744ee506fdafep-109, 0x1.6324c054647adp-54, 0x1.5ab07dd485429p0}, - {-0x1.95f9ab75fa7d6p-108, 0x1.ba6f93080e65ep-54, 0x1.5e76f15ad2148p0}, - {0x1.5d8e757cfb991p-111, -0x1.383c17e40b497p-54, 0x1.6247eb03a5585p0}, - {0x1.4a337f4dc0a3bp-108, -0x1.bb60987591c34p-54, 0x1.6623882552225p0}, - {0x1.57d3e3adec175p-108, -0x1.bdd3413b26456p-54, 0x1.6a09e667f3bcdp0}, - {0x1.a59f88abbe778p-115, -0x1.bbe3a683c88abp-57, 0x1.6dfb23c651a2fp0}, - {-0x1.269796953a4c3p-109, -0x1.16e4786887a99p-55, 0x1.71f75e8ec5f74p0}, - {-0x1.8f8e7fa19e5e8p-108, -0x1.0245957316dd3p-54, 0x1.75feb564267c9p0}, - {-0x1.4217a932d10d4p-113, -0x1.41577ee04992fp-55, 0x1.7a11473eb0187p0}, - {0x1.70a1427f8fcdfp-112, 0x1.05d02ba15797ep-56, 0x1.7e2f336cf4e62p0}, - {0x1.0f6ad65cbbac1p-112, -0x1.d4c1dd41532d8p-54, 0x1.82589994cce13p0}, - {-0x1.f16f65181d921p-109, -0x1.fc6f89bd4f6bap-54, 0x1.868d99b4492edp0}, - {-0x1.30644a7836333p-110, 0x1.6e9f156864b27p-54, 0x1.8ace5422aa0dbp0}, - {0x1.3bf26d2b85163p-114, 0x1.5cc13a2e3976cp-55, 0x1.8f1ae99157736p0}, - {0x1.697e257ac0db2p-111, -0x1.75fc781b57ebcp-57, 0x1.93737b0cdc5e5p0}, - {0x1.7edb9d7144b6fp-108, -0x1.d185b7c1b85d1p-54, 0x1.97d829fde4e5p0}, - {0x1.6376b7943085cp-110, 0x1.c7c46b071f2bep-56, 0x1.9c49182a3f09p0}, - {0x1.354084551b4fbp-109, -0x1.359495d1cd533p-54, 0x1.a0c667b5de565p0}, - {-0x1.bfd7adfd63f48p-111, -0x1.d2f6edb8d41e1p-54, 0x1.a5503b23e255dp0}, - {0x1.8b16ae39e8cb9p-109, 0x1.0fac90ef7fd31p-54, 0x1.a9e6b5579fdbfp0}, - {0x1.a7fbc3ae675eap-108, 0x1.7a1cd345dcc81p-54, 0x1.ae89f995ad3adp0}, - {0x1.2babc0edda4d9p-111, -0x1.2805e3084d708p-57, 0x1.b33a2b84f15fbp0}, - {0x1.aa64481e1ab72p-111, -0x1.5584f7e54ac3bp-56, 0x1.b7f76f2fb5e47p0}, - {0x1.9a164050e1258p-109, 0x1.23dd07a2d9e84p-55, 0x1.bcc1e904bc1d2p0}, - {0x1.99e51125928dap-110, 0x1.11065895048ddp-55, 0x1.c199bdd85529cp0}, - {-0x1.fc44c329d5cb2p-109, 0x1.2884dff483cadp-54, 0x1.c67f12e57d14bp0}, - {0x1.d8765566b032ep-110, 0x1.503cbd1e949dbp-56, 0x1.cb720dcef9069p0}, - {-0x1.e7044039da0f6p-108, -0x1.cbc3743797a9cp-54, 0x1.d072d4a07897cp0}, - {-0x1.ab053b05531fcp-111, 0x1.2ed02d75b3707p-55, 0x1.d5818dcfba487p0}, - {0x1.7f6246f0ec615p-108, 0x1.c2300696db532p-54, 0x1.da9e603db3285p0}, - {0x1.b7225a944efd6p-108, -0x1.1a5cd4f184b5cp-54, 0x1.dfc97337b9b5fp0}, - {0x1.1e92cb3c2d278p-109, 0x1.39e8980a9cc8fp-55, 0x1.e502ee78b3ff6p0}, - {-0x1.fc0f242bbf3dep-109, -0x1.e9c23179c2893p-54, 0x1.ea4afa2a490dap0}, - {0x1.f6dd5d229ff69p-108, 0x1.dc7f486a4b6bp-54, 0x1.efa1bee615a27p0}, - {-0x1.4019bffc80ef3p-110, 0x1.9d3e12dd8a18bp-54, 0x1.f50765b6e454p0}, - {0x1.dc060c36f7651p-112, 0x1.74853f3a5931ep-55, 0x1.fa7c1819e90d8p0}, -}; - -// Lookup table for 2^(k * 2^-12) with k = 0..63. -// Generated by Sollya with: -// > display=hexadecimal; -// > prec = 500; -// > for i from 0 to 63 do { -// a = 2^(i * 2^-12); -// b = round(a, D, RN); -// c = round(a - b, D, RN); -// d = round(a - b - c, D, RN); -// print("{", d, ",", c, ",", b, "},"); -// }; -alignas(16) const fputil::TripleDouble EXP2_MID2[64] = { - {0, 0, 0x1p0}, - {0x1.39726694630e3p-108, 0x1.ae8e38c59c72ap-54, 0x1.000b175effdc7p0}, - {0x1.e5e06ddd31156p-112, -0x1.7b5d0d58ea8f4p-58, 0x1.00162f3904052p0}, - {0x1.5a0768b51f609p-111, 0x1.4115cb6b16a8ep-54, 0x1.0021478e11ce6p0}, - {0x1.d008403605217p-111, -0x1.d7c96f201bb2fp-55, 0x1.002c605e2e8cfp0}, - {0x1.89bc16f765708p-109, 0x1.84711d4c35e9fp-54, 0x1.003779a95f959p0}, - {-0x1.4535b7f8c1e2dp-109, -0x1.0484245243777p-55, 0x1.0042936faa3d8p0}, - {-0x1.8ba92f6b25456p-108, -0x1.4b237da2025f9p-54, 0x1.004dadb113dap0}, - {-0x1.30c72e81f4294p-113, -0x1.5e00e62d6b30dp-56, 0x1.0058c86da1c0ap0}, - {-0x1.34a5384e6f0b9p-110, 0x1.a1d6cedbb9481p-54, 0x1.0063e3a559473p0}, - {0x1.f8d0580865d2ep-108, -0x1.4acf197a00142p-54, 0x1.006eff583fc3dp0}, - {-0x1.002bcb3ae9a99p-111, -0x1.eaf2ea42391a5p-57, 0x1.007a1b865a8cap0}, - {0x1.c3c5aedee9851p-111, 0x1.da93f90835f75p-56, 0x1.0085382faef83p0}, - {0x1.7217851d1ec6ep-109, -0x1.6a79084ab093cp-55, 0x1.00905554425d4p0}, - {-0x1.80cbca335a7c3p-110, 0x1.86364f8fbe8f8p-54, 0x1.009b72f41a12bp0}, - {-0x1.706bd4eb22595p-110, -0x1.82e8e14e3110ep-55, 0x1.00a6910f3b6fdp0}, - {-0x1.b55dd523f3c08p-111, -0x1.4f6b2a7609f71p-55, 0x1.00b1afa5abcbfp0}, - {0x1.90a1e207cced1p-110, -0x1.e1a258ea8f71bp-56, 0x1.00bcceb7707ecp0}, - {0x1.78d0472db37c5p-110, 0x1.4362ca5bc26f1p-56, 0x1.00c7ee448ee02p0}, - {-0x1.bcd4db3cb52fep-109, 0x1.095a56c919d02p-54, 0x1.00d30e4d0c483p0}, - {-0x1.cf1b131575ec2p-112, -0x1.406ac4e81a645p-57, 0x1.00de2ed0ee0f5p0}, - {-0x1.6aaa1fa7ff913p-112, 0x1.b5a6902767e09p-54, 0x1.00e94fd0398ep0}, - {0x1.68f236dff3218p-110, -0x1.91b2060859321p-54, 0x1.00f4714af41d3p0}, - {-0x1.e8bb58067e60ap-109, 0x1.427068ab22306p-55, 0x1.00ff93412315cp0}, - {0x1.d4cd5e1d71fdfp-108, 0x1.c1d0660524e08p-54, 0x1.010ab5b2cbd11p0}, - {0x1.e4ecf350ebe88p-108, -0x1.e7bdfb3204be8p-54, 0x1.0115d89ff3a8bp0}, - {0x1.6a2aa2c89c4f8p-109, 0x1.843aa8b9cbbc6p-55, 0x1.0120fc089ff63p0}, - {0x1.1ca368a20ed05p-110, -0x1.34104ee7edae9p-56, 0x1.012c1fecd613bp0}, - {0x1.edb1095d925cfp-114, -0x1.2b6aeb6176892p-56, 0x1.0137444c9b5b5p0}, - {-0x1.488c78eded75fp-111, 0x1.a8cd33b8a1bb3p-56, 0x1.01426927f5278p0}, - {-0x1.7480f5ea1b3c9p-113, 0x1.2edc08e5da99ap-56, 0x1.014d8e7ee8d2fp0}, - {-0x1.ae45989a04dd5p-111, 0x1.57ba2dc7e0c73p-55, 0x1.0158b4517bb88p0}, - {0x1.bf48007d80987p-109, 0x1.b61299ab8cdb7p-54, 0x1.0163da9fb3335p0}, - {0x1.1aa91a059292cp-109, -0x1.90565902c5f44p-54, 0x1.016f0169949edp0}, - {0x1.b6663292855f5p-110, 0x1.70fc41c5c2d53p-55, 0x1.017a28af25567p0}, - {0x1.e7fbca6793d94p-108, 0x1.4b9a6e145d76cp-54, 0x1.018550706ab62p0}, - {-0x1.5b9f5c7de3b93p-110, -0x1.008eff5142bf9p-56, 0x1.019078ad6a19fp0}, - {0x1.4638bf2f6acabp-110, -0x1.77669f033c7dep-54, 0x1.019ba16628de2p0}, - {-0x1.ab237b9a069c5p-109, -0x1.09bb78eeead0ap-54, 0x1.01a6ca9aac5f3p0}, - {0x1.3ab358be97cefp-108, 0x1.371231477ece5p-54, 0x1.01b1f44af9f9ep0}, - {-0x1.4027b2294bb64p-110, 0x1.5e7626621eb5bp-56, 0x1.01bd1e77170b4p0}, - {0x1.656394426c99p-111, -0x1.bc72b100828a5p-54, 0x1.01c8491f08f08p0}, - {0x1.bf9785189bdd8p-111, -0x1.ce39cbbab8bbep-57, 0x1.01d37442d507p0}, - {0x1.7c12f86114fe3p-109, 0x1.16996709da2e2p-55, 0x1.01de9fe280ac8p0}, - {-0x1.653d5d24b5d28p-109, -0x1.c11f5239bf535p-55, 0x1.01e9cbfe113efp0}, - {0x1.04a0cdc1d86d7p-109, 0x1.e1d4eb5edc6b3p-55, 0x1.01f4f8958c1c6p0}, - {0x1.c678c46149782p-109, -0x1.afb99946ee3fp-54, 0x1.020025a8f6a35p0}, - {0x1.48524e1e9df7p-108, -0x1.8f06d8a148a32p-54, 0x1.020b533856324p0}, - {0x1.9953ea727ff0bp-109, -0x1.2bf310fc54eb6p-55, 0x1.02168143b0281p0}, - {-0x1.ccfbbec22d28ep-108, -0x1.c95a035eb4175p-54, 0x1.0221afcb09e3ep0}, - {0x1.9e2bb6e181de1p-108, -0x1.491793e46834dp-54, 0x1.022cdece68c4fp0}, - {0x1.f17609ae29308p-110, -0x1.3e8d0d9c49091p-56, 0x1.02380e4dd22adp0}, - {-0x1.c7dc2c476bfb8p-110, -0x1.314aa16278aa3p-54, 0x1.02433e494b755p0}, - {-0x1.fab994971d4a3p-109, 0x1.48daf888e9651p-55, 0x1.024e6ec0da046p0}, - {0x1.848b62cbdd0afp-109, 0x1.56dc8046821f4p-55, 0x1.02599fb483385p0}, - {-0x1.bf603ba715d0cp-109, 0x1.45b42356b9d47p-54, 0x1.0264d1244c719p0}, - {0x1.89434e751e1aap-110, -0x1.082ef51b61d7ep-56, 0x1.027003103b10ep0}, - {-0x1.03b54fd64e8acp-110, 0x1.2106ed0920a34p-56, 0x1.027b357854772p0}, - {0x1.7785ea0acc486p-109, -0x1.fd4cf26ea5d0fp-54, 0x1.0286685c9e059p0}, - {-0x1.ce447fdb35ff9p-109, -0x1.09f8775e78084p-54, 0x1.02919bbd1d1d8p0}, - {0x1.5b884aab5642ap-112, 0x1.64cbba902ca27p-58, 0x1.029ccf99d720ap0}, - {-0x1.cfb3e46d7c1cp-108, 0x1.4383ef231d207p-54, 0x1.02a803f2d170dp0}, - {-0x1.0d40cee4b81afp-112, 0x1.4a47a505b3a47p-54, 0x1.02b338c811703p0}, - {0x1.6ae7d36d7c1f7p-109, 0x1.e47120223467fp-54, 0x1.02be6e199c811p0}, -}; - } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/common_constants.h b/libc/src/math/generic/common_constants.h index e65f00284595..291816a7889a 100644 --- a/libc/src/math/generic/common_constants.h +++ b/libc/src/math/generic/common_constants.h @@ -11,6 +11,7 @@ #include "src/__support/FPUtil/triple_double.h" #include "src/__support/macros/config.h" +#include "src/__support/math/exp_constants.h" #include "src/__support/number_pair.h" namespace LIBC_NAMESPACE_DECL { @@ -80,12 +81,6 @@ extern const double EXP_M1[195]; // > for i from 0 to 127 do { D(exp(i / 128)); }; extern const double EXP_M2[128]; -// Lookup table for 2^(k * 2^-6) with k = 0..63. -extern const fputil::TripleDouble EXP2_MID1[64]; - -// Lookup table for 2^(k * 2^-12) with k = 0..63. -extern const fputil::TripleDouble EXP2_MID2[64]; - } // namespace LIBC_NAMESPACE_DECL #endif // LLVM_LIBC_SRC_MATH_GENERIC_COMMON_CONSTANTS_H diff --git a/libc/src/math/generic/coshf.cpp b/libc/src/math/generic/coshf.cpp index c869f7d9dec5..9f87564d524a 100644 --- a/libc/src/math/generic/coshf.cpp +++ b/libc/src/math/generic/coshf.cpp @@ -7,8 +7,8 @@ //===----------------------------------------------------------------------===// #include "src/math/coshf.h" +#include "src/__support/FPUtil/FEnvImpl.h" #include "src/__support/FPUtil/FPBits.h" -#include "src/__support/FPUtil/multiply_add.h" #include "src/__support/FPUtil/rounding_mode.h" #include "src/__support/macros/config.h" #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY diff --git a/libc/src/math/generic/exp.cpp b/libc/src/math/generic/exp.cpp index 143800ca078a..dc4d2ca480cb 100644 --- a/libc/src/math/generic/exp.cpp +++ b/libc/src/math/generic/exp.cpp @@ -7,434 +7,9 @@ //===----------------------------------------------------------------------===// #include "src/math/exp.h" -#include "common_constants.h" // Lookup tables EXP_M1 and EXP_M2. -#include "explogxf.h" // ziv_test_denorm. -#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" // LIBC_UNLIKELY - +#include "src/__support/math/exp.h" namespace LIBC_NAMESPACE_DECL { -using fputil::DoubleDouble; -using fputil::TripleDouble; -using Float128 = typename fputil::DyadicFloat<128>; - -using LIBC_NAMESPACE::operator""_u128; - -// log2(e) -constexpr double LOG2_E = 0x1.71547652b82fep+0; - -// Error bounds: -// Errors when using double precision. -constexpr double ERR_D = 0x1.8p-63; - -#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS -// Errors when using double-double precision. -constexpr double ERR_DD = 0x1.0p-99; -#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS - -// -2^-12 * log(2) -// > a = -2^-12 * log(2); -// > b = round(a, 30, RN); -// > c = round(a - b, 30, RN); -// > d = round(a - b - c, D, RN); -// Errors < 1.5 * 2^-133 -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 // LIBC_MATH_HAS_SKIP_ACCURATE_PASS - -namespace { - -// Polynomial approximations with double precision: -// Return expm1(dx) / x ~ 1 + dx / 2 + dx^2 / 6 + dx^3 / 24. -// For |dx| < 2^-13 + 2^-30: -// | output - expm1(dx) / dx | < 2^-51. -LIBC_INLINE double poly_approx_d(double dx) { - // dx^2 - double dx2 = dx * dx; - // c0 = 1 + dx / 2 - double c0 = fputil::multiply_add(dx, 0.5, 1.0); - // c1 = 1/6 + dx / 24 - double c1 = - fputil::multiply_add(dx, 0x1.5555555555555p-5, 0x1.5555555555555p-3); - // p = dx^2 * c1 + c0 = 1 + dx / 2 + dx^2 / 6 + dx^3 / 24 - double p = fputil::multiply_add(dx2, c1, c0); - return p; -} - -#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS -// Polynomial approximation with double-double precision: -// Return exp(dx) ~ 1 + dx + dx^2 / 2 + ... + dx^6 / 720 -// For |dx| < 2^-13 + 2^-30: -// | output - exp(dx) | < 2^-101 -DoubleDouble poly_approx_dd(const DoubleDouble &dx) { - // Taylor polynomial. - constexpr DoubleDouble COEFFS[] = { - {0, 0x1p0}, // 1 - {0, 0x1p0}, // 1 - {0, 0x1p-1}, // 1/2 - {0x1.5555555555555p-57, 0x1.5555555555555p-3}, // 1/6 - {0x1.5555555555555p-59, 0x1.5555555555555p-5}, // 1/24 - {0x1.1111111111111p-63, 0x1.1111111111111p-7}, // 1/120 - {-0x1.f49f49f49f49fp-65, 0x1.6c16c16c16c17p-10}, // 1/720 - }; - - DoubleDouble p = fputil::polyeval(dx, COEFFS[0], COEFFS[1], COEFFS[2], - COEFFS[3], COEFFS[4], COEFFS[5], COEFFS[6]); - return p; -} - -// Polynomial approximation with 128-bit precision: -// Return exp(dx) ~ 1 + dx + dx^2 / 2 + ... + dx^7 / 5040 -// For |dx| < 2^-13 + 2^-30: -// | output - exp(dx) | < 2^-126. -Float128 poly_approx_f128(const Float128 &dx) { - constexpr Float128 COEFFS_128[]{ - {Sign::POS, -127, 0x80000000'00000000'00000000'00000000_u128}, // 1.0 - {Sign::POS, -127, 0x80000000'00000000'00000000'00000000_u128}, // 1.0 - {Sign::POS, -128, 0x80000000'00000000'00000000'00000000_u128}, // 0.5 - {Sign::POS, -130, 0xaaaaaaaa'aaaaaaaa'aaaaaaaa'aaaaaaab_u128}, // 1/6 - {Sign::POS, -132, 0xaaaaaaaa'aaaaaaaa'aaaaaaaa'aaaaaaab_u128}, // 1/24 - {Sign::POS, -134, 0x88888888'88888888'88888888'88888889_u128}, // 1/120 - {Sign::POS, -137, 0xb60b60b6'0b60b60b'60b60b60'b60b60b6_u128}, // 1/720 - {Sign::POS, -140, 0xd00d00d0'0d00d00d'00d00d00'd00d00d0_u128}, // 1/5040 - }; - - 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; -} - -// Compute exp(x) using 128-bit precision. -// TODO(lntue): investigate triple-double precision implementation for this -// step. -Float128 exp_f128(double x, double kd, int idx1, int idx2) { - // Recalculate dx: - - double t1 = fputil::multiply_add(kd, MLOG_2_EXP2_M12_HI, x); // exact - double t2 = kd * MLOG_2_EXP2_M12_MID_30; // exact - double t3 = kd * MLOG_2_EXP2_M12_LO; // Error < 2^-133 - - Float128 dx = fputil::quick_add( - Float128(t1), fputil::quick_add(Float128(t2), Float128(t3))); - - // TODO: Skip recalculating exp_mid1 and exp_mid2. - 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; -} - -// Compute exp(x) with double-double precision. -DoubleDouble exp_double_double(double x, double kd, - const DoubleDouble &exp_mid) { - // Recalculate dx: - // dx = x - k * 2^-12 * log(2) - double t1 = fputil::multiply_add(kd, MLOG_2_EXP2_M12_HI, x); // exact - double t2 = kd * MLOG_2_EXP2_M12_MID_30; // exact - double t3 = kd * MLOG_2_EXP2_M12_LO; // Error < 2^-130 - - DoubleDouble dx = fputil::exact_add(t1, t2); - dx.lo += t3; - - // Degree-6 Taylor polynomial approximation in double-double precision. - // | p - exp(x) | < 2^-100. - DoubleDouble p = poly_approx_dd(dx); - - // Error bounds: 2^-99. - DoubleDouble r = fputil::quick_mult(exp_mid, p); - - return r; -} -#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS - -// Check for exceptional cases when -// |x| <= 2^-53 or x < log(2^-1075) or x >= 0x1.6232bdd7abcd3p+9 -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(); - - // |x| <= 2^-53 - if (x_abs <= 0x3ca0'0000'0000'0000ULL) { - // exp(x) ~ 1 + x - return 1 + x; - } - - // x <= log(2^-1075) || x >= 0x1.6232bdd7abcd3p+9 or inf/nan. - - // x <= log(2^-1075) or -inf/nan - if (x_u >= 0xc087'4910'd52d'3052ULL) { - // exp(-Inf) = 0 - if (xbits.is_inf()) - return 0.0; - - // exp(nan) = nan - 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; - } - - // x >= round(log(MAX_NORMAL), D, RU) = 0x1.62e42fefa39fp+9 or +inf/nan - // x is finite - 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); - } - // x is +inf or nan - return x + FPBits::inf().get_val(); -} - -} // namespace - -LLVM_LIBC_FUNCTION(double, exp, (double x)) { - using FPBits = typename fputil::FPBits<double>; - FPBits xbits(x); - - uint64_t x_u = xbits.uintval(); - - // Upper bound: max normal number = 2^1023 * (2 - 2^-52) - // > round(log (2^1023 ( 2 - 2^-52 )), D, RU) = 0x1.62e42fefa39fp+9 - // > round(log (2^1023 ( 2 - 2^-52 )), D, RD) = 0x1.62e42fefa39efp+9 - // > round(log (2^1023 ( 2 - 2^-52 )), D, RN) = 0x1.62e42fefa39efp+9 - // > round(exp(0x1.62e42fefa39fp+9), D, RN) = infty - - // Lower bound: min denormal number / 2 = 2^-1075 - // > round(log(2^-1075), D, RN) = -0x1.74910d52d3052p9 - - // Another lower bound: min normal number = 2^-1022 - // > round(log(2^-1022), D, RN) = -0x1.6232bdd7abcd2p9 - - // x < log(2^-1075) or x >= 0x1.6232bdd7abcd3p+9 or |x| < 2^-53. - if (LIBC_UNLIKELY(x_u >= 0xc0874910d52d3052 || - (x_u < 0xbca0000000000000 && x_u >= 0x40862e42fefa39f0) || - x_u < 0x3ca0000000000000)) { - return set_exceptional(x); - } - - // Now log(2^-1075) <= x <= -2^-53 or 2^-53 <= x < log(2^1023 * (2 - 2^-52)) - - // Range reduction: - // Let x = log(2) * (hi + mid1 + mid2) + lo - // in which: - // hi is an integer - // mid1 * 2^6 is an integer - // mid2 * 2^12 is an integer - // then: - // exp(x) = 2^hi * 2^(mid1) * 2^(mid2) * exp(lo). - // With this formula: - // - multiplying by 2^hi is exact and cheap, simply by adding the exponent - // field. - // - 2^(mid1) and 2^(mid2) are stored in 2 x 64-element tables. - // - exp(lo) ~ 1 + lo + a0 * lo^2 + ... - // - // They can be defined by: - // hi + mid1 + mid2 = 2^(-12) * round(2^12 * log_2(e) * x) - // If we store L2E = round(log2(e), D, RN), then: - // log2(e) - L2E ~ 1.5 * 2^(-56) - // So the errors when computing in double precision is: - // | x * 2^12 * log_2(e) - D(x * 2^12 * L2E) | <= - // <= | x * 2^12 * log_2(e) - x * 2^12 * L2E | + - // + | x * 2^12 * L2E - D(x * 2^12 * L2E) | - // <= 2^12 * ( |x| * 1.5 * 2^-56 + eps(x)) for RN - // 2^12 * ( |x| * 1.5 * 2^-56 + 2*eps(x)) for other rounding modes. - // So if: - // hi + mid1 + mid2 = 2^(-12) * round(x * 2^12 * L2E) is computed entirely - // in double precision, the reduced argument: - // lo = x - log(2) * (hi + mid1 + mid2) is bounded by: - // |lo| <= 2^-13 + (|x| * 1.5 * 2^-56 + 2*eps(x)) - // < 2^-13 + (1.5 * 2^9 * 1.5 * 2^-56 + 2*2^(9 - 52)) - // < 2^-13 + 2^-41 - // - - // The following trick computes the round(x * L2E) more efficiently - // than using the rounding instructions, with the tradeoff for less accuracy, - // and hence a slightly larger range for the reduced argument `lo`. - // - // To be precise, since |x| < |log(2^-1075)| < 1.5 * 2^9, - // |x * 2^12 * L2E| < 1.5 * 2^9 * 1.5 < 2^23, - // So we can fit the rounded result round(x * 2^12 * L2E) in int32_t. - // Thus, the goal is to be able to use an additional addition and fixed width - // shift to get an int32_t representing round(x * 2^12 * L2E). - // - // Assuming int32_t using 2-complement representation, since the mantissa part - // of a double precision is unsigned with the leading bit hidden, if we add an - // extra constant C = 2^e1 + 2^e2 with e1 > e2 >= 2^25 to the product, the - // part that are < 2^e2 in resulted mantissa of (x*2^12*L2E + C) can be - // considered as a proper 2-complement representations of x*2^12*L2E. - // - // One small problem with this approach is that the sum (x*2^12*L2E + C) in - // double precision is rounded to the least significant bit of the dorminant - // factor C. In order to minimize the rounding errors from this addition, we - // want to minimize e1. Another constraint that we want is that after - // shifting the mantissa so that the least significant bit of int32_t - // corresponds to the unit bit of (x*2^12*L2E), the sign is correct without - // any adjustment. So combining these 2 requirements, we can choose - // C = 2^33 + 2^32, so that the sign bit corresponds to 2^31 bit, and hence - // after right shifting the mantissa, the resulting int32_t has correct sign. - // With this choice of C, the number of mantissa bits we need to shift to the - // right is: 52 - 33 = 19. - // - // Moreover, since the integer right shifts are equivalent to rounding down, - // we can add an extra 0.5 so that it will become round-to-nearest, tie-to- - // +infinity. So in particular, we can compute: - // hmm = x * 2^12 * L2E + C, - // where C = 2^33 + 2^32 + 2^-1, then if - // k = int32_t(lower 51 bits of double(x * 2^12 * L2E + C) >> 19), - // the reduced argument: - // lo = x - log(2) * 2^-12 * k is bounded by: - // |lo| <= 2^-13 + 2^-41 + 2^-12*2^-19 - // = 2^-13 + 2^-31 + 2^-41. - // - // Finally, notice that k only uses the mantissa of x * 2^12 * L2E, so the - // exponent 2^12 is not needed. So we can simply define - // C = 2^(33 - 12) + 2^(32 - 12) + 2^(-13 - 12), and - // k = int32_t(lower 51 bits of double(x * L2E + C) >> 19). - - // Rounding errors <= 2^-31 + 2^-41. - 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); - - // |x - (hi + mid1 + mid2) * log(2) - dx| < 2^11 * eps(M_LOG_2_EXP2_M12.lo) - // = 2^11 * 2^-13 * 2^-52 - // = 2^-54. - // |dx| < 2^-13 + 2^-30. - double lo_h = fputil::multiply_add(kd, MLOG_2_EXP2_M12_HI, x); // exact - double dx = fputil::multiply_add(kd, MLOG_2_EXP2_M12_MID, lo_h); - - // We use the degree-4 Taylor polynomial to approximate exp(lo): - // exp(lo) ~ 1 + lo + lo^2 / 2 + lo^3 / 6 + lo^4 / 24 = 1 + lo * P(lo) - // So that the errors are bounded by: - // |P(lo) - expm1(lo)/lo| < |lo|^4 / 64 < 2^(-13 * 4) / 64 = 2^-58 - // Let P_ be an evaluation of P where all intermediate computations are in - // double precision. Using either Horner's or Estrin's schemes, the evaluated - // errors can be bounded by: - // |P_(dx) - P(dx)| < 2^-51 - // => |dx * P_(dx) - expm1(lo) | < 1.5 * 2^-64 - // => 2^(mid1 + mid2) * |dx * P_(dx) - expm1(lo)| < 1.5 * 2^-63. - // Since we approximate - // 2^(mid1 + mid2) ~ exp_mid.hi + exp_mid.lo, - // We use the expression: - // (exp_mid.hi + exp_mid.lo) * (1 + dx * P_(dx)) ~ - // ~ exp_mid.hi + (exp_mid.hi * dx * P_(dx) + exp_mid.lo) - // with errors bounded by 1.5 * 2^-63. - - double mid_lo = dx * exp_mid.hi; - - // Approximate expm1(dx)/dx ~ 1 + dx / 2 + dx^2 / 6 + dx^3 / 24. - 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</*SKIP_ZIV_TEST=*/true>(hi, exp_mid.hi, lo, ERR_D) - .value(); - } else { - // to multiply by 2^hi, a fast way is to simply add hi to the exponent - // field. - 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)) { - // to multiply by 2^hi, a fast way is to simply add hi to the exponent - // field. - 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; - } - } - - // Use double-double - 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; - } - } - - // Use 128-bit precision - Float128 r_f128 = exp_f128(x, kd, idx1, idx2); - - return static_cast<double>(r_f128); -#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS -} +LLVM_LIBC_FUNCTION(double, exp, (double x)) { return math::exp(x); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/exp10.cpp b/libc/src/math/generic/exp10.cpp index c464979b092c..5c36d28c166a 100644 --- a/libc/src/math/generic/exp10.cpp +++ b/libc/src/math/generic/exp10.cpp @@ -7,491 +7,10 @@ //===----------------------------------------------------------------------===// #include "src/math/exp10.h" -#include "common_constants.h" // Lookup tables EXP2_MID1 and EXP_M2. -#include "explogxf.h" // ziv_test_denorm. -#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" // LIBC_UNLIKELY +#include "src/__support/math/exp10.h" namespace LIBC_NAMESPACE_DECL { -using fputil::DoubleDouble; -using fputil::TripleDouble; -using Float128 = typename fputil::DyadicFloat<128>; - -using LIBC_NAMESPACE::operator""_u128; - -// log2(10) -constexpr double LOG2_10 = 0x1.a934f0979a371p+1; - -// -2^-12 * log10(2) -// > a = -2^-12 * log10(2); -// > b = round(a, 32, RN); -// > c = round(a - b, 32, RN); -// > d = round(a - b - c, D, RN); -// Errors < 1.5 * 2^-144 -constexpr double MLOG10_2_EXP2_M12_HI = -0x1.3441350ap-14; -constexpr double MLOG10_2_EXP2_M12_MID = 0x1.0c0219dc1da99p-51; - -#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS -constexpr double MLOG10_2_EXP2_M12_MID_32 = 0x1.0c0219dcp-51; -constexpr double MLOG10_2_EXP2_M12_LO = 0x1.da994fd20dba2p-87; -#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS - -// Error bounds: -// Errors when using double precision. -constexpr double ERR_D = 0x1.8p-63; - -#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS -// Errors when using double-double precision. -constexpr double ERR_DD = 0x1.8p-99; -#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS - -namespace { - -// Polynomial approximations with double precision. Generated by Sollya with: -// > P = fpminimax((10^x - 1)/x, 3, [|D...|], [-2^-14, 2^-14]); -// > P; -// Error bounds: -// | output - (10^dx - 1) / dx | < 2^-52. -LIBC_INLINE double poly_approx_d(double dx) { - // dx^2 - double dx2 = dx * dx; - double c0 = - fputil::multiply_add(dx, 0x1.53524c73cea6ap+1, 0x1.26bb1bbb55516p+1); - double c1 = - fputil::multiply_add(dx, 0x1.2bd75cc6afc65p+0, 0x1.0470587aa264cp+1); - double p = fputil::multiply_add(dx2, c1, c0); - return p; -} - -#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS -// Polynomial approximation with double-double precision. Generated by Solya -// with: -// > P = fpminimax((10^x - 1)/x, 5, [|DD...|], [-2^-14, 2^-14]); -// Error bounds: -// | output - 10^(dx) | < 2^-101 -DoubleDouble poly_approx_dd(const DoubleDouble &dx) { - // Taylor polynomial. - constexpr DoubleDouble COEFFS[] = { - {0, 0x1p0}, - {-0x1.f48ad494e927bp-53, 0x1.26bb1bbb55516p1}, - {-0x1.e2bfab3191cd2p-53, 0x1.53524c73cea69p1}, - {0x1.80fb65ec3b503p-53, 0x1.0470591de2ca4p1}, - {0x1.338fc05e21e55p-54, 0x1.2bd7609fd98c4p0}, - {0x1.d4ea116818fbp-56, 0x1.1429ffd519865p-1}, - {-0x1.872a8ff352077p-57, 0x1.a7ed70847c8b3p-3}, - - }; - - DoubleDouble p = fputil::polyeval(dx, COEFFS[0], COEFFS[1], COEFFS[2], - COEFFS[3], COEFFS[4], COEFFS[5], COEFFS[6]); - return p; -} - -// Polynomial approximation with 128-bit precision: -// Return exp(dx) ~ 1 + a0 * dx + a1 * dx^2 + ... + a6 * dx^7 -// For |dx| < 2^-14: -// | output - 10^dx | < 1.5 * 2^-124. -Float128 poly_approx_f128(const Float128 &dx) { - constexpr Float128 COEFFS_128[]{ - {Sign::POS, -127, 0x80000000'00000000'00000000'00000000_u128}, // 1.0 - {Sign::POS, -126, 0x935d8ddd'aaa8ac16'ea56d62b'82d30a2d_u128}, - {Sign::POS, -126, 0xa9a92639'e753443a'80a99ce7'5f4d5bdb_u128}, - {Sign::POS, -126, 0x82382c8e'f1652304'6a4f9d7d'bf6c9635_u128}, - {Sign::POS, -124, 0x12bd7609'fd98c44c'34578701'9216c7af_u128}, - {Sign::POS, -127, 0x450a7ff4'7535d889'cc41ed7e'0d27aee5_u128}, - {Sign::POS, -130, 0xd3f6b844'702d636b'8326bb91'a6e7601d_u128}, - {Sign::POS, -130, 0x45b937f0'd05bb1cd'fa7b46df'314112a9_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; -} - -// Compute 10^(x) using 128-bit precision. -// TODO(lntue): investigate triple-double precision implementation for this -// step. -Float128 exp10_f128(double x, double kd, int idx1, int idx2) { - double t1 = fputil::multiply_add(kd, MLOG10_2_EXP2_M12_HI, x); // exact - double t2 = kd * MLOG10_2_EXP2_M12_MID_32; // exact - double t3 = kd * MLOG10_2_EXP2_M12_LO; // Error < 2^-144 - - Float128 dx = fputil::quick_add( - Float128(t1), fputil::quick_add(Float128(t2), Float128(t3))); - - // TODO: Skip recalculating exp_mid1 and exp_mid2. - 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; -} - -// Compute 10^x with double-double precision. -DoubleDouble exp10_double_double(double x, double kd, - const DoubleDouble &exp_mid) { - // Recalculate dx: - // dx = x - k * 2^-12 * log10(2) - double t1 = fputil::multiply_add(kd, MLOG10_2_EXP2_M12_HI, x); // exact - double t2 = kd * MLOG10_2_EXP2_M12_MID_32; // exact - double t3 = kd * MLOG10_2_EXP2_M12_LO; // Error < 2^-140 - - DoubleDouble dx = fputil::exact_add(t1, t2); - dx.lo += t3; - - // Degree-6 polynomial approximation in double-double precision. - // | p - 10^x | < 2^-103. - DoubleDouble p = poly_approx_dd(dx); - - // Error bounds: 2^-102. - DoubleDouble r = fputil::quick_mult(exp_mid, p); - - return r; -} -#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS - -// When output is denormal. -double exp10_denorm(double x) { - // Range reduction. - double tmp = fputil::multiply_add(x, LOG2_10, 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; - - 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); - - // |dx| < 1.5 * 2^-15 + 2^-31 < 2^-14 - double lo_h = fputil::multiply_add(kd, MLOG10_2_EXP2_M12_HI, x); // exact - double dx = fputil::multiply_add(kd, MLOG10_2_EXP2_M12_MID, lo_h); - - double mid_lo = dx * exp_mid.hi; - - // Approximate (10^dx - 1)/dx ~ 1 + a0*dx + a1*dx^2 + a2*dx^3 + a3*dx^4. - double p = poly_approx_d(dx); - - double lo = fputil::multiply_add(p, mid_lo, exp_mid.lo); - -#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS - return ziv_test_denorm</*SKIP_ZIV_TEST=*/true>(hi, exp_mid.hi, lo, ERR_D) - .value(); -#else - if (auto r = ziv_test_denorm(hi, exp_mid.hi, lo, ERR_D); - LIBC_LIKELY(r.has_value())) - return r.value(); - - // Use double-double - DoubleDouble r_dd = exp10_double_double(x, kd, exp_mid); - - if (auto r = ziv_test_denorm(hi, r_dd.hi, r_dd.lo, ERR_DD); - LIBC_LIKELY(r.has_value())) - return r.value(); - - // Use 128-bit precision - Float128 r_f128 = exp10_f128(x, kd, idx1, idx2); - - return static_cast<double>(r_f128); -#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS -} - -// Check for exceptional cases when: -// * log10(1 - 2^-54) < x < log10(1 + 2^-53) -// * x >= log10(2^1024) -// * x <= log10(2^-1022) -// * x is inf or nan -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(); - - // |x| < log10(1 + 2^-53) - if (x_abs <= 0x3c8bcb7b1526e50e) { - // 10^(x) ~ 1 + x/2 - return fputil::multiply_add(x, 0.5, 1.0); - } - - // x <= log10(2^-1022) || x >= log10(2^1024) or inf/nan. - if (x_u >= 0xc0733a7146f72a42) { - // x <= log10(2^-1075) or -inf/nan - if (x_u > 0xc07439b746e36b52) { - // exp(-Inf) = 0 - if (xbits.is_inf()) - return 0.0; - - // exp(nan) = nan - 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; - } - - return exp10_denorm(x); - } - - // x >= log10(2^1024) or +inf/nan - // x is finite - 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); - } - // x is +inf or nan - return x + FPBits::inf().get_val(); -} - -} // namespace - -LLVM_LIBC_FUNCTION(double, exp10, (double x)) { - using FPBits = typename fputil::FPBits<double>; - FPBits xbits(x); - - uint64_t x_u = xbits.uintval(); - - // x <= log10(2^-1022) or x >= log10(2^1024) or - // log10(1 - 2^-54) < x < log10(1 + 2^-53). - if (LIBC_UNLIKELY(x_u >= 0xc0733a7146f72a42 || - (x_u <= 0xbc7bcb7b1526e50e && x_u >= 0x40734413509f79ff) || - x_u < 0x3c8bcb7b1526e50e)) { - return set_exceptional(x); - } - - // Now log10(2^-1075) < x <= log10(1 - 2^-54) or - // log10(1 + 2^-53) < x < log10(2^1024) - - // Range reduction: - // Let x = log10(2) * (hi + mid1 + mid2) + lo - // in which: - // hi is an integer - // mid1 * 2^6 is an integer - // mid2 * 2^12 is an integer - // then: - // 10^(x) = 2^hi * 2^(mid1) * 2^(mid2) * 10^(lo). - // With this formula: - // - multiplying by 2^hi is exact and cheap, simply by adding the exponent - // field. - // - 2^(mid1) and 2^(mid2) are stored in 2 x 64-element tables. - // - 10^(lo) ~ 1 + a0*lo + a1 * lo^2 + ... - // - // We compute (hi + mid1 + mid2) together by perform the rounding on - // x * log2(10) * 2^12. - // Since |x| < |log10(2^-1075)| < 2^9, - // |x * 2^12| < 2^9 * 2^12 < 2^21, - // So we can fit the rounded result round(x * 2^12) in int32_t. - // Thus, the goal is to be able to use an additional addition and fixed width - // shift to get an int32_t representing round(x * 2^12). - // - // Assuming int32_t using 2-complement representation, since the mantissa part - // of a double precision is unsigned with the leading bit hidden, if we add an - // extra constant C = 2^e1 + 2^e2 with e1 > e2 >= 2^23 to the product, the - // part that are < 2^e2 in resulted mantissa of (x*2^12*L2E + C) can be - // considered as a proper 2-complement representations of x*2^12. - // - // One small problem with this approach is that the sum (x*2^12 + C) in - // double precision is rounded to the least significant bit of the dorminant - // factor C. In order to minimize the rounding errors from this addition, we - // want to minimize e1. Another constraint that we want is that after - // shifting the mantissa so that the least significant bit of int32_t - // corresponds to the unit bit of (x*2^12*L2E), the sign is correct without - // any adjustment. So combining these 2 requirements, we can choose - // C = 2^33 + 2^32, so that the sign bit corresponds to 2^31 bit, and hence - // after right shifting the mantissa, the resulting int32_t has correct sign. - // With this choice of C, the number of mantissa bits we need to shift to the - // right is: 52 - 33 = 19. - // - // Moreover, since the integer right shifts are equivalent to rounding down, - // we can add an extra 0.5 so that it will become round-to-nearest, tie-to- - // +infinity. So in particular, we can compute: - // hmm = x * 2^12 + C, - // where C = 2^33 + 2^32 + 2^-1, then if - // k = int32_t(lower 51 bits of double(x * 2^12 + C) >> 19), - // the reduced argument: - // lo = x - log10(2) * 2^-12 * k is bounded by: - // |lo| = |x - log10(2) * 2^-12 * k| - // = log10(2) * 2^-12 * | x * log2(10) * 2^12 - k | - // <= log10(2) * 2^-12 * (2^-1 + 2^-19) - // < 1.5 * 2^-2 * (2^-13 + 2^-31) - // = 1.5 * (2^-15 * 2^-31) - // - // Finally, notice that k only uses the mantissa of x * 2^12, so the - // exponent 2^12 is not needed. So we can simply define - // C = 2^(33 - 12) + 2^(32 - 12) + 2^(-13 - 12), and - // k = int32_t(lower 51 bits of double(x + C) >> 19). - - // Rounding errors <= 2^-31. - double tmp = fputil::multiply_add(x, LOG2_10, 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; - - 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); - - // |dx| < 1.5 * 2^-15 + 2^-31 < 2^-14 - double lo_h = fputil::multiply_add(kd, MLOG10_2_EXP2_M12_HI, x); // exact - double dx = fputil::multiply_add(kd, MLOG10_2_EXP2_M12_MID, lo_h); - - // We use the degree-4 polynomial to approximate 10^(lo): - // 10^(lo) ~ 1 + a0 * lo + a1 * lo^2 + a2 * lo^3 + a3 * lo^4 - // = 1 + lo * P(lo) - // So that the errors are bounded by: - // |P(lo) - (10^lo - 1)/lo| < |lo|^4 / 64 < 2^(-13 * 4) / 64 = 2^-58 - // Let P_ be an evaluation of P where all intermediate computations are in - // double precision. Using either Horner's or Estrin's schemes, the evaluated - // errors can be bounded by: - // |P_(lo) - P(lo)| < 2^-51 - // => |lo * P_(lo) - (2^lo - 1) | < 2^-65 - // => 2^(mid1 + mid2) * |lo * P_(lo) - expm1(lo)| < 2^-64. - // Since we approximate - // 2^(mid1 + mid2) ~ exp_mid.hi + exp_mid.lo, - // We use the expression: - // (exp_mid.hi + exp_mid.lo) * (1 + dx * P_(dx)) ~ - // ~ exp_mid.hi + (exp_mid.hi * dx * P_(dx) + exp_mid.lo) - // with errors bounded by 2^-64. - - double mid_lo = dx * exp_mid.hi; - - // Approximate (10^dx - 1)/dx ~ 1 + a0*dx + a1*dx^2 + a2*dx^3 + a3*dx^4. - double p = poly_approx_d(dx); - - double lo = fputil::multiply_add(p, mid_lo, exp_mid.lo); - -#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS - 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 - double upper = exp_mid.hi + (lo + ERR_D); - double lower = exp_mid.hi + (lo - ERR_D); - - if (LIBC_LIKELY(upper == lower)) { - // To multiply by 2^hi, a fast way is to simply add hi to the exponent - // field. - 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; - } - - // Exact outputs when x = 1, 2, ..., 22 + hard to round with x = 23. - // Quick check mask: 0x800f'ffffU = ~(bits of 1.0 | ... | bits of 23.0) - if (LIBC_UNLIKELY((x_u & 0x8000'ffff'ffff'ffffULL) == 0ULL)) { - switch (x_u) { - case 0x3ff0000000000000: // x = 1.0 - return 10.0; - case 0x4000000000000000: // x = 2.0 - return 100.0; - case 0x4008000000000000: // x = 3.0 - return 1'000.0; - case 0x4010000000000000: // x = 4.0 - return 10'000.0; - case 0x4014000000000000: // x = 5.0 - return 100'000.0; - case 0x4018000000000000: // x = 6.0 - return 1'000'000.0; - case 0x401c000000000000: // x = 7.0 - return 10'000'000.0; - case 0x4020000000000000: // x = 8.0 - return 100'000'000.0; - case 0x4022000000000000: // x = 9.0 - return 1'000'000'000.0; - case 0x4024000000000000: // x = 10.0 - return 10'000'000'000.0; - case 0x4026000000000000: // x = 11.0 - return 100'000'000'000.0; - case 0x4028000000000000: // x = 12.0 - return 1'000'000'000'000.0; - case 0x402a000000000000: // x = 13.0 - return 10'000'000'000'000.0; - case 0x402c000000000000: // x = 14.0 - return 100'000'000'000'000.0; - case 0x402e000000000000: // x = 15.0 - return 1'000'000'000'000'000.0; - case 0x4030000000000000: // x = 16.0 - return 10'000'000'000'000'000.0; - case 0x4031000000000000: // x = 17.0 - return 100'000'000'000'000'000.0; - case 0x4032000000000000: // x = 18.0 - return 1'000'000'000'000'000'000.0; - case 0x4033000000000000: // x = 19.0 - return 10'000'000'000'000'000'000.0; - case 0x4034000000000000: // x = 20.0 - return 100'000'000'000'000'000'000.0; - case 0x4035000000000000: // x = 21.0 - return 1'000'000'000'000'000'000'000.0; - case 0x4036000000000000: // x = 22.0 - return 10'000'000'000'000'000'000'000.0; - case 0x4037000000000000: // x = 23.0 - return 0x1.52d02c7e14af6p76 + x; - } - } - - // Use double-double - DoubleDouble r_dd = exp10_double_double(x, kd, exp_mid); - - 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)) { - // To multiply by 2^hi, a fast way is to simply add hi to the exponent - // field. - 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; - } - - // Use 128-bit precision - Float128 r_f128 = exp10_f128(x, kd, idx1, idx2); - - return static_cast<double>(r_f128); -#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS -} +LLVM_LIBC_FUNCTION(double, exp10, (double x)) { return math::exp10(x); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/exp10f.cpp b/libc/src/math/generic/exp10f.cpp index 5284c380f52e..b2d4f097bc7c 100644 --- a/libc/src/math/generic/exp10f.cpp +++ b/libc/src/math/generic/exp10f.cpp @@ -7,12 +7,11 @@ //===----------------------------------------------------------------------===// #include "src/math/exp10f.h" -#include "src/__support/common.h" -#include "src/__support/macros/config.h" -#include "src/math/generic/exp10f_impl.h" + +#include "src/__support/math/exp10f.h" namespace LIBC_NAMESPACE_DECL { -LLVM_LIBC_FUNCTION(float, exp10f, (float x)) { return generic::exp10f(x); } +LLVM_LIBC_FUNCTION(float, exp10f, (float x)) { return math::exp10f(x); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/exp10f16.cpp b/libc/src/math/generic/exp10f16.cpp index 31abf3b4f89b..cb3c8599c923 100644 --- a/libc/src/math/generic/exp10f16.cpp +++ b/libc/src/math/generic/exp10f16.cpp @@ -7,128 +7,10 @@ //===----------------------------------------------------------------------===// #include "src/math/exp10f16.h" -#include "expxf16.h" -#include "hdr/errno_macros.h" -#include "hdr/fenv_macros.h" -#include "src/__support/CPP/array.h" -#include "src/__support/FPUtil/FEnvImpl.h" -#include "src/__support/FPUtil/FPBits.h" -#include "src/__support/FPUtil/PolyEval.h" -#include "src/__support/FPUtil/cast.h" -#include "src/__support/FPUtil/except_value_utils.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/common.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/exp10f16.h" namespace LIBC_NAMESPACE_DECL { -#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS -#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT -static constexpr size_t N_EXP10F16_EXCEPTS = 5; -#else -static constexpr size_t N_EXP10F16_EXCEPTS = 8; -#endif - -static constexpr fputil::ExceptValues<float16, N_EXP10F16_EXCEPTS> - EXP10F16_EXCEPTS = {{ - // x = 0x1.8f4p-2, exp10f16(x) = 0x1.3ap+1 (RZ) - {0x363dU, 0x40e8U, 1U, 0U, 1U}, - // x = 0x1.95cp-2, exp10f16(x) = 0x1.3ecp+1 (RZ) - {0x3657U, 0x40fbU, 1U, 0U, 0U}, - // x = -0x1.018p-4, exp10f16(x) = 0x1.bbp-1 (RZ) - {0xac06U, 0x3aecU, 1U, 0U, 0U}, - // x = -0x1.c28p+0, exp10f16(x) = 0x1.1ccp-6 (RZ) - {0xbf0aU, 0x2473U, 1U, 0U, 0U}, - // x = -0x1.e1cp+1, exp10f16(x) = 0x1.694p-13 (RZ) - {0xc387U, 0x09a5U, 1U, 0U, 0U}, -#ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT - // x = 0x1.0cp+1, exp10f16(x) = 0x1.f04p+6 (RZ) - {0x4030U, 0x57c1U, 1U, 0U, 1U}, - // x = 0x1.1b8p+1, exp10f16(x) = 0x1.47cp+7 (RZ) - {0x406eU, 0x591fU, 1U, 0U, 1U}, - // x = 0x1.1b8p+2, exp10f16(x) = 0x1.a4p+14 (RZ) - {0x446eU, 0x7690U, 1U, 0U, 1U}, -#endif - }}; -#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS - -LLVM_LIBC_FUNCTION(float16, exp10f16, (float16 x)) { - using FPBits = fputil::FPBits<float16>; - FPBits x_bits(x); - - uint16_t x_u = x_bits.uintval(); - uint16_t x_abs = x_u & 0x7fffU; - - // When |x| >= 5, or x is NaN. - if (LIBC_UNLIKELY(x_abs >= 0x4500U)) { - // exp10(NaN) = NaN - if (x_bits.is_nan()) { - if (x_bits.is_signaling_nan()) { - fputil::raise_except_if_required(FE_INVALID); - return FPBits::quiet_nan().get_val(); - } - - return x; - } - - // When x >= 5. - if (x_bits.is_pos()) { - // exp10(+inf) = +inf - if (x_bits.is_inf()) - return FPBits::inf().get_val(); - - switch (fputil::quick_get_round()) { - case FE_TONEAREST: - case FE_UPWARD: - fputil::set_errno_if_required(ERANGE); - fputil::raise_except_if_required(FE_OVERFLOW); - return FPBits::inf().get_val(); - default: - return FPBits::max_normal().get_val(); - } - } - - // When x <= -8. - if (x_u >= 0xc800U) { - // exp10(-inf) = +0 - if (x_bits.is_inf()) - return FPBits::zero().get_val(); - - fputil::set_errno_if_required(ERANGE); - fputil::raise_except_if_required(FE_UNDERFLOW | FE_INEXACT); - - if (fputil::fenv_is_round_up()) - return FPBits::min_subnormal().get_val(); - return FPBits::zero().get_val(); - } - } - - // When x is 1, 2, 3, or 4. These are hard-to-round cases with exact results. - if (LIBC_UNLIKELY((x_u & ~(0x3c00U | 0x4000U | 0x4200U | 0x4400U)) == 0)) { - switch (x_u) { - case 0x3c00U: // x = 1.0f16 - return fputil::cast<float16>(10.0); - case 0x4000U: // x = 2.0f16 - return fputil::cast<float16>(100.0); - case 0x4200U: // x = 3.0f16 - return fputil::cast<float16>(1'000.0); - case 0x4400U: // x = 4.0f16 - return fputil::cast<float16>(10'000.0); - } - } - -#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS - if (auto r = EXP10F16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value())) - return r.value(); -#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS - - // 10^x = 2^((hi + mid) * log2(10)) * 10^lo - auto [exp2_hi_mid, exp10_lo] = exp10_range_reduction(x); - return fputil::cast<float16>(exp2_hi_mid * exp10_lo); -} +LLVM_LIBC_FUNCTION(float16, exp10f16, (float16 x)) { return math::exp10f16(x); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/exp10m1f16.cpp b/libc/src/math/generic/exp10m1f16.cpp index 545c47969481..6c2fdbea418d 100644 --- a/libc/src/math/generic/exp10m1f16.cpp +++ b/libc/src/math/generic/exp10m1f16.cpp @@ -7,7 +7,6 @@ //===----------------------------------------------------------------------===// #include "src/math/exp10m1f16.h" -#include "expxf16.h" #include "hdr/errno_macros.h" #include "hdr/fenv_macros.h" #include "src/__support/FPUtil/FEnvImpl.h" @@ -21,6 +20,7 @@ #include "src/__support/macros/config.h" #include "src/__support/macros/optimization.h" #include "src/__support/macros/properties/cpu_features.h" +#include "src/__support/math/exp10f16_utils.h" namespace LIBC_NAMESPACE_DECL { diff --git a/libc/src/math/generic/explogxf.cpp b/libc/src/math/generic/explogxf.cpp deleted file mode 100644 index d38efa026969..000000000000 --- a/libc/src/math/generic/explogxf.cpp +++ /dev/null @@ -1,75 +0,0 @@ -//===-- Single-precision general exp/log functions ------------------------===// -// -// 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 -// -//===----------------------------------------------------------------------===// - -#include "explogxf.h" -#include "src/__support/macros/config.h" - -namespace LIBC_NAMESPACE_DECL { - -// N[Table[Log[2, 1 + x], {x, 0/64, 63/64, 1/64}], 40] -alignas(8) const double LOG_P1_LOG2[LOG_P1_SIZE] = { - 0x0.0000000000000p+0, 0x1.6e79685c2d22ap-6, 0x1.6bad3758efd87p-5, - 0x1.0eb389fa29f9bp-4, 0x1.663f6fac91316p-4, 0x1.bc84240adabbap-4, - 0x1.08c588cda79e4p-3, 0x1.32ae9e278ae1ap-3, 0x1.5c01a39fbd688p-3, - 0x1.84c2bd02f03b3p-3, 0x1.acf5e2db4ec94p-3, 0x1.d49ee4c325970p-3, - 0x1.fbc16b902680ap-3, 0x1.11307dad30b76p-2, 0x1.24407ab0e073ap-2, - 0x1.37124cea4cdedp-2, 0x1.49a784bcd1b8bp-2, 0x1.5c01a39fbd688p-2, - 0x1.6e221cd9d0cdep-2, 0x1.800a563161c54p-2, 0x1.91bba891f1709p-2, - 0x1.a33760a7f6051p-2, 0x1.b47ebf73882a1p-2, 0x1.c592fad295b56p-2, - 0x1.d6753e032ea0fp-2, 0x1.e726aa1e754d2p-2, 0x1.f7a8568cb06cfp-2, - 0x1.03fda8b97997fp-1, 0x1.0c10500d63aa6p-1, 0x1.140c9faa1e544p-1, - 0x1.1bf311e95d00ep-1, 0x1.23c41d42727c8p-1, 0x1.2b803473f7ad1p-1, - 0x1.3327c6ab49ca7p-1, 0x1.3abb3faa02167p-1, 0x1.423b07e986aa9p-1, - 0x1.49a784bcd1b8bp-1, 0x1.510118708a8f9p-1, 0x1.5848226989d34p-1, - 0x1.5f7cff41e09afp-1, 0x1.66a008e4788ccp-1, 0x1.6db196a76194ap-1, - 0x1.74b1fd64e0754p-1, 0x1.7ba18f93502e4p-1, 0x1.82809d5be7073p-1, - 0x1.894f74b06ef8bp-1, 0x1.900e6160002cdp-1, 0x1.96bdad2acb5f6p-1, - 0x1.9d5d9fd5010b3p-1, 0x1.a3ee7f38e181fp-1, 0x1.aa708f58014d3p-1, - 0x1.b0e4126bcc86cp-1, 0x1.b74948f5532dap-1, 0x1.bda071cc67e6ep-1, - 0x1.c3e9ca2e1a055p-1, 0x1.ca258dca93316p-1, 0x1.d053f6d260896p-1, - 0x1.d6753e032ea0fp-1, 0x1.dc899ab3ff56cp-1, 0x1.e29142e0e0140p-1, - 0x1.e88c6b3626a73p-1, 0x1.ee7b471b3a950p-1, 0x1.f45e08bcf0655p-1, - 0x1.fa34e1177c233p-1, -}; - -// N[Table[1/(1 + x), {x, 0/64, 63/64, 1/64}], 40] -alignas(8) const double LOG_P1_1_OVER[LOG_P1_SIZE] = { - 0x1.0000000000000p+0, 0x1.f81f81f81f820p-1, 0x1.f07c1f07c1f08p-1, - 0x1.e9131abf0b767p-1, 0x1.e1e1e1e1e1e1ep-1, 0x1.dae6076b981dbp-1, - 0x1.d41d41d41d41dp-1, 0x1.cd85689039b0bp-1, 0x1.c71c71c71c71cp-1, - 0x1.c0e070381c0e0p-1, 0x1.bacf914c1bad0p-1, 0x1.b4e81b4e81b4fp-1, - 0x1.af286bca1af28p-1, 0x1.a98ef606a63bep-1, 0x1.a41a41a41a41ap-1, - 0x1.9ec8e951033d9p-1, 0x1.999999999999ap-1, 0x1.948b0fcd6e9e0p-1, - 0x1.8f9c18f9c18fap-1, 0x1.8acb90f6bf3aap-1, 0x1.8618618618618p-1, - 0x1.8181818181818p-1, 0x1.7d05f417d05f4p-1, 0x1.78a4c8178a4c8p-1, - 0x1.745d1745d1746p-1, 0x1.702e05c0b8170p-1, 0x1.6c16c16c16c17p-1, - 0x1.6816816816817p-1, 0x1.642c8590b2164p-1, 0x1.6058160581606p-1, - 0x1.5c9882b931057p-1, 0x1.58ed2308158edp-1, 0x1.5555555555555p-1, - 0x1.51d07eae2f815p-1, 0x1.4e5e0a72f0539p-1, 0x1.4afd6a052bf5bp-1, - 0x1.47ae147ae147bp-1, 0x1.446f86562d9fbp-1, 0x1.4141414141414p-1, - 0x1.3e22cbce4a902p-1, 0x1.3b13b13b13b14p-1, 0x1.3813813813814p-1, - 0x1.3521cfb2b78c1p-1, 0x1.323e34a2b10bfp-1, 0x1.2f684bda12f68p-1, - 0x1.2c9fb4d812ca0p-1, 0x1.29e4129e4129ep-1, 0x1.27350b8812735p-1, - 0x1.2492492492492p-1, 0x1.21fb78121fb78p-1, 0x1.1f7047dc11f70p-1, - 0x1.1cf06ada2811dp-1, 0x1.1a7b9611a7b96p-1, 0x1.1811811811812p-1, - 0x1.15b1e5f75270dp-1, 0x1.135c81135c811p-1, 0x1.1111111111111p-1, - 0x1.0ecf56be69c90p-1, 0x1.0c9714fbcda3bp-1, 0x1.0a6810a6810a7p-1, - 0x1.0842108421084p-1, 0x1.0624dd2f1a9fcp-1, 0x1.0410410410410p-1, - 0x1.0204081020408p-1}; - -// Taylos series expansion for Log[2, 1 + x] splitted to EVEN AND ODD numbers -// K_LOG2_ODD starts from x^3 -alignas(8) const - double K_LOG2_ODD[4] = {0x1.ec709dc3a03fdp-2, 0x1.2776c50ef9bfep-2, - 0x1.a61762a7aded9p-3, 0x1.484b13d7c02a9p-3}; - -alignas(8) const - double K_LOG2_EVEN[4] = {-0x1.71547652b82fep-1, -0x1.71547652b82fep-2, - -0x1.ec709dc3a03fdp-3, -0x1.2776c50ef9bfep-3}; - -} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/explogxf.h b/libc/src/math/generic/explogxf.h index 212ede475854..be4328a4f48b 100644 --- a/libc/src/math/generic/explogxf.h +++ b/libc/src/math/generic/explogxf.h @@ -10,167 +10,17 @@ #define LLVM_LIBC_SRC_MATH_GENERIC_EXPLOGXF_H #include "common_constants.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/nearest_integer.h" + #include "src/__support/common.h" -#include "src/__support/macros/config.h" #include "src/__support/macros/properties/cpu_features.h" +#include "src/__support/math/exp10f_utils.h" +#include "src/__support/math/exp_utils.h" namespace LIBC_NAMESPACE_DECL { -struct ExpBase { - // Base = e - static constexpr int MID_BITS = 5; - static constexpr int MID_MASK = (1 << MID_BITS) - 1; - // log2(e) * 2^5 - static constexpr double LOG2_B = 0x1.71547652b82fep+0 * (1 << MID_BITS); - // High and low parts of -log(2) * 2^(-5) - static constexpr double M_LOGB_2_HI = -0x1.62e42fefa0000p-1 / (1 << MID_BITS); - static constexpr double M_LOGB_2_LO = - -0x1.cf79abc9e3b3ap-40 / (1 << MID_BITS); - // Look up table for bit fields of 2^(i/32) for i = 0..31, generated by Sollya - // with: - // > for i from 0 to 31 do printdouble(round(2^(i/32), D, RN)); - static constexpr int64_t EXP_2_MID[1 << MID_BITS] = { - 0x3ff0000000000000, 0x3ff059b0d3158574, 0x3ff0b5586cf9890f, - 0x3ff11301d0125b51, 0x3ff172b83c7d517b, 0x3ff1d4873168b9aa, - 0x3ff2387a6e756238, 0x3ff29e9df51fdee1, 0x3ff306fe0a31b715, - 0x3ff371a7373aa9cb, 0x3ff3dea64c123422, 0x3ff44e086061892d, - 0x3ff4bfdad5362a27, 0x3ff5342b569d4f82, 0x3ff5ab07dd485429, - 0x3ff6247eb03a5585, 0x3ff6a09e667f3bcd, 0x3ff71f75e8ec5f74, - 0x3ff7a11473eb0187, 0x3ff82589994cce13, 0x3ff8ace5422aa0db, - 0x3ff93737b0cdc5e5, 0x3ff9c49182a3f090, 0x3ffa5503b23e255d, - 0x3ffae89f995ad3ad, 0x3ffb7f76f2fb5e47, 0x3ffc199bdd85529c, - 0x3ffcb720dcef9069, 0x3ffd5818dcfba487, 0x3ffdfc97337b9b5f, - 0x3ffea4afa2a490da, 0x3fff50765b6e4540, - }; - - // Approximating e^dx with degree-5 minimax polynomial generated by Sollya: - // > Q = fpminimax(expm1(x)/x, 4, [|1, D...|], [-log(2)/64, log(2)/64]); - // Then: - // e^dx ~ P(dx) = 1 + dx + COEFFS[0] * dx^2 + ... + COEFFS[3] * dx^5. - static constexpr double COEFFS[4] = { - 0x1.ffffffffe5bc8p-2, 0x1.555555555cd67p-3, 0x1.5555c2a9b48b4p-5, - 0x1.11112a0e34bdbp-7}; - - LIBC_INLINE static double powb_lo(double dx) { - using fputil::multiply_add; - double dx2 = dx * dx; - double c0 = 1.0 + dx; - // c1 = COEFFS[0] + COEFFS[1] * dx - double c1 = multiply_add(dx, ExpBase::COEFFS[1], ExpBase::COEFFS[0]); - // c2 = COEFFS[2] + COEFFS[3] * dx - double c2 = multiply_add(dx, ExpBase::COEFFS[3], ExpBase::COEFFS[2]); - // r = c4 + c5 * dx^4 - // = 1 + dx + COEFFS[0] * dx^2 + ... + COEFFS[5] * dx^7 - return fputil::polyeval(dx2, c0, c1, c2); - } -}; - -struct Exp10Base : public ExpBase { - // log2(10) * 2^5 - static constexpr double LOG2_B = 0x1.a934f0979a371p1 * (1 << MID_BITS); - // High and low parts of -log10(2) * 2^(-5). - // Notice that since |x * log2(10)| < 150: - // |k| = |round(x * log2(10) * 2^5)| < 2^8 * 2^5 = 2^13 - // So when the FMA instructions are not available, in order for the product - // k * M_LOGB_2_HI - // to be exact, we only store the high part of log10(2) up to 38 bits - // (= 53 - 15) of precision. - // It is generated by Sollya with: - // > round(log10(2), 44, RN); - static constexpr double M_LOGB_2_HI = -0x1.34413509f8p-2 / (1 << MID_BITS); - // > round(log10(2) - 0x1.34413509f8p-2, D, RN); - static constexpr double M_LOGB_2_LO = 0x1.80433b83b532ap-44 / (1 << MID_BITS); - - // Approximating 10^dx with degree-5 minimax polynomial generated by Sollya: - // > Q = fpminimax((10^x - 1)/x, 4, [|D...|], [-log10(2)/2^6, log10(2)/2^6]); - // Then: - // 10^dx ~ P(dx) = 1 + COEFFS[0] * dx + ... + COEFFS[4] * dx^5. - static constexpr double COEFFS[5] = {0x1.26bb1bbb55515p1, 0x1.53524c73bd3eap1, - 0x1.0470591dff149p1, 0x1.2bd7c0a9fbc4dp0, - 0x1.1429e74a98f43p-1}; - - static double powb_lo(double dx) { - using fputil::multiply_add; - double dx2 = dx * dx; - // c0 = 1 + COEFFS[0] * dx - double c0 = multiply_add(dx, Exp10Base::COEFFS[0], 1.0); - // c1 = COEFFS[1] + COEFFS[2] * dx - double c1 = multiply_add(dx, Exp10Base::COEFFS[2], Exp10Base::COEFFS[1]); - // c2 = COEFFS[3] + COEFFS[4] * dx - double c2 = multiply_add(dx, Exp10Base::COEFFS[4], Exp10Base::COEFFS[3]); - // r = c0 + dx^2 * (c1 + c2 * dx^2) - // = c0 + c1 * dx^2 + c2 * dx^4 - // = 1 + COEFFS[0] * dx + ... + COEFFS[4] * dx^5. - return fputil::polyeval(dx2, c0, c1, c2); - } -}; - constexpr int LOG_P1_BITS = 6; constexpr int LOG_P1_SIZE = 1 << LOG_P1_BITS; -// N[Table[Log[2, 1 + x], {x, 0/64, 63/64, 1/64}], 40] -extern const double LOG_P1_LOG2[LOG_P1_SIZE]; - -// N[Table[1/(1 + x), {x, 0/64, 63/64, 1/64}], 40] -extern const double LOG_P1_1_OVER[LOG_P1_SIZE]; - -// Taylor series expansion for Log[2, 1 + x] splitted to EVEN AND ODD numbers -// K_LOG2_ODD starts from x^3 -extern const double K_LOG2_ODD[4]; -extern const double K_LOG2_EVEN[4]; - -// Output of range reduction for exp_b: (2^(mid + hi), lo) -// where: -// b^x = 2^(mid + hi) * b^lo -struct exp_b_reduc_t { - double mh; // 2^(mid + hi) - double lo; -}; - -// The function correctly calculates b^x value with at least float precision -// in a limited range. -// Range reduction: -// b^x = 2^(hi + mid) * b^lo -// where: -// x = (hi + mid) * log_b(2) + lo -// hi is an integer, -// 0 <= mid * 2^MID_BITS < 2^MID_BITS is an integer -// -2^(-MID_BITS - 1) <= lo * log2(b) <= 2^(-MID_BITS - 1) -// Base class needs to provide the following constants: -// - MID_BITS : number of bits after decimal points used for mid -// - MID_MASK : 2^MID_BITS - 1, mask to extract mid bits -// - LOG2_B : log2(b) * 2^MID_BITS for scaling -// - M_LOGB_2_HI : high part of -log_b(2) * 2^(-MID_BITS) -// - M_LOGB_2_LO : low part of -log_b(2) * 2^(-MID_BITS) -// - EXP_2_MID : look up table for bit fields of 2^mid -// Return: -// { 2^(hi + mid), lo } -template <class Base> LIBC_INLINE exp_b_reduc_t exp_b_range_reduc(float x) { - double xd = static_cast<double>(x); - // kd = round((hi + mid) * log2(b) * 2^MID_BITS) - double kd = fputil::nearest_integer(Base::LOG2_B * xd); - // k = round((hi + mid) * log2(b) * 2^MID_BITS) - int k = static_cast<int>(kd); - // hi = floor(kd * 2^(-MID_BITS)) - // exp_hi = shift hi to the exponent field of double precision. - uint64_t exp_hi = static_cast<uint64_t>(k >> Base::MID_BITS) - << fputil::FPBits<double>::FRACTION_LEN; - // mh = 2^hi * 2^mid - // mh_bits = bit field of mh - uint64_t mh_bits = Base::EXP_2_MID[k & Base::MID_MASK] + exp_hi; - double mh = fputil::FPBits<double>(mh_bits).get_val(); - // dx = lo = x - (hi + mid) * log(2) - double dx = fputil::multiply_add( - kd, Base::M_LOGB_2_LO, fputil::multiply_add(kd, Base::M_LOGB_2_HI, xd)); - return {mh, dx}; -} - // The function correctly calculates sinh(x) and cosh(x) by calculating exp(x) // and exp(-x) simultaneously. // To compute e^x, we perform the following range @@ -271,33 +121,6 @@ template <bool is_sinh> LIBC_INLINE double exp_pm_eval(float x) { } // x should be positive, normal finite value -LIBC_INLINE static double log2_eval(double x) { - using FPB = fputil::FPBits<double>; - FPB bs(x); - - double result = 0; - result += bs.get_exponent(); - - int p1 = (bs.get_mantissa() >> (FPB::FRACTION_LEN - LOG_P1_BITS)) & - (LOG_P1_SIZE - 1); - - bs.set_uintval(bs.uintval() & (FPB::FRACTION_MASK >> LOG_P1_BITS)); - bs.set_biased_exponent(FPB::EXP_BIAS); - double dx = (bs.get_val() - 1.0) * LOG_P1_1_OVER[p1]; - - // Taylor series for log(2,1+x) - double c1 = fputil::multiply_add(dx, K_LOG2_ODD[0], K_LOG2_EVEN[0]); - double c2 = fputil::multiply_add(dx, K_LOG2_ODD[1], K_LOG2_EVEN[1]); - double c3 = fputil::multiply_add(dx, K_LOG2_ODD[2], K_LOG2_EVEN[2]); - double c4 = fputil::multiply_add(dx, K_LOG2_ODD[3], K_LOG2_EVEN[3]); - - // c0 = dx * (1.0 / ln(2)) + LOG_P1_LOG2[p1] - double c0 = fputil::multiply_add(dx, 0x1.71547652b82fep+0, LOG_P1_LOG2[p1]); - result += LIBC_NAMESPACE::fputil::polyeval(dx * dx, c0, c1, c2, c3, c4); - return result; -} - -// x should be positive, normal finite value // TODO: Simplify range reduction and polynomial degree for float16. // See issue #137190. LIBC_INLINE static float log_eval_f(float x) { @@ -375,58 +198,6 @@ LIBC_INLINE static double log_eval(double x) { return result; } -// 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). -// So if we scale x up by 2^1022, we can use -// double(1.0 + 2^1022 * x) - 1.0 to test how x is rounded in denormal range. -template <bool SKIP_ZIV_TEST = false> -LIBC_INLINE static cpp::optional<double> -ziv_test_denorm(int hi, double mid, double lo, double err) { - using FPBits = typename fputil::FPBits<double>; - - // Scaling factor = 1/(min normal number) = 2^1022 - int64_t exp_hi = static_cast<int64_t>(hi + 1022) << FPBits::FRACTION_LEN; - double mid_hi = cpp::bit_cast<double>(exp_hi + cpp::bit_cast<int64_t>(mid)); - double lo_scaled = - (lo != 0.0) ? cpp::bit_cast<double>(exp_hi + cpp::bit_cast<int64_t>(lo)) - : 0.0; - - double extra_factor = 0.0; - uint64_t scale_down = 0x3FE0'0000'0000'0000; // 1022 in the exponent field. - - // Result is denormal if (mid_hi + lo_scale < 1.0). - if ((1.0 - mid_hi) > lo_scaled) { - // Extra rounding step is needed, which adds more rounding errors. - err += 0x1.0p-52; - extra_factor = 1.0; - scale_down = 0x3FF0'0000'0000'0000; // 1023 in the exponent field. - } - - // By adding 1.0, the results will have similar rounding points as denormal - // outputs. - 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 = - cpp::bit_cast<double>(exp_hi + cpp::bit_cast<int64_t>(err)); - - 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; - } -} - } // namespace LIBC_NAMESPACE_DECL #endif // LLVM_LIBC_SRC_MATH_GENERIC_EXPLOGXF_H diff --git a/libc/src/math/generic/expxf16.h b/libc/src/math/generic/expxf16.h index 05ac95d58682..b17b14fa2d75 100644 --- a/libc/src/math/generic/expxf16.h +++ b/libc/src/math/generic/expxf16.h @@ -17,18 +17,11 @@ #include "src/__support/macros/config.h" #include <stdint.h> +#include "src/__support/math/exp10_float16_constants.h" #include "src/__support/math/expf16_utils.h" namespace LIBC_NAMESPACE_DECL { -// Generated by Sollya with the following commands: -// > display = hexadecimal; -// > for i from 0 to 7 do printsingle(round(2^(i * 2^-3), SG, RN)); -constexpr cpp::array<uint32_t, 8> EXP2_MID_BITS = { - 0x3f80'0000U, 0x3f8b'95c2U, 0x3f98'37f0U, 0x3fa5'fed7U, - 0x3fb5'04f3U, 0x3fc5'672aU, 0x3fd7'44fdU, 0x3fea'c0c7U, -}; - LIBC_INLINE ExpRangeReduction exp2_range_reduction(float16 x) { // For -25 < x < 16, to compute 2^x, we perform the following range reduction: // find hi, mid, lo, such that: @@ -68,53 +61,6 @@ LIBC_INLINE ExpRangeReduction exp2_range_reduction(float16 x) { // Generated by Sollya with the following commands: // > display = hexadecimal; -// > round(log2(10), SG, RN); -static constexpr float LOG2F_10 = 0x1.a934fp+1f; - -// Generated by Sollya with the following commands: -// > display = hexadecimal; -// > round(log10(2), SG, RN); -static constexpr float LOG10F_2 = 0x1.344136p-2f; - -LIBC_INLINE ExpRangeReduction exp10_range_reduction(float16 x) { - // For -8 < x < 5, to compute 10^x, we perform the following range reduction: - // find hi, mid, lo, such that: - // x = (hi + mid) * log2(10) + lo, in which - // hi is an integer, - // mid * 2^3 is an integer, - // -2^(-4) <= lo < 2^(-4). - // In particular, - // hi + mid = round(x * 2^3) * 2^(-3). - // Then, - // 10^x = 10^(hi + mid + lo) = 2^((hi + mid) * log2(10)) + 10^lo - // We store 2^mid in the lookup table EXP2_MID_BITS, and compute 2^hi * 2^mid - // by adding hi to the exponent field of 2^mid. 10^lo is computed using a - // degree-4 minimax polynomial generated by Sollya. - - float xf = x; - float kf = fputil::nearest_integer(xf * (LOG2F_10 * 0x1.0p+3f)); - int x_hi_mid = static_cast<int>(kf); - unsigned x_hi = static_cast<unsigned>(x_hi_mid) >> 3; - unsigned x_mid = static_cast<unsigned>(x_hi_mid) & 0x7; - // lo = x - (hi + mid) = round(x * 2^3 * log2(10)) * log10(2) * (-2^(-3)) + x - float lo = fputil::multiply_add(kf, LOG10F_2 * -0x1.0p-3f, xf); - - uint32_t exp2_hi_mid_bits = - EXP2_MID_BITS[x_mid] + - static_cast<uint32_t>(x_hi << fputil::FPBits<float>::FRACTION_LEN); - float exp2_hi_mid = fputil::FPBits<float>(exp2_hi_mid_bits).get_val(); - // Degree-4 minimax polynomial generated by Sollya with the following - // commands: - // > display = hexadecimal; - // > P = fpminimax((10^x - 1)/x, 3, [|SG...|], [-2^-4, 2^-4]); - // > 1 + x * P; - float exp10_lo = fputil::polyeval(lo, 0x1p+0f, 0x1.26bb14p+1f, 0x1.53526p+1f, - 0x1.04b434p+1f, 0x1.2bcf9ep+0f); - return {exp2_hi_mid, exp10_lo}; -} - -// Generated by Sollya with the following commands: -// > display = hexadecimal; // > round(log2(exp(1)), SG, RN); static constexpr float LOG2F_E = 0x1.715476p+0f; diff --git a/libc/src/math/generic/frexpf.cpp b/libc/src/math/generic/frexpf.cpp index 09227badc341..783880d44ce6 100644 --- a/libc/src/math/generic/frexpf.cpp +++ b/libc/src/math/generic/frexpf.cpp @@ -7,14 +7,13 @@ //===----------------------------------------------------------------------===// #include "src/math/frexpf.h" -#include "src/__support/FPUtil/ManipulationFunctions.h" -#include "src/__support/common.h" -#include "src/__support/macros/config.h" + +#include "src/__support/math/frexpf.h" namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(float, frexpf, (float x, int *exp)) { - return fputil::frexp(x, *exp); + return math::frexpf(x, exp); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/frexpf128.cpp b/libc/src/math/generic/frexpf128.cpp index eb816c476970..55f7afcf4aea 100644 --- a/libc/src/math/generic/frexpf128.cpp +++ b/libc/src/math/generic/frexpf128.cpp @@ -7,14 +7,13 @@ //===----------------------------------------------------------------------===// #include "src/math/frexpf128.h" -#include "src/__support/FPUtil/ManipulationFunctions.h" -#include "src/__support/common.h" -#include "src/__support/macros/config.h" + +#include "src/__support/math/frexpf128.h" namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(float128, frexpf128, (float128 x, int *exp)) { - return fputil::frexp(x, *exp); + return math::frexpf128(x, exp); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/frexpf16.cpp b/libc/src/math/generic/frexpf16.cpp index 4571b0d0ea38..fa918fd7ffc3 100644 --- a/libc/src/math/generic/frexpf16.cpp +++ b/libc/src/math/generic/frexpf16.cpp @@ -7,14 +7,13 @@ //===----------------------------------------------------------------------===// #include "src/math/frexpf16.h" -#include "src/__support/FPUtil/ManipulationFunctions.h" -#include "src/__support/common.h" -#include "src/__support/macros/config.h" + +#include "src/__support/math/frexpf16.h" namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(float16, frexpf16, (float16 x, int *exp)) { - return fputil::frexp(x, *exp); + return math::frexpf16(x, exp); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/ldexpf.cpp b/libc/src/math/generic/ldexpf.cpp index 63c5d219f7a7..c5f30bb725e6 100644 --- a/libc/src/math/generic/ldexpf.cpp +++ b/libc/src/math/generic/ldexpf.cpp @@ -7,14 +7,12 @@ //===----------------------------------------------------------------------===// #include "src/math/ldexpf.h" -#include "src/__support/FPUtil/ManipulationFunctions.h" -#include "src/__support/common.h" -#include "src/__support/macros/config.h" +#include "src/__support/math/ldexpf.h" namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(float, ldexpf, (float x, int exp)) { - return fputil::ldexp(x, exp); + return math::ldexpf(x, exp); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/ldexpf128.cpp b/libc/src/math/generic/ldexpf128.cpp index 03b1a2d2032e..40afa5b285bc 100644 --- a/libc/src/math/generic/ldexpf128.cpp +++ b/libc/src/math/generic/ldexpf128.cpp @@ -7,14 +7,13 @@ //===----------------------------------------------------------------------===// #include "src/math/ldexpf128.h" -#include "src/__support/FPUtil/ManipulationFunctions.h" -#include "src/__support/common.h" -#include "src/__support/macros/config.h" + +#include "src/__support/math/ldexpf128.h" namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(float128, ldexpf128, (float128 x, int exp)) { - return fputil::ldexp(x, exp); + return math::ldexpf128(x, exp); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/ldexpf16.cpp b/libc/src/math/generic/ldexpf16.cpp index caa344b41476..ecf16337ee79 100644 --- a/libc/src/math/generic/ldexpf16.cpp +++ b/libc/src/math/generic/ldexpf16.cpp @@ -7,14 +7,13 @@ //===----------------------------------------------------------------------===// #include "src/math/ldexpf16.h" -#include "src/__support/FPUtil/ManipulationFunctions.h" -#include "src/__support/common.h" -#include "src/__support/macros/config.h" + +#include "src/__support/math/ldexpf16.h" namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(float16, ldexpf16, (float16 x, int exp)) { - return fputil::ldexp(x, exp); + return math::ldexpf16(x, exp); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/powf.cpp b/libc/src/math/generic/powf.cpp index dfdfd5d6d576..a45ef511c9ba 100644 --- a/libc/src/math/generic/powf.cpp +++ b/libc/src/math/generic/powf.cpp @@ -9,20 +9,17 @@ #include "src/math/powf.h" #include "common_constants.h" // Lookup tables EXP_M1 and EXP_M2. #include "src/__support/CPP/bit.h" -#include "src/__support/CPP/optional.h" #include "src/__support/FPUtil/FPBits.h" #include "src/__support/FPUtil/PolyEval.h" #include "src/__support/FPUtil/double_double.h" -#include "src/__support/FPUtil/except_value_utils.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/sqrt.h" // Speedup for powf(x, 1/2) = sqrtf(x) #include "src/__support/common.h" #include "src/__support/macros/config.h" #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY +#include "src/__support/math/exp10f.h" // Speedup for powf(10, y) = exp10f(y) -#include "exp10f_impl.h" // Speedup for powf(10, y) = exp10f(y) #include "exp2f_impl.h" // Speedup for powf(2, y) = exp2f(y) namespace LIBC_NAMESPACE_DECL { @@ -781,7 +778,7 @@ LLVM_LIBC_FUNCTION(float, powf, (float x, float y)) { return generic::exp2f(y); case 0x4120'0000: // x = 10.0f // pow(10, y) = exp10(y) - return generic::exp10f(y); + return math::exp10f(y); #endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS } diff --git a/libc/src/math/generic/sinhf.cpp b/libc/src/math/generic/sinhf.cpp index d6158fd30253..63111f84de14 100644 --- a/libc/src/math/generic/sinhf.cpp +++ b/libc/src/math/generic/sinhf.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "src/math/sinhf.h" +#include "src/__support/FPUtil/FEnvImpl.h" #include "src/__support/FPUtil/FPBits.h" #include "src/__support/FPUtil/rounding_mode.h" #include "src/__support/macros/config.h" diff --git a/libc/src/math/generic/tanpif.cpp b/libc/src/math/generic/tanpif.cpp new file mode 100644 index 000000000000..58d46c9481aa --- /dev/null +++ b/libc/src/math/generic/tanpif.cpp @@ -0,0 +1,106 @@ +//===-- Single-precision tanpi function -----------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "src/math/tanpif.h" +#include "sincosf_utils.h" +#include "src/__support/FPUtil/FEnvImpl.h" +#include "src/__support/FPUtil/FPBits.h" +#include "src/__support/FPUtil/cast.h" +#include "src/__support/FPUtil/except_value_utils.h" +#include "src/__support/FPUtil/multiply_add.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" +#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY + +namespace LIBC_NAMESPACE_DECL { + +#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS +constexpr size_t N_EXCEPTS = 3; + +constexpr fputil::ExceptValues<float, N_EXCEPTS> TANPIF_EXCEPTS{{ + // (input, RZ output, RU offset, RD offset, RN offset) + {0x38F26685, 0x39BE6182, 1, 0, 0}, + {0x3E933802, 0x3FA267DD, 1, 0, 0}, + {0x3F3663FF, 0xBFA267DD, 0, 1, 0}, +}}; +#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS + +LLVM_LIBC_FUNCTION(float, tanpif, (float x)) { + using FPBits = typename fputil::FPBits<float>; + FPBits xbits(x); + + uint32_t x_u = xbits.uintval(); + uint32_t x_abs = x_u & 0x7fff'ffffU; + double xd = static_cast<double>(xbits.get_val()); + + // Handle exceptional values + if (LIBC_UNLIKELY(x_abs <= 0x3F3663FF)) { + if (LIBC_UNLIKELY(x_abs == 0U)) + return x; + +#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS + bool x_sign = x_u >> 31; + + if (auto r = TANPIF_EXCEPTS.lookup_odd(x_abs, x_sign); + LIBC_UNLIKELY(r.has_value())) + return r.value(); +#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS + } + + // Numbers greater or equal to 2^23 are always integers, or infinity, or NaN + if (LIBC_UNLIKELY(x_abs >= 0x4B00'0000)) { + // x is inf or NaN. + if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) { + if (xbits.is_signaling_nan()) { + fputil::raise_except_if_required(FE_INVALID); + return FPBits::quiet_nan().get_val(); + } + + if (x_abs == 0x7f80'0000U) { + fputil::set_errno_if_required(EDOM); + fputil::raise_except_if_required(FE_INVALID); + } + + return x + FPBits::quiet_nan().get_val(); + } + + return FPBits::zero(xbits.sign()).get_val(); + } + + // Range reduction: + // For |x| > 1/32, we perform range reduction as follows: + // Find k and y such that: + // x = (k + y) * 1/32 + // k is an integer + // |y| < 0.5 + // + // This is done by performing: + // k = round(x * 32) + // y = x * 32 - k + // + // Once k and y are computed, we then deduce the answer by the formula: + // tan(x) = sin(x) / cos(x) + // = (sin_y * cos_k + cos_y * sin_k) / (cos_y * cos_k - sin_y * sin_k) + double sin_k, cos_k, sin_y, cosm1_y; + sincospif_eval(xd, sin_k, cos_k, sin_y, cosm1_y); + + if (LIBC_UNLIKELY(sin_y == 0 && cos_k == 0)) { + fputil::set_errno_if_required(EDOM); + fputil::raise_except_if_required(FE_DIVBYZERO); + + int32_t x_mp5_i = static_cast<int32_t>(xd - 0.5); + return FPBits::inf((x_mp5_i & 0x1) ? Sign::NEG : Sign::POS).get_val(); + } + + using fputil::multiply_add; + return fputil::cast<float>( + multiply_add(sin_y, cos_k, multiply_add(cosm1_y, sin_k, sin_k)) / + multiply_add(sin_y, -sin_k, multiply_add(cosm1_y, cos_k, cos_k))); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/tanpif.h b/libc/src/math/tanpif.h new file mode 100644 index 000000000000..59e6dcfa9ff7 --- /dev/null +++ b/libc/src/math/tanpif.h @@ -0,0 +1,20 @@ +//===-- Implementation header for tanpif ------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC_MATH_TANPIF_H +#define LLVM_LIBC_SRC_MATH_TANPIF_H + +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +float tanpif(float x); + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC_MATH_TANPIF_H diff --git a/libc/src/string/memory_utils/CMakeLists.txt b/libc/src/string/memory_utils/CMakeLists.txt index a967247db53f..8ab1c9ff98d2 100644 --- a/libc/src/string/memory_utils/CMakeLists.txt +++ b/libc/src/string/memory_utils/CMakeLists.txt @@ -7,7 +7,9 @@ add_header_library( aarch64/inline_memcpy.h aarch64/inline_memmove.h aarch64/inline_memset.h + arm/common.h arm/inline_memcpy.h + arm/inline_memset.h generic/aligned_access.h generic/byte_per_byte.h inline_bcmp.h diff --git a/libc/src/string/memory_utils/arm/common.h b/libc/src/string/memory_utils/arm/common.h new file mode 100644 index 000000000000..b9f40b64fed9 --- /dev/null +++ b/libc/src/string/memory_utils/arm/common.h @@ -0,0 +1,55 @@ +//===-- Common constants and defines for arm --------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ARM_COMMON_H +#define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ARM_COMMON_H + +#include "src/__support/macros/attributes.h" // LIBC_INLINE_VAR +#include "src/string/memory_utils/utils.h" // CPtr, Ptr, distance_to_align + +#include <stddef.h> // size_t + +// Our minimum supported compiler version does not recognize the standard +// [[likely]] / [[unlikely]] attributes so we use the preprocessor. + +// https://libc.llvm.org/compiler_support.html +// Support for [[likely]] / [[unlikely]] +// [X] GCC 12.2 +// [X] Clang 12 +// [ ] Clang 11 +#define LIBC_ATTR_LIKELY [[likely]] +#define LIBC_ATTR_UNLIKELY [[unlikely]] + +#if defined(LIBC_COMPILER_IS_CLANG) +#if LIBC_COMPILER_CLANG_VER < 1200 +#undef LIBC_ATTR_LIKELY +#undef LIBC_ATTR_UNLIKELY +#define LIBC_ATTR_LIKELY +#define LIBC_ATTR_UNLIKELY +#endif +#endif + +namespace LIBC_NAMESPACE_DECL { + +LIBC_INLINE_VAR constexpr size_t kWordSize = sizeof(uint32_t); + +enum class AssumeAccess { kUnknown, kAligned }; +enum class BlockOp { kFull, kByWord }; + +LIBC_INLINE auto misaligned(CPtr ptr) { + return distance_to_align_down<kWordSize>(ptr); +} + +LIBC_INLINE CPtr bitwise_or(CPtr a, CPtr b) { + return cpp::bit_cast<CPtr>(cpp::bit_cast<uintptr_t>(a) | + cpp::bit_cast<uintptr_t>(b)); +} + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ARM_COMMON_H diff --git a/libc/src/string/memory_utils/arm/inline_memcpy.h b/libc/src/string/memory_utils/arm/inline_memcpy.h index 61efebe29b48..c748048a3e58 100644 --- a/libc/src/string/memory_utils/arm/inline_memcpy.h +++ b/libc/src/string/memory_utils/arm/inline_memcpy.h @@ -5,63 +5,57 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// +// The functions defined in this file give approximate code size. These sizes +// assume the following configuration options: +// - LIBC_CONF_KEEP_FRAME_POINTER = false +// - LIBC_CONF_ENABLE_STRONG_STACK_PROTECTOR = false +// - LIBC_ADD_NULL_CHECKS = false #ifndef LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ARM_INLINE_MEMCPY_H #define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ARM_INLINE_MEMCPY_H +#include "src/__support/CPP/type_traits.h" // always_false #include "src/__support/macros/attributes.h" // LIBC_INLINE #include "src/__support/macros/optimization.h" // LIBC_LOOP_NOUNROLL +#include "src/string/memory_utils/arm/common.h" // LIBC_ATTR_LIKELY, LIBC_ATTR_UNLIKELY #include "src/string/memory_utils/utils.h" // memcpy_inline, distance_to_align #include <stddef.h> // size_t -// https://libc.llvm.org/compiler_support.html -// Support for [[likely]] / [[unlikely]] -// [X] GCC 12.2 -// [X] Clang 12 -// [ ] Clang 11 -#define LIBC_ATTR_LIKELY [[likely]] -#define LIBC_ATTR_UNLIKELY [[unlikely]] - -#if defined(LIBC_COMPILER_IS_CLANG) -#if LIBC_COMPILER_CLANG_VER < 1200 -#undef LIBC_ATTR_LIKELY -#undef LIBC_ATTR_UNLIKELY -#define LIBC_ATTR_LIKELY -#define LIBC_ATTR_UNLIKELY -#endif -#endif - namespace LIBC_NAMESPACE_DECL { namespace { -LIBC_INLINE_VAR constexpr size_t kWordSize = sizeof(uint32_t); - -enum Strategy { - ForceWordLdStChain, - AssumeWordAligned, - AssumeUnaligned, -}; +// Performs a copy of `bytes` byte from `src` to `dst`. This function has the +// semantics of `memcpy` where `src` and `dst` are `__restrict`. The compiler is +// free to use whatever instruction is best for the size and assumed access. +template <size_t bytes, AssumeAccess access> +LIBC_INLINE void copy(void *dst, const void *src) { + if constexpr (access == AssumeAccess::kAligned) { + constexpr size_t alignment = bytes > kWordSize ? kWordSize : bytes; + memcpy_inline<bytes>(assume_aligned<alignment>(dst), + assume_aligned<alignment>(src)); + } else if constexpr (access == AssumeAccess::kUnknown) { + memcpy_inline<bytes>(dst, src); + } else { + static_assert(cpp::always_false<decltype(access)>, "Invalid AssumeAccess"); + } +} -template <size_t bytes, Strategy strategy = AssumeUnaligned> -LIBC_INLINE void copy_and_bump_pointers(Ptr &dst, CPtr &src) { - if constexpr (strategy == AssumeUnaligned) { - memcpy_inline<bytes>(assume_aligned<1>(dst), assume_aligned<1>(src)); - } else if constexpr (strategy == AssumeWordAligned) { - static_assert(bytes >= kWordSize); - memcpy_inline<bytes>(assume_aligned<kWordSize>(dst), - assume_aligned<kWordSize>(src)); - } else if constexpr (strategy == ForceWordLdStChain) { +template <size_t bytes, BlockOp block_op = BlockOp::kFull, + AssumeAccess access = AssumeAccess::kUnknown> +LIBC_INLINE void copy_block_and_bump_pointers(Ptr &dst, CPtr &src) { + if constexpr (block_op == BlockOp::kFull) { + copy<bytes, access>(dst, src); + } else if constexpr (block_op == BlockOp::kByWord) { // We restrict loads/stores to 4 byte to prevent the use of load/store - // multiple (LDM, STM) and load/store double (LDRD, STRD). First, they may - // fault (see notes below) and second, they use more registers which in turn - // adds push/pop instructions in the hot path. + // multiple (LDM, STM) and load/store double (LDRD, STRD). static_assert((bytes % kWordSize == 0) && (bytes >= kWordSize)); LIBC_LOOP_UNROLL - for (size_t i = 0; i < bytes / kWordSize; ++i) { - const size_t offset = i * kWordSize; - memcpy_inline<kWordSize>(dst + offset, src + offset); + for (size_t offset = 0; offset < bytes; offset += kWordSize) { + copy<kWordSize, access>(dst + offset, src + offset); } + } else { + static_assert(cpp::always_false<decltype(block_op)>, "Invalid BlockOp"); } // In the 1, 2, 4 byte copy case, the compiler can fold pointer offsetting // into the load/store instructions. @@ -72,39 +66,27 @@ LIBC_INLINE void copy_and_bump_pointers(Ptr &dst, CPtr &src) { src += bytes; } -LIBC_INLINE void copy_bytes_and_bump_pointers(Ptr &dst, CPtr &src, - const size_t size) { +template <size_t bytes, BlockOp block_op, AssumeAccess access> +LIBC_INLINE void consume_by_block(Ptr &dst, CPtr &src, size_t &size) { LIBC_LOOP_NOUNROLL - for (size_t i = 0; i < size; ++i) - *dst++ = *src++; + for (size_t i = 0; i < size / bytes; ++i) + copy_block_and_bump_pointers<bytes, block_op, access>(dst, src); + size %= bytes; } -template <size_t block_size, Strategy strategy> -LIBC_INLINE void copy_blocks_and_update_args(Ptr &dst, CPtr &src, - size_t &size) { +[[maybe_unused]] LIBC_INLINE void +copy_bytes_and_bump_pointers(Ptr &dst, CPtr &src, size_t size) { LIBC_LOOP_NOUNROLL - for (size_t i = 0; i < size / block_size; ++i) - copy_and_bump_pointers<block_size, strategy>(dst, src); - // Update `size` once at the end instead of once per iteration. - size %= block_size; -} - -LIBC_INLINE CPtr bitwise_or(CPtr a, CPtr b) { - return cpp::bit_cast<CPtr>(cpp::bit_cast<uintptr_t>(a) | - cpp::bit_cast<uintptr_t>(b)); -} - -LIBC_INLINE auto misaligned(CPtr a) { - return distance_to_align_down<kWordSize>(a); + for (size_t i = 0; i < size; ++i) + *dst++ = *src++; } } // namespace -// Implementation for Cortex-M0, M0+, M1. -// Notes: -// - It compiles down to 196 bytes, but 220 bytes when used through `memcpy` -// that also needs to return the `dst` ptr. -// - These cores do not allow for unaligned loads/stores. +// Implementation for Cortex-M0, M0+, M1 cores that do not allow for unaligned +// loads/stores. It compiles down to 208 bytes when used through `memcpy` that +// also needs to return the `dst` ptr. +// Note: // - When `src` and `dst` are coaligned, we start by aligning them and perform // bulk copies. We let the compiler know the pointers are aligned so it can // use load/store multiple (LDM, STM). This significantly increase throughput @@ -121,13 +103,20 @@ LIBC_INLINE auto misaligned(CPtr a) { copy_bytes_and_bump_pointers(dst, src, offset); size -= offset; } + constexpr AssumeAccess kAligned = AssumeAccess::kAligned; const auto src_alignment = distance_to_align_down<kWordSize>(src); if (src_alignment == 0) LIBC_ATTR_LIKELY { // Both `src` and `dst` are now word-aligned. - copy_blocks_and_update_args<64, AssumeWordAligned>(dst, src, size); - copy_blocks_and_update_args<16, AssumeWordAligned>(dst, src, size); - copy_blocks_and_update_args<4, AssumeWordAligned>(dst, src, size); + // We first copy by blocks of 64 bytes, the compiler will use 4 + // load/store multiple (LDM, STM), each of 4 words. This requires more + // registers so additional push/pop are needed but the speedup is worth + // it. + consume_by_block<64, BlockOp::kFull, kAligned>(dst, src, size); + // Then we use blocks of 4 word load/store. + consume_by_block<16, BlockOp::kByWord, kAligned>(dst, src, size); + // Then we use word by word copy. + consume_by_block<4, BlockOp::kByWord, kAligned>(dst, src, size); } else { // `dst` is aligned but `src` is not. @@ -138,7 +127,7 @@ LIBC_INLINE auto misaligned(CPtr a) { src_alignment == 2 ? load_aligned<uint32_t, uint16_t, uint16_t>(src) : load_aligned<uint32_t, uint8_t, uint16_t, uint8_t>(src); - memcpy_inline<kWordSize>(assume_aligned<kWordSize>(dst), &value); + copy<kWordSize, kAligned>(dst, &value); dst += kWordSize; src += kWordSize; size -= kWordSize; @@ -151,17 +140,8 @@ LIBC_INLINE auto misaligned(CPtr a) { } // Implementation for Cortex-M3, M4, M7, M23, M33, M35P, M52 with hardware -// support for unaligned loads and stores. -// Notes: -// - It compiles down to 266 bytes. -// - `dst` and `src` are not `__restrict` to prevent the compiler from -// reordering loads/stores. -// - We keep state variables to a strict minimum to keep everything in the free -// registers and prevent costly push / pop. -// - If unaligned single loads/stores to normal memory are supported, unaligned -// accesses for load/store multiple (LDM, STM) and load/store double (LDRD, -// STRD) instructions are generally not supported and will still fault so we -// make sure to restrict unrolling to word loads/stores. +// support for unaligned loads and stores. It compiles down to 272 bytes when +// used through `memcpy` that also needs to return the `dst` ptr. [[maybe_unused]] LIBC_INLINE void inline_memcpy_arm_mid_end(Ptr dst, CPtr src, size_t size) { if (misaligned(bitwise_or(src, dst))) @@ -169,38 +149,59 @@ LIBC_INLINE auto misaligned(CPtr a) { if (size < 8) LIBC_ATTR_UNLIKELY { if (size & 1) - copy_and_bump_pointers<1>(dst, src); + copy_block_and_bump_pointers<1>(dst, src); if (size & 2) - copy_and_bump_pointers<2>(dst, src); + copy_block_and_bump_pointers<2>(dst, src); if (size & 4) - copy_and_bump_pointers<4>(dst, src); + copy_block_and_bump_pointers<4>(dst, src); return; } if (misaligned(src)) LIBC_ATTR_UNLIKELY { const size_t offset = distance_to_align_up<kWordSize>(dst); if (offset & 1) - copy_and_bump_pointers<1>(dst, src); + copy_block_and_bump_pointers<1>(dst, src); if (offset & 2) - copy_and_bump_pointers<2>(dst, src); + copy_block_and_bump_pointers<2>(dst, src); size -= offset; } } - copy_blocks_and_update_args<64, ForceWordLdStChain>(dst, src, size); - copy_blocks_and_update_args<16, ForceWordLdStChain>(dst, src, size); - copy_blocks_and_update_args<4, AssumeUnaligned>(dst, src, size); + // `dst` and `src` are not necessarily both aligned at that point but this + // implementation assumes hardware support for unaligned loads and stores so + // it is still fast to perform unrolled word by word copy. Note that wider + // accesses through the use of load/store multiple (LDM, STM) and load/store + // double (LDRD, STRD) instructions are generally not supported and can fault. + // By forcing decomposition of 64 bytes copy into word by word copy, the + // compiler uses a load to prefetch the next cache line: + // ldr r3, [r1, #64]! <- prefetch next cache line + // str r3, [r0] + // ldr r3, [r1, #0x4] + // str r3, [r0, #0x4] + // ... + // ldr r3, [r1, #0x3c] + // str r3, [r0, #0x3c] + // This is a bit detrimental for sizes between 64 and 256 (less than 10% + // penalty) but the prefetch yields better throughput for larger copies. + constexpr AssumeAccess kUnknown = AssumeAccess::kUnknown; + consume_by_block<64, BlockOp::kByWord, kUnknown>(dst, src, size); + consume_by_block<16, BlockOp::kByWord, kUnknown>(dst, src, size); + consume_by_block<4, BlockOp::kByWord, kUnknown>(dst, src, size); if (size & 1) - copy_and_bump_pointers<1>(dst, src); + copy_block_and_bump_pointers<1>(dst, src); if (size & 2) - LIBC_ATTR_UNLIKELY - copy_and_bump_pointers<2>(dst, src); + copy_block_and_bump_pointers<2>(dst, src); } -[[maybe_unused]] LIBC_INLINE void inline_memcpy_arm(void *__restrict dst_, - const void *__restrict src_, +[[maybe_unused]] LIBC_INLINE void inline_memcpy_arm(Ptr dst, CPtr src, size_t size) { - Ptr dst = cpp::bit_cast<Ptr>(dst_); - CPtr src = cpp::bit_cast<CPtr>(src_); + // The compiler performs alias analysis and is able to prove that `dst` and + // `src` do not alias by propagating the `__restrict` keyword from the + // `memcpy` prototype. This allows the compiler to merge consecutive + // load/store (LDR, STR) instructions generated in + // `copy_block_and_bump_pointers` with `BlockOp::kByWord` into load/store + // double (LDRD, STRD) instructions, this is is undesirable so we prevent the + // compiler from inferring `__restrict` with the following line. + asm volatile("" : "+r"(dst), "+r"(src)); #ifdef __ARM_FEATURE_UNALIGNED return inline_memcpy_arm_mid_end(dst, src, size); #else @@ -210,8 +211,4 @@ LIBC_INLINE auto misaligned(CPtr a) { } // namespace LIBC_NAMESPACE_DECL -// Cleanup local macros -#undef LIBC_ATTR_LIKELY -#undef LIBC_ATTR_UNLIKELY - #endif // LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ARM_INLINE_MEMCPY_H diff --git a/libc/src/string/memory_utils/arm/inline_memset.h b/libc/src/string/memory_utils/arm/inline_memset.h new file mode 100644 index 000000000000..a7ef9cc7df91 --- /dev/null +++ b/libc/src/string/memory_utils/arm/inline_memset.h @@ -0,0 +1,156 @@ +//===-- Memset implementation for arm ---------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// The functions defined in this file give approximate code size. These sizes +// assume the following configuration options: +// - LIBC_CONF_KEEP_FRAME_POINTER = false +// - LIBC_CONF_ENABLE_STRONG_STACK_PROTECTOR = false +// - LIBC_ADD_NULL_CHECKS = false +#ifndef LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ARM_INLINE_MEMSET_H +#define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ARM_INLINE_MEMSET_H + +#include "src/__support/CPP/type_traits.h" // always_false +#include "src/__support/macros/attributes.h" // LIBC_INLINE +#include "src/__support/macros/optimization.h" // LIBC_LOOP_NOUNROLL +#include "src/string/memory_utils/arm/common.h" // LIBC_ATTR_LIKELY, LIBC_ATTR_UNLIKELY +#include "src/string/memory_utils/utils.h" // memcpy_inline, distance_to_align + +#include <stddef.h> // size_t + +namespace LIBC_NAMESPACE_DECL { + +namespace { + +template <size_t bytes, AssumeAccess access> +LIBC_INLINE void set(void *dst, uint32_t value) { + static_assert(bytes == 1 || bytes == 2 || bytes == 4); + if constexpr (access == AssumeAccess::kAligned) { + constexpr size_t alignment = bytes > kWordSize ? kWordSize : bytes; + memcpy_inline<bytes>(assume_aligned<alignment>(dst), &value); + } else if constexpr (access == AssumeAccess::kUnknown) { + memcpy_inline<bytes>(dst, &value); + } else { + static_assert(cpp::always_false<decltype(access)>, "Invalid AssumeAccess"); + } +} + +template <size_t bytes, AssumeAccess access = AssumeAccess::kUnknown> +LIBC_INLINE void set_block_and_bump_pointers(Ptr &dst, uint32_t value) { + if constexpr (bytes <= kWordSize) { + set<bytes, access>(dst, value); + } else { + static_assert(bytes % kWordSize == 0 && bytes >= kWordSize); + LIBC_LOOP_UNROLL + for (size_t offset = 0; offset < bytes; offset += kWordSize) { + set<kWordSize, access>(dst + offset, value); + } + } + // In the 1, 2, 4 byte set case, the compiler can fold pointer offsetting + // into the store instructions. + // e.g., + // strb r3, [r0], #1 + dst += bytes; +} + +template <size_t bytes, AssumeAccess access> +LIBC_INLINE void consume_by_block(Ptr &dst, uint32_t value, size_t &size) { + LIBC_LOOP_NOUNROLL + for (size_t i = 0; i < size / bytes; ++i) + set_block_and_bump_pointers<bytes, access>(dst, value); + size %= bytes; +} + +[[maybe_unused]] LIBC_INLINE void +set_bytes_and_bump_pointers(Ptr &dst, uint32_t value, size_t size) { + LIBC_LOOP_NOUNROLL + for (size_t i = 0; i < size; ++i) { + set<1, AssumeAccess::kUnknown>(dst++, value); + } +} + +} // namespace + +// Implementation for Cortex-M0, M0+, M1. It compiles down to 140 bytes when +// used through `memset` that also needs to return the `dst` ptr. These cores do +// not allow unaligned stores so all accesses are aligned. +[[maybe_unused]] LIBC_INLINE void +inline_memset_arm_low_end(Ptr dst, uint8_t value, size_t size) { + if (size >= 8) + LIBC_ATTR_LIKELY { + // Align `dst` to word boundary. + if (const size_t offset = distance_to_align_up<kWordSize>(dst)) + LIBC_ATTR_UNLIKELY { + set_bytes_and_bump_pointers(dst, value, offset); + size -= offset; + } + const uint32_t value32 = value * 0x01010101U; // splat value in each byte + consume_by_block<64, AssumeAccess::kAligned>(dst, value32, size); + consume_by_block<16, AssumeAccess::kAligned>(dst, value32, size); + consume_by_block<4, AssumeAccess::kAligned>(dst, value32, size); + } + set_bytes_and_bump_pointers(dst, value, size); +} + +// Implementation for Cortex-M3, M4, M7, M23, M33, M35P, M52 with hardware +// support for unaligned loads and stores. It compiles down to 186 bytes when +// used through `memset` that also needs to return the `dst` ptr. +[[maybe_unused]] LIBC_INLINE void +inline_memset_arm_mid_end(Ptr dst, uint8_t value, size_t size) { + const uint32_t value32 = value * 0x01010101U; // splat value in each byte + if (misaligned(dst)) + LIBC_ATTR_UNLIKELY { + if (size < 8) + LIBC_ATTR_UNLIKELY { + if (size & 1) + set_block_and_bump_pointers<1>(dst, value32); + if (size & 2) + set_block_and_bump_pointers<2>(dst, value32); + if (size & 4) + set_block_and_bump_pointers<4>(dst, value32); + return; + } + const size_t offset = distance_to_align_up<kWordSize>(dst); + if (offset & 1) + set_block_and_bump_pointers<1>(dst, value32); + if (offset & 2) + set_block_and_bump_pointers<2>(dst, value32); + size -= offset; + } + // If we tell the compiler that the stores are aligned it will generate 8 x + // STRD instructions. By not specifying alignment, the compiler conservatively + // uses 16 x STR.W and is able to use the first one to prefetch the + // destination in advance leading to better asymptotic performances. + // str r12, [r3, #64]! <- prefetch next cache line + // str.w r12, [r3, #0x4] + // str.w r12, [r3, #0x8] + // ... + // str.w r12, [r3, #0x38] + // str.w r12, [r3, #0x3c] + consume_by_block<64, AssumeAccess::kUnknown>(dst, value32, size); + // Prefetching does not matter anymore at this scale so using STRD yields + // better results. + consume_by_block<16, AssumeAccess::kAligned>(dst, value32, size); + consume_by_block<4, AssumeAccess::kAligned>(dst, value32, size); + if (size & 1) + set_block_and_bump_pointers<1>(dst, value32); + if (size & 2) + LIBC_ATTR_UNLIKELY + set_block_and_bump_pointers<2>(dst, value32); +} + +[[maybe_unused]] LIBC_INLINE void +inline_memset_arm_dispatch(Ptr dst, uint8_t value, size_t size) { +#ifdef __ARM_FEATURE_UNALIGNED + return inline_memset_arm_mid_end(dst, value, size); +#else + return inline_memset_arm_low_end(dst, value, size); +#endif +} + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ARM_INLINE_MEMCPY_H diff --git a/libc/src/string/memory_utils/inline_memset.h b/libc/src/string/memory_utils/inline_memset.h index fd9c29ea4410..e41bdb626d60 100644 --- a/libc/src/string/memory_utils/inline_memset.h +++ b/libc/src/string/memory_utils/inline_memset.h @@ -18,6 +18,9 @@ #if defined(LIBC_TARGET_ARCH_IS_X86) #include "src/string/memory_utils/x86_64/inline_memset.h" #define LIBC_SRC_STRING_MEMORY_UTILS_MEMSET inline_memset_x86 +#elif defined(LIBC_TARGET_ARCH_IS_ARM) +#include "src/string/memory_utils/arm/inline_memset.h" +#define LIBC_SRC_STRING_MEMORY_UTILS_MEMSET inline_memset_arm_dispatch #elif defined(LIBC_TARGET_ARCH_IS_AARCH64) #include "src/string/memory_utils/aarch64/inline_memset.h" #define LIBC_SRC_STRING_MEMORY_UTILS_MEMSET inline_memset_aarch64_dispatch @@ -34,7 +37,8 @@ namespace LIBC_NAMESPACE_DECL { -LIBC_INLINE static void inline_memset(void *dst, uint8_t value, size_t count) { +[[gnu::flatten]] LIBC_INLINE void inline_memset(void *dst, uint8_t value, + size_t count) { LIBC_SRC_STRING_MEMORY_UTILS_MEMSET(reinterpret_cast<Ptr>(dst), value, count); } diff --git a/libc/src/time/baremetal/CMakeLists.txt b/libc/src/time/baremetal/CMakeLists.txt index bf0d42e265d6..3072c8b14959 100644 --- a/libc/src/time/baremetal/CMakeLists.txt +++ b/libc/src/time/baremetal/CMakeLists.txt @@ -1,4 +1,15 @@ add_entrypoint_object( + clock + SRCS + clock.cpp + HDRS + ../clock.h + DEPENDS + libc.hdr.time_macros + libc.hdr.types.struct_timespec +) + +add_entrypoint_object( timespec_get SRCS timespec_get.cpp diff --git a/libc/src/time/baremetal/clock.cpp b/libc/src/time/baremetal/clock.cpp new file mode 100644 index 000000000000..26b75d782952 --- /dev/null +++ b/libc/src/time/baremetal/clock.cpp @@ -0,0 +1,45 @@ +//===-- Baremetal implementation of the clock function --------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "src/time/clock.h" +#include "hdr/time_macros.h" +#include "hdr/types/struct_timespec.h" +#include "src/__support/CPP/limits.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" +#include "src/__support/time/units.h" + +namespace LIBC_NAMESPACE_DECL { + +extern "C" bool __llvm_libc_timespec_get_active(struct timespec *ts); + +LLVM_LIBC_FUNCTION(clock_t, clock, ()) { + using namespace time_units; + struct timespec ts; + if (!__llvm_libc_timespec_get_active(&ts)) + return clock_t(-1); + + // The above call gets the CPU time in seconds plus nanoseconds. + // The standard requires that we return clock_t(-1) if we cannot represent + // clocks as a clock_t value. + constexpr clock_t CLOCK_SECS_MAX = + cpp::numeric_limits<clock_t>::max() / CLOCKS_PER_SEC; + if (ts.tv_sec > CLOCK_SECS_MAX) + return clock_t(-1); + if (ts.tv_nsec / 1_s_ns > CLOCK_SECS_MAX - ts.tv_sec) + return clock_t(-1); + + // For the integer computation converting tv_nsec to clocks to work + // correctly, we want CLOCKS_PER_SEC to be less than 1000000000. + static_assert(1_s_ns > CLOCKS_PER_SEC, + "Expected CLOCKS_PER_SEC to be less than 1'000'000'000."); + return clock_t(ts.tv_sec * CLOCKS_PER_SEC + + ts.tv_nsec / (1_s_ns / CLOCKS_PER_SEC)); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/wchar/CMakeLists.txt b/libc/src/wchar/CMakeLists.txt index e3bd357a8fd1..7ace1a6ca66b 100644 --- a/libc/src/wchar/CMakeLists.txt +++ b/libc/src/wchar/CMakeLists.txt @@ -46,6 +46,50 @@ add_entrypoint_object( ) add_entrypoint_object( + wcstol + SRCS + wcstol.cpp + HDRS + wcstol.h + DEPENDS + libc.src.errno.errno + libc.src.__support.wcs_to_integer +) + +add_entrypoint_object( + wcstoll + SRCS + wcstoll.cpp + HDRS + wcstoll.h + DEPENDS + libc.src.errno.errno + libc.src.__support.wcs_to_integer +) + +add_entrypoint_object( + wcstoul + SRCS + wcstoul.cpp + HDRS + wcstoul.h + DEPENDS + libc.src.errno.errno + libc.src.__support.wcs_to_integer +) + +add_entrypoint_object( + wcstoull + SRCS + wcstoull.cpp + HDRS + wcstoull.h + DEPENDS + libc.src.errno.errno + libc.src.__support.wcs_to_integer +) + +add_entrypoint_object( wcstok SRCS wcstok.cpp diff --git a/libc/src/wchar/wcstol.cpp b/libc/src/wchar/wcstol.cpp new file mode 100644 index 000000000000..a05718f706df --- /dev/null +++ b/libc/src/wchar/wcstol.cpp @@ -0,0 +1,30 @@ +//===-- Implementation of wcstol ------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "src/wchar/wcstol.h" +#include "src/__support/common.h" +#include "src/__support/libc_errno.h" +#include "src/__support/macros/config.h" +#include "src/__support/wcs_to_integer.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(long, wcstol, + (const wchar_t *__restrict str, wchar_t **__restrict str_end, + int base)) { + auto result = internal::wcstointeger<long>(str, base); + if (result.has_error()) + libc_errno = result.error; + + if (str_end != nullptr) + *str_end = const_cast<wchar_t *>(str + result.parsed_len); + + return result; +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/wchar/wcstol.h b/libc/src/wchar/wcstol.h new file mode 100644 index 000000000000..08acd9717a23 --- /dev/null +++ b/libc/src/wchar/wcstol.h @@ -0,0 +1,22 @@ +//===-- Implementation header for wcstol ------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC_WCHAR_WCSTOL_H +#define LLVM_LIBC_SRC_WCHAR_WCSTOL_H + +#include "hdr/types/wint_t.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +long wcstol(const wchar_t *__restrict str, wchar_t **__restrict str_end, + int base); + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC_WCHAR_WCSTOL_H diff --git a/libc/src/wchar/wcstoll.cpp b/libc/src/wchar/wcstoll.cpp new file mode 100644 index 000000000000..de1299d681cd --- /dev/null +++ b/libc/src/wchar/wcstoll.cpp @@ -0,0 +1,30 @@ +//===-- Implementation of wcstoll -----------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "src/wchar/wcstoll.h" +#include "src/__support/common.h" +#include "src/__support/libc_errno.h" +#include "src/__support/macros/config.h" +#include "src/__support/wcs_to_integer.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(long long, wcstoll, + (const wchar_t *__restrict str, wchar_t **__restrict str_end, + int base)) { + auto result = internal::wcstointeger<long long>(str, base); + if (result.has_error()) + libc_errno = result.error; + + if (str_end != nullptr) + *str_end = const_cast<wchar_t *>(str + result.parsed_len); + + return result; +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/wchar/wcstoll.h b/libc/src/wchar/wcstoll.h new file mode 100644 index 000000000000..627804356949 --- /dev/null +++ b/libc/src/wchar/wcstoll.h @@ -0,0 +1,22 @@ +//===-- Implementation header for wcstoll -----------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC_WCHAR_WCSTOLL_H +#define LLVM_LIBC_SRC_WCHAR_WCSTOLL_H + +#include "hdr/types/wint_t.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +long long wcstoll(const wchar_t *__restrict str, wchar_t **__restrict str_end, + int base); + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC_WCHAR_WCSTOLL_H diff --git a/libc/src/wchar/wcstoul.cpp b/libc/src/wchar/wcstoul.cpp new file mode 100644 index 000000000000..79b8c9b5c9fa --- /dev/null +++ b/libc/src/wchar/wcstoul.cpp @@ -0,0 +1,30 @@ +//===-- Implementation of wcstoul -----------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "src/wchar/wcstoul.h" +#include "src/__support/common.h" +#include "src/__support/libc_errno.h" +#include "src/__support/macros/config.h" +#include "src/__support/wcs_to_integer.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(unsigned long, wcstoul, + (const wchar_t *__restrict str, wchar_t **__restrict str_end, + int base)) { + auto result = internal::wcstointeger<unsigned long>(str, base); + if (result.has_error()) + libc_errno = result.error; + + if (str_end != nullptr) + *str_end = const_cast<wchar_t *>(str + result.parsed_len); + + return result; +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/wchar/wcstoul.h b/libc/src/wchar/wcstoul.h new file mode 100644 index 000000000000..81f530534e81 --- /dev/null +++ b/libc/src/wchar/wcstoul.h @@ -0,0 +1,22 @@ +//===-- Implementation header for wcstoul -----------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC_WCHAR_WCSTOUL_H +#define LLVM_LIBC_SRC_WCHAR_WCSTOUL_H + +#include "hdr/types/wint_t.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +unsigned long wcstoul(const wchar_t *__restrict str, + wchar_t **__restrict str_end, int base); + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC_WCHAR_WCSTOUL_H diff --git a/libc/src/wchar/wcstoull.cpp b/libc/src/wchar/wcstoull.cpp new file mode 100644 index 000000000000..768e03c4bd18 --- /dev/null +++ b/libc/src/wchar/wcstoull.cpp @@ -0,0 +1,30 @@ +//===-- Implementation of wcstoull ----------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "src/wchar/wcstoull.h" +#include "src/__support/common.h" +#include "src/__support/libc_errno.h" +#include "src/__support/macros/config.h" +#include "src/__support/wcs_to_integer.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(unsigned long long, wcstoull, + (const wchar_t *__restrict str, wchar_t **__restrict str_end, + int base)) { + auto result = internal::wcstointeger<unsigned long long>(str, base); + if (result.has_error()) + libc_errno = result.error; + + if (str_end != nullptr) + *str_end = const_cast<wchar_t *>(str + result.parsed_len); + + return result; +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/wchar/wcstoull.h b/libc/src/wchar/wcstoull.h new file mode 100644 index 000000000000..e970a5792338 --- /dev/null +++ b/libc/src/wchar/wcstoull.h @@ -0,0 +1,22 @@ +//===-- Implementation header for wcstoull -----------------------*- C++-*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC_WCHAR_WCSTOULL_H +#define LLVM_LIBC_SRC_WCHAR_WCSTOULL_H + +#include "hdr/types/wint_t.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +unsigned long long wcstoull(const wchar_t *__restrict str, + wchar_t **__restrict str_end, int base); + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC_WCHAR_WCSTOULL_H diff --git a/libc/src/wctype/CMakeLists.txt b/libc/src/wctype/CMakeLists.txt new file mode 100644 index 000000000000..3ac5eaef8ed8 --- /dev/null +++ b/libc/src/wctype/CMakeLists.txt @@ -0,0 +1,9 @@ +add_entrypoint_object( + iswalpha + SRCS + iswalpha.cpp + HDRS + iswalpha.h + DEPENDS + libc.src.__support.wctype_utils +) diff --git a/libc/src/wctype/iswalpha.cpp b/libc/src/wctype/iswalpha.cpp new file mode 100644 index 000000000000..e18f29370fbd --- /dev/null +++ b/libc/src/wctype/iswalpha.cpp @@ -0,0 +1,19 @@ +//===-- Implementation of iswalpha ----------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "src/wctype/iswalpha.h" +#include "src/__support/common.h" +#include "src/__support/wctype_utils.h" + +#include "hdr/types/wint_t.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(bool, iswalpha, (wint_t c)) { return internal::iswalpha(c); } + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/wctype/iswalpha.h b/libc/src/wctype/iswalpha.h new file mode 100644 index 000000000000..681fc6ba79a5 --- /dev/null +++ b/libc/src/wctype/iswalpha.h @@ -0,0 +1,21 @@ +//===-- Implementation header for iswalpha ----------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC_WCTYPE_ISWALPHA_H +#define LLVM_LIBC_SRC_WCTYPE_ISWALPHA_H + +#include "hdr/types/wint_t.h" +#include "src/__support/common.h" + +namespace LIBC_NAMESPACE_DECL { + +bool iswalpha(wint_t c); + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC_WCTYPE_ISWALPHA_H |
