Skip to content

Commit b524eed

Browse files
authored
Revert "[libc] Remove unnecessary FPBits functions and properties" (#79118)
Reverts #79113 It broke aarch64 build bot machines.
1 parent 8e09f13 commit b524eed

Some content is hidden

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

68 files changed

+421
-426
lines changed

libc/fuzzing/stdlib/strtofloat_fuzz.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ using LIBC_NAMESPACE::fputil::FPBits;
2828
// exponent. Subnormals have a lower effective precision since they don't
2929
// necessarily use all of the bits of the mantissa.
3030
template <typename F> inline constexpr int effective_precision(int exponent) {
31-
const int full_precision = FPBits<F>::FRACTION_LEN + 1;
31+
const int full_precision = FPBits<F>::MANTISSA_PRECISION;
3232

3333
// This is intended to be 0 when the exponent is the lowest normal and
3434
// increase as the exponent's magnitude increases.

libc/src/__support/FPUtil/DivisionAndRemainderOperations.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ LIBC_INLINE T remquo(T x, T y, int &q) {
3131
if (ybits.is_nan())
3232
return y;
3333
if (xbits.is_inf() || ybits.is_zero())
34-
return FPBits<T>::build_quiet_nan(fputil::Sign::POS, 1).get_val();
34+
return FPBits<T>::build_quiet_nan(1);
3535

3636
if (xbits.is_zero()) {
3737
q = 0;

libc/src/__support/FPUtil/FPBits.h

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ struct FPRepSem : public FPStorage<fp_type> {
390390
return exp_bits() == encode(BiasedExp::BITS_ALL_ZEROES());
391391
}
392392
LIBC_INLINE constexpr bool is_normal() const {
393-
return is_finite() && !is_subnormal();
393+
return is_finite() && !UP::is_subnormal();
394394
}
395395
// Returns the mantissa with the implicit bit set iff the current
396396
// value is a valid normal number.
@@ -556,14 +556,6 @@ struct FPRep : public FPRepSem<fp_type, RetT> {
556556
using UP::FRACTION_MASK;
557557
using UP::SIGN_MASK;
558558

559-
// Comparison
560-
LIBC_INLINE constexpr friend bool operator==(FPRep a, FPRep b) {
561-
return a.uintval() == b.uintval();
562-
}
563-
LIBC_INLINE constexpr friend bool operator!=(FPRep a, FPRep b) {
564-
return a.uintval() != b.uintval();
565-
}
566-
567559
// Representation
568560
LIBC_INLINE constexpr StorageType uintval() const { return bits & FP_MASK; }
569561
LIBC_INLINE constexpr void set_uintval(StorageType value) {
@@ -706,6 +698,16 @@ struct FPBits final : public internal::FPRep<get_fp_type<T>(), FPBits<T>> {
706698
using UP::bits;
707699

708700
// Constants.
701+
LIBC_INLINE_VAR static constexpr uint32_t MANTISSA_PRECISION =
702+
UP::FRACTION_LEN + 1;
703+
LIBC_INLINE_VAR static constexpr StorageType MIN_NORMAL =
704+
UP::min_normal(Sign::POS).uintval();
705+
LIBC_INLINE_VAR static constexpr StorageType MAX_NORMAL =
706+
UP::max_normal(Sign::POS).uintval();
707+
LIBC_INLINE_VAR static constexpr StorageType MIN_SUBNORMAL =
708+
UP::min_subnormal(Sign::POS).uintval();
709+
LIBC_INLINE_VAR static constexpr StorageType MAX_SUBNORMAL =
710+
UP::max_subnormal(Sign::POS).uintval();
709711
LIBC_INLINE_VAR static constexpr int MAX_BIASED_EXPONENT =
710712
(1 << UP::EXP_LEN) - 1;
711713

@@ -729,6 +731,37 @@ struct FPBits final : public internal::FPRep<get_fp_type<T>(), FPBits<T>> {
729731

730732
LIBC_INLINE constexpr explicit operator T() const { return get_val(); }
731733

734+
// Methods below this are used by tests.
735+
// TODO: inline and remove.
736+
LIBC_INLINE static constexpr T one(Sign sign = Sign::POS) {
737+
return T(UP::one(sign));
738+
}
739+
LIBC_INLINE static constexpr T zero(Sign sign = Sign::POS) {
740+
return T(UP::zero(sign));
741+
}
742+
LIBC_INLINE static constexpr T inf(Sign sign = Sign::POS) {
743+
return T(UP::inf(sign));
744+
}
745+
LIBC_INLINE static constexpr T min_normal() {
746+
return T(UP::min_normal(Sign::POS));
747+
}
748+
LIBC_INLINE static constexpr T max_normal() {
749+
return T(UP::max_normal(Sign::POS));
750+
}
751+
LIBC_INLINE static constexpr T min_denormal() {
752+
return T(UP::min_subnormal(Sign::POS));
753+
}
754+
LIBC_INLINE static constexpr T max_denormal() {
755+
return T(UP::max_subnormal(Sign::POS));
756+
}
757+
LIBC_INLINE static constexpr T build_nan(StorageType v) {
758+
return T(UP::build_nan(Sign::POS, v));
759+
}
760+
LIBC_INLINE static constexpr T build_quiet_nan(StorageType v,
761+
Sign sign = Sign::POS) {
762+
return T(UP::build_quiet_nan(sign, v));
763+
}
764+
732765
// TODO: Use an uint32_t for 'biased_exp'.
733766
LIBC_INLINE static constexpr FPBits<T>
734767
create_value(Sign sign, StorageType biased_exp, StorageType mantissa) {

libc/src/__support/FPUtil/Hypot.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ LIBC_INLINE T hypot(T x, T y) {
197197
if (int round_mode = quick_get_round();
198198
round_mode == FE_TONEAREST || round_mode == FE_UPWARD)
199199
return T(FPBits_t::inf());
200-
return T(FPBits_t::max_normal());
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
@@ -254,7 +254,7 @@ LIBC_INLINE T hypot(T x, T y) {
254254
if (out_exp >= FPBits_t::MAX_BIASED_EXPONENT) {
255255
if (round_mode == FE_TONEAREST || round_mode == FE_UPWARD)
256256
return T(FPBits_t::inf());
257-
return T(FPBits_t::max_normal());
257+
return T(FPBits_t(FPBits_t::MAX_NORMAL));
258258
}
259259
}
260260

libc/src/__support/FPUtil/ManipulationFunctions.h

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ LIBC_INLINE T logb(T x) {
108108
return x;
109109
} else if (bits.is_inf()) {
110110
// Return positive infinity.
111-
return T(FPBits<T>::inf());
111+
return T(FPBits<T>::inf(Sign::POS));
112112
}
113113

114114
NormalFloat<T> normal(bits);
@@ -127,7 +127,7 @@ LIBC_INLINE T ldexp(T x, int exp) {
127127
// that adding |exp| to it does not lead to integer rollover. But, if |exp|
128128
// value is larger the exponent range for type T, then we can return infinity
129129
// early. Because the result of the ldexp operation can be a subnormal number,
130-
// we need to accommodate the (mantissaWidth + 1) worth of shift in
130+
// we need to accommodate the (mantissaWidht + 1) worth of shift in
131131
// calculating the limit.
132132
int exp_limit = FPBits<T>::MAX_BIASED_EXPONENT + FPBits<T>::FRACTION_LEN + 1;
133133
if (exp > exp_limit)
@@ -164,22 +164,26 @@ LIBC_INLINE T nextafter(T from, U to) {
164164
return static_cast<T>(to);
165165

166166
using StorageType = typename FPBits<T>::StorageType;
167-
if (from != T(0)) {
168-
if ((static_cast<U>(from) < to) == (from > T(0))) {
169-
from_bits = FPBits<T>(StorageType(from_bits.uintval() + 1));
167+
StorageType int_val = from_bits.uintval();
168+
if (from != FPBits<T>::zero()) {
169+
if ((static_cast<U>(from) < to) == (from > FPBits<T>::zero())) {
170+
++int_val;
170171
} else {
171-
from_bits = FPBits<T>(StorageType(from_bits.uintval() - 1));
172+
--int_val;
172173
}
173174
} else {
174-
from_bits = FPBits<T>::min_subnormal(to_bits.sign());
175+
int_val = FPBits<T>::MIN_SUBNORMAL;
176+
if (to_bits.is_neg())
177+
int_val |= FPBits<T>::SIGN_MASK;
175178
}
176179

177-
if (from_bits.is_subnormal())
180+
StorageType exponent_bits = int_val & FPBits<T>::EXP_MASK;
181+
if (exponent_bits == StorageType(0))
178182
raise_except_if_required(FE_UNDERFLOW | FE_INEXACT);
179-
else if (from_bits.is_inf())
183+
else if (exponent_bits == FPBits<T>::EXP_MASK)
180184
raise_except_if_required(FE_OVERFLOW | FE_INEXACT);
181185

182-
return from_bits.get_val();
186+
return cpp::bit_cast<T>(int_val);
183187
}
184188

185189
} // namespace fputil

libc/src/__support/FPUtil/NormalFloat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ template <> LIBC_INLINE NormalFloat<long double>::operator long double() const {
215215
// Max exponent is of the form 0xFF...E. That is why -2 and not -1.
216216
constexpr int MAX_EXPONENT_VALUE = (1 << LDBits::EXP_LEN) - 2;
217217
if (biased_exponent > MAX_EXPONENT_VALUE) {
218-
return LDBits::inf(sign).get_val();
218+
return LDBits::inf(sign);
219219
}
220220

221221
FPBits<long double> result(0.0l);

libc/src/__support/FPUtil/dyadic_float.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ template <size_t Bits> struct DyadicFloat {
9393
return 0.0;
9494

9595
// Assume that it is normalized, and output is also normal.
96-
constexpr uint32_t PRECISION = FPBits<T>::FRACTION_LEN + 1;
96+
constexpr uint32_t PRECISION = FPBits<T>::MANTISSA_PRECISION;
9797
using output_bits_t = typename FPBits<T>::StorageType;
9898

9999
int exp_hi = exponent + static_cast<int>((Bits - 1) + FPBits<T>::EXP_BIAS);

libc/src/__support/FPUtil/except_value_utils.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,15 @@ template <typename T, size_t N> struct ExceptValues {
102102
// Helper functions to set results for exceptional cases.
103103
template <typename T> LIBC_INLINE T round_result_slightly_down(T value_rn) {
104104
volatile T tmp = value_rn;
105-
tmp -= FPBits<T>::min_normal().get_val();
105+
const T MIN_NORMAL = FPBits<T>::min_normal();
106+
tmp = tmp - MIN_NORMAL;
106107
return tmp;
107108
}
108109

109110
template <typename T> LIBC_INLINE T round_result_slightly_up(T value_rn) {
110111
volatile T tmp = value_rn;
111-
tmp += FPBits<T>::min_normal().get_val();
112+
const T MIN_NORMAL = FPBits<T>::min_normal();
113+
tmp = tmp + MIN_NORMAL;
112114
return tmp;
113115
}
114116

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ template <> LIBC_INLINE double fma<double>(double x, double y, double z) {
130130
return x * y + z;
131131

132132
// Extract mantissa and append hidden leading bits.
133-
UInt128 x_mant = x_bits.get_explicit_mantissa();
134-
UInt128 y_mant = y_bits.get_explicit_mantissa();
135-
UInt128 z_mant = z_bits.get_explicit_mantissa();
133+
UInt128 x_mant = x_bits.get_mantissa() | FPBits::MIN_NORMAL;
134+
UInt128 y_mant = y_bits.get_mantissa() | FPBits::MIN_NORMAL;
135+
UInt128 z_mant = z_bits.get_mantissa() | FPBits::MIN_NORMAL;
136136

137137
// If the exponent of the product x*y > the exponent of z, then no extra
138138
// precision beside the entire product x*y is needed. On the other hand, when
@@ -255,7 +255,9 @@ template <> LIBC_INLINE double fma<double>(double x, double y, double z) {
255255
if ((round_mode == FE_TOWARDZERO) ||
256256
(round_mode == FE_UPWARD && prod_sign.is_neg()) ||
257257
(round_mode == FE_DOWNWARD && prod_sign.is_pos())) {
258-
return FPBits::max_normal(prod_sign).get_val();
258+
result = FPBits::MAX_NORMAL;
259+
return prod_sign.is_neg() ? -cpp::bit_cast<double>(result)
260+
: cpp::bit_cast<double>(result);
259261
}
260262
return static_cast<double>(FPBits::inf(prod_sign));
261263
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ template <typename T> struct FModExceptionalInputHandler {
124124

125125
LIBC_INLINE static bool pre_check(T x, T y, T &out) {
126126
using FPB = fputil::FPBits<T>;
127-
const T quiet_nan = FPB::build_quiet_nan().get_val();
127+
const T quiet_nan = FPB::build_quiet_nan(0);
128128
FPB sx(x), sy(y);
129129
if (LIBC_LIKELY(!sy.is_zero() && !sy.is_inf_or_nan() &&
130130
!sx.is_inf_or_nan())) {

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,15 @@ LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, T> sqrt(T x) {
7171
return x86::sqrt(x);
7272
} else {
7373
// IEEE floating points formats.
74-
using Sign = fputil::Sign;
75-
using FPBits_t = typename fputil::FPBits<T>;
76-
using StorageType = typename FPBits_t::StorageType;
77-
constexpr StorageType ONE = StorageType(1) << FPBits_t::FRACTION_LEN;
78-
constexpr auto FLT_NAN =
79-
FPBits_t::build_quiet_nan(Sign::POS, ONE >> 1).get_val();
74+
using StorageType = typename FPBits<T>::StorageType;
75+
constexpr StorageType ONE = StorageType(1) << FPBits<T>::FRACTION_LEN;
8076

81-
FPBits_t bits(x);
77+
FPBits<T> bits(x);
8278

8379
if (bits.is_inf_or_nan()) {
8480
if (bits.is_neg() && (bits.get_mantissa() == 0)) {
8581
// sqrt(-Inf) = NaN
86-
return FLT_NAN;
82+
return FPBits<T>::build_quiet_nan(ONE >> 1);
8783
} else {
8884
// sqrt(NaN) = NaN
8985
// sqrt(+Inf) = +Inf
@@ -95,7 +91,7 @@ LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, T> sqrt(T x) {
9591
return x;
9692
} else if (bits.is_neg()) {
9793
// sqrt( negative numbers ) = NaN
98-
return FLT_NAN;
94+
return FPBits<T>::build_quiet_nan(ONE >> 1);
9995
} else {
10096
int x_exp = bits.get_exponent();
10197
StorageType x_mant = bits.get_mantissa();
@@ -149,10 +145,10 @@ LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, T> sqrt(T x) {
149145
}
150146

151147
// Remove hidden bit and append the exponent field.
152-
x_exp = ((x_exp >> 1) + FPBits_t::EXP_BIAS);
148+
x_exp = ((x_exp >> 1) + FPBits<T>::EXP_BIAS);
153149

154150
y = (y - ONE) |
155-
(static_cast<StorageType>(x_exp) << FPBits_t::FRACTION_LEN);
151+
(static_cast<StorageType>(x_exp) << FPBits<T>::FRACTION_LEN);
156152

157153
switch (quick_get_round()) {
158154
case FE_TONEAREST:

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,14 @@ LIBC_INLINE long double sqrt(long double x);
3838
LIBC_INLINE long double sqrt(long double x) {
3939
using LDBits = FPBits<long double>;
4040
using StorageType = typename LDBits::StorageType;
41-
using Sign = fputil::Sign;
4241
constexpr StorageType ONE = StorageType(1) << int(LDBits::FRACTION_LEN);
43-
constexpr auto LDNAN = LDBits::build_quiet_nan(Sign::POS, ONE >> 1).get_val();
4442

45-
LDBits bits(x);
43+
FPBits<long double> bits(x);
4644

4745
if (bits.is_inf_or_nan()) {
4846
if (bits.is_neg() && (bits.get_mantissa() == 0)) {
4947
// sqrt(-Inf) = NaN
50-
return LDNAN;
48+
return LDBits::build_quiet_nan(ONE >> 1);
5149
} else {
5250
// sqrt(NaN) = NaN
5351
// sqrt(+Inf) = +Inf
@@ -59,7 +57,7 @@ LIBC_INLINE long double sqrt(long double x) {
5957
return x;
6058
} else if (bits.is_neg()) {
6159
// sqrt( negative numbers ) = NaN
62-
return LDNAN;
60+
return LDBits::build_quiet_nan(ONE >> 1);
6361
} else {
6462
int x_exp = bits.get_explicit_exponent();
6563
StorageType x_mant = bits.get_mantissa();

0 commit comments

Comments
 (0)