Skip to content

Commit ea43c8e

Browse files
authored
[libc][NFC] Make EXP_MANT_MASK an implementation detail (#75810)
This mask is an implementation detail of `FPBits` and shouldn't really leak outside of it.
1 parent c373f58 commit ea43c8e

File tree

14 files changed

+27
-24
lines changed

14 files changed

+27
-24
lines changed

libc/src/__support/FPUtil/FPBits.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ template <typename T> struct FPBits : private FloatProperties<T> {
3333
"FPBits instantiated with invalid type.");
3434
using typename FloatProperties<T>::StorageType;
3535
using FloatProperties<T>::TOTAL_LEN;
36-
using FloatProperties<T>::EXP_MANT_MASK;
36+
37+
private:
38+
using FloatProperties<T>::EXP_SIG_MASK;
39+
40+
public:
3741
using FloatProperties<T>::EXP_MASK;
3842
using FloatProperties<T>::EXP_BIAS;
3943
using FloatProperties<T>::EXP_LEN;
@@ -149,21 +153,25 @@ template <typename T> struct FPBits : private FloatProperties<T> {
149153
}
150154

151155
LIBC_INLINE constexpr bool is_inf() const {
152-
return (bits & EXP_MANT_MASK) == EXP_MASK;
156+
return (bits & EXP_SIG_MASK) == EXP_MASK;
153157
}
154158

155159
LIBC_INLINE constexpr bool is_nan() const {
156-
return (bits & EXP_MANT_MASK) > EXP_MASK;
160+
return (bits & EXP_SIG_MASK) > EXP_MASK;
157161
}
158162

159163
LIBC_INLINE constexpr bool is_quiet_nan() const {
160-
return (bits & EXP_MANT_MASK) == (EXP_MASK | QUIET_NAN_MASK);
164+
return (bits & EXP_SIG_MASK) == (EXP_MASK | QUIET_NAN_MASK);
161165
}
162166

163167
LIBC_INLINE constexpr bool is_inf_or_nan() const {
164168
return (bits & EXP_MASK) == EXP_MASK;
165169
}
166170

171+
LIBC_INLINE constexpr FPBits abs() const {
172+
return FPBits(bits & EXP_SIG_MASK);
173+
}
174+
167175
LIBC_INLINE static constexpr T zero(bool sign = false) {
168176
return FPBits(sign ? SIGN_MASK : StorageType(0)).get_val();
169177
}

libc/src/__support/FPUtil/FloatProperties.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ struct FPProperties : public internal::FPBaseProperties<fp_type> {
131131
// The bit pattern that keeps only the *sign* part.
132132
LIBC_INLINE_VAR static constexpr StorageType SIGN_MASK =
133133
mask_trailing_ones<StorageType, SIGN_LEN>() << SIGN_MASK_SHIFT;
134+
// The bit pattern that keeps only the *exponent + significand* part.
135+
LIBC_INLINE_VAR static constexpr StorageType EXP_SIG_MASK =
136+
mask_trailing_ones<StorageType, EXP_LEN + SIG_LEN>();
134137
// The bit pattern that keeps only the *sign + exponent + significand* part.
135138
LIBC_INLINE_VAR static constexpr StorageType FP_MASK =
136139
mask_trailing_ones<StorageType, TOTAL_LEN>();
@@ -152,8 +155,6 @@ struct FPProperties : public internal::FPBaseProperties<fp_type> {
152155
FRACTION_LEN + 1;
153156
LIBC_INLINE_VAR static constexpr StorageType FRACTION_MASK =
154157
mask_trailing_ones<StorageType, FRACTION_LEN>();
155-
LIBC_INLINE_VAR static constexpr StorageType EXP_MANT_MASK =
156-
EXP_MASK | SIG_MASK;
157158

158159
protected:
159160
// If a number x is a NAN, then it is a quiet NAN if:

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ namespace fputil {
2929
template <> struct FPBits<long double> : private FloatProperties<long double> {
3030
using typename FloatProperties<long double>::StorageType;
3131
using FloatProperties<long double>::TOTAL_LEN;
32-
using FloatProperties<long double>::EXP_MANT_MASK;
3332
using FloatProperties<long double>::EXP_MASK;
3433
using FloatProperties<long double>::EXP_BIAS;
3534
using FloatProperties<long double>::EXP_LEN;

libc/src/math/generic/acoshf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ LLVM_LIBC_FUNCTION(float, acoshf, (float x)) {
3434

3535
if (LIBC_UNLIKELY(x_u >= 0x4f8ffb03)) {
3636
// Check for exceptional values.
37-
uint32_t x_abs = x_u & FPBits_t::EXP_MANT_MASK;
37+
uint32_t x_abs = xbits.abs().uintval();
3838
if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) {
3939
// x is +inf or NaN.
4040
return x;

libc/src/math/generic/asinhf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ LLVM_LIBC_FUNCTION(float, asinhf, (float x)) {
2121
using FPBits_t = typename fputil::FPBits<float>;
2222
FPBits_t xbits(x);
2323
uint32_t x_u = xbits.uintval();
24-
uint32_t x_abs = x_u & FPBits_t::EXP_MANT_MASK;
24+
uint32_t x_abs = xbits.abs().uintval();
2525

2626
// |x| <= 2^-3
2727
if (LIBC_UNLIKELY(x_abs <= 0x3e80'0000U)) {

libc/src/math/generic/atanhf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ LLVM_LIBC_FUNCTION(float, atanhf, (float x)) {
1717
using FPBits = typename fputil::FPBits<float>;
1818
FPBits xbits(x);
1919
bool sign = xbits.get_sign();
20-
uint32_t x_abs = xbits.uintval() & FPBits::EXP_MANT_MASK;
20+
uint32_t x_abs = xbits.abs().uintval();
2121

2222
// |x| >= 1.0
2323
if (LIBC_UNLIKELY(x_abs >= 0x3F80'0000U)) {

libc/src/math/generic/exp.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,10 @@ DoubleDouble exp_double_double(double x, double kd,
174174
// |x| <= 2^-53 or x < log(2^-1075) or x >= 0x1.6232bdd7abcd3p+9
175175
double set_exceptional(double x) {
176176
using FPBits = typename fputil::FPBits<double>;
177-
using FloatProp = typename fputil::FloatProperties<double>;
178177
FPBits xbits(x);
179178

180179
uint64_t x_u = xbits.uintval();
181-
uint64_t x_abs = x_u & FloatProp::EXP_MANT_MASK;
180+
uint64_t x_abs = xbits.abs().uintval();
182181

183182
// |x| <= 2^-53
184183
if (x_abs <= 0x3ca0'0000'0000'0000ULL) {

libc/src/math/generic/exp10.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,10 @@ double exp10_denorm(double x) {
221221
// * x is inf or nan
222222
double set_exceptional(double x) {
223223
using FPBits = typename fputil::FPBits<double>;
224-
using FloatProp = typename fputil::FloatProperties<double>;
225224
FPBits xbits(x);
226225

227226
uint64_t x_u = xbits.uintval();
228-
uint64_t x_abs = x_u & FloatProp::EXP_MANT_MASK;
227+
uint64_t x_abs = xbits.abs().uintval();
229228

230229
// |x| < log10(1 + 2^-53)
231230
if (x_abs <= 0x3c8bcb7b1526e50e) {

libc/src/math/generic/exp2.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,10 @@ double exp2_denorm(double x) {
196196
// * x is inf or nan
197197
double set_exceptional(double x) {
198198
using FPBits = typename fputil::FPBits<double>;
199-
using FloatProp = typename fputil::FloatProperties<double>;
200199
FPBits xbits(x);
201200

202201
uint64_t x_u = xbits.uintval();
203-
uint64_t x_abs = x_u & FloatProp::EXP_MANT_MASK;
202+
uint64_t x_abs = xbits.abs().uintval();
204203

205204
// |x| < log2(1 + 2^-53)
206205
if (x_abs <= 0x3ca71547652b82fd) {

libc/src/math/generic/expm1.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,10 @@ DoubleDouble exp_double_double(double x, double kd, const DoubleDouble &exp_mid,
219219
// |x| <= 2^-53 or x < log(2^-54) or x >= 0x1.6232bdd7abcd3p+9
220220
double set_exceptional(double x) {
221221
using FPBits = typename fputil::FPBits<double>;
222-
using FloatProp = typename fputil::FloatProperties<double>;
223222
FPBits xbits(x);
224223

225224
uint64_t x_u = xbits.uintval();
226-
uint64_t x_abs = x_u & FloatProp::EXP_MANT_MASK;
225+
uint64_t x_abs = xbits.abs().uintval();
227226

228227
// |x| <= 2^-53.
229228
if (x_abs <= 0x3ca0'0000'0000'0000ULL) {

libc/src/math/generic/inv_trigf_utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ LIBC_INLINE double atan_eval(double x) {
5050

5151
FPB bs(x);
5252
bool sign = bs.get_sign();
53-
auto x_abs = bs.uintval() & FPB::EXP_MANT_MASK;
53+
auto x_abs = bs.abs().uintval();
5454

5555
if (x_abs <= umin) {
5656
double pe = LIBC_NAMESPACE::fputil::polyeval(

libc/src/math/generic/powf.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,9 +517,9 @@ LLVM_LIBC_FUNCTION(float, powf, (float x, float y)) {
517517
FloatBits xbits(x), ybits(y);
518518

519519
uint32_t x_u = xbits.uintval();
520-
uint32_t x_abs = x_u & FloatProp::EXP_MANT_MASK;
520+
uint32_t x_abs = xbits.abs().uintval();
521521
uint32_t y_u = ybits.uintval();
522-
uint32_t y_abs = y_u & FloatProp::EXP_MANT_MASK;
522+
uint32_t y_abs = ybits.abs().uintval();
523523

524524
///////// BEGIN - Check exceptional cases ////////////////////////////////////
525525

libc/src/math/generic/sinhf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace LIBC_NAMESPACE {
1717
LLVM_LIBC_FUNCTION(float, sinhf, (float x)) {
1818
using FPBits = typename fputil::FPBits<float>;
1919
FPBits xbits(x);
20-
uint32_t x_abs = xbits.uintval() & FPBits::EXP_MANT_MASK;
20+
uint32_t x_abs = xbits.abs().uintval();
2121

2222
// When |x| >= 90, or x is inf or nan
2323
if (LIBC_UNLIKELY(x_abs >= 0x42b4'0000U || x_abs <= 0x3da0'0000U)) {

libc/src/math/generic/tanhf.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ constexpr double LOG2_E_EXP2_6 = ExpBase::LOG2_B * 2.0;
2323
LLVM_LIBC_FUNCTION(float, tanhf, (float x)) {
2424
using FPBits = typename fputil::FPBits<float>;
2525
FPBits xbits(x);
26-
uint32_t x_u = xbits.uintval();
27-
uint32_t x_abs = x_u & FPBits::EXP_MANT_MASK;
26+
uint32_t x_abs = xbits.abs().uintval();
2827

2928
// When |x| >= 15, or x is inf or nan, or |x| <= 0.078125
3029
if (LIBC_UNLIKELY((x_abs >= 0x4170'0000U) || (x_abs <= 0x3da0'0000U))) {

0 commit comments

Comments
 (0)