Skip to content

Commit 3ae5a9b

Browse files
authored
[libc][NFC] Rename MAX_EXPONENT to MAX_BIASED_EXPONENT (llvm#75932)
As currently defined `MAX_EXPONENT` actually corresponds to the biased exponent (i.e. an unsigned value).
1 parent 36a073a commit 3ae5a9b

File tree

10 files changed

+45
-43
lines changed

10 files changed

+45
-43
lines changed

libc/src/__support/FPUtil/FPBits.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ template <typename T> struct FPBits : private FloatProperties<T> {
9797
static_assert(sizeof(T) == sizeof(StorageType),
9898
"Data type and integral representation have different sizes.");
9999

100-
static constexpr int MAX_EXPONENT = (1 << EXP_LEN) - 1;
100+
static constexpr int MAX_BIASED_EXPONENT = (1 << EXP_LEN) - 1;
101101
static constexpr StorageType MIN_SUBNORMAL = StorageType(1);
102102
static constexpr StorageType MAX_SUBNORMAL = FRACTION_MASK;
103103
static constexpr StorageType MIN_NORMAL = (StorageType(1) << FRACTION_LEN);
104104
static constexpr StorageType MAX_NORMAL =
105-
((StorageType(MAX_EXPONENT) - 1) << FRACTION_LEN) | MAX_SUBNORMAL;
105+
((StorageType(MAX_BIASED_EXPONENT) - 1) << FRACTION_LEN) | MAX_SUBNORMAL;
106106

107107
// We don't want accidental type promotions/conversions, so we require exact
108108
// type match.

libc/src/__support/FPUtil/Hypot.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ LIBC_INLINE T hypot(T x, T y) {
193193
sticky_bits = sticky_bits || ((sum & 0x3U) != 0);
194194
sum >>= 2;
195195
++out_exp;
196-
if (out_exp >= FPBits_t::MAX_EXPONENT) {
196+
if (out_exp >= FPBits_t::MAX_BIASED_EXPONENT) {
197197
if (int round_mode = quick_get_round();
198198
round_mode == FE_TONEAREST || round_mode == FE_UPWARD)
199199
return T(FPBits_t::inf());
@@ -251,7 +251,7 @@ LIBC_INLINE T hypot(T x, T y) {
251251
if (y_new >= (ONE >> 1)) {
252252
y_new -= ONE >> 1;
253253
++out_exp;
254-
if (out_exp >= FPBits_t::MAX_EXPONENT) {
254+
if (out_exp >= FPBits_t::MAX_BIASED_EXPONENT) {
255255
if (round_mode == FE_TONEAREST || round_mode == FE_UPWARD)
256256
return T(FPBits_t::inf());
257257
return T(FPBits_t(FPBits_t::MAX_NORMAL));

libc/src/__support/FPUtil/ManipulationFunctions.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ LIBC_INLINE T ldexp(T x, int exp) {
130130
// early. Because the result of the ldexp operation can be a subnormal number,
131131
// we need to accommodate the (mantissaWidht + 1) worth of shift in
132132
// calculating the limit.
133-
int exp_limit = FPBits<T>::MAX_EXPONENT + FPBits<T>::FRACTION_LEN + 1;
133+
int exp_limit = FPBits<T>::MAX_BIASED_EXPONENT + FPBits<T>::FRACTION_LEN + 1;
134134
if (exp > exp_limit)
135135
return bits.get_sign() ? T(FPBits<T>::neg_inf()) : T(FPBits<T>::inf());
136136

libc/src/__support/FPUtil/generic/FMA.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ template <> LIBC_INLINE double fma<double>(double x, double y, double z) {
128128
y_exp += y_bits.get_biased_exponent();
129129
z_exp += z_bits.get_biased_exponent();
130130

131-
if (LIBC_UNLIKELY(x_exp == FPBits::MAX_EXPONENT ||
132-
y_exp == FPBits::MAX_EXPONENT ||
133-
z_exp == FPBits::MAX_EXPONENT))
131+
if (LIBC_UNLIKELY(x_exp == FPBits::MAX_BIASED_EXPONENT ||
132+
y_exp == FPBits::MAX_BIASED_EXPONENT ||
133+
z_exp == FPBits::MAX_BIASED_EXPONENT))
134134
return x * y + z;
135135

136136
// Extract mantissa and append hidden leading bits.
@@ -255,7 +255,7 @@ template <> LIBC_INLINE double fma<double>(double x, double y, double z) {
255255

256256
// Finalize the result.
257257
int round_mode = fputil::quick_get_round();
258-
if (LIBC_UNLIKELY(r_exp >= FPBits::MAX_EXPONENT)) {
258+
if (LIBC_UNLIKELY(r_exp >= FPBits::MAX_BIASED_EXPONENT)) {
259259
if ((round_mode == FE_TOWARDZERO) ||
260260
(round_mode == FE_UPWARD && prod_sign) ||
261261
(round_mode == FE_DOWNWARD && !prod_sign)) {

libc/src/__support/FPUtil/x86_64/LongDoubleBits.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ template <> struct FPBits<long double> : private FloatProperties<long double> {
4141
public:
4242
using FloatProperties<long double>::SIGN_MASK;
4343

44-
static constexpr int MAX_EXPONENT = 0x7FFF;
44+
static constexpr int MAX_BIASED_EXPONENT = 0x7FFF;
4545
static constexpr StorageType MIN_SUBNORMAL = StorageType(1);
4646
// Subnormal numbers include the implicit bit in x86 long double formats.
4747
static constexpr StorageType MAX_SUBNORMAL =
4848
(StorageType(1) << FRACTION_LEN) - 1;
4949
static constexpr StorageType MIN_NORMAL = (StorageType(3) << FRACTION_LEN);
5050
static constexpr StorageType MAX_NORMAL =
51-
(StorageType(MAX_EXPONENT - 1) << (FRACTION_LEN + 1)) |
51+
(StorageType(MAX_BIASED_EXPONENT - 1) << (FRACTION_LEN + 1)) |
5252
(StorageType(1) << FRACTION_LEN) | MAX_SUBNORMAL;
5353

5454
StorageType bits;
@@ -154,12 +154,12 @@ template <> struct FPBits<long double> : private FloatProperties<long double> {
154154
}
155155

156156
LIBC_INLINE constexpr bool is_inf() const {
157-
return get_biased_exponent() == MAX_EXPONENT && get_mantissa() == 0 &&
158-
get_implicit_bit() == 1;
157+
return get_biased_exponent() == MAX_BIASED_EXPONENT &&
158+
get_mantissa() == 0 && get_implicit_bit() == 1;
159159
}
160160

161161
LIBC_INLINE constexpr bool is_nan() const {
162-
if (get_biased_exponent() == MAX_EXPONENT) {
162+
if (get_biased_exponent() == MAX_BIASED_EXPONENT) {
163163
return (get_implicit_bit() == 0) || get_mantissa() != 0;
164164
} else if (get_biased_exponent() != 0) {
165165
return get_implicit_bit() == 0;
@@ -168,7 +168,7 @@ template <> struct FPBits<long double> : private FloatProperties<long double> {
168168
}
169169

170170
LIBC_INLINE constexpr bool is_inf_or_nan() const {
171-
return (get_biased_exponent() == MAX_EXPONENT) ||
171+
return (get_biased_exponent() == MAX_BIASED_EXPONENT) ||
172172
(get_biased_exponent() != 0 && get_implicit_bit() == 0);
173173
}
174174

@@ -180,7 +180,7 @@ template <> struct FPBits<long double> : private FloatProperties<long double> {
180180

181181
LIBC_INLINE static constexpr long double inf(bool sign = false) {
182182
FPBits<long double> bits(0.0l);
183-
bits.set_biased_exponent(MAX_EXPONENT);
183+
bits.set_biased_exponent(MAX_BIASED_EXPONENT);
184184
bits.set_implicit_bit(1);
185185
if (sign) {
186186
bits.set_sign(true);
@@ -192,7 +192,7 @@ template <> struct FPBits<long double> : private FloatProperties<long double> {
192192

193193
LIBC_INLINE static constexpr long double build_nan(StorageType v) {
194194
FPBits<long double> bits(0.0l);
195-
bits.set_biased_exponent(MAX_EXPONENT);
195+
bits.set_biased_exponent(MAX_BIASED_EXPONENT);
196196
bits.set_implicit_bit(1);
197197
bits.set_mantissa(v);
198198
return bits;

libc/src/__support/high_precision_decimal.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,8 @@ class HighPrecisionDecimal {
344344
int64_t temp_exponent = static_cast<int64_t>(this->decimal_point) +
345345
static_cast<int64_t>(add_to_exponent);
346346

347-
// Theoretically these numbers should be MAX_EXPONENT for long double,
348-
// but that should be ~16,000 which is much less than 1 << 30.
347+
// Theoretically these numbers should be MAX_BIASED_EXPONENT for long
348+
// double, but that should be ~16,000 which is much less than 1 << 30.
349349
if (temp_exponent > (1 << 30)) {
350350
temp_exponent = (1 << 30);
351351
} else if (temp_exponent < -(1 << 30)) {

libc/src/__support/str_to_float.h

+13-13
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ simple_decimal_conversion(const char *__restrict numStart,
338338
// float, return inf.
339339
if (hpd.get_decimal_point() > 0 &&
340340
exp10_to_exp2(hpd.get_decimal_point() - 1) > FloatProp::EXP_BIAS) {
341-
output.num = {0, fputil::FPBits<T>::MAX_EXPONENT};
341+
output.num = {0, fputil::FPBits<T>::MAX_BIASED_EXPONENT};
342342
output.error = ERANGE;
343343
return output;
344344
}
@@ -388,8 +388,8 @@ simple_decimal_conversion(const char *__restrict numStart,
388388
exp2 += FloatProp::EXP_BIAS;
389389

390390
// Handle the exponent being too large (and return inf).
391-
if (exp2 >= FPBits::MAX_EXPONENT) {
392-
output.num = {0, FPBits::MAX_EXPONENT};
391+
if (exp2 >= FPBits::MAX_BIASED_EXPONENT) {
392+
output.num = {0, FPBits::MAX_BIASED_EXPONENT};
393393
output.error = ERANGE;
394394
return output;
395395
}
@@ -424,7 +424,7 @@ simple_decimal_conversion(const char *__restrict numStart,
424424
// Check if this rounding causes exp2 to go out of range and make the result
425425
// INF. If this is the case, then finalMantissa and exp2 are already the
426426
// correct values for an INF result.
427-
if (exp2 >= FPBits::MAX_EXPONENT) {
427+
if (exp2 >= FPBits::MAX_BIASED_EXPONENT) {
428428
output.error = ERANGE;
429429
}
430430
}
@@ -658,7 +658,7 @@ decimal_exp_to_float(ExpandedFloat<T> init_num, const char *__restrict numStart,
658658
// float, return inf. These bounds are relatively loose, but are mostly
659659
// serving as a first pass. Some close numbers getting through is okay.
660660
if (exp10 > get_upper_bound<T>()) {
661-
output.num = {0, FPBits::MAX_EXPONENT};
661+
output.num = {0, FPBits::MAX_BIASED_EXPONENT};
662662
output.error = ERANGE;
663663
return output;
664664
}
@@ -920,10 +920,10 @@ decimal_string_to_float(const char *__restrict src, const char DECIMAL_POINT,
920920

921921
// If the result is in the valid range, then we use it. The valid range is
922922
// also within the int32 range, so this prevents overflow issues.
923-
if (temp_exponent > FPBits::MAX_EXPONENT) {
924-
exponent = FPBits::MAX_EXPONENT;
925-
} else if (temp_exponent < -FPBits::MAX_EXPONENT) {
926-
exponent = -FPBits::MAX_EXPONENT;
923+
if (temp_exponent > FPBits::MAX_BIASED_EXPONENT) {
924+
exponent = FPBits::MAX_BIASED_EXPONENT;
925+
} else if (temp_exponent < -FPBits::MAX_BIASED_EXPONENT) {
926+
exponent = -FPBits::MAX_BIASED_EXPONENT;
927927
} else {
928928
exponent = static_cast<int32_t>(temp_exponent);
929929
}
@@ -1034,10 +1034,10 @@ hexadecimal_string_to_float(const char *__restrict src,
10341034

10351035
// If the result is in the valid range, then we use it. The valid range is
10361036
// also within the int32 range, so this prevents overflow issues.
1037-
if (temp_exponent > FPBits::MAX_EXPONENT) {
1038-
exponent = FPBits::MAX_EXPONENT;
1039-
} else if (temp_exponent < -FPBits::MAX_EXPONENT) {
1040-
exponent = -FPBits::MAX_EXPONENT;
1037+
if (temp_exponent > FPBits::MAX_BIASED_EXPONENT) {
1038+
exponent = FPBits::MAX_BIASED_EXPONENT;
1039+
} else if (temp_exponent < -FPBits::MAX_BIASED_EXPONENT) {
1040+
exponent = -FPBits::MAX_BIASED_EXPONENT;
10411041
} else {
10421042
exponent = static_cast<int32_t>(temp_exponent);
10431043
}

libc/test/src/math/LdExpTest.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::Test {
5858
}
5959

6060
void testOverflow(LdExpFunc func) {
61-
NormalFloat x(FPBits::MAX_EXPONENT - 10, NormalFloat::ONE + 0xF00BA, 0);
61+
NormalFloat x(FPBits::MAX_BIASED_EXPONENT - 10, NormalFloat::ONE + 0xF00BA,
62+
0);
6263
for (int32_t exp = 10; exp < 100; ++exp) {
6364
ASSERT_FP_EQ(inf, func(T(x), exp));
6465
ASSERT_FP_EQ(neg_inf, func(-T(x), exp));
@@ -125,25 +126,25 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::Test {
125126
// Start with a normal number high exponent but pass a very low number for
126127
// exp. The result should be a subnormal number.
127128
x = NormalFloat(FPBits::EXP_BIAS, NormalFloat::ONE, 0);
128-
int exp = -FPBits::MAX_EXPONENT - 5;
129+
int exp = -FPBits::MAX_BIASED_EXPONENT - 5;
129130
T result = func(x, exp);
130131
FPBits result_bits(result);
131132
ASSERT_FALSE(result_bits.is_zero());
132133
// Verify that the result is indeed subnormal.
133134
ASSERT_EQ(result_bits.get_biased_exponent(), uint16_t(0));
134135
// But if the exp is so less that normalization leads to zero, then
135136
// the result should be zero.
136-
result = func(x, -FPBits::MAX_EXPONENT - FPBits::FRACTION_LEN - 5);
137+
result = func(x, -FPBits::MAX_BIASED_EXPONENT - FPBits::FRACTION_LEN - 5);
137138
ASSERT_TRUE(FPBits(result).is_zero());
138139

139140
// Start with a subnormal number but pass a very high number for exponent.
140141
// The result should not be infinity.
141142
x = NormalFloat(-FPBits::EXP_BIAS + 1, NormalFloat::ONE >> 10, 0);
142-
exp = FPBits::MAX_EXPONENT + 5;
143+
exp = FPBits::MAX_BIASED_EXPONENT + 5;
143144
ASSERT_FALSE(FPBits(func(x, exp)).is_inf());
144145
// But if the exp is large enough to oversome than the normalization shift,
145146
// then it should result in infinity.
146-
exp = FPBits::MAX_EXPONENT + 15;
147+
exp = FPBits::MAX_BIASED_EXPONENT + 15;
147148
ASSERT_FP_EQ(func(x, exp), inf);
148149
}
149150
};

libc/test/src/math/smoke/LdExpTest.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::Test {
5858
}
5959

6060
void testOverflow(LdExpFunc func) {
61-
NormalFloat x(FPBits::MAX_EXPONENT - 10, NormalFloat::ONE + 0xF00BA, 0);
61+
NormalFloat x(FPBits::MAX_BIASED_EXPONENT - 10, NormalFloat::ONE + 0xF00BA,
62+
0);
6263
for (int32_t exp = 10; exp < 100; ++exp) {
6364
ASSERT_FP_EQ(inf, func(T(x), exp));
6465
ASSERT_FP_EQ(neg_inf, func(-T(x), exp));
@@ -125,25 +126,25 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::Test {
125126
// Start with a normal number high exponent but pass a very low number for
126127
// exp. The result should be a subnormal number.
127128
x = NormalFloat(FPBits::EXP_BIAS, NormalFloat::ONE, 0);
128-
int exp = -FPBits::MAX_EXPONENT - 5;
129+
int exp = -FPBits::MAX_BIASED_EXPONENT - 5;
129130
T result = func(x, exp);
130131
FPBits result_bits(result);
131132
ASSERT_FALSE(result_bits.is_zero());
132133
// Verify that the result is indeed subnormal.
133134
ASSERT_EQ(result_bits.get_biased_exponent(), uint16_t(0));
134135
// But if the exp is so less that normalization leads to zero, then
135136
// the result should be zero.
136-
result = func(x, -FPBits::MAX_EXPONENT - FPBits::FRACTION_LEN - 5);
137+
result = func(x, -FPBits::MAX_BIASED_EXPONENT - FPBits::FRACTION_LEN - 5);
137138
ASSERT_TRUE(FPBits(result).is_zero());
138139

139140
// Start with a subnormal number but pass a very high number for exponent.
140141
// The result should not be infinity.
141142
x = NormalFloat(-FPBits::EXP_BIAS + 1, NormalFloat::ONE >> 10, 0);
142-
exp = FPBits::MAX_EXPONENT + 5;
143+
exp = FPBits::MAX_BIASED_EXPONENT + 5;
143144
ASSERT_FALSE(FPBits(func(x, exp)).is_inf());
144145
// But if the exp is large enough to oversome than the normalization shift,
145146
// then it should result in infinity.
146-
exp = FPBits::MAX_EXPONENT + 15;
147+
exp = FPBits::MAX_BIASED_EXPONENT + 15;
147148
ASSERT_FP_EQ(func(x, exp), inf);
148149
}
149150
};

libc/test/utils/FPUtil/x86_long_double_test.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ TEST(LlvmLibcX86LongDoubleTest, is_nan) {
2222
constexpr uint32_t COUNT = 100'000;
2323

2424
FPBits bits(0.0l);
25-
bits.set_biased_exponent(FPBits::MAX_EXPONENT);
25+
bits.set_biased_exponent(FPBits::MAX_BIASED_EXPONENT);
2626
for (unsigned int i = 0; i < COUNT; ++i) {
2727
// If exponent has the max value and the implicit bit is 0,
2828
// then the number is a NaN for all values of mantissa.

0 commit comments

Comments
 (0)