Skip to content

Commit 6b02d2f

Browse files
authored
[reland][libc] Remove unnecessary FPBits functions and properties (#79128)
- reland #79113 - Fix aarch64 RISC-V build
1 parent e60d780 commit 6b02d2f

Some content is hidden

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

69 files changed

+428
-423
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>::MANTISSA_PRECISION;
31+
const int full_precision = FPBits<F>::FRACTION_LEN + 1;
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(1);
34+
return FPBits<T>::build_quiet_nan(fputil::Sign::POS, 1).get_val();
3535

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

libc/src/__support/FPUtil/FPBits.h

Lines changed: 9 additions & 42 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() && !UP::is_subnormal();
393+
return is_finite() && !is_subnormal();
394394
}
395395
// Returns the mantissa with the implicit bit set iff the current
396396
// value is a valid normal number.
@@ -556,6 +556,14 @@ 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+
559567
// Representation
560568
LIBC_INLINE constexpr StorageType uintval() const { return bits & FP_MASK; }
561569
LIBC_INLINE constexpr void set_uintval(StorageType value) {
@@ -698,16 +706,6 @@ struct FPBits final : public internal::FPRep<get_fp_type<T>(), FPBits<T>> {
698706
using UP::bits;
699707

700708
// 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();
711709
LIBC_INLINE_VAR static constexpr int MAX_BIASED_EXPONENT =
712710
(1 << UP::EXP_LEN) - 1;
713711

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

732730
LIBC_INLINE constexpr explicit operator T() const { return get_val(); }
733731

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-
765732
// TODO: Use an uint32_t for 'biased_exp'.
766733
LIBC_INLINE static constexpr FPBits<T>
767734
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(FPBits_t::MAX_NORMAL));
200+
return 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(FPBits_t::MAX_NORMAL));
257+
return T(FPBits_t::max_normal());
258258
}
259259
}
260260

libc/src/__support/FPUtil/ManipulationFunctions.h

Lines changed: 10 additions & 14 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(Sign::POS));
111+
return T(FPBits<T>::inf());
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 (mantissaWidht + 1) worth of shift in
130+
// we need to accommodate the (mantissaWidth + 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,26 +164,22 @@ LIBC_INLINE T nextafter(T from, U to) {
164164
return static_cast<T>(to);
165165

166166
using StorageType = typename FPBits<T>::StorageType;
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;
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));
171170
} else {
172-
--int_val;
171+
from_bits = FPBits<T>(StorageType(from_bits.uintval() - 1));
173172
}
174173
} else {
175-
int_val = FPBits<T>::MIN_SUBNORMAL;
176-
if (to_bits.is_neg())
177-
int_val |= FPBits<T>::SIGN_MASK;
174+
from_bits = FPBits<T>::min_subnormal(to_bits.sign());
178175
}
179176

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

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

189185
} // 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);
218+
return LDBits::inf(sign).get_val();
219219
}
220220

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

libc/src/__support/FPUtil/aarch64/FEnvImpl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ LIBC_INLINE int set_except(int excepts) {
155155
LIBC_INLINE int raise_except(int excepts) {
156156
float zero = 0.0f;
157157
float one = 1.0f;
158-
float largeValue = FPBits<float>::max_normal();
159-
float smallValue = FPBits<float>::min_normal();
158+
float largeValue = FPBits<float>::max_normal().get_val();
159+
float smallValue = FPBits<float>::min_normal().get_val();
160160
auto divfunc = [](float a, float b) {
161161
__asm__ __volatile__("ldr s0, %0\n\t"
162162
"ldr s1, %1\n\t"

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>::MANTISSA_PRECISION;
96+
constexpr uint32_t PRECISION = FPBits<T>::FRACTION_LEN + 1;
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: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,13 @@ 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-
const T MIN_NORMAL = FPBits<T>::min_normal();
106-
tmp = tmp - MIN_NORMAL;
105+
tmp -= FPBits<T>::min_normal().get_val();
107106
return tmp;
108107
}
109108

110109
template <typename T> LIBC_INLINE T round_result_slightly_up(T value_rn) {
111110
volatile T tmp = value_rn;
112-
const T MIN_NORMAL = FPBits<T>::min_normal();
113-
tmp = tmp + MIN_NORMAL;
111+
tmp += FPBits<T>::min_normal().get_val();
114112
return tmp;
115113
}
116114

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

Lines changed: 4 additions & 6 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_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;
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();
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,9 +255,7 @@ 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-
result = FPBits::MAX_NORMAL;
259-
return prod_sign.is_neg() ? -cpp::bit_cast<double>(result)
260-
: cpp::bit_cast<double>(result);
258+
return FPBits::max_normal(prod_sign).get_val();
261259
}
262260
return static_cast<double>(FPBits::inf(prod_sign));
263261
}

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(0);
127+
const T quiet_nan = FPB::build_quiet_nan().get_val();
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: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,19 @@ 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 StorageType = typename FPBits<T>::StorageType;
75-
constexpr StorageType ONE = StorageType(1) << FPBits<T>::FRACTION_LEN;
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();
7680

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

7983
if (bits.is_inf_or_nan()) {
8084
if (bits.is_neg() && (bits.get_mantissa() == 0)) {
8185
// sqrt(-Inf) = NaN
82-
return FPBits<T>::build_quiet_nan(ONE >> 1);
86+
return FLT_NAN;
8387
} else {
8488
// sqrt(NaN) = NaN
8589
// sqrt(+Inf) = +Inf
@@ -91,7 +95,7 @@ LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, T> sqrt(T x) {
9195
return x;
9296
} else if (bits.is_neg()) {
9397
// sqrt( negative numbers ) = NaN
94-
return FPBits<T>::build_quiet_nan(ONE >> 1);
98+
return FLT_NAN;
9599
} else {
96100
int x_exp = bits.get_exponent();
97101
StorageType x_mant = bits.get_mantissa();
@@ -145,10 +149,10 @@ LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, T> sqrt(T x) {
145149
}
146150

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

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

153157
switch (quick_get_round()) {
154158
case FE_TONEAREST:

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,16 @@ 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;
4142
constexpr StorageType ONE = StorageType(1) << int(LDBits::FRACTION_LEN);
43+
constexpr auto LDNAN = LDBits::build_quiet_nan(Sign::POS, ONE >> 1).get_val();
4244

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

4547
if (bits.is_inf_or_nan()) {
4648
if (bits.is_neg() && (bits.get_mantissa() == 0)) {
4749
// sqrt(-Inf) = NaN
48-
return LDBits::build_quiet_nan(ONE >> 1);
50+
return LDNAN;
4951
} else {
5052
// sqrt(NaN) = NaN
5153
// sqrt(+Inf) = +Inf
@@ -57,7 +59,7 @@ LIBC_INLINE long double sqrt(long double x) {
5759
return x;
5860
} else if (bits.is_neg()) {
5961
// sqrt( negative numbers ) = NaN
60-
return LDBits::build_quiet_nan(ONE >> 1);
62+
return LDNAN;
6163
} else {
6264
int x_exp = bits.get_explicit_exponent();
6365
StorageType x_mant = bits.get_mantissa();

0 commit comments

Comments
 (0)