Skip to content

Commit 3546f4d

Browse files
authored
[libc][NFC] Rename MANTISSA_WIDTH in FRACTION_LEN (#75489)
This one might be a bit controversial since the terminology has been introduced from the start but I think `FRACTION_LEN` is a better name here. AFAICT it really is "the number of bits after the decimal dot when the number is in normal form." `MANTISSA_WIDTH` is less precise as it's unclear whether we take the leading bit into account. This patch also renames most of the properties to use the `_LEN` suffix and fixes useless casts or variables.
1 parent 3a31cf8 commit 3546f4d

File tree

97 files changed

+877
-888
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+877
-888
lines changed

libc/fuzzing/stdio/printf_float_conv_fuzz.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
9090
int prec = 0;
9191
int width = 0;
9292

93-
LIBC_NAMESPACE::fputil::FPBits<double>::UIntType raw_num = 0;
93+
LIBC_NAMESPACE::fputil::FPBits<double>::StorageType raw_num = 0;
9494

9595
// Copy as many bytes of data as will fit into num, prec, and with. Any extras
9696
// are ignored.

libc/fuzzing/stdlib/strtofloat_fuzz.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ template <typename F> inline constexpr int effective_precision(int exponent) {
3333
// This is intended to be 0 when the exponent is the lowest normal and
3434
// increase as the exponent's magnitude increases.
3535
const int bits_below_normal =
36-
(-exponent) - (FloatProperties<F>::EXPONENT_BIAS - 1);
36+
(-exponent) - (FloatProperties<F>::EXP_BIAS - 1);
3737

3838
// The precision should be the normal, full precision, minus the bits lost
3939
// by this being a subnormal, minus one for the implicit leading one.

libc/src/__support/FPUtil/DivisionAndRemainderOperations.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ LIBC_INLINE T remquo(T x, T y, int &q) {
5353

5454
NormalFloat<T> normalx(xbits), normaly(ybits);
5555
int exp = normalx.exponent - normaly.exponent;
56-
typename NormalFloat<T>::UIntType mx = normalx.mantissa,
57-
my = normaly.mantissa;
56+
typename NormalFloat<T>::StorageType mx = normalx.mantissa,
57+
my = normaly.mantissa;
5858

5959
q = 0;
6060
while (exp >= 0) {
6161
unsigned shift_count = 0;
62-
typename NormalFloat<T>::UIntType n = mx;
62+
typename NormalFloat<T>::StorageType n = mx;
6363
for (shift_count = 0; n < my; n <<= 1, ++shift_count)
6464
;
6565

libc/src/__support/FPUtil/FPBits.h

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -31,49 +31,49 @@ namespace fputil {
3131
template <typename T> struct FPBits : private FloatProperties<T> {
3232
static_assert(cpp::is_floating_point_v<T>,
3333
"FPBits instantiated with invalid type.");
34-
using typename FloatProperties<T>::UIntType;
35-
using FloatProperties<T>::BIT_WIDTH;
34+
using typename FloatProperties<T>::StorageType;
35+
using FloatProperties<T>::TOTAL_LEN;
3636
using FloatProperties<T>::EXP_MANT_MASK;
37-
using FloatProperties<T>::EXPONENT_MASK;
38-
using FloatProperties<T>::EXPONENT_BIAS;
39-
using FloatProperties<T>::EXPONENT_WIDTH;
40-
using FloatProperties<T>::MANTISSA_MASK;
41-
using FloatProperties<T>::MANTISSA_WIDTH;
37+
using FloatProperties<T>::EXP_MASK;
38+
using FloatProperties<T>::EXP_BIAS;
39+
using FloatProperties<T>::EXP_LEN;
40+
using FloatProperties<T>::FRACTION_MASK;
41+
using FloatProperties<T>::FRACTION_LEN;
4242
using FloatProperties<T>::QUIET_NAN_MASK;
4343
using FloatProperties<T>::SIGN_MASK;
4444

4545
// Reinterpreting bits as an integer value and interpreting the bits of an
4646
// integer value as a floating point value is used in tests. So, a convenient
4747
// type is provided for such reinterpretations.
48-
UIntType bits;
48+
StorageType bits;
4949

50-
LIBC_INLINE constexpr void set_mantissa(UIntType mantVal) {
51-
mantVal &= MANTISSA_MASK;
52-
bits &= ~MANTISSA_MASK;
50+
LIBC_INLINE constexpr void set_mantissa(StorageType mantVal) {
51+
mantVal &= FRACTION_MASK;
52+
bits &= ~FRACTION_MASK;
5353
bits |= mantVal;
5454
}
5555

56-
LIBC_INLINE constexpr UIntType get_mantissa() const {
57-
return bits & MANTISSA_MASK;
56+
LIBC_INLINE constexpr StorageType get_mantissa() const {
57+
return bits & FRACTION_MASK;
5858
}
5959

60-
LIBC_INLINE constexpr void set_biased_exponent(UIntType expVal) {
61-
expVal = (expVal << MANTISSA_WIDTH) & EXPONENT_MASK;
62-
bits &= ~EXPONENT_MASK;
60+
LIBC_INLINE constexpr void set_biased_exponent(StorageType expVal) {
61+
expVal = (expVal << FRACTION_LEN) & EXP_MASK;
62+
bits &= ~EXP_MASK;
6363
bits |= expVal;
6464
}
6565

6666
LIBC_INLINE constexpr uint16_t get_biased_exponent() const {
67-
return uint16_t((bits & EXPONENT_MASK) >> MANTISSA_WIDTH);
67+
return uint16_t((bits & EXP_MASK) >> FRACTION_LEN);
6868
}
6969

7070
// The function return mantissa with the implicit bit set iff the current
7171
// value is a valid normal number.
72-
LIBC_INLINE constexpr UIntType get_explicit_mantissa() {
72+
LIBC_INLINE constexpr StorageType get_explicit_mantissa() {
7373
return ((get_biased_exponent() > 0 && !is_inf_or_nan())
74-
? (MANTISSA_MASK + 1)
74+
? (FRACTION_MASK + 1)
7575
: 0) |
76-
(MANTISSA_MASK & bits);
76+
(FRACTION_MASK & bits);
7777
}
7878

7979
LIBC_INLINE constexpr void set_sign(bool signVal) {
@@ -86,41 +86,41 @@ template <typename T> struct FPBits : private FloatProperties<T> {
8686
return (bits & SIGN_MASK) != 0;
8787
}
8888

89-
static_assert(sizeof(T) == sizeof(UIntType),
89+
static_assert(sizeof(T) == sizeof(StorageType),
9090
"Data type and integral representation have different sizes.");
9191

92-
static constexpr int MAX_EXPONENT = (1 << EXPONENT_WIDTH) - 1;
92+
static constexpr int MAX_EXPONENT = (1 << EXP_LEN) - 1;
9393

94-
static constexpr UIntType MIN_SUBNORMAL = UIntType(1);
95-
static constexpr UIntType MAX_SUBNORMAL = (UIntType(1) << MANTISSA_WIDTH) - 1;
96-
static constexpr UIntType MIN_NORMAL = (UIntType(1) << MANTISSA_WIDTH);
97-
static constexpr UIntType MAX_NORMAL =
98-
((UIntType(MAX_EXPONENT) - 1) << MANTISSA_WIDTH) | MAX_SUBNORMAL;
94+
static constexpr StorageType MIN_SUBNORMAL = StorageType(1);
95+
static constexpr StorageType MAX_SUBNORMAL = FRACTION_MASK;
96+
static constexpr StorageType MIN_NORMAL = (StorageType(1) << FRACTION_LEN);
97+
static constexpr StorageType MAX_NORMAL =
98+
((StorageType(MAX_EXPONENT) - 1) << FRACTION_LEN) | MAX_SUBNORMAL;
9999

100100
// We don't want accidental type promotions/conversions, so we require exact
101101
// type match.
102102
template <typename XType, cpp::enable_if_t<cpp::is_same_v<T, XType>, int> = 0>
103103
LIBC_INLINE constexpr explicit FPBits(XType x)
104-
: bits(cpp::bit_cast<UIntType>(x)) {}
104+
: bits(cpp::bit_cast<StorageType>(x)) {}
105105

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

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

112112
LIBC_INLINE constexpr T get_val() const { return cpp::bit_cast<T>(bits); }
113113

114114
LIBC_INLINE constexpr void set_val(T value) {
115-
bits = cpp::bit_cast<UIntType>(value);
115+
bits = cpp::bit_cast<StorageType>(value);
116116
}
117117

118118
LIBC_INLINE constexpr explicit operator T() const { return get_val(); }
119119

120-
LIBC_INLINE constexpr UIntType uintval() const { return bits; }
120+
LIBC_INLINE constexpr StorageType uintval() const { return bits; }
121121

122122
LIBC_INLINE constexpr int get_exponent() const {
123-
return int(get_biased_exponent()) - EXPONENT_BIAS;
123+
return int(get_biased_exponent()) - EXP_BIAS;
124124
}
125125

126126
// If the number is subnormal, the exponent is treated as if it were the
@@ -134,9 +134,9 @@ template <typename T> struct FPBits : private FloatProperties<T> {
134134
if (is_zero()) {
135135
return 0;
136136
} else if (biased_exp == 0) {
137-
return 1 - EXPONENT_BIAS;
137+
return 1 - EXP_BIAS;
138138
} else {
139-
return biased_exp - EXPONENT_BIAS;
139+
return biased_exp - EXP_BIAS;
140140
}
141141
}
142142

@@ -146,29 +146,29 @@ template <typename T> struct FPBits : private FloatProperties<T> {
146146
}
147147

148148
LIBC_INLINE constexpr bool is_inf() const {
149-
return (bits & EXP_MANT_MASK) == EXPONENT_MASK;
149+
return (bits & EXP_MANT_MASK) == EXP_MASK;
150150
}
151151

152152
LIBC_INLINE constexpr bool is_nan() const {
153-
return (bits & EXP_MANT_MASK) > EXPONENT_MASK;
153+
return (bits & EXP_MANT_MASK) > EXP_MASK;
154154
}
155155

156156
LIBC_INLINE constexpr bool is_quiet_nan() const {
157-
return (bits & EXP_MANT_MASK) == (EXPONENT_MASK | QUIET_NAN_MASK);
157+
return (bits & EXP_MANT_MASK) == (EXP_MASK | QUIET_NAN_MASK);
158158
}
159159

160160
LIBC_INLINE constexpr bool is_inf_or_nan() const {
161-
return (bits & EXPONENT_MASK) == EXPONENT_MASK;
161+
return (bits & EXP_MASK) == EXP_MASK;
162162
}
163163

164164
LIBC_INLINE static constexpr T zero(bool sign = false) {
165-
return FPBits(sign ? SIGN_MASK : UIntType(0)).get_val();
165+
return FPBits(sign ? SIGN_MASK : StorageType(0)).get_val();
166166
}
167167

168168
LIBC_INLINE static constexpr T neg_zero() { return zero(true); }
169169

170170
LIBC_INLINE static constexpr T inf(bool sign = false) {
171-
return FPBits((sign ? SIGN_MASK : UIntType(0)) | EXPONENT_MASK).get_val();
171+
return FPBits((sign ? SIGN_MASK : StorageType(0)) | EXP_MASK).get_val();
172172
}
173173

174174
LIBC_INLINE static constexpr T neg_inf() { return inf(true); }
@@ -189,13 +189,13 @@ template <typename T> struct FPBits : private FloatProperties<T> {
189189
return FPBits(MAX_SUBNORMAL).get_val();
190190
}
191191

192-
LIBC_INLINE static constexpr T build_nan(UIntType v) {
192+
LIBC_INLINE static constexpr T build_nan(StorageType v) {
193193
FPBits<T> bits(inf());
194194
bits.set_mantissa(v);
195195
return T(bits);
196196
}
197197

198-
LIBC_INLINE static constexpr T build_quiet_nan(UIntType v) {
198+
LIBC_INLINE static constexpr T build_quiet_nan(StorageType v) {
199199
return build_nan(QUIET_NAN_MASK | v);
200200
}
201201

@@ -209,10 +209,11 @@ template <typename T> struct FPBits : private FloatProperties<T> {
209209
// 3) The function did not check exponent high limit.
210210
// 4) "number" zero value is not processed correctly.
211211
// 5) Number is unsigned, so the result can be only positive.
212-
LIBC_INLINE static constexpr FPBits<T> make_value(UIntType number, int ep) {
212+
LIBC_INLINE static constexpr FPBits<T> make_value(StorageType number,
213+
int ep) {
213214
FPBits<T> result;
214215
// offset: +1 for sign, but -1 for implicit first bit
215-
int lz = cpp::countl_zero(number) - EXPONENT_WIDTH;
216+
int lz = cpp::countl_zero(number) - EXP_LEN;
216217
number <<= lz;
217218
ep -= lz;
218219

@@ -227,7 +228,7 @@ template <typename T> struct FPBits : private FloatProperties<T> {
227228
}
228229

229230
LIBC_INLINE static constexpr FPBits<T>
230-
create_value(bool sign, UIntType biased_exp, UIntType mantissa) {
231+
create_value(bool sign, StorageType biased_exp, StorageType mantissa) {
231232
FPBits<T> result;
232233
result.set_sign(sign);
233234
result.set_biased_exponent(biased_exp);

0 commit comments

Comments
 (0)