Skip to content

Commit 2856db0

Browse files
authored
[libc][NFC] Remove FPBits cast operator (#79142)
The semantics for casting can range from "bitcast" (same representation) to "different representation", to "type promotion". Here we remove the cast operator and force usage of `get_val` as the only function to get the floating point value, making the intent clearer and more consistent.
1 parent e1aa5b1 commit 2856db0

File tree

102 files changed

+366
-369
lines changed

Some content is hidden

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

102 files changed

+366
-369
lines changed

libc/src/__support/FPUtil/BasicOperations.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
2121
LIBC_INLINE T abs(T x) {
2222
FPBits<T> bits(x);
2323
bits.set_sign(Sign::POS);
24-
return T(bits);
24+
return bits.get_val();
2525
}
2626

2727
template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>

libc/src/__support/FPUtil/DivisionAndRemainderOperations.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ LIBC_INLINE T remquo(T x, T y, int &q) {
8686
// then the conversion to native remainder value should be updated
8787
// appropriately and some directed tests added.
8888
T native_remainder(remainder);
89-
T absy = T(ybits);
89+
T absy = ybits.get_val();
9090
int cmp = remainder.mul2(1).cmp(normaly);
9191
if (cmp > 0) {
9292
q = q + 1;

libc/src/__support/FPUtil/FPBits.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,8 +727,6 @@ struct FPBits final : public internal::FPRep<get_fp_type<T>(), FPBits<T>> {
727727
// Floating-point conversions.
728728
LIBC_INLINE constexpr T get_val() const { return cpp::bit_cast<T>(bits); }
729729

730-
LIBC_INLINE constexpr explicit operator T() const { return get_val(); }
731-
732730
// TODO: Use an uint32_t for 'biased_exp'.
733731
LIBC_INLINE static constexpr FPBits<T>
734732
create_value(Sign sign, StorageType biased_exp, StorageType mantissa) {

libc/src/__support/FPUtil/Hypot.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ LIBC_INLINE T hypot(T x, T y) {
111111
FPBits_t x_bits(x), y_bits(y);
112112

113113
if (x_bits.is_inf() || y_bits.is_inf()) {
114-
return T(FPBits_t::inf());
114+
return FPBits_t::inf().get_val();
115115
}
116116
if (x_bits.is_nan()) {
117117
return x;
@@ -196,8 +196,8 @@ LIBC_INLINE T hypot(T x, T y) {
196196
if (out_exp >= FPBits_t::MAX_BIASED_EXPONENT) {
197197
if (int round_mode = quick_get_round();
198198
round_mode == FE_TONEAREST || round_mode == FE_UPWARD)
199-
return T(FPBits_t::inf());
200-
return T(FPBits_t::max_normal());
199+
return FPBits_t::inf().get_val();
200+
return FPBits_t::max_normal().get_val();
201201
}
202202
} else {
203203
// For denormal result, we simply move the leading bit of the result to
@@ -253,8 +253,8 @@ LIBC_INLINE T hypot(T x, T y) {
253253
++out_exp;
254254
if (out_exp >= FPBits_t::MAX_BIASED_EXPONENT) {
255255
if (round_mode == FE_TONEAREST || round_mode == FE_UPWARD)
256-
return T(FPBits_t::inf());
257-
return T(FPBits_t::max_normal());
256+
return FPBits_t::inf().get_val();
257+
return FPBits_t::max_normal().get_val();
258258
}
259259
}
260260

libc/src/__support/FPUtil/ManipulationFunctions.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ LIBC_INLINE T modf(T x, T &iptr) {
4949
return x;
5050
} else if (bits.is_inf()) {
5151
iptr = x;
52-
return T(FPBits<T>::zero(bits.sign()));
52+
return FPBits<T>::zero(bits.sign()).get_val();
5353
} else {
5454
iptr = trunc(x);
5555
if (x == iptr) {
5656
// If x is already an integer value, then return zero with the right
5757
// sign.
58-
return T(FPBits<T>::zero(bits.sign()));
58+
return FPBits<T>::zero(bits.sign()).get_val();
5959
} else {
6060
return x - iptr;
6161
}
@@ -66,7 +66,7 @@ template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
6666
LIBC_INLINE T copysign(T x, T y) {
6767
FPBits<T> xbits(x);
6868
xbits.set_sign(FPBits<T>(y).sign());
69-
return T(xbits);
69+
return xbits.get_val();
7070
}
7171

7272
template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
@@ -103,12 +103,12 @@ LIBC_INLINE T logb(T x) {
103103
if (bits.is_zero()) {
104104
// TODO(Floating point exception): Raise div-by-zero exception.
105105
// TODO(errno): POSIX requires setting errno to ERANGE.
106-
return T(FPBits<T>::inf(Sign::NEG));
106+
return FPBits<T>::inf(Sign::NEG).get_val();
107107
} else if (bits.is_nan()) {
108108
return x;
109109
} else if (bits.is_inf()) {
110110
// Return positive infinity.
111-
return T(FPBits<T>::inf());
111+
return FPBits<T>::inf().get_val();
112112
}
113113

114114
NormalFloat<T> normal(bits);
@@ -131,11 +131,11 @@ LIBC_INLINE T ldexp(T x, int exp) {
131131
// calculating the limit.
132132
int exp_limit = FPBits<T>::MAX_BIASED_EXPONENT + FPBits<T>::FRACTION_LEN + 1;
133133
if (exp > exp_limit)
134-
return T(FPBits<T>::inf(bits.sign()));
134+
return FPBits<T>::inf(bits.sign()).get_val();
135135

136136
// Similarly on the negative side we return zero early if |exp| is too small.
137137
if (exp < -exp_limit)
138-
return T(FPBits<T>::zero(bits.sign()));
138+
return FPBits<T>::zero(bits.sign()).get_val();
139139

140140
// For all other values, NormalFloat to T conversion handles it the right way.
141141
NormalFloat<T> normal(bits);

libc/src/__support/FPUtil/NearestIntegerOperations.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ LIBC_INLINE T trunc(T x) {
4141

4242
// If the exponent is such that abs(x) is less than 1, then return 0.
4343
if (exponent <= -1)
44-
return T(FPBits<T>::zero(bits.sign()));
44+
return FPBits<T>::zero(bits.sign()).get_val();
4545

4646
int trim_size = FPBits<T>::FRACTION_LEN - exponent;
4747
bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
48-
return T(bits);
48+
return bits.get_val();
4949
}
5050

5151
template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
@@ -73,7 +73,7 @@ LIBC_INLINE T ceil(T x) {
7373

7474
uint32_t trim_size = FPBits<T>::FRACTION_LEN - exponent;
7575
bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
76-
T trunc_value = T(bits);
76+
T trunc_value = bits.get_val();
7777

7878
// If x is already an integer, return it.
7979
if (trunc_value == x)
@@ -114,19 +114,19 @@ LIBC_INLINE T round(T x) {
114114

115115
if (exponent == -1) {
116116
// Absolute value of x is greater than equal to 0.5 but less than 1.
117-
return T(FPBits<T>::one(bits.sign()));
117+
return FPBits<T>::one(bits.sign()).get_val();
118118
}
119119

120120
if (exponent <= -2) {
121121
// Absolute value of x is less than 0.5.
122-
return T(FPBits<T>::zero(bits.sign()));
122+
return FPBits<T>::zero(bits.sign()).get_val();
123123
}
124124

125125
uint32_t trim_size = FPBits<T>::FRACTION_LEN - exponent;
126126
bool half_bit_set =
127127
bool(bits.get_mantissa() & (StorageType(1) << (trim_size - 1)));
128128
bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
129-
T trunc_value = T(bits);
129+
T trunc_value = bits.get_val();
130130

131131
// If x is already an integer, return it.
132132
if (trunc_value == x)
@@ -180,7 +180,7 @@ LIBC_INLINE T round_using_current_rounding_mode(T x) {
180180
uint32_t trim_size = FPBits<T>::FRACTION_LEN - exponent;
181181
FPBits<T> new_bits = bits;
182182
new_bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
183-
T trunc_value = T(new_bits);
183+
T trunc_value = new_bits.get_val();
184184

185185
// If x is already an integer, return it.
186186
if (trunc_value == x)

libc/src/__support/FPUtil/NormalFloat.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ template <typename T> struct NormalFloat {
9696
// Max exponent is of the form 0xFF...E. That is why -2 and not -1.
9797
constexpr int MAX_EXPONENT_VALUE = (1 << FPBits<T>::EXP_LEN) - 2;
9898
if (biased_exponent > MAX_EXPONENT_VALUE) {
99-
return T(FPBits<T>::inf(sign));
99+
return FPBits<T>::inf(sign).get_val();
100100
}
101101

102102
FPBits<T> result(T(0.0));
@@ -129,15 +129,15 @@ template <typename T> struct NormalFloat {
129129
// the overflow into the exponent.
130130
if (new_mantissa == ONE)
131131
result.set_biased_exponent(1);
132-
return T(result);
132+
return result.get_val();
133133
} else {
134-
return T(result);
134+
return result.get_val();
135135
}
136136
}
137137

138138
result.set_biased_exponent(exponent + FPBits<T>::EXP_BIAS);
139139
result.set_mantissa(mantissa);
140-
return T(result);
140+
return result.get_val();
141141
}
142142

143143
private:
@@ -250,16 +250,16 @@ template <> LIBC_INLINE NormalFloat<long double>::operator long double() const {
250250
} else {
251251
result.set_implicit_bit(0);
252252
}
253-
return static_cast<long double>(result);
253+
return result.get_val();
254254
} else {
255-
return static_cast<long double>(result);
255+
return result.get_val();
256256
}
257257
}
258258

259259
result.set_biased_exponent(biased_exponent);
260260
result.set_mantissa(mantissa);
261261
result.set_implicit_bit(1);
262-
return static_cast<long double>(result);
262+
return result.get_val();
263263
}
264264
#endif // LIBC_LONG_DOUBLE_IS_X86_FLOAT80
265265

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ template <> LIBC_INLINE float fma<float>(float x, float y, float z) {
5858
// correct (when it matters).
5959
fputil::FPBits<double> t(
6060
(bit_prod.get_biased_exponent() >= bitz.get_biased_exponent())
61-
? ((double(bit_sum) - double(bit_prod)) - double(bitz))
62-
: ((double(bit_sum) - double(bitz)) - double(bit_prod)));
61+
? ((bit_sum.get_val() - bit_prod.get_val()) - bitz.get_val())
62+
: ((bit_sum.get_val() - bitz.get_val()) - bit_prod.get_val()));
6363

6464
// Update sticky bits if t != 0.0 and the least (52 - 23 - 1 = 28) bits are
6565
// zero.
@@ -72,7 +72,7 @@ template <> LIBC_INLINE float fma<float>(float x, float y, float z) {
7272
}
7373
}
7474

75-
return static_cast<float>(static_cast<double>(bit_sum));
75+
return static_cast<float>(bit_sum.get_val());
7676
}
7777

7878
namespace internal {
@@ -257,7 +257,7 @@ template <> LIBC_INLINE double fma<double>(double x, double y, double z) {
257257
(round_mode == FE_DOWNWARD && prod_sign.is_pos())) {
258258
return FPBits::max_normal(prod_sign).get_val();
259259
}
260-
return static_cast<double>(FPBits::inf(prod_sign));
260+
return FPBits::inf(prod_sign).get_val();
261261
}
262262

263263
// Remove hidden bit and append the exponent field and sign bit.

libc/src/__support/str_to_float.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -568,11 +568,11 @@ clinger_fast_path(ExpandedFloat<T> init_num,
568568
ClingerConsts<T>::POWERS_OF_TEN_ARRAY[exp10]);
569569

570570
// If the results are equal, then we don't need to use the rounding mode.
571-
if (T(result) != -T(negative_result)) {
571+
if (result.get_val() != -negative_result.get_val()) {
572572
FPBits lower_result;
573573
FPBits higher_result;
574574

575-
if (T(result) < -T(negative_result)) {
575+
if (result.get_val() < -negative_result.get_val()) {
576576
lower_result = result;
577577
higher_result = negative_result;
578578
} else {
@@ -1194,7 +1194,7 @@ LIBC_INLINE StrToNumResult<T> strtofloatingpoint(const char *__restrict src) {
11941194
// special 80 bit long doubles. Otherwise it should be inlined out.
11951195
set_implicit_bit<T>(result);
11961196

1197-
return {T(result), index, error};
1197+
return {result.get_val(), index, error};
11981198
}
11991199

12001200
template <class T> LIBC_INLINE StrToNumResult<T> strtonan(const char *arg) {
@@ -1216,7 +1216,7 @@ template <class T> LIBC_INLINE StrToNumResult<T> strtonan(const char *arg) {
12161216
}
12171217

12181218
result = FPBits::build_quiet_nan(fputil::Sign::POS, nan_mantissa);
1219-
return {T(result), 0, error};
1219+
return {result.get_val(), 0, error};
12201220
}
12211221

12221222
} // namespace internal

libc/src/math/generic/exp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ double set_exceptional(double x) {
222222
fputil::raise_except_if_required(FE_OVERFLOW);
223223
}
224224
// x is +inf or nan
225-
return x + static_cast<double>(FPBits::inf());
225+
return x + FPBits::inf().get_val();
226226
}
227227

228228
} // namespace

libc/src/math/generic/exp10.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ double set_exceptional(double x) {
268268
fputil::raise_except_if_required(FE_OVERFLOW);
269269
}
270270
// x is +inf or nan
271-
return x + static_cast<double>(FPBits::inf());
271+
return x + FPBits::inf().get_val();
272272
}
273273

274274
} // namespace

libc/src/math/generic/exp2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ double set_exceptional(double x) {
243243
fputil::raise_except_if_required(FE_OVERFLOW);
244244
}
245245
// x is +inf or nan
246-
return x + static_cast<double>(FPBits::inf());
246+
return x + FPBits::inf().get_val();
247247
}
248248

249249
} // namespace

libc/src/math/generic/expf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ LLVM_LIBC_FUNCTION(float, expf, (float x)) {
6767
fputil::raise_except_if_required(FE_OVERFLOW);
6868
}
6969
// x is +inf or nan
70-
return x + static_cast<float>(FPBits::inf());
70+
return x + FPBits::inf().get_val();
7171
}
7272
}
7373
// For -104 < x < 89, to compute exp(x), we perform the following range

libc/src/math/generic/hypotf.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ LLVM_LIBC_FUNCTION(float, hypotf, (float x, float y)) {
4646

4747
if (!DoubleBits(sum_sq).is_inf_or_nan()) {
4848
// Correct rounding.
49-
double r_sq = static_cast<double>(result) * static_cast<double>(result);
49+
double r_sq = result.get_val() * result.get_val();
5050
double diff = sum_sq - r_sq;
5151
constexpr uint64_t mask = 0x0000'0000'3FFF'FFFFULL;
5252
uint64_t lrs = result.uintval() & mask;
@@ -60,14 +60,14 @@ LLVM_LIBC_FUNCTION(float, hypotf, (float x, float y)) {
6060
FPBits bits_x(x), bits_y(y);
6161
if (bits_x.is_inf_or_nan() || bits_y.is_inf_or_nan()) {
6262
if (bits_x.is_inf() || bits_y.is_inf())
63-
return static_cast<float>(FPBits::inf());
63+
return FPBits::inf().get_val();
6464
if (bits_x.is_nan())
6565
return x;
6666
return y;
6767
}
6868
}
6969

70-
return static_cast<float>(static_cast<double>(result));
70+
return static_cast<float>(result.get_val());
7171
}
7272

7373
} // namespace LIBC_NAMESPACE

libc/src/math/generic/log10f.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ LLVM_LIBC_FUNCTION(float, log10f, (float x)) {
191191
// Set bits to 1.m
192192
xbits.set_biased_exponent(0x7F);
193193

194-
float u = static_cast<float>(xbits);
194+
float u = xbits.get_val();
195195
double v;
196196
#ifdef LIBC_TARGET_CPU_HAS_FMA
197197
v = static_cast<double>(fputil::multiply_add(u, R[index], -1.0f)); // Exact.

libc/src/math/generic/log1pf.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ LIBC_INLINE float log(double x) {
6666
// Clear the lowest 45 bits.
6767
f.bits &= ~0x0000'1FFF'FFFF'FFFFULL;
6868

69-
double d = static_cast<double>(xbits) - static_cast<double>(f);
69+
double d = xbits.get_val() - f.get_val();
7070
d *= ONE_OVER_F[f_index];
7171

7272
double extra_factor = fputil::multiply_add(m, LOG_2, LOG_F[f_index]);
@@ -106,7 +106,7 @@ LLVM_LIBC_FUNCTION(float, log1pf, (float x)) {
106106
case 0xbf800000U: // x = -1.0
107107
fputil::set_errno_if_required(ERANGE);
108108
fputil::raise_except_if_required(FE_DIVBYZERO);
109-
return static_cast<float>(fputil::FPBits<float>::inf(fputil::Sign::NEG));
109+
return FPBits::inf(fputil::Sign::NEG).get_val();
110110
#ifndef LIBC_TARGET_CPU_HAS_FMA
111111
case 0x4cc1c80bU: // x = 0x1.839016p+26f
112112
return fputil::round_result_slightly_down(0x1.26fc04p+4f);

libc/src/math/generic/log2f.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ LLVM_LIBC_FUNCTION(float, log2f, (float x)) {
9494
// Set bits to 1.m
9595
xbits.set_biased_exponent(0x7F);
9696

97-
float u = static_cast<float>(xbits);
97+
float u = xbits.get_val();
9898
double v;
9999
#ifdef LIBC_TARGET_CPU_HAS_FMA
100100
v = static_cast<double>(fputil::multiply_add(u, R[index], -1.0f)); // Exact.

libc/src/math/generic/logf.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ LLVM_LIBC_FUNCTION(float, logf, (float x)) {
8585
// Return -inf and raise FE_DIVBYZERO
8686
fputil::set_errno_if_required(ERANGE);
8787
fputil::raise_except_if_required(FE_DIVBYZERO);
88-
return static_cast<float>(FPBits::inf(fputil::Sign::NEG));
88+
return FPBits::inf(Sign::NEG).get_val();
8989
}
9090
// Normalize denormal inputs.
9191
xbits = FPBits(xbits.get_val() * 0x1.0p23f);
@@ -149,7 +149,7 @@ LLVM_LIBC_FUNCTION(float, logf, (float x)) {
149149
// Set bits to 1.m
150150
xbits.set_biased_exponent(0x7F);
151151

152-
float u = static_cast<float>(xbits);
152+
float u = xbits.get_val();
153153
double v;
154154
#ifdef LIBC_TARGET_CPU_HAS_FMA
155155
v = static_cast<double>(fputil::multiply_add(u, R[index], -1.0f)); // Exact.

0 commit comments

Comments
 (0)