Skip to content

Commit a9af317

Browse files
authored
Revert "[libc][NFC] Implement FPBits in terms of FloatProperties to reduce clutter" (#75304)
Reverts #75196 GCC complains about change of meaning for `FPBits` ``` /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/__support/FPUtil/generic/FMod.h:188:9: error: declaration of ‘using FPBits = struct __llvm_libc_18_0_0_git::fputil::FPBits<T>’ changes meaning of ‘FPBits’ [-fpermissive] 188 | using FPBits = FPBits<T>; | ^~~~~~ ``` I'll reland with a different name.
1 parent 5b32740 commit a9af317

File tree

17 files changed

+191
-190
lines changed

17 files changed

+191
-190
lines changed

libc/src/__support/FPUtil/FPBits.h

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -36,74 +36,71 @@ template <typename T> struct ExponentWidth {
3636
// floating numbers. On x86 platforms however, the 'long double' type maps to
3737
// an x87 floating point format. This format is an IEEE 754 extension format.
3838
// It is handled as an explicit specialization of this class.
39-
template <typename T> struct FPBits : private FloatProperties<T> {
39+
template <typename T> struct FPBits {
4040
static_assert(cpp::is_floating_point_v<T>,
4141
"FPBits instantiated with invalid type.");
42-
using typename FloatProperties<T>::UIntType;
43-
using FloatProperties<T>::BIT_WIDTH;
44-
using FloatProperties<T>::EXP_MANT_MASK;
45-
using FloatProperties<T>::EXPONENT_MASK;
46-
using FloatProperties<T>::EXPONENT_BIAS;
47-
using FloatProperties<T>::EXPONENT_WIDTH;
48-
using FloatProperties<T>::MANTISSA_MASK;
49-
using FloatProperties<T>::MANTISSA_WIDTH;
50-
using FloatProperties<T>::QUIET_NAN_MASK;
51-
using FloatProperties<T>::SIGN_MASK;
5242

5343
// Reinterpreting bits as an integer value and interpreting the bits of an
5444
// integer value as a floating point value is used in tests. So, a convenient
5545
// type is provided for such reinterpretations.
46+
using FloatProp = FloatProperties<T>;
47+
using UIntType = typename FloatProp::UIntType;
48+
5649
UIntType bits;
5750

5851
LIBC_INLINE constexpr void set_mantissa(UIntType mantVal) {
59-
mantVal &= MANTISSA_MASK;
60-
bits &= ~MANTISSA_MASK;
52+
mantVal &= (FloatProp::MANTISSA_MASK);
53+
bits &= ~(FloatProp::MANTISSA_MASK);
6154
bits |= mantVal;
6255
}
6356

6457
LIBC_INLINE constexpr UIntType get_mantissa() const {
65-
return bits & MANTISSA_MASK;
58+
return bits & FloatProp::MANTISSA_MASK;
6659
}
6760

6861
LIBC_INLINE constexpr void set_biased_exponent(UIntType expVal) {
69-
expVal = (expVal << MANTISSA_WIDTH) & EXPONENT_MASK;
70-
bits &= ~EXPONENT_MASK;
62+
expVal = (expVal << (FloatProp::MANTISSA_WIDTH)) & FloatProp::EXPONENT_MASK;
63+
bits &= ~(FloatProp::EXPONENT_MASK);
7164
bits |= expVal;
7265
}
7366

7467
LIBC_INLINE constexpr uint16_t get_biased_exponent() const {
75-
return uint16_t((bits & EXPONENT_MASK) >> MANTISSA_WIDTH);
68+
return uint16_t((bits & FloatProp::EXPONENT_MASK) >>
69+
(FloatProp::MANTISSA_WIDTH));
7670
}
7771

7872
// The function return mantissa with the implicit bit set iff the current
7973
// value is a valid normal number.
8074
LIBC_INLINE constexpr UIntType get_explicit_mantissa() {
8175
return ((get_biased_exponent() > 0 && !is_inf_or_nan())
82-
? (MANTISSA_MASK + 1)
76+
? (FloatProp::MANTISSA_MASK + 1)
8377
: 0) |
84-
(MANTISSA_MASK & bits);
78+
(FloatProp::MANTISSA_MASK & bits);
8579
}
8680

8781
LIBC_INLINE constexpr void set_sign(bool signVal) {
88-
bits |= SIGN_MASK;
82+
bits |= FloatProp::SIGN_MASK;
8983
if (!signVal)
90-
bits -= SIGN_MASK;
84+
bits -= FloatProp::SIGN_MASK;
9185
}
9286

9387
LIBC_INLINE constexpr bool get_sign() const {
94-
return (bits & SIGN_MASK) != 0;
88+
return (bits & FloatProp::SIGN_MASK) != 0;
9589
}
9690

9791
static_assert(sizeof(T) == sizeof(UIntType),
9892
"Data type and integral representation have different sizes.");
9993

100-
static constexpr int MAX_EXPONENT = (1 << EXPONENT_WIDTH) - 1;
94+
static constexpr int EXPONENT_BIAS = (1 << (ExponentWidth<T>::VALUE - 1)) - 1;
95+
static constexpr int MAX_EXPONENT = (1 << ExponentWidth<T>::VALUE) - 1;
10196

10297
static constexpr UIntType MIN_SUBNORMAL = UIntType(1);
103-
static constexpr UIntType MAX_SUBNORMAL = (UIntType(1) << MANTISSA_WIDTH) - 1;
104-
static constexpr UIntType MIN_NORMAL = (UIntType(1) << MANTISSA_WIDTH);
98+
static constexpr UIntType MAX_SUBNORMAL =
99+
(UIntType(1) << MantissaWidth<T>::VALUE) - 1;
100+
static constexpr UIntType MIN_NORMAL =
101+
(UIntType(1) << MantissaWidth<T>::VALUE);
105102
static constexpr UIntType MAX_NORMAL =
106-
((UIntType(MAX_EXPONENT) - 1) << MANTISSA_WIDTH) | MAX_SUBNORMAL;
103+
((UIntType(MAX_EXPONENT) - 1) << MantissaWidth<T>::VALUE) | MAX_SUBNORMAL;
107104

108105
// We don't want accidental type promotions/conversions, so we require exact
109106
// type match.
@@ -154,29 +151,32 @@ template <typename T> struct FPBits : private FloatProperties<T> {
154151
}
155152

156153
LIBC_INLINE constexpr bool is_inf() const {
157-
return (bits & EXP_MANT_MASK) == EXPONENT_MASK;
154+
return (bits & FloatProp::EXP_MANT_MASK) == FloatProp::EXPONENT_MASK;
158155
}
159156

160157
LIBC_INLINE constexpr bool is_nan() const {
161-
return (bits & EXP_MANT_MASK) > EXPONENT_MASK;
158+
return (bits & FloatProp::EXP_MANT_MASK) > FloatProp::EXPONENT_MASK;
162159
}
163160

164161
LIBC_INLINE constexpr bool is_quiet_nan() const {
165-
return (bits & EXP_MANT_MASK) == (EXPONENT_MASK | QUIET_NAN_MASK);
162+
return (bits & FloatProp::EXP_MANT_MASK) ==
163+
(FloatProp::EXPONENT_MASK | FloatProp::QUIET_NAN_MASK);
166164
}
167165

168166
LIBC_INLINE constexpr bool is_inf_or_nan() const {
169-
return (bits & EXPONENT_MASK) == EXPONENT_MASK;
167+
return (bits & FloatProp::EXPONENT_MASK) == FloatProp::EXPONENT_MASK;
170168
}
171169

172170
LIBC_INLINE static constexpr T zero(bool sign = false) {
173-
return FPBits(sign ? SIGN_MASK : UIntType(0)).get_val();
171+
return FPBits(sign ? FloatProp::SIGN_MASK : UIntType(0)).get_val();
174172
}
175173

176174
LIBC_INLINE static constexpr T neg_zero() { return zero(true); }
177175

178176
LIBC_INLINE static constexpr T inf(bool sign = false) {
179-
return FPBits((sign ? SIGN_MASK : UIntType(0)) | EXPONENT_MASK).get_val();
177+
return FPBits((sign ? FloatProp::SIGN_MASK : UIntType(0)) |
178+
FloatProp::EXPONENT_MASK)
179+
.get_val();
180180
}
181181

182182
LIBC_INLINE static constexpr T neg_inf() { return inf(true); }
@@ -204,7 +204,7 @@ template <typename T> struct FPBits : private FloatProperties<T> {
204204
}
205205

206206
LIBC_INLINE static constexpr T build_quiet_nan(UIntType v) {
207-
return build_nan(QUIET_NAN_MASK | v);
207+
return build_nan(FloatProp::QUIET_NAN_MASK | v);
208208
}
209209

210210
// The function convert integer number and unbiased exponent to proper float
@@ -220,7 +220,7 @@ template <typename T> struct FPBits : private FloatProperties<T> {
220220
LIBC_INLINE static constexpr FPBits<T> make_value(UIntType number, int ep) {
221221
FPBits<T> result;
222222
// offset: +1 for sign, but -1 for implicit first bit
223-
int lz = cpp::countl_zero(number) - EXPONENT_WIDTH;
223+
int lz = cpp::countl_zero(number) - FloatProp::EXPONENT_WIDTH;
224224
number <<= lz;
225225
ep -= lz;
226226

libc/src/__support/FPUtil/Hypot.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,14 @@ template <> struct DoubleLength<uint64_t> {
104104
//
105105
template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
106106
LIBC_INLINE T hypot(T x, T y) {
107-
using FPBits = FPBits<T>;
108-
using UIntType = typename FPBits::UIntType;
107+
using FPBits_t = FPBits<T>;
108+
using UIntType = typename FPBits<T>::UIntType;
109109
using DUIntType = typename DoubleLength<UIntType>::Type;
110110

111-
FPBits x_bits(x), y_bits(y);
111+
FPBits_t x_bits(x), y_bits(y);
112112

113113
if (x_bits.is_inf() || y_bits.is_inf()) {
114-
return T(FPBits::inf());
114+
return T(FPBits_t::inf());
115115
}
116116
if (x_bits.is_nan()) {
117117
return x;
@@ -193,11 +193,11 @@ 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::MAX_EXPONENT) {
196+
if (out_exp >= FPBits_t::MAX_EXPONENT) {
197197
if (int round_mode = quick_get_round();
198198
round_mode == FE_TONEAREST || round_mode == FE_UPWARD)
199-
return T(FPBits::inf());
200-
return T(FPBits(FPBits::MAX_NORMAL));
199+
return T(FPBits_t::inf());
200+
return T(FPBits_t(FPBits_t::MAX_NORMAL));
201201
}
202202
} else {
203203
// For denormal result, we simply move the leading bit of the result to
@@ -251,10 +251,10 @@ 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::MAX_EXPONENT) {
254+
if (out_exp >= FPBits_t::MAX_EXPONENT) {
255255
if (round_mode == FE_TONEAREST || round_mode == FE_UPWARD)
256-
return T(FPBits::inf());
257-
return T(FPBits(FPBits::MAX_NORMAL));
256+
return T(FPBits_t::inf());
257+
return T(FPBits_t(FPBits_t::MAX_NORMAL));
258258
}
259259
}
260260

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

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ template <typename T> struct FModExceptionalInputHandler {
123123
"FModCStandardWrapper instantiated with invalid type.");
124124

125125
LIBC_INLINE static bool pre_check(T x, T y, T &out) {
126-
using FPBits = fputil::FPBits<T>;
127-
const T quiet_nan = FPBits::build_quiet_nan(0);
128-
FPBits sx(x), sy(y);
126+
using FPB = fputil::FPBits<T>;
127+
const T quiet_nan = FPB::build_quiet_nan(0);
128+
FPB sx(x), sy(y);
129129
if (LIBC_LIKELY(!sy.is_zero() && !sy.is_inf_or_nan() &&
130130
!sx.is_inf_or_nan())) {
131131
return false;
@@ -167,11 +167,11 @@ template <typename T> struct FModFastMathWrapper {
167167

168168
template <typename T> class FModDivisionSimpleHelper {
169169
private:
170-
using UIntType = typename FPBits<T>::UIntType;
170+
using intU_t = typename FPBits<T>::UIntType;
171171

172172
public:
173-
LIBC_INLINE constexpr static UIntType
174-
execute(int exp_diff, int sides_zeroes_count, UIntType m_x, UIntType m_y) {
173+
LIBC_INLINE constexpr static intU_t
174+
execute(int exp_diff, int sides_zeroes_count, intU_t m_x, intU_t m_y) {
175175
while (exp_diff > sides_zeroes_count) {
176176
exp_diff -= sides_zeroes_count;
177177
m_x <<= sides_zeroes_count;
@@ -185,24 +185,24 @@ template <typename T> class FModDivisionSimpleHelper {
185185

186186
template <typename T> class FModDivisionInvMultHelper {
187187
private:
188-
using FPBits = FPBits<T>;
189-
using UIntType = typename FPBits::UIntType;
188+
using FPB = FPBits<T>;
189+
using intU_t = typename FPB::UIntType;
190190

191191
public:
192-
LIBC_INLINE constexpr static UIntType
193-
execute(int exp_diff, int sides_zeroes_count, UIntType m_x, UIntType m_y) {
192+
LIBC_INLINE constexpr static intU_t
193+
execute(int exp_diff, int sides_zeroes_count, intU_t m_x, intU_t m_y) {
194194
if (exp_diff > sides_zeroes_count) {
195-
UIntType inv_hy = (cpp::numeric_limits<UIntType>::max() / m_y);
195+
intU_t inv_hy = (cpp::numeric_limits<intU_t>::max() / m_y);
196196
while (exp_diff > sides_zeroes_count) {
197197
exp_diff -= sides_zeroes_count;
198-
UIntType hd =
199-
(m_x * inv_hy) >> (FPBits::BIT_WIDTH - sides_zeroes_count);
198+
intU_t hd =
199+
(m_x * inv_hy) >> (FPB::FloatProp::BIT_WIDTH - sides_zeroes_count);
200200
m_x <<= sides_zeroes_count;
201201
m_x -= hd * m_y;
202202
while (LIBC_UNLIKELY(m_x > m_y))
203203
m_x -= m_y;
204204
}
205-
UIntType hd = (m_x * inv_hy) >> (FPBits::BIT_WIDTH - exp_diff);
205+
intU_t hd = (m_x * inv_hy) >> (FPB::FloatProp::BIT_WIDTH - exp_diff);
206206
m_x <<= exp_diff;
207207
m_x -= hd * m_y;
208208
while (LIBC_UNLIKELY(m_x > m_y))
@@ -222,44 +222,44 @@ class FMod {
222222
"FMod instantiated with invalid type.");
223223

224224
private:
225-
using FPBits = FPBits<T>;
226-
using UIntType = typename FPBits::UIntType;
225+
using FPB = FPBits<T>;
226+
using intU_t = typename FPB::UIntType;
227227

228-
LIBC_INLINE static constexpr FPBits eval_internal(FPBits sx, FPBits sy) {
228+
LIBC_INLINE static constexpr FPB eval_internal(FPB sx, FPB sy) {
229229

230230
if (LIBC_LIKELY(sx.uintval() <= sy.uintval())) {
231231
if (sx.uintval() < sy.uintval())
232-
return sx; // |x|<|y| return x
233-
return FPBits(FPBits::zero()); // |x|=|y| return 0.0
232+
return sx; // |x|<|y| return x
233+
return FPB(FPB::zero()); // |x|=|y| return 0.0
234234
}
235235

236236
int e_x = sx.get_biased_exponent();
237237
int e_y = sy.get_biased_exponent();
238238

239239
// Most common case where |y| is "very normal" and |x/y| < 2^EXPONENT_WIDTH
240-
if (LIBC_LIKELY(e_y > int(FPBits::MANTISSA_WIDTH) &&
241-
e_x - e_y <= int(FPBits::EXPONENT_WIDTH))) {
242-
UIntType m_x = sx.get_explicit_mantissa();
243-
UIntType m_y = sy.get_explicit_mantissa();
244-
UIntType d = (e_x == e_y) ? (m_x - m_y) : (m_x << (e_x - e_y)) % m_y;
240+
if (LIBC_LIKELY(e_y > int(FPB::FloatProp::MANTISSA_WIDTH) &&
241+
e_x - e_y <= int(FPB::FloatProp::EXPONENT_WIDTH))) {
242+
intU_t m_x = sx.get_explicit_mantissa();
243+
intU_t m_y = sy.get_explicit_mantissa();
244+
intU_t d = (e_x == e_y) ? (m_x - m_y) : (m_x << (e_x - e_y)) % m_y;
245245
if (d == 0)
246-
return FPBits(FPBits::zero());
246+
return FPB(FPB::zero());
247247
// iy - 1 because of "zero power" for number with power 1
248-
return FPBits::make_value(d, e_y - 1);
248+
return FPB::make_value(d, e_y - 1);
249249
}
250250
/* Both subnormal special case. */
251251
if (LIBC_UNLIKELY(e_x == 0 && e_y == 0)) {
252-
FPBits d;
252+
FPB d;
253253
d.set_mantissa(sx.uintval() % sy.uintval());
254254
return d;
255255
}
256256

257257
// Note that hx is not subnormal by conditions above.
258-
UIntType m_x = sx.get_explicit_mantissa();
258+
intU_t m_x = sx.get_explicit_mantissa();
259259
e_x--;
260260

261-
UIntType m_y = sy.get_explicit_mantissa();
262-
int lead_zeros_m_y = FPBits::EXPONENT_WIDTH;
261+
intU_t m_y = sy.get_explicit_mantissa();
262+
int lead_zeros_m_y = FPB::FloatProp::EXPONENT_WIDTH;
263263
if (LIBC_LIKELY(e_y > 0)) {
264264
e_y--;
265265
} else {
@@ -282,34 +282,34 @@ class FMod {
282282

283283
{
284284
// Shift hx left until the end or n = 0
285-
int left_shift = exp_diff < int(FPBits::EXPONENT_WIDTH)
285+
int left_shift = exp_diff < int(FPB::FloatProp::EXPONENT_WIDTH)
286286
? exp_diff
287-
: FPBits::EXPONENT_WIDTH;
287+
: FPB::FloatProp::EXPONENT_WIDTH;
288288
m_x <<= left_shift;
289289
exp_diff -= left_shift;
290290
}
291291

292292
m_x %= m_y;
293293
if (LIBC_UNLIKELY(m_x == 0))
294-
return FPBits(FPBits::zero());
294+
return FPB(FPB::zero());
295295

296296
if (exp_diff == 0)
297-
return FPBits::make_value(m_x, e_y);
297+
return FPB::make_value(m_x, e_y);
298298

299299
/* hx next can't be 0, because hx < hy, hy % 2 == 1 hx * 2^i % hy != 0 */
300300
m_x = DivisionHelper::execute(exp_diff, sides_zeroes_count, m_x, m_y);
301-
return FPBits::make_value(m_x, e_y);
301+
return FPB::make_value(m_x, e_y);
302302
}
303303

304304
public:
305305
LIBC_INLINE static T eval(T x, T y) {
306306
if (T out; Wrapper::pre_check(x, y, out))
307307
return out;
308-
FPBits sx(x), sy(y);
308+
FPB sx(x), sy(y);
309309
bool sign = sx.get_sign();
310310
sx.set_sign(false);
311311
sy.set_sign(false);
312-
FPBits result = eval_internal(sx, sy);
312+
FPB result = eval_internal(sx, sy);
313313
result.set_sign(sign);
314314
return result.get_val();
315315
}

0 commit comments

Comments
 (0)