Skip to content

[libc][NFC] Make EXPONENT_BIAS int32_t #75046

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions libc/src/__support/FPUtil/FloatProperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ struct FPCommonProperties : private FPBaseProperties<fp_type> {
LIBC_INLINE_VAR static constexpr BitsType MANTISSA_MASK =
mask_trailing_ones<UIntType, MANTISSA_WIDTH>();
LIBC_INLINE_VAR static constexpr uint32_t EXPONENT_WIDTH = EXP_BITS;
LIBC_INLINE_VAR static constexpr uint32_t EXPONENT_BIAS =
static_cast<uint32_t>(EXP_BIAS);
LIBC_INLINE_VAR static constexpr int32_t EXPONENT_BIAS = EXP_BIAS;
LIBC_INLINE_VAR static constexpr BitsType SIGN_MASK = SIGN_MASK_;
LIBC_INLINE_VAR static constexpr BitsType EXPONENT_MASK = EXP_MASK;
LIBC_INLINE_VAR static constexpr BitsType EXP_MANT_MASK = EXP_MASK | SIG_MASK;
Expand Down
5 changes: 3 additions & 2 deletions libc/src/__support/detailed_powers_of_ten.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ constexpr int32_t DETAILED_POWERS_OF_TEN_MIN_EXP_10 = -348;
constexpr int32_t DETAILED_POWERS_OF_TEN_MAX_EXP_10 = 347;

// This rescales the base 10 exponent by a factor of log(10)/log(2).
LIBC_INLINE int64_t exp10_to_exp2(int64_t exp10) {
return (217706 * exp10) >> 16;
LIBC_INLINE int32_t exp10_to_exp2(int32_t exp10) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is always called with an int32_t and should also return an int32_t.
I've highlighted the overflow risk. Note that the risk was already there before this patch, only that it was handled on the callee side.

// Valid if exp10 < 646 456 636.
return static_cast<int32_t>((217706 * static_cast<int64_t>(exp10)) >> 16);
}

static constexpr uint64_t DETAILED_POWERS_OF_TEN[696][2] = {
Expand Down
27 changes: 13 additions & 14 deletions libc/src/__support/str_to_float.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ eisel_lemire(ExpandedFloat<T> init_num,
uint32_t clz = cpp::countl_zero<UIntType>(mantissa);
mantissa <<= clz;

uint32_t exp2 = static_cast<uint32_t>(exp10_to_exp2(exp10)) +
BITS_IN_MANTISSA + FloatProp::EXPONENT_BIAS - clz;
int32_t exp2 =
exp10_to_exp2(exp10) + BITS_IN_MANTISSA + FloatProp::EXPONENT_BIAS - clz;

// Multiplication
const uint64_t *power_of_ten =
Expand Down Expand Up @@ -168,7 +168,7 @@ eisel_lemire(ExpandedFloat<T> init_num,

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

Expand Down Expand Up @@ -211,8 +211,8 @@ eisel_lemire<long double>(ExpandedFloat<long double> init_num,
uint32_t clz = cpp::countl_zero<UIntType>(mantissa);
mantissa <<= clz;

uint32_t exp2 = static_cast<uint32_t>(exp10_to_exp2(exp10)) +
BITS_IN_MANTISSA + FloatProp::EXPONENT_BIAS - clz;
int32_t exp2 =
exp10_to_exp2(exp10) + BITS_IN_MANTISSA + FloatProp::EXPONENT_BIAS - clz;

// Multiplication
const uint64_t *power_of_ten =
Expand Down Expand Up @@ -338,17 +338,16 @@ simple_decimal_conversion(const char *__restrict numStart,
// If the exponent is too large and can't be represented in this size of
// float, return inf.
if (hpd.get_decimal_point() > 0 &&
exp10_to_exp2(hpd.get_decimal_point() - 1) >
static_cast<int64_t>(FloatProp::EXPONENT_BIAS)) {
output.num = {0, FPBits::MAX_EXPONENT};
exp10_to_exp2(hpd.get_decimal_point() - 1) > FloatProp::EXPONENT_BIAS) {
output.num = {0, fputil::FPBits<T>::MAX_EXPONENT};
output.error = ERANGE;
return output;
}
// If the exponent is too small even for a subnormal, return 0.
if (hpd.get_decimal_point() < 0 &&
exp10_to_exp2(-hpd.get_decimal_point()) >
static_cast<int64_t>(FloatProp::EXPONENT_BIAS +
FloatProp::MANTISSA_WIDTH)) {
(FloatProp::EXPONENT_BIAS +
static_cast<int32_t>(FloatProp::MANTISSA_WIDTH))) {
output.num = {0, 0};
output.error = ERANGE;
return output;
Expand Down Expand Up @@ -607,7 +606,7 @@ clinger_fast_path(ExpandedFloat<T> init_num,
// log10(2^(exponent bias)).
// The generic approximation uses the fact that log10(2^x) ~= x/3
template <typename T> constexpr int32_t get_upper_bound() {
return static_cast<int32_t>(fputil::FloatProperties<T>::EXPONENT_BIAS) / 3;
return fputil::FloatProperties<T>::EXPONENT_BIAS / 3;
}

template <> constexpr int32_t get_upper_bound<float>() { return 39; }
Expand All @@ -623,9 +622,9 @@ template <> constexpr int32_t get_upper_bound<double>() { return 309; }
// other out, and subnormal numbers allow for the result to be at the very low
// end of the final mantissa.
template <typename T> constexpr int32_t get_lower_bound() {
return -(static_cast<int32_t>(fputil::FloatProperties<T>::EXPONENT_BIAS +
fputil::FloatProperties<T>::MANTISSA_WIDTH +
(sizeof(T) * 8)) /
return -((fputil::FloatProperties<T>::EXPONENT_BIAS +
static_cast<int32_t>(fputil::FloatProperties<T>::MANTISSA_WIDTH +
(sizeof(T) * 8))) /
3);
}

Expand Down
22 changes: 12 additions & 10 deletions libc/src/math/generic/powf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,22 +389,24 @@ 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);
int x_e = static_cast<int>((x_u & FloatProp::EXPONENT_MASK) >>
FloatProp::MANTISSA_WIDTH);
int lsb = cpp::countr_zero(x_u | FloatProp::EXPONENT_MASK);
constexpr int UNIT_EXPONENT =
static_cast<int>(FloatProp::EXPONENT_BIAS + FloatProp::MANTISSA_WIDTH);
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);
constexpr int32_t UNIT_EXPONENT =
FloatProp::EXPONENT_BIAS +
static_cast<int32_t>(FloatProp::MANTISSA_WIDTH);
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);
int x_e = static_cast<int>((x_u & FloatProp::EXPONENT_MASK) >>
FloatProp::MANTISSA_WIDTH);
int lsb = cpp::countr_zero(x_u | FloatProp::EXPONENT_MASK);
constexpr int UNIT_EXPONENT =
static_cast<int>(FloatProp::EXPONENT_BIAS + FloatProp::MANTISSA_WIDTH);
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);
constexpr int32_t UNIT_EXPONENT =
FloatProp::EXPONENT_BIAS +
static_cast<int32_t>(FloatProp::MANTISSA_WIDTH);
return (x_e + lsb >= UNIT_EXPONENT);
}

Expand Down