132 changes: 64 additions & 68 deletions libc/src/__support/FPUtil/FloatProperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,42 +41,42 @@ enum class FPEncoding {
template <FPType> struct FPBaseProperties {};

template <> struct FPBaseProperties<FPType::IEEE754_Binary16> {
using UIntType = uint16_t;
LIBC_INLINE_VAR static constexpr int TOTAL_BITS = 16;
LIBC_INLINE_VAR static constexpr int SIG_BITS = 10;
LIBC_INLINE_VAR static constexpr int EXP_BITS = 5;
using StorageType = uint16_t;
LIBC_INLINE_VAR static constexpr int TOTAL_LEN = 16;
LIBC_INLINE_VAR static constexpr int SIG_LEN = 10;
LIBC_INLINE_VAR static constexpr int EXP_LEN = 5;
LIBC_INLINE_VAR static constexpr auto ENCODING = FPEncoding::IEEE754;
};

template <> struct FPBaseProperties<FPType::IEEE754_Binary32> {
using UIntType = uint32_t;
LIBC_INLINE_VAR static constexpr int TOTAL_BITS = 32;
LIBC_INLINE_VAR static constexpr int SIG_BITS = 23;
LIBC_INLINE_VAR static constexpr int EXP_BITS = 8;
using StorageType = uint32_t;
LIBC_INLINE_VAR static constexpr int TOTAL_LEN = 32;
LIBC_INLINE_VAR static constexpr int SIG_LEN = 23;
LIBC_INLINE_VAR static constexpr int EXP_LEN = 8;
LIBC_INLINE_VAR static constexpr auto ENCODING = FPEncoding::IEEE754;
};

template <> struct FPBaseProperties<FPType::IEEE754_Binary64> {
using UIntType = uint64_t;
LIBC_INLINE_VAR static constexpr int TOTAL_BITS = 64;
LIBC_INLINE_VAR static constexpr int SIG_BITS = 52;
LIBC_INLINE_VAR static constexpr int EXP_BITS = 11;
using StorageType = uint64_t;
LIBC_INLINE_VAR static constexpr int TOTAL_LEN = 64;
LIBC_INLINE_VAR static constexpr int SIG_LEN = 52;
LIBC_INLINE_VAR static constexpr int EXP_LEN = 11;
LIBC_INLINE_VAR static constexpr auto ENCODING = FPEncoding::IEEE754;
};

template <> struct FPBaseProperties<FPType::IEEE754_Binary128> {
using UIntType = UInt128;
LIBC_INLINE_VAR static constexpr int TOTAL_BITS = 128;
LIBC_INLINE_VAR static constexpr int SIG_BITS = 112;
LIBC_INLINE_VAR static constexpr int EXP_BITS = 15;
using StorageType = UInt128;
LIBC_INLINE_VAR static constexpr int TOTAL_LEN = 128;
LIBC_INLINE_VAR static constexpr int SIG_LEN = 112;
LIBC_INLINE_VAR static constexpr int EXP_LEN = 15;
LIBC_INLINE_VAR static constexpr auto ENCODING = FPEncoding::IEEE754;
};

template <> struct FPBaseProperties<FPType::X86_Binary80> {
using UIntType = UInt128;
LIBC_INLINE_VAR static constexpr int TOTAL_BITS = 80;
LIBC_INLINE_VAR static constexpr int SIG_BITS = 64;
LIBC_INLINE_VAR static constexpr int EXP_BITS = 15;
using StorageType = UInt128;
LIBC_INLINE_VAR static constexpr int TOTAL_LEN = 80;
LIBC_INLINE_VAR static constexpr int SIG_LEN = 64;
LIBC_INLINE_VAR static constexpr int EXP_LEN = 15;
LIBC_INLINE_VAR static constexpr auto ENCODING =
FPEncoding::X86_ExtendedPrecision;
};
Expand All @@ -87,92 +87,88 @@ template <FPType fp_type>
struct FPProperties : public internal::FPBaseProperties<fp_type> {
private:
using UP = internal::FPBaseProperties<fp_type>;
// The number of bits to represent sign. For documentation purpose, always 1.
LIBC_INLINE_VAR static constexpr int SIGN_BITS = 1;
using UP::EXP_BITS; // The number of bits for the *exponent* part
using UP::SIG_BITS; // The number of bits for the *significand* part
using UP::TOTAL_BITS; // For convenience, the sum of `SIG_BITS`, `EXP_BITS`,
// and `SIGN_BITS`.
static_assert(SIGN_BITS + EXP_BITS + SIG_BITS == TOTAL_BITS);

public:
// The number of bits to represent sign. For documentation purpose, always 1.
LIBC_INLINE_VAR static constexpr int SIGN_LEN = 1;
using UP::EXP_LEN; // The number of bits for the *exponent* part
using UP::SIG_LEN; // The number of bits for the *significand* part
using UP::TOTAL_LEN; // For convenience, the sum of `SIG_LEN`, `EXP_LEN`,
// and `SIGN_LEN`.
static_assert(SIGN_LEN + EXP_LEN + SIG_LEN == TOTAL_LEN);

// An unsigned integer that is wide enough to contain all of the floating
// point bits.
using UIntType = typename UP::UIntType;
using StorageType = typename UP::StorageType;

// The number of bits in UIntType.
LIBC_INLINE_VAR static constexpr int UINTTYPE_BITS =
sizeof(UIntType) * CHAR_BIT;
static_assert(UINTTYPE_BITS >= TOTAL_BITS);
// The number of bits in StorageType.
LIBC_INLINE_VAR static constexpr int STORAGE_LEN =
sizeof(StorageType) * CHAR_BIT;
static_assert(STORAGE_LEN >= TOTAL_LEN);

private:
// The exponent bias. Always positive.
LIBC_INLINE_VAR static constexpr int32_t EXP_BIAS =
(1U << (EXP_BITS - 1U)) - 1U;
(1U << (EXP_LEN - 1U)) - 1U;
static_assert(EXP_BIAS > 0);

private:
// The shift amount to get the *significand* part to the least significant
// bit. Always `0` but kept for consistency.
LIBC_INLINE_VAR static constexpr int SIG_MASK_SHIFT = 0;
// The shift amount to get the *exponent* part to the least significant bit.
LIBC_INLINE_VAR static constexpr int EXP_MASK_SHIFT = SIG_BITS;
LIBC_INLINE_VAR static constexpr int EXP_MASK_SHIFT = SIG_LEN;
// The shift amount to get the *sign* part to the least significant bit.
LIBC_INLINE_VAR static constexpr int SIGN_MASK_SHIFT = SIG_BITS + EXP_BITS;
LIBC_INLINE_VAR static constexpr int SIGN_MASK_SHIFT = SIG_LEN + EXP_LEN;

// The bit pattern that keeps only the *significand* part.
LIBC_INLINE_VAR static constexpr UIntType SIG_MASK =
mask_trailing_ones<UIntType, SIG_BITS>() << SIG_MASK_SHIFT;
// The bit pattern that keeps only the *exponent* part.
LIBC_INLINE_VAR static constexpr UIntType EXP_MASK =
mask_trailing_ones<UIntType, EXP_BITS>() << EXP_MASK_SHIFT;
LIBC_INLINE_VAR static constexpr StorageType SIG_MASK =
mask_trailing_ones<StorageType, SIG_LEN>() << SIG_MASK_SHIFT;

public:
// The bit pattern that keeps only the *exponent* part.
LIBC_INLINE_VAR static constexpr StorageType EXP_MASK =
mask_trailing_ones<StorageType, EXP_LEN>() << EXP_MASK_SHIFT;
// The bit pattern that keeps only the *sign* part.
LIBC_INLINE_VAR static constexpr UIntType SIGN_MASK =
mask_trailing_ones<UIntType, SIGN_BITS>() << SIGN_MASK_SHIFT;
LIBC_INLINE_VAR static constexpr StorageType SIGN_MASK =
mask_trailing_ones<StorageType, SIGN_LEN>() << SIGN_MASK_SHIFT;
// The bit pattern that keeps only the *sign + exponent + significand* part.
LIBC_INLINE_VAR static constexpr UIntType FP_MASK =
mask_trailing_ones<UIntType, TOTAL_BITS>();
LIBC_INLINE_VAR static constexpr StorageType FP_MASK =
mask_trailing_ones<StorageType, TOTAL_LEN>();

static_assert((SIG_MASK & EXP_MASK & SIGN_MASK) == 0, "masks disjoint");
static_assert((SIG_MASK | EXP_MASK | SIGN_MASK) == FP_MASK, "masks cover");

private:
LIBC_INLINE static constexpr UIntType bit_at(int position) {
return UIntType(1) << position;
LIBC_INLINE static constexpr StorageType bit_at(int position) {
return StorageType(1) << position;
}

LIBC_INLINE_VAR static constexpr UIntType QNAN_MASK =
LIBC_INLINE_VAR static constexpr StorageType QNAN_MASK =
UP::ENCODING == internal::FPEncoding::X86_ExtendedPrecision
? bit_at(SIG_BITS - 1) | bit_at(SIG_BITS - 2) // 0b1100...
: bit_at(SIG_BITS - 1); // 0b1000...
? bit_at(SIG_LEN - 1) | bit_at(SIG_LEN - 2) // 0b1100...
: bit_at(SIG_LEN - 1); // 0b1000...

LIBC_INLINE_VAR static constexpr UIntType SNAN_MASK =
LIBC_INLINE_VAR static constexpr StorageType SNAN_MASK =
UP::ENCODING == internal::FPEncoding::X86_ExtendedPrecision
? bit_at(SIG_BITS - 1) | bit_at(SIG_BITS - 3) // 0b1010...
: bit_at(SIG_BITS - 2); // 0b0100...

// The number of bits after the decimal dot when the number if in normal form.
LIBC_INLINE_VAR static constexpr int FRACTION_BITS =
UP::ENCODING == internal::FPEncoding::X86_ExtendedPrecision ? SIG_BITS - 1
: SIG_BITS;
? bit_at(SIG_LEN - 1) | bit_at(SIG_LEN - 3) // 0b1010...
: bit_at(SIG_LEN - 2); // 0b0100...

public:
LIBC_INLINE_VAR static constexpr uint32_t BIT_WIDTH = TOTAL_BITS;
LIBC_INLINE_VAR static constexpr uint32_t MANTISSA_WIDTH = FRACTION_BITS;
// The number of bits after the decimal dot when the number is in normal form.
LIBC_INLINE_VAR static constexpr int FRACTION_LEN =
UP::ENCODING == internal::FPEncoding::X86_ExtendedPrecision ? SIG_LEN - 1
: SIG_LEN;
LIBC_INLINE_VAR static constexpr uint32_t MANTISSA_PRECISION =
MANTISSA_WIDTH + 1;
LIBC_INLINE_VAR static constexpr UIntType MANTISSA_MASK =
mask_trailing_ones<UIntType, MANTISSA_WIDTH>();
LIBC_INLINE_VAR static constexpr uint32_t EXPONENT_WIDTH = EXP_BITS;
LIBC_INLINE_VAR static constexpr int32_t EXPONENT_BIAS = EXP_BIAS;
LIBC_INLINE_VAR static constexpr UIntType EXPONENT_MASK = EXP_MASK;
LIBC_INLINE_VAR static constexpr UIntType EXP_MANT_MASK = EXP_MASK | SIG_MASK;
FRACTION_LEN + 1;
LIBC_INLINE_VAR static constexpr StorageType FRACTION_MASK =
mask_trailing_ones<StorageType, FRACTION_LEN>();
LIBC_INLINE_VAR static constexpr StorageType EXP_MANT_MASK =
EXP_MASK | SIG_MASK;

// If a number x is a NAN, then it is a quiet NAN if:
// QuietNaNMask & bits(x) != 0
// Else, it is a signalling NAN.
static constexpr UIntType QUIET_NAN_MASK = QNAN_MASK;
static constexpr StorageType QUIET_NAN_MASK = QNAN_MASK;
};

//-----------------------------------------------------------------------------
Expand Down
44 changes: 22 additions & 22 deletions libc/src/__support/FPUtil/Hypot.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ template <> struct DoubleLength<uint64_t> {
template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
LIBC_INLINE T hypot(T x, T y) {
using FPBits_t = FPBits<T>;
using UIntType = typename FPBits<T>::UIntType;
using DUIntType = typename DoubleLength<UIntType>::Type;
using StorageType = typename FPBits<T>::StorageType;
using DStorageType = typename DoubleLength<StorageType>::Type;

FPBits_t x_bits(x), y_bits(y);

Expand All @@ -124,13 +124,13 @@ LIBC_INLINE T hypot(T x, T y) {
uint16_t y_exp = y_bits.get_biased_exponent();
uint16_t exp_diff = (x_exp > y_exp) ? (x_exp - y_exp) : (y_exp - x_exp);

if ((exp_diff >= FPBits_t::MANTISSA_WIDTH + 2) || (x == 0) || (y == 0)) {
if ((exp_diff >= FPBits_t::FRACTION_LEN + 2) || (x == 0) || (y == 0)) {
return abs(x) + abs(y);
}

uint16_t a_exp, b_exp, out_exp;
UIntType a_mant, b_mant;
DUIntType a_mant_sq, b_mant_sq;
StorageType a_mant, b_mant;
DStorageType a_mant_sq, b_mant_sq;
bool sticky_bits;

if (abs(x) >= abs(y)) {
Expand All @@ -148,17 +148,17 @@ LIBC_INLINE T hypot(T x, T y) {
out_exp = a_exp;

// Add an extra bit to simplify the final rounding bit computation.
constexpr UIntType ONE = UIntType(1) << (FPBits_t::MANTISSA_WIDTH + 1);
constexpr StorageType ONE = StorageType(1) << (FPBits_t::FRACTION_LEN + 1);

a_mant <<= 1;
b_mant <<= 1;

UIntType leading_one;
StorageType leading_one;
int y_mant_width;
if (a_exp != 0) {
leading_one = ONE;
a_mant |= ONE;
y_mant_width = FPBits_t::MANTISSA_WIDTH + 1;
y_mant_width = FPBits_t::FRACTION_LEN + 1;
} else {
leading_one = internal::find_leading_one(a_mant, y_mant_width);
a_exp = 1;
Expand All @@ -170,8 +170,8 @@ LIBC_INLINE T hypot(T x, T y) {
b_exp = 1;
}

a_mant_sq = static_cast<DUIntType>(a_mant) * a_mant;
b_mant_sq = static_cast<DUIntType>(b_mant) * b_mant;
a_mant_sq = static_cast<DStorageType>(a_mant) * a_mant;
b_mant_sq = static_cast<DStorageType>(b_mant) * b_mant;

// At this point, a_exp >= b_exp > a_exp - 25, so in order to line up aSqMant
// and bSqMant, we need to shift bSqMant to the right by (a_exp - b_exp) bits.
Expand All @@ -180,12 +180,12 @@ LIBC_INLINE T hypot(T x, T y) {
// difference between a and b.
uint16_t shift_length = static_cast<uint16_t>(2 * (a_exp - b_exp));
sticky_bits =
((b_mant_sq & ((DUIntType(1) << shift_length) - DUIntType(1))) !=
DUIntType(0));
((b_mant_sq & ((DStorageType(1) << shift_length) - DStorageType(1))) !=
DStorageType(0));
b_mant_sq >>= shift_length;

DUIntType sum = a_mant_sq + b_mant_sq;
if (sum >= (DUIntType(1) << (2 * y_mant_width + 2))) {
DStorageType sum = a_mant_sq + b_mant_sq;
if (sum >= (DStorageType(1) << (2 * y_mant_width + 2))) {
// a^2 + b^2 >= 4* leading_one^2, so we will need an extra bit to the left.
if (leading_one == ONE) {
// For normal result, we discard the last 2 bits of the sum and increase
Expand All @@ -207,22 +207,22 @@ LIBC_INLINE T hypot(T x, T y) {
}
}

UIntType y_new = leading_one;
UIntType r = static_cast<UIntType>(sum >> y_mant_width) - leading_one;
UIntType tail_bits = static_cast<UIntType>(sum) & (leading_one - 1);
StorageType y_new = leading_one;
StorageType r = static_cast<StorageType>(sum >> y_mant_width) - leading_one;
StorageType tail_bits = static_cast<StorageType>(sum) & (leading_one - 1);

for (UIntType current_bit = leading_one >> 1; current_bit;
for (StorageType current_bit = leading_one >> 1; current_bit;
current_bit >>= 1) {
r = (r << 1) + ((tail_bits & current_bit) ? 1 : 0);
UIntType tmp = (y_new << 1) + current_bit; // 2*y_new(n - 1) + 2^(-n)
StorageType tmp = (y_new << 1) + current_bit; // 2*y_new(n - 1) + 2^(-n)
if (r >= tmp) {
r -= tmp;
y_new += current_bit;
}
}

bool round_bit = y_new & UIntType(1);
bool lsb = y_new & UIntType(2);
bool round_bit = y_new & StorageType(1);
bool lsb = y_new & StorageType(2);

if (y_new >= ONE) {
y_new -= ONE;
Expand Down Expand Up @@ -258,7 +258,7 @@ LIBC_INLINE T hypot(T x, T y) {
}
}

y_new |= static_cast<UIntType>(out_exp) << FPBits_t::MANTISSA_WIDTH;
y_new |= static_cast<StorageType>(out_exp) << FPBits_t::FRACTION_LEN;
return cpp::bit_cast<T>(y_new);
}

Expand Down
12 changes: 6 additions & 6 deletions libc/src/__support/FPUtil/ManipulationFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ LIBC_INLINE T ldexp(T x, int exp) {
// early. Because the result of the ldexp operation can be a subnormal number,
// we need to accommodate the (mantissaWidht + 1) worth of shift in
// calculating the limit.
int exp_limit = FPBits<T>::MAX_EXPONENT + FPBits<T>::MANTISSA_WIDTH + 1;
int exp_limit = FPBits<T>::MAX_EXPONENT + FPBits<T>::FRACTION_LEN + 1;
if (exp > exp_limit)
return bits.get_sign() ? T(FPBits<T>::neg_inf()) : T(FPBits<T>::inf());

Expand Down Expand Up @@ -164,8 +164,8 @@ LIBC_INLINE T nextafter(T from, U to) {
if (static_cast<U>(from) == to)
return static_cast<T>(to);

using UIntType = typename FPBits<T>::UIntType;
UIntType int_val = from_bits.uintval();
using StorageType = typename FPBits<T>::StorageType;
StorageType int_val = from_bits.uintval();
if (from != FPBits<T>::zero()) {
if ((static_cast<U>(from) < to) == (from > FPBits<T>::zero())) {
++int_val;
Expand All @@ -178,10 +178,10 @@ LIBC_INLINE T nextafter(T from, U to) {
int_val |= FloatProperties<T>::SIGN_MASK;
}

UIntType exponent_bits = int_val & FloatProperties<T>::EXPONENT_MASK;
if (exponent_bits == UIntType(0))
StorageType exponent_bits = int_val & FloatProperties<T>::EXP_MASK;
if (exponent_bits == StorageType(0))
raise_except_if_required(FE_UNDERFLOW | FE_INEXACT);
else if (exponent_bits == FloatProperties<T>::EXPONENT_MASK)
else if (exponent_bits == FloatProperties<T>::EXP_MASK)
raise_except_if_required(FE_OVERFLOW | FE_INEXACT);

return cpp::bit_cast<T>(int_val);
Expand Down
30 changes: 16 additions & 14 deletions libc/src/__support/FPUtil/NearestIntegerOperations.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ LIBC_INLINE T trunc(T x) {

// If the exponent is greater than the most negative mantissa
// exponent, then x is already an integer.
if (exponent >= static_cast<int>(FPBits<T>::MANTISSA_WIDTH))
if (exponent >= static_cast<int>(FPBits<T>::FRACTION_LEN))
return x;

// If the exponent is such that abs(x) is less than 1, then return 0.
Expand All @@ -47,7 +47,7 @@ LIBC_INLINE T trunc(T x) {
return T(0.0);
}

int trim_size = FPBits<T>::MANTISSA_WIDTH - exponent;
int trim_size = FPBits<T>::FRACTION_LEN - exponent;
bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
return T(bits);
}
Expand All @@ -65,7 +65,7 @@ LIBC_INLINE T ceil(T x) {

// If the exponent is greater than the most negative mantissa
// exponent, then x is already an integer.
if (exponent >= static_cast<int>(FPBits<T>::MANTISSA_WIDTH))
if (exponent >= static_cast<int>(FPBits<T>::FRACTION_LEN))
return x;

if (exponent <= -1) {
Expand All @@ -75,7 +75,7 @@ LIBC_INLINE T ceil(T x) {
return T(1.0);
}

uint32_t trim_size = FPBits<T>::MANTISSA_WIDTH - exponent;
uint32_t trim_size = FPBits<T>::FRACTION_LEN - exponent;
bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
T trunc_value = T(bits);

Expand All @@ -102,7 +102,7 @@ LIBC_INLINE T floor(T x) {

template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
LIBC_INLINE T round(T x) {
using UIntType = typename FPBits<T>::UIntType;
using StorageType = typename FPBits<T>::StorageType;
FPBits<T> bits(x);

// If x is infinity NaN or zero, return it.
Expand All @@ -114,7 +114,7 @@ LIBC_INLINE T round(T x) {

// If the exponent is greater than the most negative mantissa
// exponent, then x is already an integer.
if (exponent >= static_cast<int>(FPBits<T>::MANTISSA_WIDTH))
if (exponent >= static_cast<int>(FPBits<T>::FRACTION_LEN))
return x;

if (exponent == -1) {
Expand All @@ -133,9 +133,9 @@ LIBC_INLINE T round(T x) {
return T(0.0);
}

uint32_t trim_size = FPBits<T>::MANTISSA_WIDTH - exponent;
uint32_t trim_size = FPBits<T>::FRACTION_LEN - exponent;
bool half_bit_set =
bool(bits.get_mantissa() & (UIntType(1) << (trim_size - 1)));
bool(bits.get_mantissa() & (StorageType(1) << (trim_size - 1)));
bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
T trunc_value = T(bits);

Expand All @@ -154,7 +154,7 @@ LIBC_INLINE T round(T x) {

template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
LIBC_INLINE T round_using_current_rounding_mode(T x) {
using UIntType = typename FPBits<T>::UIntType;
using StorageType = typename FPBits<T>::StorageType;
FPBits<T> bits(x);

// If x is infinity NaN or zero, return it.
Expand All @@ -167,7 +167,7 @@ LIBC_INLINE T round_using_current_rounding_mode(T x) {

// If the exponent is greater than the most negative mantissa
// exponent, then x is already an integer.
if (exponent >= static_cast<int>(FPBits<T>::MANTISSA_WIDTH))
if (exponent >= static_cast<int>(FPBits<T>::FRACTION_LEN))
return x;

if (exponent <= -1) {
Expand All @@ -188,7 +188,7 @@ LIBC_INLINE T round_using_current_rounding_mode(T x) {
}
}

uint32_t trim_size = FPBits<T>::MANTISSA_WIDTH - exponent;
uint32_t trim_size = FPBits<T>::FRACTION_LEN - exponent;
FPBits<T> new_bits = bits;
new_bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
T trunc_value = T(new_bits);
Expand All @@ -197,12 +197,14 @@ LIBC_INLINE T round_using_current_rounding_mode(T x) {
if (trunc_value == x)
return x;

UIntType trim_value = bits.get_mantissa() & ((UIntType(1) << trim_size) - 1);
UIntType half_value = (UIntType(1) << (trim_size - 1));
StorageType trim_value =
bits.get_mantissa() & ((StorageType(1) << trim_size) - 1);
StorageType half_value = (StorageType(1) << (trim_size - 1));
// If exponent is 0, trimSize will be equal to the mantissa width, and
// truncIsOdd` will not be correct. So, we handle it as a special case
// below.
UIntType trunc_is_odd = new_bits.get_mantissa() & (UIntType(1) << trim_size);
StorageType trunc_is_odd =
new_bits.get_mantissa() & (StorageType(1) << trim_size);

switch (rounding_mode) {
case FE_DOWNWARD:
Expand Down
57 changes: 29 additions & 28 deletions libc/src/__support/FPUtil/NormalFloat.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,22 @@ template <typename T> struct NormalFloat {
cpp::is_floating_point_v<T>,
"NormalFloat template parameter has to be a floating point type.");

using UIntType = typename FPBits<T>::UIntType;
static constexpr UIntType ONE = (UIntType(1) << FPBits<T>::MANTISSA_WIDTH);
using StorageType = typename FPBits<T>::StorageType;
static constexpr StorageType ONE =
(StorageType(1) << FPBits<T>::FRACTION_LEN);

// Unbiased exponent value.
int32_t exponent;

UIntType mantissa;
// We want |UIntType| to have atleast one bit more than the actual mantissa
StorageType mantissa;
// We want |StorageType| to have atleast one bit more than the actual mantissa
// bit width to accommodate the implicit 1 value.
static_assert(sizeof(UIntType) * 8 >= FPBits<T>::MANTISSA_WIDTH + 1,
static_assert(sizeof(StorageType) * 8 >= FPBits<T>::FRACTION_LEN + 1,
"Bad type for mantissa in NormalFloat.");

bool sign;

LIBC_INLINE NormalFloat(int32_t e, UIntType m, bool s)
LIBC_INLINE NormalFloat(int32_t e, StorageType m, bool s)
: exponent(e), mantissa(m), sign(s) {
if (mantissa >= ONE)
return;
Expand Down Expand Up @@ -90,30 +91,30 @@ template <typename T> struct NormalFloat {
}

LIBC_INLINE operator T() const {
int biased_exponent = exponent + FPBits<T>::EXPONENT_BIAS;
int biased_exponent = exponent + FPBits<T>::EXP_BIAS;
// Max exponent is of the form 0xFF...E. That is why -2 and not -1.
constexpr int MAX_EXPONENT_VALUE = (1 << FPBits<T>::EXPONENT_WIDTH) - 2;
constexpr int MAX_EXPONENT_VALUE = (1 << FPBits<T>::EXP_LEN) - 2;
if (biased_exponent > MAX_EXPONENT_VALUE) {
return sign ? T(FPBits<T>::neg_inf()) : T(FPBits<T>::inf());
}

FPBits<T> result(T(0.0));
result.set_sign(sign);

constexpr int SUBNORMAL_EXPONENT = -FPBits<T>::EXPONENT_BIAS + 1;
constexpr int SUBNORMAL_EXPONENT = -FPBits<T>::EXP_BIAS + 1;
if (exponent < SUBNORMAL_EXPONENT) {
unsigned shift = SUBNORMAL_EXPONENT - exponent;
// Since exponent > subnormalExponent, shift is strictly greater than
// zero.
if (shift <= FPBits<T>::MANTISSA_WIDTH + 1) {
if (shift <= FPBits<T>::FRACTION_LEN + 1) {
// Generate a subnormal number. Might lead to loss of precision.
// We round to nearest and round halfway cases to even.
const UIntType shift_out_mask = (UIntType(1) << shift) - 1;
const UIntType shift_out_value = mantissa & shift_out_mask;
const UIntType halfway_value = UIntType(1) << (shift - 1);
const StorageType shift_out_mask = (StorageType(1) << shift) - 1;
const StorageType shift_out_value = mantissa & shift_out_mask;
const StorageType halfway_value = StorageType(1) << (shift - 1);
result.set_biased_exponent(0);
result.set_mantissa(mantissa >> shift);
UIntType new_mantissa = result.get_mantissa();
StorageType new_mantissa = result.get_mantissa();
if (shift_out_value > halfway_value) {
new_mantissa += 1;
} else if (shift_out_value == halfway_value) {
Expand All @@ -133,7 +134,7 @@ template <typename T> struct NormalFloat {
}
}

result.set_biased_exponent(exponent + FPBits<T>::EXPONENT_BIAS);
result.set_biased_exponent(exponent + FPBits<T>::EXP_BIAS);
result.set_mantissa(mantissa);
return T(result);
}
Expand All @@ -153,17 +154,17 @@ template <typename T> struct NormalFloat {
// Normalize subnormal numbers.
if (bits.get_biased_exponent() == 0) {
unsigned shift = evaluate_normalization_shift(bits.get_mantissa());
mantissa = UIntType(bits.get_mantissa()) << shift;
exponent = 1 - FPBits<T>::EXPONENT_BIAS - shift;
mantissa = StorageType(bits.get_mantissa()) << shift;
exponent = 1 - FPBits<T>::EXP_BIAS - shift;
} else {
exponent = bits.get_biased_exponent() - FPBits<T>::EXPONENT_BIAS;
exponent = bits.get_biased_exponent() - FPBits<T>::EXP_BIAS;
mantissa = ONE | bits.get_mantissa();
}
}

LIBC_INLINE unsigned evaluate_normalization_shift(UIntType m) {
LIBC_INLINE unsigned evaluate_normalization_shift(StorageType m) {
unsigned shift = 0;
for (; (ONE & m) == 0 && (shift < FPBits<T>::MANTISSA_WIDTH);
for (; (ONE & m) == 0 && (shift < FPBits<T>::FRACTION_LEN);
m <<= 1, ++shift)
;
return shift;
Expand Down Expand Up @@ -209,28 +210,28 @@ NormalFloat<long double>::init_from_bits(FPBits<long double> bits) {

template <> LIBC_INLINE NormalFloat<long double>::operator long double() const {
using LDBits = FPBits<long double>;
int biased_exponent = exponent + LDBits::EXPONENT_BIAS;
int biased_exponent = exponent + LDBits::EXP_BIAS;
// Max exponent is of the form 0xFF...E. That is why -2 and not -1.
constexpr int MAX_EXPONENT_VALUE = (1 << LDBits::EXPONENT_WIDTH) - 2;
constexpr int MAX_EXPONENT_VALUE = (1 << LDBits::EXP_LEN) - 2;
if (biased_exponent > MAX_EXPONENT_VALUE) {
return sign ? LDBits::neg_inf() : LDBits::inf();
}

FPBits<long double> result(0.0l);
result.set_sign(sign);

constexpr int SUBNORMAL_EXPONENT = -LDBits::EXPONENT_BIAS + 1;
constexpr int SUBNORMAL_EXPONENT = -LDBits::EXP_BIAS + 1;
if (exponent < SUBNORMAL_EXPONENT) {
unsigned shift = SUBNORMAL_EXPONENT - exponent;
if (shift <= LDBits::MANTISSA_WIDTH + 1) {
if (shift <= LDBits::FRACTION_LEN + 1) {
// Generate a subnormal number. Might lead to loss of precision.
// We round to nearest and round halfway cases to even.
const UIntType shift_out_mask = (UIntType(1) << shift) - 1;
const UIntType shift_out_value = mantissa & shift_out_mask;
const UIntType halfway_value = UIntType(1) << (shift - 1);
const StorageType shift_out_mask = (StorageType(1) << shift) - 1;
const StorageType shift_out_value = mantissa & shift_out_mask;
const StorageType halfway_value = StorageType(1) << (shift - 1);
result.set_biased_exponent(0);
result.set_mantissa(mantissa >> shift);
UIntType new_mantissa = result.get_mantissa();
StorageType new_mantissa = result.get_mantissa();
if (shift_out_value > halfway_value) {
new_mantissa += 1;
} else if (shift_out_value == halfway_value) {
Expand Down
30 changes: 15 additions & 15 deletions libc/src/__support/FPUtil/dyadic_float.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ template <size_t Bits> struct DyadicFloat {

template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
DyadicFloat(T x) {
static_assert(FloatProperties<T>::MANTISSA_WIDTH < Bits);
static_assert(FloatProperties<T>::FRACTION_LEN < Bits);
FPBits<T> x_bits(x);
sign = x_bits.get_sign();
exponent = x_bits.get_exponent() - FloatProperties<T>::MANTISSA_WIDTH;
exponent = x_bits.get_exponent() - FloatProperties<T>::FRACTION_LEN;
mantissa = MantissaType(x_bits.get_explicit_mantissa());
normalize();
}
Expand Down Expand Up @@ -86,7 +86,7 @@ template <size_t Bits> struct DyadicFloat {
// TODO(lntue): Test or add specialization for x86 long double.
template <typename T, typename = cpp::enable_if_t<
cpp::is_floating_point_v<T> &&
(FloatProperties<T>::MANTISSA_WIDTH < Bits),
(FloatProperties<T>::FRACTION_LEN < Bits),
void>>
explicit operator T() const {
// TODO(lntue): Do we need to treat signed zeros properly?
Expand All @@ -95,10 +95,10 @@ template <size_t Bits> struct DyadicFloat {

// Assume that it is normalized, and output is also normal.
constexpr uint32_t PRECISION = FloatProperties<T>::MANTISSA_PRECISION;
using output_bits_t = typename FPBits<T>::UIntType;
using output_bits_t = typename FPBits<T>::StorageType;

int exp_hi = exponent + static_cast<int>((Bits - 1) +
FloatProperties<T>::EXPONENT_BIAS);
int exp_hi =
exponent + static_cast<int>((Bits - 1) + FloatProperties<T>::EXP_BIAS);

bool denorm = false;
uint32_t shift = Bits - PRECISION;
Expand All @@ -107,7 +107,7 @@ template <size_t Bits> struct DyadicFloat {
denorm = true;
shift = (Bits - PRECISION) + static_cast<uint32_t>(1 - exp_hi);

exp_hi = FloatProperties<T>::EXPONENT_BIAS;
exp_hi = FloatProperties<T>::EXP_BIAS;
}

int exp_lo = exp_hi - static_cast<int>(PRECISION) - 1;
Expand All @@ -116,7 +116,7 @@ template <size_t Bits> struct DyadicFloat {

T d_hi = FPBits<T>::create_value(sign, exp_hi,
static_cast<output_bits_t>(m_hi) &
FloatProperties<T>::MANTISSA_MASK)
FloatProperties<T>::FRACTION_MASK)
.get_val();

const MantissaType round_mask = MantissaType(1) << (shift - 1);
Expand All @@ -130,14 +130,14 @@ template <size_t Bits> struct DyadicFloat {
if (LIBC_UNLIKELY(exp_lo <= 0)) {
// d_lo is denormal, but the output is normal.
int scale_up_exponent = 2 * PRECISION;
T scale_up_factor =
FPBits<T>::create_value(
sign, FloatProperties<T>::EXPONENT_BIAS + scale_up_exponent,
output_bits_t(0))
.get_val();
T scale_up_factor = FPBits<T>::create_value(sign,
FloatProperties<T>::EXP_BIAS +
scale_up_exponent,
output_bits_t(0))
.get_val();
T scale_down_factor =
FPBits<T>::create_value(
sign, FloatProperties<T>::EXPONENT_BIAS - scale_up_exponent,
sign, FloatProperties<T>::EXP_BIAS - scale_up_exponent,
output_bits_t(0))
.get_val();

Expand All @@ -157,7 +157,7 @@ template <size_t Bits> struct DyadicFloat {
if (LIBC_UNLIKELY(denorm)) {
// Output is denormal, simply clear the exponent field.
output_bits_t clear_exp = output_bits_t(exp_hi)
<< FloatProperties<T>::MANTISSA_WIDTH;
<< FloatProperties<T>::FRACTION_LEN;
output_bits_t r_bits = FPBits<T>(r).uintval() - clear_exp;
return FPBits<T>(r_bits).get_val();
}
Expand Down
22 changes: 11 additions & 11 deletions libc/src/__support/FPUtil/except_value_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace fputil {
//
// Define list of exceptional inputs and outputs:
// static constexpr int N = ...; // Number of exceptional values.
// static constexpr fputil::ExceptValues<UIntType, N> Excepts {
// static constexpr fputil::ExceptValues<StorageType, N> Excepts {
// <list of input bits, output bits and offsets>
// };
//
Expand All @@ -37,22 +37,22 @@ namespace fputil {
template <typename T, size_t N> struct ExceptValues {
static_assert(cpp::is_floating_point_v<T>, "Must be a floating point type.");

using UIntType = typename FPBits<T>::UIntType;
using StorageType = typename FPBits<T>::StorageType;

struct Mapping {
UIntType input;
UIntType rnd_towardzero_result;
UIntType rnd_upward_offset;
UIntType rnd_downward_offset;
UIntType rnd_tonearest_offset;
StorageType input;
StorageType rnd_towardzero_result;
StorageType rnd_upward_offset;
StorageType rnd_downward_offset;
StorageType rnd_tonearest_offset;
};

Mapping values[N];

LIBC_INLINE constexpr cpp::optional<T> lookup(UIntType x_bits) const {
LIBC_INLINE constexpr cpp::optional<T> lookup(StorageType x_bits) const {
for (size_t i = 0; i < N; ++i) {
if (LIBC_UNLIKELY(x_bits == values[i].input)) {
UIntType out_bits = values[i].rnd_towardzero_result;
StorageType out_bits = values[i].rnd_towardzero_result;
switch (fputil::quick_get_round()) {
case FE_UPWARD:
out_bits += values[i].rnd_upward_offset;
Expand All @@ -70,11 +70,11 @@ template <typename T, size_t N> struct ExceptValues {
return cpp::nullopt;
}

LIBC_INLINE constexpr cpp::optional<T> lookup_odd(UIntType x_abs,
LIBC_INLINE constexpr cpp::optional<T> lookup_odd(StorageType x_abs,
bool sign) const {
for (size_t i = 0; i < N; ++i) {
if (LIBC_UNLIKELY(x_abs == values[i].input)) {
UIntType out_bits = values[i].rnd_towardzero_result;
StorageType out_bits = values[i].rnd_towardzero_result;
switch (fputil::quick_get_round()) {
case FE_UPWARD:
out_bits += sign ? values[i].rnd_downward_offset
Expand Down
9 changes: 4 additions & 5 deletions libc/src/__support/FPUtil/fpbits_str.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ using ZeroPaddedHexFmt = IntegerToString<
// 3. The exponent is always 16 bits wide irrespective of the type of the
// floating encoding.
template <typename T> LIBC_INLINE cpp::string str(fputil::FPBits<T> x) {
using UIntType = typename fputil::FPBits<T>::UIntType;
using StorageType = typename fputil::FPBits<T>::StorageType;

if (x.is_nan())
return "(NaN)";
Expand All @@ -46,7 +46,7 @@ template <typename T> LIBC_INLINE cpp::string str(fputil::FPBits<T> x) {

cpp::string s;

const details::ZeroPaddedHexFmt<UIntType> bits(x.bits);
const details::ZeroPaddedHexFmt<StorageType> bits(x.bits);
s += bits.view();

s += " = (S: ";
Expand All @@ -56,14 +56,13 @@ template <typename T> LIBC_INLINE cpp::string str(fputil::FPBits<T> x) {
const details::ZeroPaddedHexFmt<uint16_t> exponent(x.get_biased_exponent());
s += exponent.view();

if constexpr (cpp::is_same_v<T, long double> &&
fputil::FloatProperties<long double>::MANTISSA_WIDTH == 63) {
if constexpr (fputil::get_fp_type<T>() == fputil::FPType::X86_Binary80) {
s += ", I: ";
s += sign_char(x.get_implicit_bit());
}

s += ", M: ";
const details::ZeroPaddedHexFmt<UIntType> mantissa(x.get_mantissa());
const details::ZeroPaddedHexFmt<StorageType> mantissa(x.get_mantissa());
s += mantissa.view();

s += ')';
Expand Down
8 changes: 4 additions & 4 deletions libc/src/__support/FPUtil/generic/FMA.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ template <> LIBC_INLINE double fma<double>(double x, double y, double z) {

UInt128 prod_mant = x_mant * y_mant << 10;
int prod_lsb_exp =
x_exp + y_exp - (FPBits::EXPONENT_BIAS + 2 * FPBits::MANTISSA_WIDTH + 10);
x_exp + y_exp - (FPBits::EXP_BIAS + 2 * FPBits::FRACTION_LEN + 10);

z_mant <<= 64;
int z_lsb_exp = z_exp - (FPBits::MANTISSA_WIDTH + 64);
int z_lsb_exp = z_exp - (FPBits::FRACTION_LEN + 64);
bool round_bit = false;
bool sticky_bits = false;
bool z_shifted = false;
Expand Down Expand Up @@ -268,8 +268,8 @@ template <> LIBC_INLINE double fma<double>(double x, double y, double z) {
}

// Remove hidden bit and append the exponent field and sign bit.
result = (result & FloatProp::MANTISSA_MASK) |
(static_cast<uint64_t>(r_exp) << FloatProp::MANTISSA_WIDTH);
result = (result & FloatProp::FRACTION_MASK) |
(static_cast<uint64_t>(r_exp) << FloatProp::FRACTION_LEN);
if (prod_sign) {
result |= FloatProp::SIGN_MASK;
}
Expand Down
46 changes: 25 additions & 21 deletions libc/src/__support/FPUtil/generic/FMod.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,13 @@ template <typename T> struct FModFastMathWrapper {

template <typename T> class FModDivisionSimpleHelper {
private:
using UIntType = typename FPBits<T>::UIntType;
using StorageType = typename FPBits<T>::StorageType;

public:
LIBC_INLINE constexpr static UIntType
execute(int exp_diff, int sides_zeroes_count, UIntType m_x, UIntType m_y) {
LIBC_INLINE constexpr static StorageType execute(int exp_diff,
int sides_zeroes_count,
StorageType m_x,
StorageType m_y) {
while (exp_diff > sides_zeroes_count) {
exp_diff -= sides_zeroes_count;
m_x <<= sides_zeroes_count;
Expand All @@ -186,22 +188,25 @@ template <typename T> class FModDivisionSimpleHelper {
template <typename T> class FModDivisionInvMultHelper {
private:
using FPB = FPBits<T>;
using UIntType = typename FPB::UIntType;
using StorageType = typename FPB::StorageType;

public:
LIBC_INLINE constexpr static UIntType
execute(int exp_diff, int sides_zeroes_count, UIntType m_x, UIntType m_y) {
LIBC_INLINE constexpr static StorageType execute(int exp_diff,
int sides_zeroes_count,
StorageType m_x,
StorageType m_y) {
if (exp_diff > sides_zeroes_count) {
UIntType inv_hy = (cpp::numeric_limits<UIntType>::max() / m_y);
StorageType inv_hy = (cpp::numeric_limits<StorageType>::max() / m_y);
while (exp_diff > sides_zeroes_count) {
exp_diff -= sides_zeroes_count;
UIntType hd = (m_x * inv_hy) >> (FPB::BIT_WIDTH - sides_zeroes_count);
StorageType hd =
(m_x * inv_hy) >> (FPB::TOTAL_LEN - sides_zeroes_count);
m_x <<= sides_zeroes_count;
m_x -= hd * m_y;
while (LIBC_UNLIKELY(m_x > m_y))
m_x -= m_y;
}
UIntType hd = (m_x * inv_hy) >> (FPB::BIT_WIDTH - exp_diff);
StorageType hd = (m_x * inv_hy) >> (FPB::TOTAL_LEN - exp_diff);
m_x <<= exp_diff;
m_x -= hd * m_y;
while (LIBC_UNLIKELY(m_x > m_y))
Expand All @@ -222,7 +227,7 @@ class FMod {

private:
using FPB = FPBits<T>;
using UIntType = typename FPB::UIntType;
using StorageType = typename FPB::StorageType;

LIBC_INLINE static constexpr FPB eval_internal(FPB sx, FPB sy) {

Expand All @@ -235,12 +240,12 @@ class FMod {
int e_x = sx.get_biased_exponent();
int e_y = sy.get_biased_exponent();

// Most common case where |y| is "very normal" and |x/y| < 2^EXPONENT_WIDTH
if (LIBC_LIKELY(e_y > int(FPB::MANTISSA_WIDTH) &&
e_x - e_y <= int(FPB::EXPONENT_WIDTH))) {
UIntType m_x = sx.get_explicit_mantissa();
UIntType m_y = sy.get_explicit_mantissa();
UIntType d = (e_x == e_y) ? (m_x - m_y) : (m_x << (e_x - e_y)) % m_y;
// Most common case where |y| is "very normal" and |x/y| < 2^EXP_LEN
if (LIBC_LIKELY(e_y > int(FPB::FRACTION_LEN) &&
e_x - e_y <= int(FPB::EXP_LEN))) {
StorageType m_x = sx.get_explicit_mantissa();
StorageType m_y = sy.get_explicit_mantissa();
StorageType d = (e_x == e_y) ? (m_x - m_y) : (m_x << (e_x - e_y)) % m_y;
if (d == 0)
return FPB(FPB::zero());
// iy - 1 because of "zero power" for number with power 1
Expand All @@ -254,11 +259,11 @@ class FMod {
}

// Note that hx is not subnormal by conditions above.
UIntType m_x = sx.get_explicit_mantissa();
StorageType m_x = sx.get_explicit_mantissa();
e_x--;

UIntType m_y = sy.get_explicit_mantissa();
int lead_zeros_m_y = FPB::EXPONENT_WIDTH;
StorageType m_y = sy.get_explicit_mantissa();
int lead_zeros_m_y = FPB::EXP_LEN;
if (LIBC_LIKELY(e_y > 0)) {
e_y--;
} else {
Expand All @@ -281,8 +286,7 @@ class FMod {

{
// Shift hx left until the end or n = 0
int left_shift =
exp_diff < int(FPB::EXPONENT_WIDTH) ? exp_diff : FPB::EXPONENT_WIDTH;
int left_shift = exp_diff < int(FPB::EXP_LEN) ? exp_diff : FPB::EXP_LEN;
m_x <<= left_shift;
exp_diff -= left_shift;
}
Expand Down
24 changes: 12 additions & 12 deletions libc/src/__support/FPUtil/generic/sqrt.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ template <> struct SpecialLongDouble<long double> {

template <typename T>
LIBC_INLINE void normalize(int &exponent,
typename FPBits<T>::UIntType &mantissa) {
typename FPBits<T>::StorageType &mantissa) {
const int shift = cpp::countl_zero(mantissa) -
(8 * sizeof(mantissa) - 1 - FPBits<T>::MANTISSA_WIDTH);
(8 * sizeof(mantissa) - 1 - FPBits<T>::FRACTION_LEN);
exponent -= shift;
mantissa <<= shift;
}
Expand Down Expand Up @@ -71,8 +71,8 @@ LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, T> sqrt(T x) {
return x86::sqrt(x);
} else {
// IEEE floating points formats.
using UIntType = typename FPBits<T>::UIntType;
constexpr UIntType ONE = UIntType(1) << FPBits<T>::MANTISSA_WIDTH;
using StorageType = typename FPBits<T>::StorageType;
constexpr StorageType ONE = StorageType(1) << FPBits<T>::FRACTION_LEN;

FPBits<T> bits(x);

Expand All @@ -94,7 +94,7 @@ LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, T> sqrt(T x) {
return FPBits<T>::build_quiet_nan(ONE >> 1);
} else {
int x_exp = bits.get_exponent();
UIntType x_mant = bits.get_mantissa();
StorageType x_mant = bits.get_mantissa();

// Step 1a: Normalize denormal input and append hidden bit to the mantissa
if (bits.get_biased_exponent() == 0) {
Expand Down Expand Up @@ -122,12 +122,12 @@ LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, T> sqrt(T x) {
// So the nth digit y_n of the mantissa of sqrt(x) can be found by:
// y_n = 1 if 2*r(n-1) >= 2*y(n - 1) + 2^(-n-1)
// 0 otherwise.
UIntType y = ONE;
UIntType r = x_mant - ONE;
StorageType y = ONE;
StorageType r = x_mant - ONE;

for (UIntType current_bit = ONE >> 1; current_bit; current_bit >>= 1) {
for (StorageType current_bit = ONE >> 1; current_bit; current_bit >>= 1) {
r <<= 1;
UIntType tmp = (y << 1) + current_bit; // 2*y(n - 1) + 2^(-n-1)
StorageType tmp = (y << 1) + current_bit; // 2*y(n - 1) + 2^(-n-1)
if (r >= tmp) {
r -= tmp;
y += current_bit;
Expand All @@ -138,17 +138,17 @@ LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, T> sqrt(T x) {
bool lsb = static_cast<bool>(y & 1); // Least significant bit
bool rb = false; // Round bit
r <<= 2;
UIntType tmp = (y << 2) + 1;
StorageType tmp = (y << 2) + 1;
if (r >= tmp) {
r -= tmp;
rb = true;
}

// Remove hidden bit and append the exponent field.
x_exp = ((x_exp >> 1) + FPBits<T>::EXPONENT_BIAS);
x_exp = ((x_exp >> 1) + FPBits<T>::EXP_BIAS);

y = (y - ONE) |
(static_cast<UIntType>(x_exp) << FPBits<T>::MANTISSA_WIDTH);
(static_cast<StorageType>(x_exp) << FPBits<T>::FRACTION_LEN);

switch (quick_get_round()) {
case FE_TONEAREST:
Expand Down
22 changes: 11 additions & 11 deletions libc/src/__support/FPUtil/generic/sqrt_80_bit_long_double.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace x86 {
LIBC_INLINE void normalize(int &exponent, UInt128 &mantissa) {
const unsigned int shift = static_cast<unsigned int>(
cpp::countl_zero(static_cast<uint64_t>(mantissa)) -
(8 * sizeof(uint64_t) - 1 - FPBits<long double>::MANTISSA_WIDTH));
(8 * sizeof(uint64_t) - 1 - FPBits<long double>::FRACTION_LEN));
exponent -= shift;
mantissa <<= shift;
}
Expand All @@ -37,8 +37,8 @@ LIBC_INLINE long double sqrt(long double x);
#if defined(LIBC_LONG_DOUBLE_IS_X86_FLOAT80)
LIBC_INLINE long double sqrt(long double x) {
using LDBits = FPBits<long double>;
using UIntType = typename LDBits::UIntType;
constexpr UIntType ONE = UIntType(1) << int(LDBits::MANTISSA_WIDTH);
using StorageType = typename LDBits::StorageType;
constexpr StorageType ONE = StorageType(1) << int(LDBits::FRACTION_LEN);

FPBits<long double> bits(x);

Expand All @@ -60,7 +60,7 @@ LIBC_INLINE long double sqrt(long double x) {
return LDBits::build_quiet_nan(ONE >> 1);
} else {
int x_exp = bits.get_explicit_exponent();
UIntType x_mant = bits.get_mantissa();
StorageType x_mant = bits.get_mantissa();

// Step 1a: Normalize denormal input
if (bits.get_implicit_bit()) {
Expand All @@ -87,12 +87,12 @@ LIBC_INLINE long double sqrt(long double x) {
// So the nth digit y_n of the mantissa of sqrt(x) can be found by:
// y_n = 1 if 2*r(n-1) >= 2*y(n - 1) + 2^(-n-1)
// 0 otherwise.
UIntType y = ONE;
UIntType r = x_mant - ONE;
StorageType y = ONE;
StorageType r = x_mant - ONE;

for (UIntType current_bit = ONE >> 1; current_bit; current_bit >>= 1) {
for (StorageType current_bit = ONE >> 1; current_bit; current_bit >>= 1) {
r <<= 1;
UIntType tmp = (y << 1) + current_bit; // 2*y(n - 1) + 2^(-n-1)
StorageType tmp = (y << 1) + current_bit; // 2*y(n - 1) + 2^(-n-1)
if (r >= tmp) {
r -= tmp;
y += current_bit;
Expand All @@ -103,15 +103,15 @@ LIBC_INLINE long double sqrt(long double x) {
bool lsb = static_cast<bool>(y & 1); // Least significant bit
bool rb = false; // Round bit
r <<= 2;
UIntType tmp = (y << 2) + 1;
StorageType tmp = (y << 2) + 1;
if (r >= tmp) {
r -= tmp;
rb = true;
}

// Append the exponent field.
x_exp = ((x_exp >> 1) + LDBits::EXPONENT_BIAS);
y |= (static_cast<UIntType>(x_exp) << (LDBits::MANTISSA_WIDTH + 1));
x_exp = ((x_exp >> 1) + LDBits::EXP_BIAS);
y |= (static_cast<StorageType>(x_exp) << (LDBits::FRACTION_LEN + 1));

switch (quick_get_round()) {
case FE_TONEAREST:
Expand Down
87 changes: 44 additions & 43 deletions libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,94 +27,95 @@ namespace LIBC_NAMESPACE {
namespace fputil {

template <> struct FPBits<long double> : private FloatProperties<long double> {
using typename FloatProperties<long double>::UIntType;
using FloatProperties<long double>::BIT_WIDTH;
using typename FloatProperties<long double>::StorageType;
using FloatProperties<long double>::TOTAL_LEN;
using FloatProperties<long double>::EXP_MANT_MASK;
using FloatProperties<long double>::EXPONENT_MASK;
using FloatProperties<long double>::EXPONENT_BIAS;
using FloatProperties<long double>::EXPONENT_WIDTH;
using FloatProperties<long double>::MANTISSA_MASK;
using FloatProperties<long double>::MANTISSA_WIDTH;
using FloatProperties<long double>::EXP_MASK;
using FloatProperties<long double>::EXP_BIAS;
using FloatProperties<long double>::EXP_LEN;
using FloatProperties<long double>::FRACTION_MASK;
using FloatProperties<long double>::FRACTION_LEN;
using FloatProperties<long double>::QUIET_NAN_MASK;
using FloatProperties<long double>::SIGN_MASK;

static constexpr int MAX_EXPONENT = 0x7FFF;
static constexpr UIntType MIN_SUBNORMAL = UIntType(1);
static constexpr StorageType MIN_SUBNORMAL = StorageType(1);
// Subnormal numbers include the implicit bit in x86 long double formats.
static constexpr UIntType MAX_SUBNORMAL = (UIntType(1) << MANTISSA_WIDTH) - 1;
static constexpr UIntType MIN_NORMAL = (UIntType(3) << MANTISSA_WIDTH);
static constexpr UIntType MAX_NORMAL =
(UIntType(MAX_EXPONENT - 1) << (MANTISSA_WIDTH + 1)) |
(UIntType(1) << MANTISSA_WIDTH) | MAX_SUBNORMAL;

UIntType bits;

LIBC_INLINE constexpr void set_mantissa(UIntType mantVal) {
mantVal &= MANTISSA_MASK;
bits &= ~MANTISSA_MASK;
static constexpr StorageType MAX_SUBNORMAL =
(StorageType(1) << FRACTION_LEN) - 1;
static constexpr StorageType MIN_NORMAL = (StorageType(3) << FRACTION_LEN);
static constexpr StorageType MAX_NORMAL =
(StorageType(MAX_EXPONENT - 1) << (FRACTION_LEN + 1)) |
(StorageType(1) << FRACTION_LEN) | MAX_SUBNORMAL;

StorageType bits;

LIBC_INLINE constexpr void set_mantissa(StorageType mantVal) {
mantVal &= FRACTION_MASK;
bits &= ~FRACTION_MASK;
bits |= mantVal;
}

LIBC_INLINE constexpr UIntType get_mantissa() const {
return bits & MANTISSA_MASK;
LIBC_INLINE constexpr StorageType get_mantissa() const {
return bits & FRACTION_MASK;
}

LIBC_INLINE constexpr UIntType get_explicit_mantissa() const {
LIBC_INLINE constexpr StorageType get_explicit_mantissa() const {
// The x86 80 bit float represents the leading digit of the mantissa
// explicitly. This is the mask for that bit.
constexpr UIntType EXPLICIT_BIT_MASK = UIntType(1) << MANTISSA_WIDTH;
return bits & (MANTISSA_MASK | EXPLICIT_BIT_MASK);
constexpr StorageType EXPLICIT_BIT_MASK = StorageType(1) << FRACTION_LEN;
return bits & (FRACTION_MASK | EXPLICIT_BIT_MASK);
}

LIBC_INLINE constexpr void set_biased_exponent(UIntType expVal) {
expVal = (expVal << (BIT_WIDTH - 1 - EXPONENT_WIDTH)) & EXPONENT_MASK;
bits &= ~EXPONENT_MASK;
LIBC_INLINE constexpr void set_biased_exponent(StorageType expVal) {
expVal = (expVal << (TOTAL_LEN - 1 - EXP_LEN)) & EXP_MASK;
bits &= ~EXP_MASK;
bits |= expVal;
}

LIBC_INLINE constexpr uint16_t get_biased_exponent() const {
return uint16_t((bits & EXPONENT_MASK) >> (BIT_WIDTH - 1 - EXPONENT_WIDTH));
return uint16_t((bits & EXP_MASK) >> (TOTAL_LEN - 1 - EXP_LEN));
}

LIBC_INLINE constexpr void set_implicit_bit(bool implicitVal) {
bits &= ~(UIntType(1) << MANTISSA_WIDTH);
bits |= (UIntType(implicitVal) << MANTISSA_WIDTH);
bits &= ~(StorageType(1) << FRACTION_LEN);
bits |= (StorageType(implicitVal) << FRACTION_LEN);
}

LIBC_INLINE constexpr bool get_implicit_bit() const {
return bool((bits & (UIntType(1) << MANTISSA_WIDTH)) >> MANTISSA_WIDTH);
return bool((bits & (StorageType(1) << FRACTION_LEN)) >> FRACTION_LEN);
}

LIBC_INLINE constexpr void set_sign(bool signVal) {
bits &= ~SIGN_MASK;
UIntType sign1 = UIntType(signVal) << (BIT_WIDTH - 1);
StorageType sign1 = StorageType(signVal) << (TOTAL_LEN - 1);
bits |= sign1;
}

LIBC_INLINE constexpr bool get_sign() const {
return bool((bits & SIGN_MASK) >> (BIT_WIDTH - 1));
return bool((bits & SIGN_MASK) >> (TOTAL_LEN - 1));
}

LIBC_INLINE constexpr FPBits() : bits(0) {}

template <typename XType,
cpp::enable_if_t<cpp::is_same_v<long double, XType>, int> = 0>
LIBC_INLINE constexpr explicit FPBits(XType x)
: bits(cpp::bit_cast<UIntType>(x)) {
: bits(cpp::bit_cast<StorageType>(x)) {
// bits starts uninitialized, and setting it to a long double only
// overwrites the first 80 bits. This clears those upper bits.
bits = bits & ((UIntType(1) << 80) - 1);
bits = bits & ((StorageType(1) << 80) - 1);
}

template <typename XType,
cpp::enable_if_t<cpp::is_same_v<XType, UIntType>, int> = 0>
cpp::enable_if_t<cpp::is_same_v<XType, StorageType>, int> = 0>
LIBC_INLINE constexpr explicit FPBits(XType x) : bits(x) {}

LIBC_INLINE constexpr operator long double() {
return cpp::bit_cast<long double>(bits);
}

LIBC_INLINE constexpr UIntType uintval() {
LIBC_INLINE constexpr StorageType uintval() {
// We zero the padding bits as they can contain garbage.
return bits & FP_MASK;
}
Expand All @@ -124,7 +125,7 @@ template <> struct FPBits<long double> : private FloatProperties<long double> {
}

LIBC_INLINE constexpr int get_exponent() const {
return int(get_biased_exponent()) - EXPONENT_BIAS;
return int(get_biased_exponent()) - EXP_BIAS;
}

// If the number is subnormal, the exponent is treated as if it were the
Expand All @@ -138,9 +139,9 @@ template <> struct FPBits<long double> : private FloatProperties<long double> {
if (is_zero()) {
return 0;
} else if (biased_exp == 0) {
return 1 - EXPONENT_BIAS;
return 1 - EXP_BIAS;
} else {
return biased_exp - EXPONENT_BIAS;
return biased_exp - EXP_BIAS;
}
}

Expand Down Expand Up @@ -186,15 +187,15 @@ template <> struct FPBits<long double> : private FloatProperties<long double> {

LIBC_INLINE static constexpr long double neg_inf() { return inf(true); }

LIBC_INLINE static constexpr long double build_nan(UIntType v) {
LIBC_INLINE static constexpr long double build_nan(StorageType v) {
FPBits<long double> bits(0.0l);
bits.set_biased_exponent(MAX_EXPONENT);
bits.set_implicit_bit(1);
bits.set_mantissa(v);
return bits;
}

LIBC_INLINE static constexpr long double build_quiet_nan(UIntType v) {
LIBC_INLINE static constexpr long double build_quiet_nan(StorageType v) {
return build_nan(QUIET_NAN_MASK | v);
}

Expand All @@ -215,7 +216,7 @@ template <> struct FPBits<long double> : private FloatProperties<long double> {
}

LIBC_INLINE static constexpr FPBits<long double>
create_value(bool sign, UIntType biased_exp, UIntType mantissa) {
create_value(bool sign, StorageType biased_exp, StorageType mantissa) {
FPBits<long double> result;
result.set_sign(sign);
result.set_biased_exponent(biased_exp);
Expand Down
25 changes: 12 additions & 13 deletions libc/src/__support/FPUtil/x86_64/NextAfterLongDouble.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,17 @@ LIBC_INLINE long double nextafter(long double from, long double to) {
from_bits.set_biased_exponent(1);
}

using UIntType = FPBits::UIntType;
constexpr UIntType SIGN_VAL = (UIntType(1) << 79);
constexpr UIntType MANTISSA_MASK =
(UIntType(1) << FPBits::MANTISSA_WIDTH) - 1;
UIntType int_val = from_bits.uintval();
using StorageType = FPBits::StorageType;
constexpr StorageType SIGN_VAL = (StorageType(1) << 79);
constexpr StorageType FRACTION_MASK = FPBits::FRACTION_MASK;
StorageType int_val = from_bits.uintval();
if (from < 0.0l) {
if (from > to) {
if (int_val == (SIGN_VAL + FPBits::MAX_SUBNORMAL)) {
// We deal with normal/subnormal boundary separately to avoid
// dealing with the implicit bit.
int_val = SIGN_VAL + FPBits::MIN_NORMAL;
} else if ((int_val & MANTISSA_MASK) == MANTISSA_MASK) {
} else if ((int_val & FRACTION_MASK) == FRACTION_MASK) {
from_bits.set_mantissa(0);
// Incrementing exponent might overflow the value to infinity,
// which is what is expected. Since NaNs are handling separately,
Expand All @@ -71,8 +70,8 @@ LIBC_INLINE long double nextafter(long double from, long double to) {
// We deal with normal/subnormal boundary separately to avoid
// dealing with the implicit bit.
int_val = SIGN_VAL + FPBits::MAX_SUBNORMAL;
} else if ((int_val & MANTISSA_MASK) == 0) {
from_bits.set_mantissa(MANTISSA_MASK);
} else if ((int_val & FRACTION_MASK) == 0) {
from_bits.set_mantissa(FRACTION_MASK);
// from == 0 is handled separately so decrementing the exponent will not
// lead to underflow.
from_bits.set_biased_exponent(from_bits.get_biased_exponent() - 1);
Expand All @@ -90,8 +89,8 @@ LIBC_INLINE long double nextafter(long double from, long double to) {
if (from > to) {
if (int_val == FPBits::MIN_NORMAL) {
int_val = FPBits::MAX_SUBNORMAL;
} else if ((int_val & MANTISSA_MASK) == 0) {
from_bits.set_mantissa(MANTISSA_MASK);
} else if ((int_val & FRACTION_MASK) == 0) {
from_bits.set_mantissa(FRACTION_MASK);
// from == 0 is handled separately so decrementing the exponent will not
// lead to underflow.
from_bits.set_biased_exponent(from_bits.get_biased_exponent() - 1);
Expand All @@ -102,7 +101,7 @@ LIBC_INLINE long double nextafter(long double from, long double to) {
} else {
if (int_val == FPBits::MAX_SUBNORMAL) {
int_val = FPBits::MIN_NORMAL;
} else if ((int_val & MANTISSA_MASK) == MANTISSA_MASK) {
} else if ((int_val & FRACTION_MASK) == FRACTION_MASK) {
from_bits.set_mantissa(0);
// Incrementing exponent might overflow the value to infinity,
// which is what is expected. Since NaNs are handling separately,
Expand All @@ -117,8 +116,8 @@ LIBC_INLINE long double nextafter(long double from, long double to) {
}
}

UIntType implicit_bit = int_val & (UIntType(1) << FPBits::MANTISSA_WIDTH);
if (implicit_bit == UIntType(0))
StorageType implicit_bit = int_val & (StorageType(1) << FPBits::FRACTION_LEN);
if (implicit_bit == StorageType(0))
raise_except_if_required(FE_UNDERFLOW | FE_INEXACT);

return cpp::bit_cast<long double>(int_val);
Expand Down
25 changes: 13 additions & 12 deletions libc/src/__support/float_to_string.h
Original file line number Diff line number Diff line change
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::UIntType mantissa,
LIBC_INLINE uint32_t mul_shift_mod_1e9(const FloatProp::StorageType mantissa,
const cpp::UInt<MID_INT_SIZE> &large,
const int32_t shift_amount) {
cpp::UInt<MID_INT_SIZE + FloatProp::UINTTYPE_BITS> val(large);
cpp::UInt<MID_INT_SIZE + FloatProp::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,10 +414,10 @@ class FloatToString {
fputil::FPBits<T> float_bits;
bool is_negative;
int exponent;
FloatProp::UIntType mantissa;
FloatProp::StorageType mantissa;

static constexpr int MANT_WIDTH = fputil::FPBits<T>::MANTISSA_WIDTH;
static constexpr int EXP_BIAS = fputil::FPBits<T>::EXPONENT_BIAS;
static constexpr int FRACTION_LEN = fputil::FPBits<T>::FRACTION_LEN;
static constexpr int EXP_BIAS = fputil::FPBits<T>::EXP_BIAS;

public:
LIBC_INLINE constexpr FloatToString(T init_float) : float_bits(init_float) {
Expand All @@ -426,7 +426,7 @@ class FloatToString {
mantissa = float_bits.get_explicit_mantissa();

// Adjust for the width of the mantissa.
exponent -= MANT_WIDTH;
exponent -= FRACTION_LEN;

// init_convert();
}
Expand All @@ -440,7 +440,7 @@ class FloatToString {
// get_block returns an integer that represents the digits in the requested
// block.
LIBC_INLINE constexpr BlockInt get_positive_block(int block_index) {
if (exponent >= -MANT_WIDTH) {
if (exponent >= -FRACTION_LEN) {
// idx is ceil(exponent/16) or 0 if exponent is negative. This is used to
// find the coarse section of the POW10_SPLIT table that will be used to
// calculate the 9 digit window, as well as some other related values.
Expand Down Expand Up @@ -567,12 +567,13 @@ class FloatToString {
}

LIBC_INLINE constexpr size_t get_positive_blocks() {
if (exponent >= -MANT_WIDTH) {
if (exponent >= -FRACTION_LEN) {
const uint32_t idx =
exponent < 0
? 0
: static_cast<uint32_t>(exponent + (IDX_SIZE - 1)) / IDX_SIZE;
const uint32_t len = internal::length_for_num(idx * IDX_SIZE, MANT_WIDTH);
const uint32_t len =
internal::length_for_num(idx * IDX_SIZE, FRACTION_LEN);
return len;
} else {
return 0;
Expand Down Expand Up @@ -608,12 +609,12 @@ class FloatToString {

template <>
LIBC_INLINE constexpr size_t FloatToString<long double>::get_positive_blocks() {
if (exponent >= -MANT_WIDTH) {
if (exponent >= -FRACTION_LEN) {
const uint32_t idx =
exponent < 0
? 0
: static_cast<uint32_t>(exponent + (IDX_SIZE - 1)) / IDX_SIZE;
const uint32_t len = internal::length_for_num(idx * IDX_SIZE, MANT_WIDTH);
const uint32_t len = internal::length_for_num(idx * IDX_SIZE, FRACTION_LEN);
return len;
} else {
return 0;
Expand All @@ -639,7 +640,7 @@ LIBC_INLINE constexpr bool FloatToString<long double>::is_lowest_block(size_t) {
template <>
LIBC_INLINE constexpr BlockInt
FloatToString<long double>::get_positive_block(int block_index) {
if (exponent >= -MANT_WIDTH) {
if (exponent >= -FRACTION_LEN) {

// idx is ceil(exponent/16) or 0 if exponent is negative. This is used to
// find the coarse section of the POW10_SPLIT table that will be used to
Expand Down
160 changes: 80 additions & 80 deletions libc/src/__support/str_to_float.h

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion libc/src/math/generic/asinf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ LLVM_LIBC_FUNCTION(float, asinf, (float x)) {
fputil::set_errno_if_required(EDOM);
fputil::raise_except_if_required(FE_INVALID);
}
return x + FPBits::build_nan(1 << (FPBits::MANTISSA_WIDTH - 1));
return x + FPBits::build_nan(FPBits::FRACTION_MASK);
}

// Check for exceptional values
Expand Down
2 changes: 1 addition & 1 deletion libc/src/math/generic/erff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ LLVM_LIBC_FUNCTION(float, erff, (float x)) {
double xd = static_cast<double>(x);
double xsq = xd * xd;

const uint32_t EIGHT = 3 << FPBits::MANTISSA_WIDTH;
const uint32_t EIGHT = 3 << FPBits::FRACTION_LEN;
int idx = static_cast<int>(FPBits(x_abs + EIGHT).get_val());

double x4 = xsq * xsq;
Expand Down
4 changes: 2 additions & 2 deletions libc/src/math/generic/exp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,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::MANTISSA_WIDTH;
int64_t exp_hi = static_cast<int64_t>(hi) << FloatProp::FRACTION_LEN;
double r = cpp::bit_cast<double>(exp_hi + cpp::bit_cast<int64_t>(upper));
return r;
}
Expand All @@ -400,7 +400,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::MANTISSA_WIDTH;
int64_t exp_hi = static_cast<int64_t>(hi) << FloatProp::FRACTION_LEN;
double r =
cpp::bit_cast<double>(exp_hi + cpp::bit_cast<int64_t>(upper_dd));
return r;
Expand Down
4 changes: 2 additions & 2 deletions libc/src/math/generic/exp10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,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::MANTISSA_WIDTH;
int64_t exp_hi = static_cast<int64_t>(hi) << FloatProp::FRACTION_LEN;
double r = cpp::bit_cast<double>(exp_hi + cpp::bit_cast<int64_t>(upper));
return r;
}
Expand Down Expand Up @@ -462,7 +462,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::MANTISSA_WIDTH;
int64_t exp_hi = static_cast<int64_t>(hi) << FloatProp::FRACTION_LEN;
double r = cpp::bit_cast<double>(exp_hi + cpp::bit_cast<int64_t>(upper_dd));
return r;
}
Expand Down
4 changes: 2 additions & 2 deletions libc/src/math/generic/exp2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,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::MANTISSA_WIDTH;
int64_t exp_hi = static_cast<int64_t>(hi) << FloatProp::FRACTION_LEN;
double r = cpp::bit_cast<double>(exp_hi + cpp::bit_cast<int64_t>(upper));
return r;
}
Expand All @@ -376,7 +376,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::MANTISSA_WIDTH;
int64_t exp_hi = static_cast<int64_t>(hi) << FloatProp::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>::MANTISSA_WIDTH);
<< fputil::FloatProperties<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
20 changes: 10 additions & 10 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>::MANTISSA_WIDTH;
<< fputil::FloatProperties<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>::MANTISSA_WIDTH;
<< fputil::FloatProperties<double>::FRACTION_LEN;
int64_t exp_hi_m = static_cast<int64_t>((k_m >> ExpBase::MID_BITS))
<< fputil::FloatProperties<double>::MANTISSA_WIDTH;
<< fputil::FloatProperties<double>::FRACTION_LEN;
// mh_p = 2^(hi + mid)
// mh_m = 2^(-(hi + mid))
// mh_bits_* = bit field of mh_*
Expand Down Expand Up @@ -280,11 +280,11 @@ LIBC_INLINE static double log2_eval(double x) {
double result = 0;
result += bs.get_exponent();

int p1 = (bs.get_mantissa() >> (FPB::MANTISSA_WIDTH - LOG_P1_BITS)) &
int p1 = (bs.get_mantissa() >> (FPB::FRACTION_LEN - LOG_P1_BITS)) &
(LOG_P1_SIZE - 1);

bs.bits &= FPB::MANTISSA_MASK >> LOG_P1_BITS;
bs.set_biased_exponent(FPB::EXPONENT_BIAS);
bs.bits &= 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)
Expand All @@ -310,11 +310,11 @@ LIBC_INLINE static double log_eval(double x) {

// p1 is the leading 7 bits of mx, i.e.
// p1 * 2^(-7) <= m_x < (p1 + 1) * 2^(-7).
int p1 = static_cast<int>(bs.get_mantissa() >> (FPB::MANTISSA_WIDTH - 7));
int p1 = static_cast<int>(bs.get_mantissa() >> (FPB::FRACTION_LEN - 7));

// Set bs to (1 + (mx - p1*2^(-7))
bs.bits &= FPB::MANTISSA_MASK >> 7;
bs.set_biased_exponent(FPB::EXPONENT_BIAS);
bs.bits &= FPB::FRACTION_MASK >> 7;
bs.set_biased_exponent(FPB::EXP_BIAS);
// dx = (mx - p1*2^(-7)) / (1 + p1*2^(-7)).
double dx = (bs.get_val() - 1.0) * ONE_OVER_F[p1];

Expand Down Expand Up @@ -345,7 +345,7 @@ LIBC_INLINE cpp::optional<double> ziv_test_denorm(int hi, double mid, double lo,
using FloatProp = typename fputil::FloatProperties<double>;

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

// -2^(-hi)
double one_scaled =
FPBits::create_value(true, FPBits::EXPONENT_BIAS - hi, 0).get_val();
FPBits::create_value(true, FPBits::EXP_BIAS - hi, 0).get_val();

// 2^(mid1 + mid2) - 2^(-hi)
DoubleDouble hi_part = x_sign ? fputil::exact_add(one_scaled, exp_mid.hi)
Expand Down Expand Up @@ -465,7 +465,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::MANTISSA_WIDTH;
int64_t exp_hi = static_cast<int64_t>(hi) << FloatProp::FRACTION_LEN;
double r = cpp::bit_cast<double>(exp_hi + cpp::bit_cast<int64_t>(upper));
return r;
}
Expand All @@ -479,7 +479,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::MANTISSA_WIDTH;
int64_t exp_hi = static_cast<int64_t>(hi) << FloatProp::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/hypotf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ LLVM_LIBC_FUNCTION(float, hypotf, (float x, float y)) {
uint16_t y_exp = y_bits.get_biased_exponent();
uint16_t exp_diff = (x_exp > y_exp) ? (x_exp - y_exp) : (y_exp - x_exp);

if (exp_diff >= FPBits::MANTISSA_WIDTH + 2) {
if (exp_diff >= FPBits::FRACTION_LEN + 2) {
return fputil::abs(x) + fputil::abs(y);
}

Expand Down
9 changes: 4 additions & 5 deletions libc/src/math/generic/inv_trigf_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@ LIBC_INLINE double atan_eval(double x) {
using FPB = fputil::FPBits<double>;
// Added some small value to umin and umax mantissa to avoid possible rounding
// errors.
FPB::UIntType umin =
FPB::create_value(false, FPB::EXPONENT_BIAS - ATAN_T_BITS - 1,
FPB::StorageType umin =
FPB::create_value(false, FPB::EXP_BIAS - ATAN_T_BITS - 1,
0x100000000000UL)
.uintval();
FPB::UIntType umax =
FPB::create_value(false, FPB::EXPONENT_BIAS + ATAN_T_BITS,
0xF000000000000UL)
FPB::StorageType umax =
FPB::create_value(false, FPB::EXP_BIAS + ATAN_T_BITS, 0xF000000000000UL)
.uintval();

FPB bs(x);
Expand Down
2 changes: 1 addition & 1 deletion libc/src/math/generic/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ LLVM_LIBC_FUNCTION(double, log, (double x)) {
FPBits_t xbits(x);
uint64_t x_u = xbits.uintval();

int x_e = -FPBits_t::EXPONENT_BIAS;
int x_e = -FPBits_t::EXP_BIAS;

if (LIBC_UNLIKELY(x_u == 0x3FF0'0000'0000'0000ULL)) {
// log(1.0) = +0.0
Expand Down
2 changes: 1 addition & 1 deletion libc/src/math/generic/log10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ LLVM_LIBC_FUNCTION(double, log10, (double x)) {
FPBits_t xbits(x);
uint64_t x_u = xbits.uintval();

int x_e = -FPBits_t::EXPONENT_BIAS;
int x_e = -FPBits_t::EXP_BIAS;

if (LIBC_UNLIKELY(x_u == 0x3FF0'0000'0000'0000ULL)) {
// log10(1.0) = +0.0
Expand Down
2 changes: 1 addition & 1 deletion libc/src/math/generic/log10f.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ LLVM_LIBC_FUNCTION(float, log10f, (float x)) {
}
}

int m = -FPBits::EXPONENT_BIAS;
int m = -FPBits::EXP_BIAS;

if (LIBC_UNLIKELY(x_u < FPBits::MIN_NORMAL || x_u > FPBits::MAX_NORMAL)) {
if (xbits.is_zero()) {
Expand Down
23 changes: 11 additions & 12 deletions libc/src/math/generic/log1p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -872,17 +872,17 @@ LIBC_INLINE double log1p_accurate(int e_x, int index,

LLVM_LIBC_FUNCTION(double, log1p, (double x)) {
using FPBits_t = typename fputil::FPBits<double>;
constexpr int EXPONENT_BIAS = FPBits_t::EXPONENT_BIAS;
constexpr int MANTISSA_WIDTH = FPBits_t::MANTISSA_WIDTH;
constexpr uint64_t MANTISSA_MASK = FPBits_t::MANTISSA_MASK;
constexpr int EXP_BIAS = FPBits_t::EXP_BIAS;
constexpr int FRACTION_LEN = FPBits_t::FRACTION_LEN;
constexpr uint64_t FRACTION_MASK = FPBits_t::FRACTION_MASK;
FPBits_t xbits(x);
uint64_t x_u = xbits.uintval();

fputil::DoubleDouble x_dd{0.0, 0.0};

uint16_t x_exp = xbits.get_biased_exponent();

if (x_exp >= EXPONENT_BIAS) {
if (x_exp >= EXP_BIAS) {
// |x| >= 1
if (LIBC_UNLIKELY(x_u >= 0x4650'0000'0000'0000ULL)) {
// x >= 2^102 or x is negative, inf, or NaN
Expand Down Expand Up @@ -910,7 +910,7 @@ LLVM_LIBC_FUNCTION(double, log1p, (double x)) {
} else {
// |x| < 1
if (LIBC_UNLIKELY(xbits.get_biased_exponent() <
EXPONENT_BIAS - MANTISSA_WIDTH - 1)) {
EXP_BIAS - FRACTION_LEN - 1)) {
// Quick return when |x| < 2^-53.
// Since log(1 + x) = x - x^2/2 + x^3/3 - ...,
// for |x| < 2^-53,
Expand Down Expand Up @@ -949,9 +949,9 @@ LLVM_LIBC_FUNCTION(double, log1p, (double x)) {
x_u = xhi_bits.uintval();
// Range reduction:
// Find k such that |x_hi - k * 2^-7| <= 2^-8.
int idx = static_cast<int>(
((x_u & MANTISSA_MASK) + (1ULL << (MANTISSA_WIDTH - 8))) >>
(MANTISSA_WIDTH - 7));
int idx =
static_cast<int>(((x_u & FRACTION_MASK) + (1ULL << (FRACTION_LEN - 8))) >>
(FRACTION_LEN - 7));
int x_e = xhi_bits.get_exponent() + (idx >> 7);
double e_x = static_cast<double>(x_e);

Expand All @@ -967,9 +967,8 @@ LLVM_LIBC_FUNCTION(double, log1p, (double x)) {
double err_hi = ERR_HI[hi == 0.0];

// Scaling factior = 2^(-xh_bits.get_exponent())
uint64_t s_u =
(static_cast<uint64_t>(EXPONENT_BIAS) << (MANTISSA_WIDTH + 1)) -
(x_u & FPBits_t::EXPONENT_MASK);
uint64_t s_u = (static_cast<uint64_t>(EXP_BIAS) << (FRACTION_LEN + 1)) -
(x_u & FPBits_t::EXP_MASK);
// When the exponent of x is 2^1023, its inverse, 2^(-1023), is subnormal.
const double EXPONENT_CORRECTION[2] = {0.0, 0x1.0p-1023};
double scaling = FPBits_t(s_u).get_val() + EXPONENT_CORRECTION[s_u == 0];
Expand Down Expand Up @@ -999,7 +998,7 @@ LLVM_LIBC_FUNCTION(double, log1p, (double x)) {
v_hi = fputil::multiply_add(r, m_dd.hi, -1.0); // Exact.
#else
// c = 1 + idx * 2^-7.
double c = FPBits_t((static_cast<uint64_t>(idx) << (MANTISSA_WIDTH - 7)) +
double c = FPBits_t((static_cast<uint64_t>(idx) << (FRACTION_LEN - 7)) +
uint64_t(0x3FF0'0000'0000'0000ULL))
.get_val();
v_hi = fputil::multiply_add(r, m_dd.hi - c, RCM1[idx]); // Exact
Expand Down
2 changes: 1 addition & 1 deletion libc/src/math/generic/log1pf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ LIBC_INLINE float log(double x) {
// Get the 8 highest bits, use 7 bits (excluding the implicit hidden bit) for
// lookup tables.
int f_index = static_cast<int>(xbits.get_mantissa() >>
(fputil::FPBits<double>::MANTISSA_WIDTH - 7));
(fputil::FPBits<double>::FRACTION_LEN - 7));

// Set bits to 1.m
xbits.set_biased_exponent(0x3FF);
Expand Down
2 changes: 1 addition & 1 deletion libc/src/math/generic/log2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ LLVM_LIBC_FUNCTION(double, log2, (double x)) {
FPBits_t xbits(x);
uint64_t x_u = xbits.uintval();

int x_e = -FPBits_t::EXPONENT_BIAS;
int x_e = -FPBits_t::EXP_BIAS;

if (LIBC_UNLIKELY(x_u == 0x3FF0'0000'0000'0000ULL)) {
// log2(1.0) = +0.0
Expand Down
2 changes: 1 addition & 1 deletion libc/src/math/generic/log2f.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ LLVM_LIBC_FUNCTION(float, log2f, (float x)) {
// Hard to round value(s).
using fputil::round_result_slightly_up;

int m = -FPBits::EXPONENT_BIAS;
int m = -FPBits::EXP_BIAS;

// log2(1.0f) = 0.0f.
if (LIBC_UNLIKELY(x_u == 0x3f80'0000U))
Expand Down
2 changes: 1 addition & 1 deletion libc/src/math/generic/logf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ LLVM_LIBC_FUNCTION(float, logf, (float x)) {
FPBits xbits(x);
uint32_t x_u = xbits.uintval();

int m = -FPBits::EXPONENT_BIAS;
int m = -FPBits::EXP_BIAS;

using fputil::round_result_slightly_down;
using fputil::round_result_slightly_up;
Expand Down
30 changes: 14 additions & 16 deletions libc/src/math/generic/powf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,24 +389,22 @@ static constexpr DoubleDouble LOG2_R2_DD[] = {
LIBC_INLINE bool is_odd_integer(float x) {
using FloatProp = typename fputil::FloatProperties<float>;
uint32_t x_u = cpp::bit_cast<uint32_t>(x);
int32_t x_e = static_cast<int32_t>((x_u & FloatProp::EXPONENT_MASK) >>
FloatProp::MANTISSA_WIDTH);
int32_t lsb = cpp::countr_zero(x_u | FloatProp::EXPONENT_MASK);
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);
constexpr int32_t UNIT_EXPONENT =
FloatProp::EXPONENT_BIAS +
static_cast<int32_t>(FloatProp::MANTISSA_WIDTH);
FloatProp::EXP_BIAS + static_cast<int32_t>(FloatProp::FRACTION_LEN);
return (x_e + lsb == UNIT_EXPONENT);
}

LIBC_INLINE bool is_integer(float x) {
using FloatProp = typename fputil::FloatProperties<float>;
uint32_t x_u = cpp::bit_cast<uint32_t>(x);
int32_t x_e = static_cast<int32_t>((x_u & FloatProp::EXPONENT_MASK) >>
FloatProp::MANTISSA_WIDTH);
int32_t lsb = cpp::countr_zero(x_u | FloatProp::EXPONENT_MASK);
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);
constexpr int32_t UNIT_EXPONENT =
FloatProp::EXPONENT_BIAS +
static_cast<int32_t>(FloatProp::MANTISSA_WIDTH);
FloatProp::EXP_BIAS + static_cast<int32_t>(FloatProp::FRACTION_LEN);
return (x_e + lsb >= UNIT_EXPONENT);
}

Expand Down Expand Up @@ -502,7 +500,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::MANTISSA_MASK) > 0) {
} else if ((r_bits & DoubleProp::FRACTION_MASK) > 0) {
--r_bits;
}
}
Expand Down Expand Up @@ -591,7 +589,7 @@ LLVM_LIBC_FUNCTION(float, powf, (float x, float y)) {
}
}

int ex = -FloatBits::EXPONENT_BIAS;
int ex = -FloatBits::EXP_BIAS;
uint64_t sign = 0;

// y is finite and non-zero.
Expand Down Expand Up @@ -671,11 +669,11 @@ LLVM_LIBC_FUNCTION(float, powf, (float x, float y)) {
x_u = FloatBits(x).uintval();

// Extract exponent field of x.
ex += (x_u >> FloatProp::MANTISSA_WIDTH);
ex += (x_u >> FloatProp::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::MANTISSA_MASK;
int idx_x = static_cast<int>(x_mant >> (FloatProp::MANTISSA_WIDTH - 7));
uint32_t x_mant = x_u & FloatProp::FRACTION_MASK;
int idx_x = static_cast<int>(x_mant >> (FloatProp::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 @@ -776,7 +774,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::MANTISSA_WIDTH;
int64_t exp_hi_i = (hm_i >> 6) << DoubleProp::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>::MANTISSA_WIDTH;
int x_lsb_exp_m4 = x_exp - fputil::FloatProperties<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/sincosf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ LLVM_LIBC_FUNCTION(void, sincosf, (float x, float *sinp, float *cosp)) {
fputil::set_errno_if_required(EDOM);
fputil::raise_except_if_required(FE_INVALID);
}
*sinp = x + FPBits::build_nan(1 << (FPBits::MANTISSA_WIDTH - 1));
*sinp = x + FPBits::build_nan(FPBits::FRACTION_MASK);
*cosp = *sinp;
return;
}
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 @@ -90,7 +90,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>::MANTISSA_WIDTH;
<< fputil::FloatProperties<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
2 changes: 1 addition & 1 deletion libc/src/stdio/printf_core/core_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct FormatSection {
int precision = -1;

// Needs to be large enough to hold a long double.
fputil::FPBits<long double>::UIntType conv_val_raw;
fputil::FPBits<long double>::StorageType conv_val_raw;
void *conv_val_ptr;

char conv_name;
Expand Down
62 changes: 31 additions & 31 deletions libc/src/stdio/printf_core/float_dec_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
namespace LIBC_NAMESPACE {
namespace printf_core {

using MantissaInt = fputil::FPBits<long double>::UIntType;
using StorageType = fputil::FPBits<long double>::StorageType;
using DecimalString = IntegerToString<intmax_t>;
using ExponentString =
IntegerToString<intmax_t, radix::Dec::WithWidth<2>::WithSign>;
Expand Down Expand Up @@ -245,7 +245,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>::EXPONENT_WIDTH <
static_assert(fputil::FloatProperties<long double>::EXP_LEN <
(sizeof(int) * 8));

public:
Expand Down Expand Up @@ -477,8 +477,8 @@ template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
LIBC_INLINE int convert_float_decimal_typed(Writer *writer,
const FormatSection &to_conv,
fputil::FPBits<T> float_bits) {
// signed because later we use -MANT_WIDTH
constexpr int32_t MANT_WIDTH = fputil::FloatProperties<T>::MANTISSA_WIDTH;
// signed because later we use -FRACTION_LEN
constexpr int32_t FRACTION_LEN = fputil::FloatProperties<T>::FRACTION_LEN;
bool is_negative = float_bits.get_sign();
int exponent = float_bits.get_explicit_exponent();

Expand Down Expand Up @@ -536,7 +536,7 @@ LIBC_INLINE int convert_float_decimal_typed(Writer *writer,
float_writer.write_first_block(0);
}

if (exponent < MANT_WIDTH) {
if (exponent < FRACTION_LEN) {
const uint32_t blocks = (precision / static_cast<uint32_t>(BLOCK_SIZE)) + 1;
uint32_t i = 0;
// if all the blocks we should write are zero
Expand Down Expand Up @@ -569,9 +569,9 @@ LIBC_INLINE int convert_float_decimal_typed(Writer *writer,
digits /= 10;
}
RoundDirection round;
const bool truncated =
!zero_after_digits(exponent - MANT_WIDTH, precision,
float_bits.get_explicit_mantissa(), MANT_WIDTH);
const bool truncated = !zero_after_digits(
exponent - FRACTION_LEN, precision,
float_bits.get_explicit_mantissa(), FRACTION_LEN);
round = get_round_direction(last_digit, truncated, is_negative);

RET_IF_RESULT_NEGATIVE(
Expand All @@ -590,11 +590,11 @@ template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
LIBC_INLINE int convert_float_dec_exp_typed(Writer *writer,
const FormatSection &to_conv,
fputil::FPBits<T> float_bits) {
// signed because later we use -MANT_WIDTH
constexpr int32_t MANT_WIDTH = fputil::FloatProperties<T>::MANTISSA_WIDTH;
// signed because later we use -FRACTION_LEN
constexpr int32_t FRACTION_LEN = fputil::FloatProperties<T>::FRACTION_LEN;
bool is_negative = float_bits.get_sign();
int exponent = float_bits.get_explicit_exponent();
MantissaInt mantissa = float_bits.get_explicit_mantissa();
StorageType mantissa = float_bits.get_explicit_mantissa();

const char a = (to_conv.conv_name & 32) | 'A';

Expand Down Expand Up @@ -733,11 +733,11 @@ LIBC_INLINE int convert_float_dec_exp_typed(Writer *writer,
}
}
// If it's still not truncated and there are digits below the decimal point
if (!truncated && exponent - MANT_WIDTH < 0) {
if (!truncated && exponent - FRACTION_LEN < 0) {
// Use the formula from %f.
truncated =
!zero_after_digits(exponent - MANT_WIDTH, precision - final_exponent,
float_bits.get_explicit_mantissa(), MANT_WIDTH);
truncated = !zero_after_digits(
exponent - FRACTION_LEN, precision - final_exponent,
float_bits.get_explicit_mantissa(), FRACTION_LEN);
}
}
round = get_round_direction(last_digit, truncated, is_negative);
Expand All @@ -753,11 +753,11 @@ template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
LIBC_INLINE int convert_float_dec_auto_typed(Writer *writer,
const FormatSection &to_conv,
fputil::FPBits<T> float_bits) {
// signed because later we use -MANT_WIDTH
constexpr int32_t MANT_WIDTH = fputil::FloatProperties<T>::MANTISSA_WIDTH;
// signed because later we use -FRACTION_LEN
constexpr int32_t FRACTION_LEN = fputil::FloatProperties<T>::FRACTION_LEN;
bool is_negative = float_bits.get_sign();
int exponent = float_bits.get_explicit_exponent();
MantissaInt mantissa = float_bits.get_explicit_mantissa();
StorageType mantissa = float_bits.get_explicit_mantissa();

// From the standard: Let P (init_precision) equal the precision if nonzero, 6
// if the precision is omitted, or 1 if the precision is zero.
Expand Down Expand Up @@ -980,11 +980,11 @@ LIBC_INLINE int convert_float_dec_auto_typed(Writer *writer,
}
}
// If it's still not truncated and there are digits below the decimal point
if (!truncated && exponent - MANT_WIDTH < 0) {
if (!truncated && exponent - FRACTION_LEN < 0) {
// Use the formula from %f.
truncated =
!zero_after_digits(exponent - MANT_WIDTH, exp_precision - base_10_exp,
float_bits.get_explicit_mantissa(), MANT_WIDTH);
truncated = !zero_after_digits(
exponent - FRACTION_LEN, exp_precision - base_10_exp,
float_bits.get_explicit_mantissa(), FRACTION_LEN);
}
}

Expand Down Expand Up @@ -1120,15 +1120,15 @@ LIBC_INLINE int convert_float_dec_auto_typed(Writer *writer,
LIBC_INLINE int convert_float_decimal(Writer *writer,
const FormatSection &to_conv) {
if (to_conv.length_modifier == LengthModifier::L) {
fputil::FPBits<long double>::UIntType float_raw = to_conv.conv_val_raw;
fputil::FPBits<long double>::StorageType float_raw = to_conv.conv_val_raw;
fputil::FPBits<long double> float_bits(float_raw);
if (!float_bits.is_inf_or_nan()) {
return convert_float_decimal_typed<long double>(writer, to_conv,
float_bits);
}
} else {
fputil::FPBits<double>::UIntType float_raw =
static_cast<fputil::FPBits<double>::UIntType>(to_conv.conv_val_raw);
fputil::FPBits<double>::StorageType float_raw =
static_cast<fputil::FPBits<double>::StorageType>(to_conv.conv_val_raw);
fputil::FPBits<double> float_bits(float_raw);
if (!float_bits.is_inf_or_nan()) {
return convert_float_decimal_typed<double>(writer, to_conv, float_bits);
Expand All @@ -1141,15 +1141,15 @@ LIBC_INLINE int convert_float_decimal(Writer *writer,
LIBC_INLINE int convert_float_dec_exp(Writer *writer,
const FormatSection &to_conv) {
if (to_conv.length_modifier == LengthModifier::L) {
fputil::FPBits<long double>::UIntType float_raw = to_conv.conv_val_raw;
fputil::FPBits<long double>::StorageType float_raw = to_conv.conv_val_raw;
fputil::FPBits<long double> float_bits(float_raw);
if (!float_bits.is_inf_or_nan()) {
return convert_float_dec_exp_typed<long double>(writer, to_conv,
float_bits);
}
} else {
fputil::FPBits<double>::UIntType float_raw =
static_cast<fputil::FPBits<double>::UIntType>(to_conv.conv_val_raw);
fputil::FPBits<double>::StorageType float_raw =
static_cast<fputil::FPBits<double>::StorageType>(to_conv.conv_val_raw);
fputil::FPBits<double> float_bits(float_raw);
if (!float_bits.is_inf_or_nan()) {
return convert_float_dec_exp_typed<double>(writer, to_conv, float_bits);
Expand All @@ -1162,15 +1162,15 @@ LIBC_INLINE int convert_float_dec_exp(Writer *writer,
LIBC_INLINE int convert_float_dec_auto(Writer *writer,
const FormatSection &to_conv) {
if (to_conv.length_modifier == LengthModifier::L) {
fputil::FPBits<long double>::UIntType float_raw = to_conv.conv_val_raw;
fputil::FPBits<long double>::StorageType float_raw = to_conv.conv_val_raw;
fputil::FPBits<long double> float_bits(float_raw);
if (!float_bits.is_inf_or_nan()) {
return convert_float_dec_auto_typed<long double>(writer, to_conv,
float_bits);
}
} else {
fputil::FPBits<double>::UIntType float_raw =
static_cast<fputil::FPBits<double>::UIntType>(to_conv.conv_val_raw);
fputil::FPBits<double>::StorageType float_raw =
static_cast<fputil::FPBits<double>::StorageType>(to_conv.conv_val_raw);
fputil::FPBits<double> float_bits(float_raw);
if (!float_bits.is_inf_or_nan()) {
return convert_float_dec_auto_typed<double>(writer, to_conv, float_bits);
Expand Down
34 changes: 17 additions & 17 deletions libc/src/stdio/printf_core/float_hex_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,30 @@ namespace printf_core {
LIBC_INLINE int convert_float_hex_exp(Writer *writer,
const FormatSection &to_conv) {
using LDBits = fputil::FPBits<long double>;
using MantissaInt = LDBits::UIntType;
using StorageType = LDBits::StorageType;
// All of the letters will be defined relative to variable a, which will be
// the appropriate case based on the name of the conversion. This converts any
// conversion name into the letter 'a' with the appropriate case.
const char a = (to_conv.conv_name & 32) | 'A';

bool is_negative;
int exponent;
MantissaInt mantissa;
StorageType mantissa;
bool is_inf_or_nan;
uint32_t mantissa_width;
uint32_t fraction_bits;
if (to_conv.length_modifier == LengthModifier::L) {
mantissa_width = LDBits::MANTISSA_WIDTH;
LDBits::UIntType float_raw = to_conv.conv_val_raw;
fraction_bits = LDBits::FRACTION_LEN;
LDBits::StorageType float_raw = to_conv.conv_val_raw;
LDBits float_bits(float_raw);
is_negative = float_bits.get_sign();
exponent = float_bits.get_explicit_exponent();
mantissa = float_bits.get_explicit_mantissa();
is_inf_or_nan = float_bits.is_inf_or_nan();
} else {
using LBits = fputil::FPBits<double>;
mantissa_width = LBits::MANTISSA_WIDTH;
LBits::UIntType float_raw =
static_cast<LBits::UIntType>(to_conv.conv_val_raw);
fraction_bits = LBits::FRACTION_LEN;
LBits::StorageType float_raw =
static_cast<LBits::StorageType>(to_conv.conv_val_raw);
LBits float_bits(float_raw);
is_negative = float_bits.get_sign();
exponent = float_bits.get_explicit_exponent();
Expand All @@ -78,19 +78,19 @@ LIBC_INLINE int convert_float_hex_exp(Writer *writer,
// digits. This is primarily relevant for x86 80 bit long doubles, which have
// 63 bit mantissas. In the case where the mantissa is 0, however, the
// exponent should stay as 0.
if (mantissa_width % BITS_IN_HEX_DIGIT != 0 && mantissa > 0) {
exponent -= mantissa_width % BITS_IN_HEX_DIGIT;
if (fraction_bits % BITS_IN_HEX_DIGIT != 0 && mantissa > 0) {
exponent -= fraction_bits % BITS_IN_HEX_DIGIT;
}

// This is the max number of digits it can take to represent the mantissa.
// Since the number is in bits, we divide by 4, and then add one to account
// for the extra implicit bit. We use the larger of the two possible values
// since the size must be constant.
constexpr size_t MANT_BUFF_LEN =
(LDBits::MANTISSA_WIDTH / BITS_IN_HEX_DIGIT) + 1;
(LDBits::FRACTION_LEN / BITS_IN_HEX_DIGIT) + 1;
char mant_buffer[MANT_BUFF_LEN];

size_t mant_len = (mantissa_width / BITS_IN_HEX_DIGIT) + 1;
size_t mant_len = (fraction_bits / BITS_IN_HEX_DIGIT) + 1;

// Precision only tracks the number of digits after the hexadecimal point, so
// we have to add one to account for the digit before the hexadecimal point.
Expand All @@ -100,9 +100,9 @@ LIBC_INLINE int convert_float_hex_exp(Writer *writer,
const size_t shift_amount =
(mant_len - intended_digits) * BITS_IN_HEX_DIGIT;

const MantissaInt truncated_bits =
mantissa & ((MantissaInt(1) << shift_amount) - 1);
const MantissaInt halfway_const = MantissaInt(1) << (shift_amount - 1);
const StorageType truncated_bits =
mantissa & ((StorageType(1) << shift_amount) - 1);
const StorageType halfway_const = StorageType(1) << (shift_amount - 1);

mantissa >>= shift_amount;

Expand All @@ -128,7 +128,7 @@ LIBC_INLINE int convert_float_hex_exp(Writer *writer,

// If the rounding caused an overflow, shift the mantissa and adjust the
// exponent to match.
if (mantissa >= (MantissaInt(1) << (intended_digits * BITS_IN_HEX_DIGIT))) {
if (mantissa >= (StorageType(1) << (intended_digits * BITS_IN_HEX_DIGIT))) {
mantissa >>= BITS_IN_HEX_DIGIT;
exponent += BITS_IN_HEX_DIGIT;
}
Expand Down Expand Up @@ -158,7 +158,7 @@ LIBC_INLINE int convert_float_hex_exp(Writer *writer,
// 15 -> 5
// 11 -> 4
// 8 -> 3
constexpr size_t EXP_LEN = (((LDBits::EXPONENT_WIDTH * 5) + 15) / 16) + 1;
constexpr size_t EXP_LEN = (((LDBits::EXP_LEN * 5) + 15) / 16) + 1;
char exp_buffer[EXP_LEN];

bool exp_is_negative = false;
Expand Down
10 changes: 5 additions & 5 deletions libc/src/stdio/printf_core/float_inf_nan_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@
namespace LIBC_NAMESPACE {
namespace printf_core {

using MantissaInt = fputil::FPBits<long double>::UIntType;
using StorageType = fputil::FPBits<long double>::StorageType;

LIBC_INLINE int convert_inf_nan(Writer *writer, const FormatSection &to_conv) {
// All of the letters will be defined relative to variable a, which will be
// the appropriate case based on the case of the conversion.
const char a = (to_conv.conv_name & 32) | 'A';

bool is_negative;
MantissaInt mantissa;
StorageType mantissa;
if (to_conv.length_modifier == LengthModifier::L) {
fputil::FPBits<long double>::UIntType float_raw = to_conv.conv_val_raw;
fputil::FPBits<long double>::StorageType float_raw = to_conv.conv_val_raw;
fputil::FPBits<long double> float_bits(float_raw);
is_negative = float_bits.get_sign();
mantissa = float_bits.get_mantissa();
} else {
fputil::FPBits<double>::UIntType float_raw =
static_cast<fputil::FPBits<double>::UIntType>(to_conv.conv_val_raw);
fputil::FPBits<double>::StorageType float_raw =
static_cast<fputil::FPBits<double>::StorageType>(to_conv.conv_val_raw);
fputil::FPBits<double> float_bits(float_raw);
is_negative = float_bits.get_sign();
mantissa = float_bits.get_mantissa();
Expand Down
4 changes: 2 additions & 2 deletions libc/src/stdio/printf_core/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ template <typename T> struct int_type_of {
using type = T;
};
template <> struct int_type_of<double> {
using type = fputil::FPBits<double>::UIntType;
using type = fputil::FPBits<double>::StorageType;
};
template <> struct int_type_of<long double> {
using type = fputil::FPBits<long double>::UIntType;
using type = fputil::FPBits<long double>::StorageType;
};
template <typename T> using int_type_of_v = typename int_type_of<T>::type;

Expand Down
4 changes: 2 additions & 2 deletions libc/test/UnitTest/FPMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ template <TestCond C, typename T> FPMatcher<T, C> getMatcher(T expectedValue) {

template <typename T> struct FPTest : public Test {
using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
using UIntType = typename FPBits::UIntType;
using StorageType = typename FPBits::StorageType;
static constexpr T zero = FPBits::zero();
static constexpr T neg_zero = FPBits::neg_zero();
static constexpr T aNaN = FPBits::build_quiet_nan(1);
Expand All @@ -87,7 +87,7 @@ template <typename T> struct FPTest : public Test {

#define DECLARE_SPECIAL_CONSTANTS(T) \
using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>; \
using UIntType = typename FPBits::UIntType; \
using StorageType = typename FPBits::StorageType; \
const T zero = FPBits::zero(); \
const T neg_zero = FPBits::neg_zero(); \
const T aNaN = FPBits::build_quiet_nan(1); \
Expand Down
2 changes: 1 addition & 1 deletion libc/test/UnitTest/PrintfMatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ static void display(FormatSection form) {
<< "\n";
else if (form.conv_name != '%')
tlog << "\tvalue: "
<< int_to_hex<fputil::FPBits<long double>::UIntType>(
<< int_to_hex<fputil::FPBits<long double>::StorageType>(
form.conv_val_raw)
<< "\n";
}
Expand Down
21 changes: 11 additions & 10 deletions libc/test/src/__support/str_to_fp_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
namespace LIBC_NAMESPACE {

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

void clinger_fast_path_test(const UIntType inputMantissa,
void clinger_fast_path_test(const StorageType inputMantissa,
const int32_t inputExp10,
const UIntType expectedOutputMantissa,
const StorageType expectedOutputMantissa,
const uint32_t expectedOutputExp2) {
UIntType actual_output_mantissa = 0;
StorageType actual_output_mantissa = 0;
uint32_t actual_output_exp2 = 0;

auto result = internal::clinger_fast_path<T>({inputMantissa, inputExp10});
Expand All @@ -36,16 +36,17 @@ template <typename T> struct LlvmLibcStrToFloatTest : public testing::Test {
EXPECT_EQ(actual_output_exp2, expectedOutputExp2);
}

void clinger_fast_path_fails_test(const UIntType inputMantissa,
void clinger_fast_path_fails_test(const StorageType inputMantissa,
const int32_t inputExp10) {
ASSERT_FALSE(internal::clinger_fast_path<T>({inputMantissa, inputExp10})
.has_value());
}

void eisel_lemire_test(const UIntType inputMantissa, const int32_t inputExp10,
const UIntType expectedOutputMantissa,
void eisel_lemire_test(const StorageType inputMantissa,
const int32_t inputExp10,
const StorageType expectedOutputMantissa,
const uint32_t expectedOutputExp2) {
UIntType actual_output_mantissa = 0;
StorageType actual_output_mantissa = 0;
uint32_t actual_output_exp2 = 0;

auto result = internal::eisel_lemire<T>({inputMantissa, inputExp10});
Expand All @@ -60,10 +61,10 @@ template <typename T> struct LlvmLibcStrToFloatTest : public testing::Test {
}

void simple_decimal_conversion_test(const char *__restrict numStart,
const UIntType expectedOutputMantissa,
const StorageType expectedOutputMantissa,
const uint32_t expectedOutputExp2,
const int expectedErrno = 0) {
UIntType actual_output_mantissa = 0;
StorageType actual_output_mantissa = 0;
uint32_t actual_output_exp2 = 0;
libc_errno = 0;

Expand Down
6 changes: 3 additions & 3 deletions libc/test/src/math/CeilTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ template <typename T> class CeilTest : public LIBC_NAMESPACE::testing::Test {
}

void testRange(CeilFunc func) {
constexpr UIntType COUNT = 100'000;
constexpr UIntType STEP = UIntType(-1) / COUNT;
for (UIntType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
constexpr StorageType COUNT = 100'000;
constexpr StorageType STEP = StorageType(-1) / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = T(FPBits(v));
if (isnan(x) || isinf(x))
continue;
Expand Down
6 changes: 3 additions & 3 deletions libc/test/src/math/CopySignTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ class CopySignTest : public LIBC_NAMESPACE::testing::Test {
}

void testRange(CopySignFunc func) {
constexpr UIntType COUNT = 100'000;
constexpr UIntType STEP = UIntType(-1) / COUNT;
for (UIntType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
constexpr StorageType COUNT = 100'000;
constexpr StorageType STEP = StorageType(-1) / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = T(FPBits(v));
if (isnan(x) || isinf(x))
continue;
Expand Down
6 changes: 3 additions & 3 deletions libc/test/src/math/FAbsTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ template <typename T> class FAbsTest : public LIBC_NAMESPACE::testing::Test {
}

void testRange(FabsFunc func) {
constexpr UIntType COUNT = 100'000;
constexpr UIntType STEP = UIntType(-1) / COUNT;
for (UIntType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
constexpr StorageType COUNT = 100'000;
constexpr StorageType STEP = StorageType(-1) / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = T(FPBits(v));
if (isnan(x) || isinf(x))
continue;
Expand Down
8 changes: 4 additions & 4 deletions libc/test/src/math/FDimTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class FDimTestTemplate : public LIBC_NAMESPACE::testing::Test {
public:
using FuncPtr = T (*)(T, T);
using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
using UIntType = typename FPBits::UIntType;
using StorageType = typename FPBits::StorageType;

void test_na_n_arg(FuncPtr func) {
EXPECT_FP_EQ(nan, func(nan, inf));
Expand Down Expand Up @@ -53,9 +53,9 @@ class FDimTestTemplate : public LIBC_NAMESPACE::testing::Test {
}

void test_in_range(FuncPtr func) {
constexpr UIntType COUNT = 100'001;
constexpr UIntType STEP = UIntType(-1) / COUNT;
for (UIntType i = 0, v = 0, w = UIntType(-1); i <= COUNT;
constexpr StorageType COUNT = 100'001;
constexpr StorageType STEP = StorageType(-1) / COUNT;
for (StorageType i = 0, v = 0, w = StorageType(-1); i <= COUNT;
++i, v += STEP, w -= STEP) {
T x = T(FPBits(v)), y = T(FPBits(w));
if (isnan(x) || isinf(x))
Expand Down
6 changes: 3 additions & 3 deletions libc/test/src/math/FMaxTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ template <typename T> class FMaxTest : public LIBC_NAMESPACE::testing::Test {
}

void testRange(FMaxFunc func) {
constexpr UIntType COUNT = 100'001;
constexpr UIntType STEP = UIntType(-1) / COUNT;
for (UIntType i = 0, v = 0, w = UIntType(-1); i <= COUNT;
constexpr StorageType COUNT = 100'001;
constexpr StorageType STEP = StorageType(-1) / COUNT;
for (StorageType i = 0, v = 0, w = StorageType(-1); i <= COUNT;
++i, v += STEP, w -= STEP) {
T x = T(FPBits(v)), y = T(FPBits(w));
if (isnan(x) || isinf(x))
Expand Down
6 changes: 3 additions & 3 deletions libc/test/src/math/FMinTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ template <typename T> class FMinTest : public LIBC_NAMESPACE::testing::Test {
}

void testRange(FMinFunc func) {
constexpr UIntType COUNT = 100'001;
constexpr UIntType STEP = UIntType(-1) / COUNT;
for (UIntType i = 0, v = 0, w = UIntType(-1); i <= COUNT;
constexpr StorageType COUNT = 100'001;
constexpr StorageType STEP = StorageType(-1) / COUNT;
for (StorageType i = 0, v = 0, w = StorageType(-1); i <= COUNT;
++i, v += STEP, w -= STEP) {
T x = T(FPBits(v)), y = T(FPBits(w));
if (isnan(x) || isinf(x))
Expand Down
6 changes: 3 additions & 3 deletions libc/test/src/math/FloorTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ template <typename T> class FloorTest : public LIBC_NAMESPACE::testing::Test {
}

void testRange(FloorFunc func) {
constexpr UIntType COUNT = 100'000;
constexpr UIntType STEP = UIntType(-1) / COUNT;
for (UIntType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
constexpr StorageType COUNT = 100'000;
constexpr StorageType STEP = StorageType(-1) / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = T(FPBits(v));
if (isnan(x) || isinf(x))
continue;
Expand Down
Loading