[libc-commits] [libc] ea43c8e - [libc][NFC] Make `EXP_MANT_MASK` an implementation detail (#75810)

via libc-commits libc-commits at lists.llvm.org
Tue Dec 19 08:20:07 PST 2023


Author: Guillaume Chatelet
Date: 2023-12-19T17:20:03+01:00
New Revision: ea43c8ee73fd34522ff074c7daeadb82c0f6035a

URL: https://github.com/llvm/llvm-project/commit/ea43c8ee73fd34522ff074c7daeadb82c0f6035a
DIFF: https://github.com/llvm/llvm-project/commit/ea43c8ee73fd34522ff074c7daeadb82c0f6035a.diff

LOG: [libc][NFC] Make `EXP_MANT_MASK` an implementation detail (#75810)

This mask is an implementation detail of `FPBits` and shouldn't really
leak outside of it.

Added: 
    

Modified: 
    libc/src/__support/FPUtil/FPBits.h
    libc/src/__support/FPUtil/FloatProperties.h
    libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
    libc/src/math/generic/acoshf.cpp
    libc/src/math/generic/asinhf.cpp
    libc/src/math/generic/atanhf.cpp
    libc/src/math/generic/exp.cpp
    libc/src/math/generic/exp10.cpp
    libc/src/math/generic/exp2.cpp
    libc/src/math/generic/expm1.cpp
    libc/src/math/generic/inv_trigf_utils.h
    libc/src/math/generic/powf.cpp
    libc/src/math/generic/sinhf.cpp
    libc/src/math/generic/tanhf.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/FPUtil/FPBits.h b/libc/src/__support/FPUtil/FPBits.h
index bee93ca60dc1fb..e2efc24ee41a18 100644
--- a/libc/src/__support/FPUtil/FPBits.h
+++ b/libc/src/__support/FPUtil/FPBits.h
@@ -33,7 +33,11 @@ template <typename T> struct FPBits : private FloatProperties<T> {
                 "FPBits instantiated with invalid type.");
   using typename FloatProperties<T>::StorageType;
   using FloatProperties<T>::TOTAL_LEN;
-  using FloatProperties<T>::EXP_MANT_MASK;
+
+private:
+  using FloatProperties<T>::EXP_SIG_MASK;
+
+public:
   using FloatProperties<T>::EXP_MASK;
   using FloatProperties<T>::EXP_BIAS;
   using FloatProperties<T>::EXP_LEN;
@@ -149,21 +153,25 @@ template <typename T> struct FPBits : private FloatProperties<T> {
   }
 
   LIBC_INLINE constexpr bool is_inf() const {
-    return (bits & EXP_MANT_MASK) == EXP_MASK;
+    return (bits & EXP_SIG_MASK) == EXP_MASK;
   }
 
   LIBC_INLINE constexpr bool is_nan() const {
-    return (bits & EXP_MANT_MASK) > EXP_MASK;
+    return (bits & EXP_SIG_MASK) > EXP_MASK;
   }
 
   LIBC_INLINE constexpr bool is_quiet_nan() const {
-    return (bits & EXP_MANT_MASK) == (EXP_MASK | QUIET_NAN_MASK);
+    return (bits & EXP_SIG_MASK) == (EXP_MASK | QUIET_NAN_MASK);
   }
 
   LIBC_INLINE constexpr bool is_inf_or_nan() const {
     return (bits & EXP_MASK) == EXP_MASK;
   }
 
+  LIBC_INLINE constexpr FPBits abs() const {
+    return FPBits(bits & EXP_SIG_MASK);
+  }
+
   LIBC_INLINE static constexpr T zero(bool sign = false) {
     return FPBits(sign ? SIGN_MASK : StorageType(0)).get_val();
   }

diff  --git a/libc/src/__support/FPUtil/FloatProperties.h b/libc/src/__support/FPUtil/FloatProperties.h
index ecc6f8d2299945..bcf1f7cfabd34b 100644
--- a/libc/src/__support/FPUtil/FloatProperties.h
+++ b/libc/src/__support/FPUtil/FloatProperties.h
@@ -131,6 +131,9 @@ struct FPProperties : public internal::FPBaseProperties<fp_type> {
   // The bit pattern that keeps only the *sign* part.
   LIBC_INLINE_VAR static constexpr StorageType SIGN_MASK =
       mask_trailing_ones<StorageType, SIGN_LEN>() << SIGN_MASK_SHIFT;
+  // The bit pattern that keeps only the *exponent + significand* part.
+  LIBC_INLINE_VAR static constexpr StorageType EXP_SIG_MASK =
+      mask_trailing_ones<StorageType, EXP_LEN + SIG_LEN>();
   // The bit pattern that keeps only the *sign + exponent + significand* part.
   LIBC_INLINE_VAR static constexpr StorageType FP_MASK =
       mask_trailing_ones<StorageType, TOTAL_LEN>();
@@ -152,8 +155,6 @@ struct FPProperties : public internal::FPBaseProperties<fp_type> {
       FRACTION_LEN + 1;
   LIBC_INLINE_VAR static constexpr StorageType FRACTION_MASK =
       mask_trailing_ones<StorageType, FRACTION_LEN>();
-  LIBC_INLINE_VAR static constexpr StorageType EXP_MANT_MASK =
-      EXP_MASK | SIG_MASK;
 
 protected:
   // If a number x is a NAN, then it is a quiet NAN if:

diff  --git a/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h b/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
index 7ac94664baf605..7e4cb87deb90a0 100644
--- a/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
+++ b/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
@@ -29,7 +29,6 @@ namespace fputil {
 template <> struct FPBits<long double> : private FloatProperties<long double> {
   using typename FloatProperties<long double>::StorageType;
   using FloatProperties<long double>::TOTAL_LEN;
-  using FloatProperties<long double>::EXP_MANT_MASK;
   using FloatProperties<long double>::EXP_MASK;
   using FloatProperties<long double>::EXP_BIAS;
   using FloatProperties<long double>::EXP_LEN;

diff  --git a/libc/src/math/generic/acoshf.cpp b/libc/src/math/generic/acoshf.cpp
index 142c17795d083a..b0b87095fbb07a 100644
--- a/libc/src/math/generic/acoshf.cpp
+++ b/libc/src/math/generic/acoshf.cpp
@@ -34,7 +34,7 @@ LLVM_LIBC_FUNCTION(float, acoshf, (float x)) {
 
   if (LIBC_UNLIKELY(x_u >= 0x4f8ffb03)) {
     // Check for exceptional values.
-    uint32_t x_abs = x_u & FPBits_t::EXP_MANT_MASK;
+    uint32_t x_abs = xbits.abs().uintval();
     if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) {
       // x is +inf or NaN.
       return x;

diff  --git a/libc/src/math/generic/asinhf.cpp b/libc/src/math/generic/asinhf.cpp
index 5b2f63d3fe144e..ac059910b4ef2a 100644
--- a/libc/src/math/generic/asinhf.cpp
+++ b/libc/src/math/generic/asinhf.cpp
@@ -21,7 +21,7 @@ LLVM_LIBC_FUNCTION(float, asinhf, (float x)) {
   using FPBits_t = typename fputil::FPBits<float>;
   FPBits_t xbits(x);
   uint32_t x_u = xbits.uintval();
-  uint32_t x_abs = x_u & FPBits_t::EXP_MANT_MASK;
+  uint32_t x_abs = xbits.abs().uintval();
 
   // |x| <= 2^-3
   if (LIBC_UNLIKELY(x_abs <= 0x3e80'0000U)) {

diff  --git a/libc/src/math/generic/atanhf.cpp b/libc/src/math/generic/atanhf.cpp
index dfec28e9a44a72..df5f53f392cf30 100644
--- a/libc/src/math/generic/atanhf.cpp
+++ b/libc/src/math/generic/atanhf.cpp
@@ -17,7 +17,7 @@ LLVM_LIBC_FUNCTION(float, atanhf, (float x)) {
   using FPBits = typename fputil::FPBits<float>;
   FPBits xbits(x);
   bool sign = xbits.get_sign();
-  uint32_t x_abs = xbits.uintval() & FPBits::EXP_MANT_MASK;
+  uint32_t x_abs = xbits.abs().uintval();
 
   // |x| >= 1.0
   if (LIBC_UNLIKELY(x_abs >= 0x3F80'0000U)) {

diff  --git a/libc/src/math/generic/exp.cpp b/libc/src/math/generic/exp.cpp
index 82a58aed8e15a5..5428a04430887f 100644
--- a/libc/src/math/generic/exp.cpp
+++ b/libc/src/math/generic/exp.cpp
@@ -174,11 +174,10 @@ DoubleDouble exp_double_double(double x, double kd,
 // |x| <= 2^-53 or x < log(2^-1075) or x >= 0x1.6232bdd7abcd3p+9
 double set_exceptional(double x) {
   using FPBits = typename fputil::FPBits<double>;
-  using FloatProp = typename fputil::FloatProperties<double>;
   FPBits xbits(x);
 
   uint64_t x_u = xbits.uintval();
-  uint64_t x_abs = x_u & FloatProp::EXP_MANT_MASK;
+  uint64_t x_abs = xbits.abs().uintval();
 
   // |x| <= 2^-53
   if (x_abs <= 0x3ca0'0000'0000'0000ULL) {

diff  --git a/libc/src/math/generic/exp10.cpp b/libc/src/math/generic/exp10.cpp
index 9e911f286a17a3..aa66d4f17a3a02 100644
--- a/libc/src/math/generic/exp10.cpp
+++ b/libc/src/math/generic/exp10.cpp
@@ -221,11 +221,10 @@ double exp10_denorm(double x) {
 //  * x is inf or nan
 double set_exceptional(double x) {
   using FPBits = typename fputil::FPBits<double>;
-  using FloatProp = typename fputil::FloatProperties<double>;
   FPBits xbits(x);
 
   uint64_t x_u = xbits.uintval();
-  uint64_t x_abs = x_u & FloatProp::EXP_MANT_MASK;
+  uint64_t x_abs = xbits.abs().uintval();
 
   // |x| < log10(1 + 2^-53)
   if (x_abs <= 0x3c8bcb7b1526e50e) {

diff  --git a/libc/src/math/generic/exp2.cpp b/libc/src/math/generic/exp2.cpp
index d2753d723d85b9..3e9f9c6855c436 100644
--- a/libc/src/math/generic/exp2.cpp
+++ b/libc/src/math/generic/exp2.cpp
@@ -196,11 +196,10 @@ double exp2_denorm(double x) {
 //  * x is inf or nan
 double set_exceptional(double x) {
   using FPBits = typename fputil::FPBits<double>;
-  using FloatProp = typename fputil::FloatProperties<double>;
   FPBits xbits(x);
 
   uint64_t x_u = xbits.uintval();
-  uint64_t x_abs = x_u & FloatProp::EXP_MANT_MASK;
+  uint64_t x_abs = xbits.abs().uintval();
 
   // |x| < log2(1 + 2^-53)
   if (x_abs <= 0x3ca71547652b82fd) {

diff  --git a/libc/src/math/generic/expm1.cpp b/libc/src/math/generic/expm1.cpp
index fc69934730a8f1..e7cc240839759c 100644
--- a/libc/src/math/generic/expm1.cpp
+++ b/libc/src/math/generic/expm1.cpp
@@ -219,11 +219,10 @@ DoubleDouble exp_double_double(double x, double kd, const DoubleDouble &exp_mid,
 // |x| <= 2^-53 or x < log(2^-54) or x >= 0x1.6232bdd7abcd3p+9
 double set_exceptional(double x) {
   using FPBits = typename fputil::FPBits<double>;
-  using FloatProp = typename fputil::FloatProperties<double>;
   FPBits xbits(x);
 
   uint64_t x_u = xbits.uintval();
-  uint64_t x_abs = x_u & FloatProp::EXP_MANT_MASK;
+  uint64_t x_abs = xbits.abs().uintval();
 
   // |x| <= 2^-53.
   if (x_abs <= 0x3ca0'0000'0000'0000ULL) {

diff  --git a/libc/src/math/generic/inv_trigf_utils.h b/libc/src/math/generic/inv_trigf_utils.h
index 588ebbfa71aeb0..4e85d4ae08d571 100644
--- a/libc/src/math/generic/inv_trigf_utils.h
+++ b/libc/src/math/generic/inv_trigf_utils.h
@@ -50,7 +50,7 @@ LIBC_INLINE double atan_eval(double x) {
 
   FPB bs(x);
   bool sign = bs.get_sign();
-  auto x_abs = bs.uintval() & FPB::EXP_MANT_MASK;
+  auto x_abs = bs.abs().uintval();
 
   if (x_abs <= umin) {
     double pe = LIBC_NAMESPACE::fputil::polyeval(

diff  --git a/libc/src/math/generic/powf.cpp b/libc/src/math/generic/powf.cpp
index dd7fa7f6115d4e..8470eb878e6035 100644
--- a/libc/src/math/generic/powf.cpp
+++ b/libc/src/math/generic/powf.cpp
@@ -517,9 +517,9 @@ LLVM_LIBC_FUNCTION(float, powf, (float x, float y)) {
   FloatBits xbits(x), ybits(y);
 
   uint32_t x_u = xbits.uintval();
-  uint32_t x_abs = x_u & FloatProp::EXP_MANT_MASK;
+  uint32_t x_abs = xbits.abs().uintval();
   uint32_t y_u = ybits.uintval();
-  uint32_t y_abs = y_u & FloatProp::EXP_MANT_MASK;
+  uint32_t y_abs = ybits.abs().uintval();
 
   ///////// BEGIN - Check exceptional cases ////////////////////////////////////
 

diff  --git a/libc/src/math/generic/sinhf.cpp b/libc/src/math/generic/sinhf.cpp
index f174a0f4fc34c2..d64519d0ec79bf 100644
--- a/libc/src/math/generic/sinhf.cpp
+++ b/libc/src/math/generic/sinhf.cpp
@@ -17,7 +17,7 @@ namespace LIBC_NAMESPACE {
 LLVM_LIBC_FUNCTION(float, sinhf, (float x)) {
   using FPBits = typename fputil::FPBits<float>;
   FPBits xbits(x);
-  uint32_t x_abs = xbits.uintval() & FPBits::EXP_MANT_MASK;
+  uint32_t x_abs = xbits.abs().uintval();
 
   // When |x| >= 90, or x is inf or nan
   if (LIBC_UNLIKELY(x_abs >= 0x42b4'0000U || x_abs <= 0x3da0'0000U)) {

diff  --git a/libc/src/math/generic/tanhf.cpp b/libc/src/math/generic/tanhf.cpp
index 9042a41c5ed3fe..073097e1208af1 100644
--- a/libc/src/math/generic/tanhf.cpp
+++ b/libc/src/math/generic/tanhf.cpp
@@ -23,8 +23,7 @@ constexpr double LOG2_E_EXP2_6 = ExpBase::LOG2_B * 2.0;
 LLVM_LIBC_FUNCTION(float, tanhf, (float x)) {
   using FPBits = typename fputil::FPBits<float>;
   FPBits xbits(x);
-  uint32_t x_u = xbits.uintval();
-  uint32_t x_abs = x_u & FPBits::EXP_MANT_MASK;
+  uint32_t x_abs = xbits.abs().uintval();
 
   // When |x| >= 15, or x is inf or nan, or |x| <= 0.078125
   if (LIBC_UNLIKELY((x_abs >= 0x4170'0000U) || (x_abs <= 0x3da0'0000U))) {


        


More information about the libc-commits mailing list