8 changes: 4 additions & 4 deletions libc/src/__support/float_to_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ namespace LIBC_NAMESPACE {
using BlockInt = uint32_t;
constexpr uint32_t BLOCK_SIZE = 9;

using FloatProp = fputil::FloatProperties<long double>;
using FPBits = fputil::FPBits<long double>;

// Larger numbers prefer a slightly larger constant than is used for the smaller
// numbers.
Expand Down Expand Up @@ -382,10 +382,10 @@ LIBC_INLINE uint32_t fast_uint_mod_1e9(const cpp::UInt<MID_INT_SIZE> &val) {
(1000000000 * shifted));
}

LIBC_INLINE uint32_t mul_shift_mod_1e9(const FloatProp::StorageType mantissa,
LIBC_INLINE uint32_t mul_shift_mod_1e9(const FPBits::StorageType mantissa,
const cpp::UInt<MID_INT_SIZE> &large,
const int32_t shift_amount) {
cpp::UInt<MID_INT_SIZE + FloatProp::STORAGE_LEN> val(large);
cpp::UInt<MID_INT_SIZE + FPBits::STORAGE_LEN> val(large);
val = (val * mantissa) >> shift_amount;
return static_cast<uint32_t>(
val.div_uint32_times_pow_2(1000000000, 0).value());
Expand Down Expand Up @@ -414,7 +414,7 @@ class FloatToString {
fputil::FPBits<T> float_bits;
bool is_negative;
int exponent;
FloatProp::StorageType mantissa;
FPBits::StorageType mantissa;

static constexpr int FRACTION_LEN = fputil::FPBits<T>::FRACTION_LEN;
static constexpr int EXP_BIAS = fputil::FPBits<T>::EXP_BIAS;
Expand Down
77 changes: 33 additions & 44 deletions libc/src/__support/str_to_float.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ LIBC_INLINE cpp::optional<ExpandedFloat<T>>
eisel_lemire(ExpandedFloat<T> init_num,
RoundDirection round = RoundDirection::Nearest) {
using FPBits = typename fputil::FPBits<T>;
using FloatProp = typename fputil::FloatProperties<T>;
using StorageType = typename FPBits::StorageType;

StorageType mantissa = init_num.mantissa;
Expand All @@ -93,7 +92,7 @@ eisel_lemire(ExpandedFloat<T> init_num,
mantissa <<= clz;

int32_t exp2 =
exp10_to_exp2(exp10) + FloatProp::STORAGE_LEN + FloatProp::EXP_BIAS - clz;
exp10_to_exp2(exp10) + FPBits::STORAGE_LEN + FPBits::EXP_BIAS - clz;

// Multiplication
const uint64_t *power_of_ten =
Expand All @@ -110,9 +109,7 @@ eisel_lemire(ExpandedFloat<T> init_num,
// accuracy, and the most significant bit is ignored.) = 9 bits. Similarly,
// it's 6 bits for floats in this case.
const uint64_t halfway_constant =
(uint64_t(1) << (FloatProp::STORAGE_LEN -
(FloatProp::FRACTION_LEN + 3))) -
1;
(uint64_t(1) << (FPBits::STORAGE_LEN - (FPBits::FRACTION_LEN + 3))) - 1;
if ((high64(first_approx) & halfway_constant) == halfway_constant &&
low64(first_approx) + mantissa < mantissa) {
UInt128 low_bits =
Expand All @@ -132,10 +129,10 @@ eisel_lemire(ExpandedFloat<T> init_num,

// Shifting to 54 bits for doubles and 25 bits for floats
StorageType msb = static_cast<StorageType>(high64(final_approx) >>
(FloatProp::STORAGE_LEN - 1));
(FPBits::STORAGE_LEN - 1));
StorageType final_mantissa = static_cast<StorageType>(
high64(final_approx) >>
(msb + FloatProp::STORAGE_LEN - (FloatProp::FRACTION_LEN + 3)));
(msb + FPBits::STORAGE_LEN - (FPBits::FRACTION_LEN + 3)));
exp2 -= static_cast<uint32_t>(1 ^ msb); // same as !msb

if (round == RoundDirection::Nearest) {
Expand All @@ -161,14 +158,14 @@ eisel_lemire(ExpandedFloat<T> init_num,

// From 54 to 53 bits for doubles and 25 to 24 bits for floats
final_mantissa >>= 1;
if ((final_mantissa >> (FloatProp::FRACTION_LEN + 1)) > 0) {
if ((final_mantissa >> (FPBits::FRACTION_LEN + 1)) > 0) {
final_mantissa >>= 1;
++exp2;
}

// The if block is equivalent to (but has fewer branches than):
// if exp2 <= 0 || exp2 >= 0x7FF { etc }
if (static_cast<uint32_t>(exp2) - 1 >= (1 << FloatProp::EXP_LEN) - 2) {
if (static_cast<uint32_t>(exp2) - 1 >= (1 << FPBits::EXP_LEN) - 2) {
return cpp::nullopt;
}

Expand All @@ -184,7 +181,6 @@ LIBC_INLINE cpp::optional<ExpandedFloat<long double>>
eisel_lemire<long double>(ExpandedFloat<long double> init_num,
RoundDirection round) {
using FPBits = typename fputil::FPBits<long double>;
using FloatProp = typename fputil::FloatProperties<long double>;
using StorageType = typename FPBits::StorageType;

StorageType mantissa = init_num.mantissa;
Expand All @@ -210,7 +206,7 @@ eisel_lemire<long double>(ExpandedFloat<long double> init_num,
mantissa <<= clz;

int32_t exp2 =
exp10_to_exp2(exp10) + FloatProp::STORAGE_LEN + FloatProp::EXP_BIAS - clz;
exp10_to_exp2(exp10) + FPBits::STORAGE_LEN + FPBits::EXP_BIAS - clz;

// Multiplication
const uint64_t *power_of_ten =
Expand Down Expand Up @@ -247,8 +243,7 @@ eisel_lemire<long double>(ExpandedFloat<long double> init_num,
// accuracy, and the most significant bit is ignored.) = 61 bits. Similarly,
// it's 12 bits for 128 bit floats in this case.
constexpr UInt128 HALFWAY_CONSTANT =
(UInt128(1) << (FloatProp::STORAGE_LEN - (FloatProp::FRACTION_LEN + 3))) -
1;
(UInt128(1) << (FPBits::STORAGE_LEN - (FPBits::FRACTION_LEN + 3))) - 1;

if ((final_approx_upper & HALFWAY_CONSTANT) == HALFWAY_CONSTANT &&
final_approx_lower + mantissa < mantissa) {
Expand All @@ -257,10 +252,10 @@ eisel_lemire<long double>(ExpandedFloat<long double> init_num,

// Shifting to 65 bits for 80 bit floats and 113 bits for 128 bit floats
uint32_t msb =
static_cast<uint32_t>(final_approx_upper >> (FloatProp::STORAGE_LEN - 1));
static_cast<uint32_t>(final_approx_upper >> (FPBits::STORAGE_LEN - 1));
StorageType final_mantissa =
final_approx_upper >>
(msb + FloatProp::STORAGE_LEN - (FloatProp::FRACTION_LEN + 3));
(msb + FPBits::STORAGE_LEN - (FPBits::FRACTION_LEN + 3));
exp2 -= static_cast<uint32_t>(1 ^ msb); // same as !msb

if (round == RoundDirection::Nearest) {
Expand All @@ -285,14 +280,14 @@ eisel_lemire<long double>(ExpandedFloat<long double> init_num,
// From 65 to 64 bits for 80 bit floats and 113 to 112 bits for 128 bit
// floats
final_mantissa >>= 1;
if ((final_mantissa >> (FloatProp::FRACTION_LEN + 1)) > 0) {
if ((final_mantissa >> (FPBits::FRACTION_LEN + 1)) > 0) {
final_mantissa >>= 1;
++exp2;
}

// The if block is equivalent to (but has fewer branches than):
// if exp2 <= 0 || exp2 >= MANTISSA_MAX { etc }
if (exp2 - 1 >= (1 << FloatProp::EXP_LEN) - 2) {
if (exp2 - 1 >= (1 << FPBits::EXP_LEN) - 2) {
return cpp::nullopt;
}

Expand Down Expand Up @@ -321,7 +316,6 @@ LIBC_INLINE FloatConvertReturn<T>
simple_decimal_conversion(const char *__restrict numStart,
RoundDirection round = RoundDirection::Nearest) {
using FPBits = typename fputil::FPBits<T>;
using FloatProp = typename fputil::FloatProperties<T>;
using StorageType = typename FPBits::StorageType;

int32_t exp2 = 0;
Expand All @@ -337,16 +331,15 @@ simple_decimal_conversion(const char *__restrict numStart,
// If the exponent is too large and can't be represented in this size of
// float, return inf.
if (hpd.get_decimal_point() > 0 &&
exp10_to_exp2(hpd.get_decimal_point() - 1) > FloatProp::EXP_BIAS) {
exp10_to_exp2(hpd.get_decimal_point() - 1) > FPBits::EXP_BIAS) {
output.num = {0, fputil::FPBits<T>::MAX_BIASED_EXPONENT};
output.error = ERANGE;
return output;
}
// If the exponent is too small even for a subnormal, return 0.
if (hpd.get_decimal_point() < 0 &&
exp10_to_exp2(-hpd.get_decimal_point()) >
(FloatProp::EXP_BIAS +
static_cast<int32_t>(FloatProp::FRACTION_LEN))) {
(FPBits::EXP_BIAS + static_cast<int32_t>(FPBits::FRACTION_LEN))) {
output.num = {0, 0};
output.error = ERANGE;
return output;
Expand Down Expand Up @@ -385,7 +378,7 @@ simple_decimal_conversion(const char *__restrict numStart,
hpd.shift(1);

// Get the biased exponent
exp2 += FloatProp::EXP_BIAS;
exp2 += FPBits::EXP_BIAS;

// Handle the exponent being too large (and return inf).
if (exp2 >= FPBits::MAX_BIASED_EXPONENT) {
Expand All @@ -395,7 +388,7 @@ simple_decimal_conversion(const char *__restrict numStart,
}

// Shift left to fill the mantissa
hpd.shift(FloatProp::FRACTION_LEN);
hpd.shift(FPBits::FRACTION_LEN);
StorageType final_mantissa = hpd.round_to_integer_type<StorageType>();

// Handle subnormals
Expand All @@ -411,13 +404,13 @@ simple_decimal_conversion(const char *__restrict numStart,
final_mantissa = hpd.round_to_integer_type<StorageType>(round);

// Check if by shifting right we've caused this to round to a normal number.
if ((final_mantissa >> FloatProp::FRACTION_LEN) != 0) {
if ((final_mantissa >> FPBits::FRACTION_LEN) != 0) {
++exp2;
}
}

// Check if rounding added a bit, and shift down if that's the case.
if (final_mantissa == StorageType(2) << FloatProp::FRACTION_LEN) {
if (final_mantissa == StorageType(2) << FPBits::FRACTION_LEN) {
final_mantissa >>= 1;
++exp2;

Expand Down Expand Up @@ -515,13 +508,12 @@ LIBC_INLINE cpp::optional<ExpandedFloat<T>>
clinger_fast_path(ExpandedFloat<T> init_num,
RoundDirection round = RoundDirection::Nearest) {
using FPBits = typename fputil::FPBits<T>;
using FloatProp = typename fputil::FloatProperties<T>;
using StorageType = typename FPBits::StorageType;

StorageType mantissa = init_num.mantissa;
int32_t exp10 = init_num.exponent;

if ((mantissa >> FloatProp::FRACTION_LEN) > 0) {
if ((mantissa >> FPBits::FRACTION_LEN) > 0) {
return cpp::nullopt;
}

Expand Down Expand Up @@ -605,7 +597,7 @@ clinger_fast_path(ExpandedFloat<T> init_num,
// log10(2^(exponent bias)).
// The generic approximation uses the fact that log10(2^x) ~= x/3
template <typename T> constexpr int32_t get_upper_bound() {
return fputil::FloatProperties<T>::EXP_BIAS / 3;
return fputil::FPBits<T>::EXP_BIAS / 3;
}

template <> constexpr int32_t get_upper_bound<float>() { return 39; }
Expand All @@ -621,11 +613,10 @@ template <> constexpr int32_t get_upper_bound<double>() { return 309; }
// other out, and subnormal numbers allow for the result to be at the very low
// end of the final mantissa.
template <typename T> constexpr int32_t get_lower_bound() {
using FloatProp = typename fputil::FloatProperties<T>;
return -(
(FloatProp::EXP_BIAS +
static_cast<int32_t>(FloatProp::FRACTION_LEN + FloatProp::STORAGE_LEN)) /
3);
using FPBits = typename fputil::FPBits<T>;
return -((FPBits::EXP_BIAS +
static_cast<int32_t>(FPBits::FRACTION_LEN + FPBits::STORAGE_LEN)) /
3);
}

template <> constexpr int32_t get_lower_bound<float>() {
Expand Down Expand Up @@ -723,7 +714,6 @@ LIBC_INLINE FloatConvertReturn<T> binary_exp_to_float(ExpandedFloat<T> init_num,
bool truncated,
RoundDirection round) {
using FPBits = typename fputil::FPBits<T>;
using FloatProp = typename fputil::FloatProperties<T>;
using StorageType = typename FPBits::StorageType;

StorageType mantissa = init_num.mantissa;
Expand All @@ -733,7 +723,7 @@ LIBC_INLINE FloatConvertReturn<T> binary_exp_to_float(ExpandedFloat<T> init_num,

// This is the number of leading zeroes a properly normalized float of type T
// should have.
constexpr int32_t INF_EXP = (1 << FloatProp::EXP_LEN) - 1;
constexpr int32_t INF_EXP = (1 << FPBits::EXP_LEN) - 1;

// Normalization step 1: Bring the leading bit to the highest bit of
// StorageType.
Expand All @@ -744,26 +734,25 @@ LIBC_INLINE FloatConvertReturn<T> binary_exp_to_float(ExpandedFloat<T> init_num,
exp2 -= amount_to_shift_left;

// biased_exponent represents the biased exponent of the most significant bit.
int32_t biased_exponent =
exp2 + FloatProp::STORAGE_LEN + FPBits::EXP_BIAS - 1;
int32_t biased_exponent = exp2 + FPBits::STORAGE_LEN + FPBits::EXP_BIAS - 1;

// Handle numbers that're too large and get squashed to inf
if (biased_exponent >= INF_EXP) {
// This indicates an overflow, so we make the result INF and set errno.
output.num = {0, (1 << FloatProp::EXP_LEN) - 1};
output.num = {0, (1 << FPBits::EXP_LEN) - 1};
output.error = ERANGE;
return output;
}

uint32_t amount_to_shift_right =
FloatProp::STORAGE_LEN - FloatProp::FRACTION_LEN - 1;
FPBits::STORAGE_LEN - FPBits::FRACTION_LEN - 1;

// Handle subnormals.
if (biased_exponent <= 0) {
amount_to_shift_right += 1 - biased_exponent;
biased_exponent = 0;

if (amount_to_shift_right > FloatProp::STORAGE_LEN) {
if (amount_to_shift_right > FPBits::STORAGE_LEN) {
// Return 0 if the exponent is too small.
output.num = {0, 0};
output.error = ERANGE;
Expand All @@ -776,10 +765,10 @@ LIBC_INLINE FloatConvertReturn<T> binary_exp_to_float(ExpandedFloat<T> init_num,
bool round_bit = static_cast<bool>(mantissa & round_bit_mask);
bool sticky_bit = static_cast<bool>(mantissa & sticky_mask) || truncated;

if (amount_to_shift_right < FloatProp::STORAGE_LEN) {
if (amount_to_shift_right < FPBits::STORAGE_LEN) {
// Shift the mantissa and clear the implicit bit.
mantissa >>= amount_to_shift_right;
mantissa &= FloatProp::FRACTION_MASK;
mantissa &= FPBits::FRACTION_MASK;
} else {
mantissa = 0;
}
Expand All @@ -802,7 +791,7 @@ LIBC_INLINE FloatConvertReturn<T> binary_exp_to_float(ExpandedFloat<T> init_num,
}
}

if (mantissa > FloatProp::FRACTION_MASK) {
if (mantissa > FPBits::FRACTION_MASK) {
// Rounding causes the exponent to increase.
++biased_exponent;

Expand All @@ -815,7 +804,7 @@ LIBC_INLINE FloatConvertReturn<T> binary_exp_to_float(ExpandedFloat<T> init_num,
output.error = ERANGE;
}

output.num = {mantissa & FloatProp::FRACTION_MASK, biased_exponent};
output.num = {mantissa & FPBits::FRACTION_MASK, biased_exponent};
return output;
}

Expand Down
5 changes: 2 additions & 3 deletions libc/src/math/generic/exp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ double set_exceptional(double x) {

LLVM_LIBC_FUNCTION(double, exp, (double x)) {
using FPBits = typename fputil::FPBits<double>;
using FloatProp = typename fputil::FloatProperties<double>;
FPBits xbits(x);

uint64_t x_u = xbits.uintval();
Expand Down Expand Up @@ -385,7 +384,7 @@ LLVM_LIBC_FUNCTION(double, exp, (double x)) {
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) << FloatProp::FRACTION_LEN;
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;
}
Expand All @@ -403,7 +402,7 @@ LLVM_LIBC_FUNCTION(double, exp, (double x)) {
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) << FloatProp::FRACTION_LEN;
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;
Expand Down
5 changes: 2 additions & 3 deletions libc/src/math/generic/exp10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ double set_exceptional(double x) {

LLVM_LIBC_FUNCTION(double, exp10, (double x)) {
using FPBits = typename fputil::FPBits<double>;
using FloatProp = typename fputil::FloatProperties<double>;
FPBits xbits(x);

uint64_t x_u = xbits.uintval();
Expand Down Expand Up @@ -398,7 +397,7 @@ LLVM_LIBC_FUNCTION(double, exp10, (double x)) {
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) << FloatProp::FRACTION_LEN;
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;
}
Expand Down Expand Up @@ -465,7 +464,7 @@ LLVM_LIBC_FUNCTION(double, exp10, (double x)) {
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) << FloatProp::FRACTION_LEN;
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;
}
Expand Down
5 changes: 2 additions & 3 deletions libc/src/math/generic/exp2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ double set_exceptional(double x) {

LLVM_LIBC_FUNCTION(double, exp2, (double x)) {
using FPBits = typename fputil::FPBits<double>;
using FloatProp = typename fputil::FloatProperties<double>;
FPBits xbits(x);

uint64_t x_u = xbits.uintval();
Expand Down Expand Up @@ -365,7 +364,7 @@ LLVM_LIBC_FUNCTION(double, exp2, (double x)) {
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) << FloatProp::FRACTION_LEN;
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;
}
Expand All @@ -379,7 +378,7 @@ LLVM_LIBC_FUNCTION(double, exp2, (double x)) {
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) << FloatProp::FRACTION_LEN;
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;
}
Expand Down
2 changes: 1 addition & 1 deletion libc/src/math/generic/exp2f_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ LIBC_INLINE float exp2f(float x) {
// exp_hi = shift hi to the exponent field of double precision.
int64_t exp_hi =
static_cast<int64_t>(static_cast<uint64_t>(k >> ExpBase::MID_BITS)
<< fputil::FloatProperties<double>::FRACTION_LEN);
<< fputil::FPBits<double>::FRACTION_LEN);
// mh = 2^hi * 2^mid
// mh_bits = bit field of mh
int64_t mh_bits = ExpBase::EXP_2_MID[k & ExpBase::MID_MASK] + exp_hi;
Expand Down
10 changes: 5 additions & 5 deletions libc/src/math/generic/explogxf.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ template <class Base> LIBC_INLINE exp_b_reduc_t exp_b_range_reduc(float x) {
// hi = floor(kd * 2^(-MID_BITS))
// exp_hi = shift hi to the exponent field of double precision.
int64_t exp_hi = static_cast<int64_t>((k >> Base::MID_BITS))
<< fputil::FloatProperties<double>::FRACTION_LEN;
<< fputil::FPBits<double>::FRACTION_LEN;
// mh = 2^hi * 2^mid
// mh_bits = bit field of mh
int64_t mh_bits = Base::EXP_2_MID[k & Base::MID_MASK] + exp_hi;
Expand Down Expand Up @@ -235,9 +235,9 @@ template <bool is_sinh> LIBC_INLINE double exp_pm_eval(float x) {
// hi = floor(kf * 2^(-5))
// exp_hi = shift hi to the exponent field of double precision.
int64_t exp_hi_p = static_cast<int64_t>((k_p >> ExpBase::MID_BITS))
<< fputil::FloatProperties<double>::FRACTION_LEN;
<< fputil::FPBits<double>::FRACTION_LEN;
int64_t exp_hi_m = static_cast<int64_t>((k_m >> ExpBase::MID_BITS))
<< fputil::FloatProperties<double>::FRACTION_LEN;
<< fputil::FPBits<double>::FRACTION_LEN;
// mh_p = 2^(hi + mid)
// mh_m = 2^(-(hi + mid))
// mh_bits_* = bit field of mh_*
Expand Down Expand Up @@ -342,10 +342,10 @@ LIBC_INLINE static double log_eval(double x) {
// double(1.0 + 2^1022 * x) - 1.0 to test how x is rounded in denormal range.
LIBC_INLINE cpp::optional<double> ziv_test_denorm(int hi, double mid, double lo,
double err) {
using FloatProp = typename fputil::FloatProperties<double>;
using FPBits = typename fputil::FPBits<double>;

// Scaling factor = 1/(min normal number) = 2^1022
int64_t exp_hi = static_cast<int64_t>(hi + 1022) << FloatProp::FRACTION_LEN;
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))
Expand Down
5 changes: 2 additions & 3 deletions libc/src/math/generic/expm1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ double set_exceptional(double x) {

LLVM_LIBC_FUNCTION(double, expm1, (double x)) {
using FPBits = typename fputil::FPBits<double>;
using FloatProp = typename fputil::FloatProperties<double>;
FPBits xbits(x);

bool x_sign = xbits.get_sign();
Expand Down Expand Up @@ -468,7 +467,7 @@ LLVM_LIBC_FUNCTION(double, expm1, (double x)) {
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) << FloatProp::FRACTION_LEN;
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;
}
Expand All @@ -482,7 +481,7 @@ LLVM_LIBC_FUNCTION(double, expm1, (double x)) {
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) << FloatProp::FRACTION_LEN;
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;
}
Expand Down
44 changes: 21 additions & 23 deletions libc/src/math/generic/powf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,24 +387,24 @@ static constexpr DoubleDouble LOG2_R2_DD[] = {
};

LIBC_INLINE bool is_odd_integer(float x) {
using FloatProp = typename fputil::FloatProperties<float>;
using FPBits = typename fputil::FPBits<float>;
uint32_t x_u = cpp::bit_cast<uint32_t>(x);
int32_t x_e = static_cast<int32_t>((x_u & FloatProp::EXP_MASK) >>
FloatProp::FRACTION_LEN);
int32_t lsb = cpp::countr_zero(x_u | FloatProp::EXP_MASK);
int32_t x_e =
static_cast<int32_t>((x_u & FPBits::EXP_MASK) >> FPBits::FRACTION_LEN);
int32_t lsb = cpp::countr_zero(x_u | FPBits::EXP_MASK);
constexpr int32_t UNIT_EXPONENT =
FloatProp::EXP_BIAS + static_cast<int32_t>(FloatProp::FRACTION_LEN);
FPBits::EXP_BIAS + static_cast<int32_t>(FPBits::FRACTION_LEN);
return (x_e + lsb == UNIT_EXPONENT);
}

LIBC_INLINE bool is_integer(float x) {
using FloatProp = typename fputil::FloatProperties<float>;
using FPBits = typename fputil::FPBits<float>;
uint32_t x_u = cpp::bit_cast<uint32_t>(x);
int32_t x_e = static_cast<int32_t>((x_u & FloatProp::EXP_MASK) >>
FloatProp::FRACTION_LEN);
int32_t lsb = cpp::countr_zero(x_u | FloatProp::EXP_MASK);
int32_t x_e =
static_cast<int32_t>((x_u & FPBits::EXP_MASK) >> FPBits::FRACTION_LEN);
int32_t lsb = cpp::countr_zero(x_u | FPBits::EXP_MASK);
constexpr int32_t UNIT_EXPONENT =
FloatProp::EXP_BIAS + static_cast<int32_t>(FloatProp::FRACTION_LEN);
FPBits::EXP_BIAS + static_cast<int32_t>(FPBits::FRACTION_LEN);
return (x_e + lsb >= UNIT_EXPONENT);
}

Expand All @@ -424,7 +424,6 @@ LIBC_INLINE bool larger_exponent(double a, double b) {
double powf_double_double(int idx_x, double dx, double y6, double lo6_hi,
const DoubleDouble &exp2_hi_mid) {
using DoubleBits = typename fputil::FPBits<double>;
using DoubleProp = typename fputil::FloatProperties<double>;
// Perform a second range reduction step:
// idx2 = round(2^14 * (dx + 2^-8)) = round ( dx * 2^14 + 2^6)
// dx2 = (1 + dx) * r2 - 1
Expand Down Expand Up @@ -500,7 +499,7 @@ double powf_double_double(int idx_x, double dx, double y6, double lo6_hi,
bool lo_sign = DoubleBits(r.lo).get_sign();
if (hi_sign == lo_sign) {
++r_bits;
} else if ((r_bits & DoubleProp::FRACTION_MASK) > 0) {
} else if ((r_bits & DoubleBits::FRACTION_MASK) > 0) {
--r_bits;
}
}
Expand All @@ -512,8 +511,7 @@ double powf_double_double(int idx_x, double dx, double y6, double lo6_hi,

LLVM_LIBC_FUNCTION(float, powf, (float x, float y)) {
using FloatBits = typename fputil::FPBits<float>;
using FloatProp = typename fputil::FloatProperties<float>;
using DoubleProp = typename fputil::FloatProperties<double>;
using DoubleBits = typename fputil::FPBits<double>;
FloatBits xbits(x), ybits(y);

uint32_t x_u = xbits.uintval();
Expand Down Expand Up @@ -584,7 +582,7 @@ LLVM_LIBC_FUNCTION(float, powf, (float x, float y)) {
// x^y will be overflow / underflow in single precision. Set y to a
// large enough exponent but not too large, so that the computations
// won't be overflow in double precision.
y = cpp::bit_cast<float>((y_u & FloatProp::SIGN_MASK) + 0x4f800000U);
y = cpp::bit_cast<float>((y_u & FloatBits::SIGN_MASK) + 0x4f800000U);
}
}
}
Expand All @@ -607,11 +605,11 @@ LLVM_LIBC_FUNCTION(float, powf, (float x, float y)) {
return generic::exp10f(y);
}

bool x_sign = x_u >= FloatProp::SIGN_MASK;
bool x_sign = x_u >= FloatBits::SIGN_MASK;

switch (x_abs) {
case 0x0000'0000: { // x = +-0.0f
bool x_sign = (x_u >= FloatProp::SIGN_MASK);
bool x_sign = (x_u >= FloatBits::SIGN_MASK);
bool out_sign = x_sign && is_odd_integer(FloatBits(y_u).get_val());
if (y_u > 0x8000'0000U) {
// pow(0, negative number) = inf
Expand All @@ -623,9 +621,9 @@ LLVM_LIBC_FUNCTION(float, powf, (float x, float y)) {
return out_sign ? -0.0f : 0.0f;
}
case 0x7f80'0000: { // x = +-Inf
bool x_sign = (x_u >= FloatProp::SIGN_MASK);
bool x_sign = (x_u >= FloatBits::SIGN_MASK);
bool out_sign = x_sign && is_odd_integer(FloatBits(y_u).get_val());
if (y_u >= FloatProp::SIGN_MASK) {
if (y_u >= FloatBits::SIGN_MASK) {
return out_sign ? -0.0f : 0.0f;
}
return FloatBits::inf(out_sign);
Expand Down Expand Up @@ -669,11 +667,11 @@ LLVM_LIBC_FUNCTION(float, powf, (float x, float y)) {
x_u = FloatBits(x).uintval();

// Extract exponent field of x.
ex += (x_u >> FloatProp::FRACTION_LEN);
ex += (x_u >> FloatBits::FRACTION_LEN);
double e_x = static_cast<double>(ex);
// Use the highest 7 fractional bits of m_x as the index for look up tables.
uint32_t x_mant = x_u & FloatProp::FRACTION_MASK;
int idx_x = static_cast<int>(x_mant >> (FloatProp::FRACTION_LEN - 7));
uint32_t x_mant = x_u & FloatBits::FRACTION_MASK;
int idx_x = static_cast<int>(x_mant >> (FloatBits::FRACTION_LEN - 7));
// Add the hidden bit to the mantissa.
// 1 <= m_x < 2
float m_x = cpp::bit_cast<float>(x_mant | 0x3f800000);
Expand Down Expand Up @@ -774,7 +772,7 @@ LLVM_LIBC_FUNCTION(float, powf, (float x, float y)) {
int idx_y = hm_i & 0x3f;

// 2^hi
int64_t exp_hi_i = (hm_i >> 6) << DoubleProp::FRACTION_LEN;
int64_t exp_hi_i = (hm_i >> 6) << DoubleBits::FRACTION_LEN;
// 2^mid
int64_t exp_mid_i = cpp::bit_cast<uint64_t>(EXP2_MID1[idx_y].hi);
// (-1)^sign * 2^hi * 2^mid
Expand Down
2 changes: 1 addition & 1 deletion libc/src/math/generic/range_reduction.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ LIBC_INLINE int64_t small_range_reduction(double x, double &y) {
LIBC_INLINE int64_t large_range_reduction(double x, int x_exp, double &y) {
int idx = 0;
y = 0;
int x_lsb_exp_m4 = x_exp - fputil::FloatProperties<float>::FRACTION_LEN;
int x_lsb_exp_m4 = x_exp - fputil::FPBits<float>::FRACTION_LEN;

// Skipping the first parts of 32/pi such that:
// LSB of x * LSB of THIRTYTWO_OVER_PI_28[i] >= 32.
Expand Down
2 changes: 1 addition & 1 deletion libc/src/math/generic/tanhf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ LLVM_LIBC_FUNCTION(float, tanhf, (float x)) {
// -hi = floor(-k * 2^(-MID_BITS))
// exp_mhi = shift -hi to the exponent field of double precision.
int64_t exp_mhi = static_cast<int64_t>(mk >> ExpBase::MID_BITS)
<< fputil::FloatProperties<double>::FRACTION_LEN;
<< fputil::FPBits<double>::FRACTION_LEN;
// mh = 2^(-hi - mid)
int64_t mh_bits = ExpBase::EXP_2_MID[mk & ExpBase::MID_MASK] + exp_mhi;
double mh = fputil::FPBits<double>(uint64_t(mh_bits)).get_val();
Expand Down
9 changes: 4 additions & 5 deletions libc/src/stdio/printf_core/float_dec_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,7 @@ class FloatWriter {
// -exponent will never overflow because all long double types we support
// have at most 15 bits of mantissa and the C standard defines an int as
// being at least 16 bits.
static_assert(fputil::FloatProperties<long double>::EXP_LEN <
(sizeof(int) * 8));
static_assert(fputil::FPBits<long double>::EXP_LEN < (sizeof(int) * 8));

public:
LIBC_INLINE FloatWriter(Writer *init_writer, bool init_has_decimal_point,
Expand Down Expand Up @@ -474,7 +473,7 @@ LIBC_INLINE int convert_float_decimal_typed(Writer *writer,
const FormatSection &to_conv,
fputil::FPBits<T> float_bits) {
// signed because later we use -FRACTION_LEN
constexpr int32_t FRACTION_LEN = fputil::FloatProperties<T>::FRACTION_LEN;
constexpr int32_t FRACTION_LEN = fputil::FPBits<T>::FRACTION_LEN;
bool is_negative = float_bits.get_sign();
int exponent = float_bits.get_explicit_exponent();

Expand Down Expand Up @@ -587,7 +586,7 @@ LIBC_INLINE int convert_float_dec_exp_typed(Writer *writer,
const FormatSection &to_conv,
fputil::FPBits<T> float_bits) {
// signed because later we use -FRACTION_LEN
constexpr int32_t FRACTION_LEN = fputil::FloatProperties<T>::FRACTION_LEN;
constexpr int32_t FRACTION_LEN = fputil::FPBits<T>::FRACTION_LEN;
bool is_negative = float_bits.get_sign();
int exponent = float_bits.get_explicit_exponent();
StorageType mantissa = float_bits.get_explicit_mantissa();
Expand Down Expand Up @@ -750,7 +749,7 @@ LIBC_INLINE int convert_float_dec_auto_typed(Writer *writer,
const FormatSection &to_conv,
fputil::FPBits<T> float_bits) {
// signed because later we use -FRACTION_LEN
constexpr int32_t FRACTION_LEN = fputil::FloatProperties<T>::FRACTION_LEN;
constexpr int32_t FRACTION_LEN = fputil::FPBits<T>::FRACTION_LEN;
bool is_negative = float_bits.get_sign();
int exponent = float_bits.get_explicit_exponent();
StorageType mantissa = float_bits.get_explicit_mantissa();
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/__support/str_to_fp_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
namespace LIBC_NAMESPACE {

template <typename T> struct LlvmLibcStrToFloatTest : public testing::Test {
using StorageType = typename fputil::FloatProperties<T>::StorageType;
using StorageType = typename fputil::FPBits<T>::StorageType;

void clinger_fast_path_test(const StorageType inputMantissa,
const int32_t inputExp10,
Expand Down
3 changes: 1 addition & 2 deletions libc/test/src/math/FrexpTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ template <typename T> class FrexpTest : public LIBC_NAMESPACE::testing::Test {
DECLARE_SPECIAL_CONSTANTS(T)

static constexpr StorageType HIDDEN_BIT =
StorageType(1)
<< LIBC_NAMESPACE::fputil::FloatProperties<T>::FRACTION_LEN;
StorageType(1) << LIBC_NAMESPACE::fputil::FPBits<T>::FRACTION_LEN;

public:
typedef T (*FrexpFunc)(T, int *);
Expand Down
3 changes: 1 addition & 2 deletions libc/test/src/math/LogbTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ template <typename T> class LogbTest : public LIBC_NAMESPACE::testing::Test {
DECLARE_SPECIAL_CONSTANTS(T)

static constexpr StorageType HIDDEN_BIT =
StorageType(1)
<< LIBC_NAMESPACE::fputil::FloatProperties<T>::FRACTION_LEN;
StorageType(1) << LIBC_NAMESPACE::fputil::FPBits<T>::FRACTION_LEN;

public:
typedef T (*LogbFunc)(T);
Expand Down
3 changes: 1 addition & 2 deletions libc/test/src/math/SqrtTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ template <typename T> class SqrtTest : public LIBC_NAMESPACE::testing::Test {
DECLARE_SPECIAL_CONSTANTS(T)

static constexpr StorageType HIDDEN_BIT =
StorageType(1)
<< LIBC_NAMESPACE::fputil::FloatProperties<T>::FRACTION_LEN;
StorageType(1) << LIBC_NAMESPACE::fputil::FPBits<T>::FRACTION_LEN;

public:
typedef T (*SqrtFunc)(T);
Expand Down
3 changes: 1 addition & 2 deletions libc/test/src/math/smoke/FrexpTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ template <typename T> class FrexpTest : public LIBC_NAMESPACE::testing::Test {
DECLARE_SPECIAL_CONSTANTS(T)

static constexpr StorageType HIDDEN_BIT =
StorageType(1)
<< LIBC_NAMESPACE::fputil::FloatProperties<T>::FRACTION_LEN;
StorageType(1) << LIBC_NAMESPACE::fputil::FPBits<T>::FRACTION_LEN;

public:
typedef T (*FrexpFunc)(T, int *);
Expand Down
3 changes: 1 addition & 2 deletions libc/test/src/math/smoke/LogbTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ template <typename T> class LogbTest : public LIBC_NAMESPACE::testing::Test {
DECLARE_SPECIAL_CONSTANTS(T)

static constexpr StorageType HIDDEN_BIT =
StorageType(1)
<< LIBC_NAMESPACE::fputil::FloatProperties<T>::FRACTION_LEN;
StorageType(1) << LIBC_NAMESPACE::fputil::FPBits<T>::FRACTION_LEN;

public:
typedef T (*LogbFunc)(T);
Expand Down
3 changes: 1 addition & 2 deletions libc/test/src/math/smoke/SqrtTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ template <typename T> class SqrtTest : public LIBC_NAMESPACE::testing::Test {
DECLARE_SPECIAL_CONSTANTS(T)

static constexpr StorageType HIDDEN_BIT =
StorageType(1)
<< LIBC_NAMESPACE::fputil::FloatProperties<T>::FRACTION_LEN;
StorageType(1) << LIBC_NAMESPACE::fputil::FPBits<T>::FRACTION_LEN;

public:
typedef T (*SqrtFunc)(T);
Expand Down
2 changes: 1 addition & 1 deletion libc/utils/MPFRWrapper/MPFRUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ template <> struct ExtraPrecision<long double> {
template <typename T>
static inline unsigned int get_precision(double ulp_tolerance) {
if (ulp_tolerance <= 0.5) {
return LIBC_NAMESPACE::fputil::FloatProperties<T>::MANTISSA_PRECISION;
return LIBC_NAMESPACE::fputil::FPBits<T>::MANTISSA_PRECISION;
} else {
return ExtraPrecision<T>::VALUE;
}
Expand Down