Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libc][NFC] Remove FPBits cast operator #79142

Merged
merged 3 commits into from
Jan 23, 2024

Conversation

gchatelet
Copy link
Contributor

@gchatelet gchatelet commented Jan 23, 2024

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.

@llvmbot
Copy link
Collaborator

llvmbot commented Jan 23, 2024

@llvm/pr-subscribers-libc

Author: Guillaume Chatelet (gchatelet)

Changes

Patch is 116.18 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/79142.diff

102 Files Affected:

  • (modified) libc/src/__support/FPUtil/BasicOperations.h (+1-1)
  • (modified) libc/src/__support/FPUtil/DivisionAndRemainderOperations.h (+1-1)
  • (modified) libc/src/__support/FPUtil/FPBits.h (-2)
  • (modified) libc/src/__support/FPUtil/Hypot.h (+5-5)
  • (modified) libc/src/__support/FPUtil/ManipulationFunctions.h (+7-7)
  • (modified) libc/src/__support/FPUtil/NearestIntegerOperations.h (+7-7)
  • (modified) libc/src/__support/FPUtil/NormalFloat.h (+7-7)
  • (modified) libc/src/__support/FPUtil/generic/FMA.h (+4-4)
  • (modified) libc/src/__support/str_to_float.h (+4-4)
  • (modified) libc/src/math/generic/exp.cpp (+1-1)
  • (modified) libc/src/math/generic/exp10.cpp (+1-1)
  • (modified) libc/src/math/generic/exp2.cpp (+1-1)
  • (modified) libc/src/math/generic/expf.cpp (+1-1)
  • (modified) libc/src/math/generic/hypotf.cpp (+3-3)
  • (modified) libc/src/math/generic/log10f.cpp (+1-1)
  • (modified) libc/src/math/generic/log1pf.cpp (+2-2)
  • (modified) libc/src/math/generic/log2f.cpp (+1-1)
  • (modified) libc/src/math/generic/logf.cpp (+2-2)
  • (modified) libc/src/math/generic/range_reduction_fma.h (+2-2)
  • (modified) libc/src/stdio/printf_core/float_dec_converter.h (+3-3)
  • (modified) libc/test/UnitTest/FPMatcher.h (+20-20)
  • (modified) libc/test/src/math/CeilTest.h (+1-1)
  • (modified) libc/test/src/math/CopySignTest.h (+1-1)
  • (modified) libc/test/src/math/FAbsTest.h (+1-1)
  • (modified) libc/test/src/math/FDimTest.h (+6-6)
  • (modified) libc/test/src/math/FMaxTest.h (+1-1)
  • (modified) libc/test/src/math/FMinTest.h (+1-1)
  • (modified) libc/test/src/math/FloorTest.h (+1-1)
  • (modified) libc/test/src/math/FmaTest.h (+14-14)
  • (modified) libc/test/src/math/FrexpTest.h (+1-1)
  • (modified) libc/test/src/math/HypotTest.h (+11-11)
  • (modified) libc/test/src/math/ILogbTest.h (+8-7)
  • (modified) libc/test/src/math/LdExpTest.h (+5-5)
  • (modified) libc/test/src/math/LogbTest.h (+1-1)
  • (modified) libc/test/src/math/ModfTest.h (+1-1)
  • (modified) libc/test/src/math/NextAfterTest.h (+5-5)
  • (modified) libc/test/src/math/RIntTest.h (+7-7)
  • (modified) libc/test/src/math/RemQuoTest.h (+7-7)
  • (modified) libc/test/src/math/RoundTest.h (+1-1)
  • (modified) libc/test/src/math/RoundToIntegerTest.h (+9-9)
  • (modified) libc/test/src/math/SqrtTest.h (+1-1)
  • (modified) libc/test/src/math/TruncTest.h (+1-1)
  • (modified) libc/test/src/math/acosf_test.cpp (+2-2)
  • (modified) libc/test/src/math/acoshf_test.cpp (+2-2)
  • (modified) libc/test/src/math/asinf_test.cpp (+2-2)
  • (modified) libc/test/src/math/asinhf_test.cpp (+2-2)
  • (modified) libc/test/src/math/atanf_test.cpp (+2-2)
  • (modified) libc/test/src/math/atanhf_test.cpp (+3-3)
  • (modified) libc/test/src/math/cos_test.cpp (+1-1)
  • (modified) libc/test/src/math/cosf_test.cpp (+3-3)
  • (modified) libc/test/src/math/coshf_test.cpp (+6-6)
  • (modified) libc/test/src/math/erff_test.cpp (+1-1)
  • (modified) libc/test/src/math/exhaustive/exhaustive_test.h (+3-4)
  • (modified) libc/test/src/math/exhaustive/hypotf_test.cpp (+2-2)
  • (modified) libc/test/src/math/exhaustive/sincosf_test.cpp (+1-1)
  • (modified) libc/test/src/math/exp10_test.cpp (+1-1)
  • (modified) libc/test/src/math/exp10f_test.cpp (+9-8)
  • (modified) libc/test/src/math/exp2_test.cpp (+1-1)
  • (modified) libc/test/src/math/exp2f_test.cpp (+9-9)
  • (modified) libc/test/src/math/exp_test.cpp (+1-1)
  • (modified) libc/test/src/math/expf_test.cpp (+12-12)
  • (modified) libc/test/src/math/expm1_test.cpp (+1-1)
  • (modified) libc/test/src/math/expm1f_test.cpp (+15-15)
  • (modified) libc/test/src/math/log10_test.cpp (+1-1)
  • (modified) libc/test/src/math/log10f_test.cpp (+2-2)
  • (modified) libc/test/src/math/log1p_test.cpp (+1-1)
  • (modified) libc/test/src/math/log1pf_test.cpp (+2-2)
  • (modified) libc/test/src/math/log2_test.cpp (+1-1)
  • (modified) libc/test/src/math/log2f_test.cpp (+2-2)
  • (modified) libc/test/src/math/log_test.cpp (+1-1)
  • (modified) libc/test/src/math/logf_test.cpp (+2-2)
  • (modified) libc/test/src/math/sin_test.cpp (+1-1)
  • (modified) libc/test/src/math/sincosf_test.cpp (+3-3)
  • (modified) libc/test/src/math/sinf_test.cpp (+5-5)
  • (modified) libc/test/src/math/sinhf_test.cpp (+8-8)
  • (modified) libc/test/src/math/smoke/FDimTest.h (+6-6)
  • (modified) libc/test/src/math/smoke/FMaxTest.h (+1-1)
  • (modified) libc/test/src/math/smoke/FMinTest.h (+1-1)
  • (modified) libc/test/src/math/smoke/FmaTest.h (+12-12)
  • (modified) libc/test/src/math/smoke/HypotTest.h (+10-10)
  • (modified) libc/test/src/math/smoke/ILogbTest.h (+8-7)
  • (modified) libc/test/src/math/smoke/LdExpTest.h (+5-5)
  • (modified) libc/test/src/math/smoke/LogbTest.h (+1-1)
  • (modified) libc/test/src/math/smoke/ModfTest.h (+1-1)
  • (modified) libc/test/src/math/smoke/NextAfterTest.h (+5-5)
  • (modified) libc/test/src/math/smoke/NextTowardTest.h (+5-5)
  • (modified) libc/test/src/math/smoke/RIntTest.h (+5-5)
  • (modified) libc/test/src/math/smoke/RemQuoTest.h (+5-5)
  • (modified) libc/test/src/math/smoke/RoundToIntegerTest.h (+6-6)
  • (modified) libc/test/src/math/smoke/coshf_test.cpp (+3-3)
  • (modified) libc/test/src/math/smoke/exp10f_test.cpp (+3-3)
  • (modified) libc/test/src/math/smoke/exp2f_test.cpp (+3-3)
  • (modified) libc/test/src/math/smoke/expf_test.cpp (+3-3)
  • (modified) libc/test/src/math/smoke/expm1f_test.cpp (+3-3)
  • (modified) libc/test/src/math/smoke/sinhf_test.cpp (+5-5)
  • (modified) libc/test/src/math/tan_test.cpp (+1-1)
  • (modified) libc/test/src/math/tanf_test.cpp (+3-3)
  • (modified) libc/test/src/math/tanhf_test.cpp (+2-2)
  • (modified) libc/test/src/stdlib/atof_test.cpp (+1-1)
  • (modified) libc/test/src/stdlib/strtod_test.cpp (+2-3)
  • (modified) libc/test/src/stdlib/strtof_test.cpp (+1-1)
  • (modified) libc/utils/MPFRWrapper/MPFRUtils.cpp (+1-1)
diff --git a/libc/src/__support/FPUtil/BasicOperations.h b/libc/src/__support/FPUtil/BasicOperations.h
index ea78809dfc7f7d..ccc61a89c5f831 100644
--- a/libc/src/__support/FPUtil/BasicOperations.h
+++ b/libc/src/__support/FPUtil/BasicOperations.h
@@ -21,7 +21,7 @@ template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
 LIBC_INLINE T abs(T x) {
   FPBits<T> bits(x);
   bits.set_sign(Sign::POS);
-  return T(bits);
+  return bits.get_val();
 }
 
 template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
diff --git a/libc/src/__support/FPUtil/DivisionAndRemainderOperations.h b/libc/src/__support/FPUtil/DivisionAndRemainderOperations.h
index ef9593a42b0055..17ebcf1beb1fc7 100644
--- a/libc/src/__support/FPUtil/DivisionAndRemainderOperations.h
+++ b/libc/src/__support/FPUtil/DivisionAndRemainderOperations.h
@@ -86,7 +86,7 @@ LIBC_INLINE T remquo(T x, T y, int &q) {
   // then the conversion to native remainder value should be updated
   // appropriately and some directed tests added.
   T native_remainder(remainder);
-  T absy = T(ybits);
+  T absy = ybits.get_val();
   int cmp = remainder.mul2(1).cmp(normaly);
   if (cmp > 0) {
     q = q + 1;
diff --git a/libc/src/__support/FPUtil/FPBits.h b/libc/src/__support/FPUtil/FPBits.h
index 0a79b505ecbe1c..e43c6168f1f47b 100644
--- a/libc/src/__support/FPUtil/FPBits.h
+++ b/libc/src/__support/FPUtil/FPBits.h
@@ -727,8 +727,6 @@ struct FPBits final : public internal::FPRep<get_fp_type<T>(), FPBits<T>> {
   // Floating-point conversions.
   LIBC_INLINE constexpr T get_val() const { return cpp::bit_cast<T>(bits); }
 
-  LIBC_INLINE constexpr explicit operator T() const { return get_val(); }
-
   // TODO: Use an uint32_t for 'biased_exp'.
   LIBC_INLINE static constexpr FPBits<T>
   create_value(Sign sign, StorageType biased_exp, StorageType mantissa) {
diff --git a/libc/src/__support/FPUtil/Hypot.h b/libc/src/__support/FPUtil/Hypot.h
index 82237dec09e42e..2e699657346446 100644
--- a/libc/src/__support/FPUtil/Hypot.h
+++ b/libc/src/__support/FPUtil/Hypot.h
@@ -111,7 +111,7 @@ LIBC_INLINE T hypot(T x, T y) {
   FPBits_t x_bits(x), y_bits(y);
 
   if (x_bits.is_inf() || y_bits.is_inf()) {
-    return T(FPBits_t::inf());
+    return FPBits_t::inf().get_val();
   }
   if (x_bits.is_nan()) {
     return x;
@@ -196,8 +196,8 @@ LIBC_INLINE T hypot(T x, T y) {
       if (out_exp >= FPBits_t::MAX_BIASED_EXPONENT) {
         if (int round_mode = quick_get_round();
             round_mode == FE_TONEAREST || round_mode == FE_UPWARD)
-          return T(FPBits_t::inf());
-        return T(FPBits_t::max_normal());
+          return FPBits_t::inf().get_val();
+        return FPBits_t::max_normal().get_val();
       }
     } else {
       // 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) {
     ++out_exp;
     if (out_exp >= FPBits_t::MAX_BIASED_EXPONENT) {
       if (round_mode == FE_TONEAREST || round_mode == FE_UPWARD)
-        return T(FPBits_t::inf());
-      return T(FPBits_t::max_normal());
+        return FPBits_t::inf().get_val();
+      return FPBits_t::max_normal().get_val();
     }
   }
 
diff --git a/libc/src/__support/FPUtil/ManipulationFunctions.h b/libc/src/__support/FPUtil/ManipulationFunctions.h
index d7114625a9b314..56e53e3d4f497c 100644
--- a/libc/src/__support/FPUtil/ManipulationFunctions.h
+++ b/libc/src/__support/FPUtil/ManipulationFunctions.h
@@ -49,13 +49,13 @@ LIBC_INLINE T modf(T x, T &iptr) {
     return x;
   } else if (bits.is_inf()) {
     iptr = x;
-    return T(FPBits<T>::zero(bits.sign()));
+    return FPBits<T>::zero(bits.sign()).get_val();
   } else {
     iptr = trunc(x);
     if (x == iptr) {
       // If x is already an integer value, then return zero with the right
       // sign.
-      return T(FPBits<T>::zero(bits.sign()));
+      return FPBits<T>::zero(bits.sign()).get_val();
     } else {
       return x - iptr;
     }
@@ -66,7 +66,7 @@ template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
 LIBC_INLINE T copysign(T x, T y) {
   FPBits<T> xbits(x);
   xbits.set_sign(FPBits<T>(y).sign());
-  return T(xbits);
+  return xbits.get_val();
 }
 
 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) {
   if (bits.is_zero()) {
     // TODO(Floating point exception): Raise div-by-zero exception.
     // TODO(errno): POSIX requires setting errno to ERANGE.
-    return T(FPBits<T>::inf(Sign::NEG));
+    return FPBits<T>::inf(Sign::NEG).get_val();
   } else if (bits.is_nan()) {
     return x;
   } else if (bits.is_inf()) {
     // Return positive infinity.
-    return T(FPBits<T>::inf());
+    return FPBits<T>::inf().get_val();
   }
 
   NormalFloat<T> normal(bits);
@@ -131,11 +131,11 @@ LIBC_INLINE T ldexp(T x, int exp) {
   // calculating the limit.
   int exp_limit = FPBits<T>::MAX_BIASED_EXPONENT + FPBits<T>::FRACTION_LEN + 1;
   if (exp > exp_limit)
-    return T(FPBits<T>::inf(bits.sign()));
+    return FPBits<T>::inf(bits.sign()).get_val();
 
   // Similarly on the negative side we return zero early if |exp| is too small.
   if (exp < -exp_limit)
-    return T(FPBits<T>::zero(bits.sign()));
+    return FPBits<T>::zero(bits.sign()).get_val();
 
   // For all other values, NormalFloat to T conversion handles it the right way.
   NormalFloat<T> normal(bits);
diff --git a/libc/src/__support/FPUtil/NearestIntegerOperations.h b/libc/src/__support/FPUtil/NearestIntegerOperations.h
index 62568977dc0c8b..19ae75ea788912 100644
--- a/libc/src/__support/FPUtil/NearestIntegerOperations.h
+++ b/libc/src/__support/FPUtil/NearestIntegerOperations.h
@@ -41,11 +41,11 @@ LIBC_INLINE T trunc(T x) {
 
   // If the exponent is such that abs(x) is less than 1, then return 0.
   if (exponent <= -1)
-    return T(FPBits<T>::zero(bits.sign()));
+    return FPBits<T>::zero(bits.sign()).get_val();
 
   int trim_size = FPBits<T>::FRACTION_LEN - exponent;
   bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
-  return T(bits);
+  return bits.get_val();
 }
 
 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) {
 
   uint32_t trim_size = FPBits<T>::FRACTION_LEN - exponent;
   bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
-  T trunc_value = T(bits);
+  T trunc_value = bits.get_val();
 
   // If x is already an integer, return it.
   if (trunc_value == x)
@@ -114,19 +114,19 @@ LIBC_INLINE T round(T x) {
 
   if (exponent == -1) {
     // Absolute value of x is greater than equal to 0.5 but less than 1.
-    return T(FPBits<T>::one(bits.sign()));
+    return FPBits<T>::one(bits.sign()).get_val();
   }
 
   if (exponent <= -2) {
     // Absolute value of x is less than 0.5.
-    return T(FPBits<T>::zero(bits.sign()));
+    return FPBits<T>::zero(bits.sign()).get_val();
   }
 
   uint32_t trim_size = FPBits<T>::FRACTION_LEN - exponent;
   bool half_bit_set =
       bool(bits.get_mantissa() & (StorageType(1) << (trim_size - 1)));
   bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
-  T trunc_value = T(bits);
+  T trunc_value = bits.get_val();
 
   // If x is already an integer, return it.
   if (trunc_value == x)
@@ -180,7 +180,7 @@ LIBC_INLINE T round_using_current_rounding_mode(T x) {
   uint32_t trim_size = FPBits<T>::FRACTION_LEN - exponent;
   FPBits<T> new_bits = bits;
   new_bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
-  T trunc_value = T(new_bits);
+  T trunc_value = new_bits.get_val();
 
   // If x is already an integer, return it.
   if (trunc_value == x)
diff --git a/libc/src/__support/FPUtil/NormalFloat.h b/libc/src/__support/FPUtil/NormalFloat.h
index fa4da33b5b17fa..57a401d911fc66 100644
--- a/libc/src/__support/FPUtil/NormalFloat.h
+++ b/libc/src/__support/FPUtil/NormalFloat.h
@@ -96,7 +96,7 @@ template <typename T> struct NormalFloat {
     // Max exponent is of the form 0xFF...E. That is why -2 and not -1.
     constexpr int MAX_EXPONENT_VALUE = (1 << FPBits<T>::EXP_LEN) - 2;
     if (biased_exponent > MAX_EXPONENT_VALUE) {
-      return T(FPBits<T>::inf(sign));
+      return FPBits<T>::inf(sign).get_val();
     }
 
     FPBits<T> result(T(0.0));
@@ -129,15 +129,15 @@ template <typename T> struct NormalFloat {
         // the overflow into the exponent.
         if (new_mantissa == ONE)
           result.set_biased_exponent(1);
-        return T(result);
+        return result.get_val();
       } else {
-        return T(result);
+        return result.get_val();
       }
     }
 
     result.set_biased_exponent(exponent + FPBits<T>::EXP_BIAS);
     result.set_mantissa(mantissa);
-    return T(result);
+    return result.get_val();
   }
 
 private:
@@ -250,16 +250,16 @@ template <> LIBC_INLINE NormalFloat<long double>::operator long double() const {
       } else {
         result.set_implicit_bit(0);
       }
-      return static_cast<long double>(result);
+      return result.get_val();
     } else {
-      return static_cast<long double>(result);
+      return result.get_val();
     }
   }
 
   result.set_biased_exponent(biased_exponent);
   result.set_mantissa(mantissa);
   result.set_implicit_bit(1);
-  return static_cast<long double>(result);
+  return result.get_val();
 }
 #endif // LIBC_LONG_DOUBLE_IS_X86_FLOAT80
 
diff --git a/libc/src/__support/FPUtil/generic/FMA.h b/libc/src/__support/FPUtil/generic/FMA.h
index 5c36463ea50213..f03af9246337ff 100644
--- a/libc/src/__support/FPUtil/generic/FMA.h
+++ b/libc/src/__support/FPUtil/generic/FMA.h
@@ -58,8 +58,8 @@ template <> LIBC_INLINE float fma<float>(float x, float y, float z) {
     // correct (when it matters).
     fputil::FPBits<double> t(
         (bit_prod.get_biased_exponent() >= bitz.get_biased_exponent())
-            ? ((double(bit_sum) - double(bit_prod)) - double(bitz))
-            : ((double(bit_sum) - double(bitz)) - double(bit_prod)));
+            ? ((bit_sum.get_val() - bit_prod.get_val()) - bitz.get_val())
+            : ((bit_sum.get_val() - bitz.get_val()) - bit_prod.get_val()));
 
     // Update sticky bits if t != 0.0 and the least (52 - 23 - 1 = 28) bits are
     // zero.
@@ -72,7 +72,7 @@ template <> LIBC_INLINE float fma<float>(float x, float y, float z) {
     }
   }
 
-  return static_cast<float>(static_cast<double>(bit_sum));
+  return static_cast<float>(bit_sum.get_val());
 }
 
 namespace internal {
@@ -257,7 +257,7 @@ template <> LIBC_INLINE double fma<double>(double x, double y, double z) {
         (round_mode == FE_DOWNWARD && prod_sign.is_pos())) {
       return FPBits::max_normal(prod_sign).get_val();
     }
-    return static_cast<double>(FPBits::inf(prod_sign));
+    return FPBits::inf(prod_sign).get_val();
   }
 
   // Remove hidden bit and append the exponent field and sign bit.
diff --git a/libc/src/__support/str_to_float.h b/libc/src/__support/str_to_float.h
index 9655c993bee28f..7ecb6c3e02fde3 100644
--- a/libc/src/__support/str_to_float.h
+++ b/libc/src/__support/str_to_float.h
@@ -568,11 +568,11 @@ clinger_fast_path(ExpandedFloat<T> init_num,
                              ClingerConsts<T>::POWERS_OF_TEN_ARRAY[exp10]);
 
     // If the results are equal, then we don't need to use the rounding mode.
-    if (T(result) != -T(negative_result)) {
+    if (result.get_val() != -negative_result.get_val()) {
       FPBits lower_result;
       FPBits higher_result;
 
-      if (T(result) < -T(negative_result)) {
+      if (result.get_val() < -negative_result.get_val()) {
         lower_result = result;
         higher_result = negative_result;
       } else {
@@ -1194,7 +1194,7 @@ LIBC_INLINE StrToNumResult<T> strtofloatingpoint(const char *__restrict src) {
   // special 80 bit long doubles. Otherwise it should be inlined out.
   set_implicit_bit<T>(result);
 
-  return {T(result), index, error};
+  return {result.get_val(), index, error};
 }
 
 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) {
   }
 
   result = FPBits::build_quiet_nan(fputil::Sign::POS, nan_mantissa);
-  return {T(result), 0, error};
+  return {result.get_val(), 0, error};
 }
 
 } // namespace internal
diff --git a/libc/src/math/generic/exp.cpp b/libc/src/math/generic/exp.cpp
index 49ea1699bb2093..f23170f8ed4259 100644
--- a/libc/src/math/generic/exp.cpp
+++ b/libc/src/math/generic/exp.cpp
@@ -222,7 +222,7 @@ double set_exceptional(double x) {
     fputil::raise_except_if_required(FE_OVERFLOW);
   }
   // x is +inf or nan
-  return x + static_cast<double>(FPBits::inf());
+  return x + FPBits::inf().get_val();
 }
 
 } // namespace
diff --git a/libc/src/math/generic/exp10.cpp b/libc/src/math/generic/exp10.cpp
index f1da03cba0b30b..6b40f5561845d8 100644
--- a/libc/src/math/generic/exp10.cpp
+++ b/libc/src/math/generic/exp10.cpp
@@ -268,7 +268,7 @@ double set_exceptional(double x) {
     fputil::raise_except_if_required(FE_OVERFLOW);
   }
   // x is +inf or nan
-  return x + static_cast<double>(FPBits::inf());
+  return x + FPBits::inf().get_val();
 }
 
 } // namespace
diff --git a/libc/src/math/generic/exp2.cpp b/libc/src/math/generic/exp2.cpp
index 508bff9bd9fc9c..01e66d1ae00f70 100644
--- a/libc/src/math/generic/exp2.cpp
+++ b/libc/src/math/generic/exp2.cpp
@@ -243,7 +243,7 @@ double set_exceptional(double x) {
     fputil::raise_except_if_required(FE_OVERFLOW);
   }
   // x is +inf or nan
-  return x + static_cast<double>(FPBits::inf());
+  return x + FPBits::inf().get_val();
 }
 
 } // namespace
diff --git a/libc/src/math/generic/expf.cpp b/libc/src/math/generic/expf.cpp
index f3ce8400d0a41e..c7ab974850a821 100644
--- a/libc/src/math/generic/expf.cpp
+++ b/libc/src/math/generic/expf.cpp
@@ -67,7 +67,7 @@ LLVM_LIBC_FUNCTION(float, expf, (float x)) {
         fputil::raise_except_if_required(FE_OVERFLOW);
       }
       // x is +inf or nan
-      return x + static_cast<float>(FPBits::inf());
+      return x + FPBits::inf().get_val();
     }
   }
   // For -104 < x < 89, to compute exp(x), we perform the following range
diff --git a/libc/src/math/generic/hypotf.cpp b/libc/src/math/generic/hypotf.cpp
index 93dd4feb36bee6..9a0805e390eb37 100644
--- a/libc/src/math/generic/hypotf.cpp
+++ b/libc/src/math/generic/hypotf.cpp
@@ -46,7 +46,7 @@ LLVM_LIBC_FUNCTION(float, hypotf, (float x, float y)) {
 
   if (!DoubleBits(sum_sq).is_inf_or_nan()) {
     // Correct rounding.
-    double r_sq = static_cast<double>(result) * static_cast<double>(result);
+    double r_sq = result.get_val() * result.get_val();
     double diff = sum_sq - r_sq;
     constexpr uint64_t mask = 0x0000'0000'3FFF'FFFFULL;
     uint64_t lrs = result.uintval() & mask;
@@ -60,14 +60,14 @@ LLVM_LIBC_FUNCTION(float, hypotf, (float x, float y)) {
     FPBits bits_x(x), bits_y(y);
     if (bits_x.is_inf_or_nan() || bits_y.is_inf_or_nan()) {
       if (bits_x.is_inf() || bits_y.is_inf())
-        return static_cast<float>(FPBits::inf());
+        return FPBits::inf().get_val();
       if (bits_x.is_nan())
         return x;
       return y;
     }
   }
 
-  return static_cast<float>(static_cast<double>(result));
+  return result.get_val();
 }
 
 } // namespace LIBC_NAMESPACE
diff --git a/libc/src/math/generic/log10f.cpp b/libc/src/math/generic/log10f.cpp
index f87e34ec5fb385..ac2e3b9cf925a8 100644
--- a/libc/src/math/generic/log10f.cpp
+++ b/libc/src/math/generic/log10f.cpp
@@ -191,7 +191,7 @@ LLVM_LIBC_FUNCTION(float, log10f, (float x)) {
   // Set bits to 1.m
   xbits.set_biased_exponent(0x7F);
 
-  float u = static_cast<float>(xbits);
+  float u = xbits.get_val();
   double v;
 #ifdef LIBC_TARGET_CPU_HAS_FMA
   v = static_cast<double>(fputil::multiply_add(u, R[index], -1.0f)); // Exact.
diff --git a/libc/src/math/generic/log1pf.cpp b/libc/src/math/generic/log1pf.cpp
index bc472caf54f89c..8de4a2067f9d32 100644
--- a/libc/src/math/generic/log1pf.cpp
+++ b/libc/src/math/generic/log1pf.cpp
@@ -66,7 +66,7 @@ LIBC_INLINE float log(double x) {
   // Clear the lowest 45 bits.
   f.bits &= ~0x0000'1FFF'FFFF'FFFFULL;
 
-  double d = static_cast<double>(xbits) - static_cast<double>(f);
+  double d = xbits.get_val() - f.get_val();
   d *= ONE_OVER_F[f_index];
 
   double extra_factor = fputil::multiply_add(m, LOG_2, LOG_F[f_index]);
@@ -106,7 +106,7 @@ LLVM_LIBC_FUNCTION(float, log1pf, (float x)) {
     case 0xbf800000U: // x = -1.0
       fputil::set_errno_if_required(ERANGE);
       fputil::raise_except_if_required(FE_DIVBYZERO);
-      return static_cast<float>(fputil::FPBits<float>::inf(fputil::Sign::NEG));
+      return FPBits::inf(fputil::Sign::NEG).get_val();
 #ifndef LIBC_TARGET_CPU_HAS_FMA
     case 0x4cc1c80bU: // x = 0x1.839016p+26f
       return fputil::round_result_slightly_down(0x1.26fc04p+4f);
diff --git a/libc/src/math/generic/log2f.cpp b/libc/src/math/generic/log2f.cpp
index 7eaa5d53ccedd3..e3417552ad35a5 100644
--- a/libc/src/math/generic/log2f.cpp
+++ b/libc/src/math/generic/log2f.cpp
@@ -94,7 +94,7 @@ LLVM_LIBC_FUNCTION(float, log2f, (float x)) {
   // Set bits to 1.m
   xbits.set_biased_exponent(0x7F);
 
-  float u = static_cast<float>(xbits);
+  float u = xbits.get_val();
   double v;
 #ifdef LIBC_TARGET_CPU_HAS_FMA
   v = static_cast<double>(fputil::multiply_add(u, R[index], -1.0f)); // Exact.
diff --git a/libc/src/math/generic/logf.cpp b/libc/src/math/generic/logf.cpp
index 88f7ea01b2f19a..4ad5a4e9b6ad8b 100644
--- a/libc/src/math/generic/logf.cpp
+++ b/libc/src/math/generic/logf.cpp
@@ -85,7 +85,7 @@ LLVM_LIBC_FUNCTION(float, logf, (float x)) {
         // Return -inf and raise FE_DIVBYZERO
         fputil::set_errno_if_required(ERANGE);
         fputil::raise_except_if_required(FE_DIVBYZERO);
-        return static_cast<float>(FPBits::inf(fputil::Sign::NEG));
+        return FPBits::inf(Sign::NEG).get_val();
       }
       // Normalize denormal inputs.
       xbits = FPBits(xbits.get_val() * 0x1.0p23f);
@@ -149,7 +149,7 @@ LLVM_LIBC_FUNCTION(float, logf, (float x)) {
   // Set bits to 1.m
   xbits.set_biased_exponent(0x7F);
 
-  float u = static_cast<float>(xbits);
+  float u = xbits.get_val();
   double v;
 #ifdef LIBC_TARGET_CPU_HAS_FMA
   v = static_cast<double>(fputil::multiply_add(u, R[index], -1.0f)); // Exact.
diff --git a/libc/src/math/generic/range_reduction_fma.h b/libc/src/math/generic/range_reduction_fma.h
index 13a7360b423328..d2ca5d348aadd5 100644
--- a/libc/src/math/generic/range_reduction_fma.h
+++ b/libc/src/math/generic/range_reduction_fma.h
@@ -52,7 +52,7 @@ LIBC_INLINE int64_t large_range_reduction(double x, int x_exp, double &y) {
     // least 2^6.
     fputil::FPBits<double> prod_hi(x * THIRTYTWO_OVER_PI[0]);
     prod_hi.bits &= (x_exp < 55) ? (~0xfffULL) : (~0ULL); // |x| < 2^55
-    double k_hi = fputil::nearest_integer(static_cast<double>(prod_hi));
+    double k_hi = fputil::nearest_integer(prod_hi.get_val());
     double truncated_prod = fputil::fma(x, THIRTYTWO_OVER_PI[0], -k_hi);
     double prod_lo = fputil::fma(x, THIRTYTWO_OVER_PI[1], truncated_prod);
     double k_lo = fputil::nearest_integer(prod_lo);
@@ -71,7 +71,7 @@ LIBC_INLINE int64_t large_range_reduction(double x, int x_exp, double &y) {
   // least 64.
   fputil::FPBits<double> prod_hi(x * THIRTYTWO_OVER_PI[1]);
   prod_hi.bits &= (x_exp < 110) ? (~0xfffULL) : (~0ULL); // |x| < 2^110
-  double k_hi = fputil::nearest_integer(static_cast<double>(prod_hi));
+  double k_hi = fputil::nearest_integer(prod_hi.get_val());
   double truncated_prod = fputil::fma(x, THIRTYTWO_OVER_PI[1], -k_hi);
   double prod_lo = fputil::fma(x, THIRTYTWO_OVER_PI[2], truncated_prod);
   double k_lo = fputil::nearest_integer(prod_lo);
diff --git a/libc/src/stdio/printf_core/float_dec_converter.h b/libc/src/stdio/printf_core/float_dec_converter.h
index 6171d1d5946e6a..b54526d3710860 100644
--- a/libc/src/stdio/printf_core/float_dec_converter.h
+++ b/libc/src/stdio/printf_core/float_dec_converter.h
@@ -498,7 +498,7 @@ LIBC_INLINE int convert_float_decimal_typed(Writer *writer,
 
   PaddingWriter padding_writer(to_conv, sign_char);
   FloatWriter float_writer(writer, has_decimal_point, padding_writer);
-  FloatToString<T> float_converter(static_cast<T>(float_bits));
+  FloatToString<T> float_converter(float_bits.get_val());
 
   const size_t positive_blocks = float_converter.get_positive_blocks();
 
@@ -608,7 +608,7 @@ LIBC_INLINE int convert_float_dec_exp_typed(Writer *writer,...
[truncated]

Copy link

github-actions bot commented Jan 23, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@gchatelet gchatelet merged commit 2856db0 into llvm:main Jan 23, 2024
3 of 4 checks passed
@gchatelet gchatelet deleted the remove_fpbit_cast_operator branch January 23, 2024 16:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants