[libc-commits] [libc] [libc][NFC] Remove MantissaWidth traits (PR #75458)
Guillaume Chatelet via libc-commits
libc-commits at lists.llvm.org
Thu Dec 14 06:00:08 PST 2023
https://github.com/gchatelet updated https://github.com/llvm/llvm-project/pull/75458
>From 5364cd5ee2bddf13fae064fc070b79afaa974e3a Mon Sep 17 00:00:00 2001
From: Guillaume Chatelet <gchatelet at google.com>
Date: Thu, 14 Dec 2023 10:39:25 +0000
Subject: [PATCH 1/2] [libc][NFC] Remove MantissaWidth traits
Same as #75362, the traits does not bring a lot of value over `FloatProperties::MANTISSA_WIDTH` (or `FPBits::MANTISSA_WIDTH`).
---
libc/src/__support/FPUtil/FPBits.h | 4 ----
libc/src/__support/FPUtil/Hypot.h | 8 ++++----
.../__support/FPUtil/ManipulationFunctions.h | 2 +-
.../__support/FPUtil/NearestIntegerOperations.h | 16 ++++++++--------
libc/src/__support/FPUtil/NormalFloat.h | 10 +++++-----
libc/src/__support/FPUtil/generic/FMA.h | 5 ++---
libc/src/__support/FPUtil/generic/sqrt.h | 7 ++++---
.../FPUtil/generic/sqrt_80_bit_long_double.h | 17 ++++++++---------
.../__support/FPUtil/x86_64/LongDoubleBits.h | 10 ++++------
.../FPUtil/x86_64/NextAfterLongDouble.h | 5 ++---
libc/src/__support/float_to_string.h | 2 +-
libc/src/math/generic/asinf.cpp | 3 +--
libc/src/math/generic/hypotf.cpp | 2 +-
libc/src/math/generic/log1pf.cpp | 2 +-
libc/src/math/generic/sincosf.cpp | 3 +--
.../src/stdio/printf_core/float_dec_converter.h | 6 +++---
.../src/stdio/printf_core/float_hex_converter.h | 2 +-
libc/test/src/math/FrexpTest.h | 2 +-
libc/test/src/math/LdExpTest.h | 3 +--
libc/test/src/math/LogbTest.h | 2 +-
libc/test/src/math/NextAfterTest.h | 5 ++---
libc/test/src/math/RoundToIntegerTest.h | 13 ++++++-------
libc/test/src/math/SqrtTest.h | 2 +-
libc/test/src/math/smoke/FrexpTest.h | 2 +-
libc/test/src/math/smoke/LdExpTest.h | 3 +--
libc/test/src/math/smoke/LogbTest.h | 2 +-
libc/test/src/math/smoke/NextAfterTest.h | 5 ++---
libc/test/src/math/smoke/NextTowardTest.h | 5 ++---
libc/test/src/math/smoke/SqrtTest.h | 2 +-
libc/utils/MPFRWrapper/MPFRUtils.cpp | 6 +++---
30 files changed, 70 insertions(+), 86 deletions(-)
diff --git a/libc/src/__support/FPUtil/FPBits.h b/libc/src/__support/FPUtil/FPBits.h
index a318c6a9fd235f..d1e26de22ef130 100644
--- a/libc/src/__support/FPUtil/FPBits.h
+++ b/libc/src/__support/FPUtil/FPBits.h
@@ -20,10 +20,6 @@
namespace LIBC_NAMESPACE {
namespace fputil {
-template <typename T> struct MantissaWidth {
- static constexpr unsigned VALUE = FloatProperties<T>::MANTISSA_WIDTH;
-};
-
// A generic class to represent single precision, double precision, and quad
// precision IEEE 754 floating point formats.
// On most platforms, the 'float' type corresponds to single precision floating
diff --git a/libc/src/__support/FPUtil/Hypot.h b/libc/src/__support/FPUtil/Hypot.h
index 42d9e1b3f8cec5..ad6b72db0524fc 100644
--- a/libc/src/__support/FPUtil/Hypot.h
+++ b/libc/src/__support/FPUtil/Hypot.h
@@ -124,7 +124,7 @@ LIBC_INLINE T hypot(T x, T y) {
uint16_t y_exp = y_bits.get_biased_exponent();
uint16_t exp_diff = (x_exp > y_exp) ? (x_exp - y_exp) : (y_exp - x_exp);
- if ((exp_diff >= MantissaWidth<T>::VALUE + 2) || (x == 0) || (y == 0)) {
+ if ((exp_diff >= FPBits_t::MANTISSA_WIDTH + 2) || (x == 0) || (y == 0)) {
return abs(x) + abs(y);
}
@@ -148,7 +148,7 @@ LIBC_INLINE T hypot(T x, T y) {
out_exp = a_exp;
// Add an extra bit to simplify the final rounding bit computation.
- constexpr UIntType ONE = UIntType(1) << (MantissaWidth<T>::VALUE + 1);
+ constexpr UIntType ONE = UIntType(1) << (FPBits_t::MANTISSA_WIDTH + 1);
a_mant <<= 1;
b_mant <<= 1;
@@ -158,7 +158,7 @@ LIBC_INLINE T hypot(T x, T y) {
if (a_exp != 0) {
leading_one = ONE;
a_mant |= ONE;
- y_mant_width = MantissaWidth<T>::VALUE + 1;
+ y_mant_width = FPBits_t::MANTISSA_WIDTH + 1;
} else {
leading_one = internal::find_leading_one(a_mant, y_mant_width);
a_exp = 1;
@@ -258,7 +258,7 @@ LIBC_INLINE T hypot(T x, T y) {
}
}
- y_new |= static_cast<UIntType>(out_exp) << MantissaWidth<T>::VALUE;
+ y_new |= static_cast<UIntType>(out_exp) << FPBits_t::MANTISSA_WIDTH;
return cpp::bit_cast<T>(y_new);
}
diff --git a/libc/src/__support/FPUtil/ManipulationFunctions.h b/libc/src/__support/FPUtil/ManipulationFunctions.h
index 08adb074b121fa..51b58ba29bab89 100644
--- a/libc/src/__support/FPUtil/ManipulationFunctions.h
+++ b/libc/src/__support/FPUtil/ManipulationFunctions.h
@@ -130,7 +130,7 @@ LIBC_INLINE T ldexp(T x, int exp) {
// early. Because the result of the ldexp operation can be a subnormal number,
// we need to accommodate the (mantissaWidht + 1) worth of shift in
// calculating the limit.
- int exp_limit = FPBits<T>::MAX_EXPONENT + MantissaWidth<T>::VALUE + 1;
+ int exp_limit = FPBits<T>::MAX_EXPONENT + FPBits<T>::MANTISSA_WIDTH + 1;
if (exp > exp_limit)
return bits.get_sign() ? T(FPBits<T>::neg_inf()) : T(FPBits<T>::inf());
diff --git a/libc/src/__support/FPUtil/NearestIntegerOperations.h b/libc/src/__support/FPUtil/NearestIntegerOperations.h
index 8c4b24803bec1d..b0ae8d0040ea19 100644
--- a/libc/src/__support/FPUtil/NearestIntegerOperations.h
+++ b/libc/src/__support/FPUtil/NearestIntegerOperations.h
@@ -36,7 +36,7 @@ LIBC_INLINE T trunc(T x) {
// If the exponent is greater than the most negative mantissa
// exponent, then x is already an integer.
- if (exponent >= static_cast<int>(MantissaWidth<T>::VALUE))
+ if (exponent >= static_cast<int>(FPBits<T>::MANTISSA_WIDTH))
return x;
// If the exponent is such that abs(x) is less than 1, then return 0.
@@ -47,7 +47,7 @@ LIBC_INLINE T trunc(T x) {
return T(0.0);
}
- int trim_size = MantissaWidth<T>::VALUE - exponent;
+ int trim_size = FPBits<T>::MANTISSA_WIDTH - exponent;
bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
return T(bits);
}
@@ -65,7 +65,7 @@ LIBC_INLINE T ceil(T x) {
// If the exponent is greater than the most negative mantissa
// exponent, then x is already an integer.
- if (exponent >= static_cast<int>(MantissaWidth<T>::VALUE))
+ if (exponent >= static_cast<int>(FPBits<T>::MANTISSA_WIDTH))
return x;
if (exponent <= -1) {
@@ -75,7 +75,7 @@ LIBC_INLINE T ceil(T x) {
return T(1.0);
}
- uint32_t trim_size = MantissaWidth<T>::VALUE - exponent;
+ uint32_t trim_size = FPBits<T>::MANTISSA_WIDTH - exponent;
bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
T trunc_value = T(bits);
@@ -114,7 +114,7 @@ LIBC_INLINE T round(T x) {
// If the exponent is greater than the most negative mantissa
// exponent, then x is already an integer.
- if (exponent >= static_cast<int>(MantissaWidth<T>::VALUE))
+ if (exponent >= static_cast<int>(FPBits<T>::MANTISSA_WIDTH))
return x;
if (exponent == -1) {
@@ -133,7 +133,7 @@ LIBC_INLINE T round(T x) {
return T(0.0);
}
- uint32_t trim_size = MantissaWidth<T>::VALUE - exponent;
+ uint32_t trim_size = FPBits<T>::MANTISSA_WIDTH - exponent;
bool half_bit_set =
bool(bits.get_mantissa() & (UIntType(1) << (trim_size - 1)));
bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
@@ -167,7 +167,7 @@ LIBC_INLINE T round_using_current_rounding_mode(T x) {
// If the exponent is greater than the most negative mantissa
// exponent, then x is already an integer.
- if (exponent >= static_cast<int>(MantissaWidth<T>::VALUE))
+ if (exponent >= static_cast<int>(FPBits<T>::MANTISSA_WIDTH))
return x;
if (exponent <= -1) {
@@ -188,7 +188,7 @@ LIBC_INLINE T round_using_current_rounding_mode(T x) {
}
}
- uint32_t trim_size = MantissaWidth<T>::VALUE - exponent;
+ uint32_t trim_size = FPBits<T>::MANTISSA_WIDTH - exponent;
FPBits<T> new_bits = bits;
new_bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
T trunc_value = T(new_bits);
diff --git a/libc/src/__support/FPUtil/NormalFloat.h b/libc/src/__support/FPUtil/NormalFloat.h
index 77fcd85017735a..397a3bb41673b0 100644
--- a/libc/src/__support/FPUtil/NormalFloat.h
+++ b/libc/src/__support/FPUtil/NormalFloat.h
@@ -32,7 +32,7 @@ template <typename T> struct NormalFloat {
"NormalFloat template parameter has to be a floating point type.");
using UIntType = typename FPBits<T>::UIntType;
- static constexpr UIntType ONE = (UIntType(1) << MantissaWidth<T>::VALUE);
+ static constexpr UIntType ONE = (UIntType(1) << FPBits<T>::MANTISSA_WIDTH);
// Unbiased exponent value.
int32_t exponent;
@@ -40,7 +40,7 @@ template <typename T> struct NormalFloat {
UIntType mantissa;
// We want |UIntType| to have atleast one bit more than the actual mantissa
// bit width to accommodate the implicit 1 value.
- static_assert(sizeof(UIntType) * 8 >= MantissaWidth<T>::VALUE + 1,
+ static_assert(sizeof(UIntType) * 8 >= FPBits<T>::MANTISSA_WIDTH + 1,
"Bad type for mantissa in NormalFloat.");
bool sign;
@@ -105,7 +105,7 @@ template <typename T> struct NormalFloat {
unsigned shift = SUBNORMAL_EXPONENT - exponent;
// Since exponent > subnormalExponent, shift is strictly greater than
// zero.
- if (shift <= MantissaWidth<T>::VALUE + 1) {
+ if (shift <= FPBits<T>::MANTISSA_WIDTH + 1) {
// Generate a subnormal number. Might lead to loss of precision.
// We round to nearest and round halfway cases to even.
const UIntType shift_out_mask = (UIntType(1) << shift) - 1;
@@ -163,7 +163,7 @@ template <typename T> struct NormalFloat {
LIBC_INLINE unsigned evaluate_normalization_shift(UIntType m) {
unsigned shift = 0;
- for (; (ONE & m) == 0 && (shift < MantissaWidth<T>::VALUE);
+ for (; (ONE & m) == 0 && (shift < FPBits<T>::MANTISSA_WIDTH);
m <<= 1, ++shift)
;
return shift;
@@ -222,7 +222,7 @@ template <> LIBC_INLINE NormalFloat<long double>::operator long double() const {
constexpr int SUBNORMAL_EXPONENT = -LDBits::EXPONENT_BIAS + 1;
if (exponent < SUBNORMAL_EXPONENT) {
unsigned shift = SUBNORMAL_EXPONENT - exponent;
- if (shift <= MantissaWidth<long double>::VALUE + 1) {
+ if (shift <= LDBits::MANTISSA_WIDTH + 1) {
// Generate a subnormal number. Might lead to loss of precision.
// We round to nearest and round halfway cases to even.
const UIntType shift_out_mask = (UIntType(1) << shift) - 1;
diff --git a/libc/src/__support/FPUtil/generic/FMA.h b/libc/src/__support/FPUtil/generic/FMA.h
index 61a1401c30e827..3c4d943a7c71fb 100644
--- a/libc/src/__support/FPUtil/generic/FMA.h
+++ b/libc/src/__support/FPUtil/generic/FMA.h
@@ -159,11 +159,10 @@ template <> LIBC_INLINE double fma<double>(double x, double y, double z) {
UInt128 prod_mant = x_mant * y_mant << 10;
int prod_lsb_exp =
- x_exp + y_exp -
- (FPBits::EXPONENT_BIAS + 2 * MantissaWidth<double>::VALUE + 10);
+ x_exp + y_exp - (FPBits::EXPONENT_BIAS + 2 * FPBits::MANTISSA_WIDTH + 10);
z_mant <<= 64;
- int z_lsb_exp = z_exp - (MantissaWidth<double>::VALUE + 64);
+ int z_lsb_exp = z_exp - (FPBits::MANTISSA_WIDTH + 64);
bool round_bit = false;
bool sticky_bits = false;
bool z_shifted = false;
diff --git a/libc/src/__support/FPUtil/generic/sqrt.h b/libc/src/__support/FPUtil/generic/sqrt.h
index 5bde9589fdc012..cd5ec58bcdbd5f 100644
--- a/libc/src/__support/FPUtil/generic/sqrt.h
+++ b/libc/src/__support/FPUtil/generic/sqrt.h
@@ -37,7 +37,7 @@ template <typename T>
LIBC_INLINE void normalize(int &exponent,
typename FPBits<T>::UIntType &mantissa) {
const int shift = cpp::countl_zero(mantissa) -
- (8 * sizeof(mantissa) - 1 - MantissaWidth<T>::VALUE);
+ (8 * sizeof(mantissa) - 1 - FPBits<T>::MANTISSA_WIDTH);
exponent -= shift;
mantissa <<= shift;
}
@@ -72,7 +72,7 @@ LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, T> sqrt(T x) {
} else {
// IEEE floating points formats.
using UIntType = typename FPBits<T>::UIntType;
- constexpr UIntType ONE = UIntType(1) << MantissaWidth<T>::VALUE;
+ constexpr UIntType ONE = UIntType(1) << FPBits<T>::MANTISSA_WIDTH;
FPBits<T> bits(x);
@@ -147,7 +147,8 @@ LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, T> sqrt(T x) {
// Remove hidden bit and append the exponent field.
x_exp = ((x_exp >> 1) + FPBits<T>::EXPONENT_BIAS);
- y = (y - ONE) | (static_cast<UIntType>(x_exp) << MantissaWidth<T>::VALUE);
+ y = (y - ONE) |
+ (static_cast<UIntType>(x_exp) << FPBits<T>::MANTISSA_WIDTH);
switch (quick_get_round()) {
case FE_TONEAREST:
diff --git a/libc/src/__support/FPUtil/generic/sqrt_80_bit_long_double.h b/libc/src/__support/FPUtil/generic/sqrt_80_bit_long_double.h
index 2f25be54e0bc36..46ca796aeb4b60 100644
--- a/libc/src/__support/FPUtil/generic/sqrt_80_bit_long_double.h
+++ b/libc/src/__support/FPUtil/generic/sqrt_80_bit_long_double.h
@@ -23,7 +23,7 @@ namespace x86 {
LIBC_INLINE void normalize(int &exponent, UInt128 &mantissa) {
const unsigned int shift = static_cast<unsigned int>(
cpp::countl_zero(static_cast<uint64_t>(mantissa)) -
- (8 * sizeof(uint64_t) - 1 - MantissaWidth<long double>::VALUE));
+ (8 * sizeof(uint64_t) - 1 - FPBits<long double>::MANTISSA_WIDTH));
exponent -= shift;
mantissa <<= shift;
}
@@ -36,16 +36,16 @@ LIBC_INLINE long double sqrt(long double x);
// Shift-and-add algorithm.
#if defined(LIBC_LONG_DOUBLE_IS_X86_FLOAT80)
LIBC_INLINE long double sqrt(long double x) {
- using UIntType = typename FPBits<long double>::UIntType;
- constexpr UIntType ONE = UIntType(1)
- << int(MantissaWidth<long double>::VALUE);
+ using LDBits = FPBits<long double>;
+ using UIntType = typename LDBits::UIntType;
+ constexpr UIntType ONE = UIntType(1) << int(LDBits::MANTISSA_WIDTH);
FPBits<long double> bits(x);
if (bits.is_inf_or_nan()) {
if (bits.get_sign() && (bits.get_mantissa() == 0)) {
// sqrt(-Inf) = NaN
- return FPBits<long double>::build_quiet_nan(ONE >> 1);
+ return LDBits::build_quiet_nan(ONE >> 1);
} else {
// sqrt(NaN) = NaN
// sqrt(+Inf) = +Inf
@@ -57,7 +57,7 @@ LIBC_INLINE long double sqrt(long double x) {
return x;
} else if (bits.get_sign()) {
// sqrt( negative numbers ) = NaN
- return FPBits<long double>::build_quiet_nan(ONE >> 1);
+ return LDBits::build_quiet_nan(ONE >> 1);
} else {
int x_exp = bits.get_explicit_exponent();
UIntType x_mant = bits.get_mantissa();
@@ -110,9 +110,8 @@ LIBC_INLINE long double sqrt(long double x) {
}
// Append the exponent field.
- x_exp = ((x_exp >> 1) + FPBits<long double>::EXPONENT_BIAS);
- y |= (static_cast<UIntType>(x_exp)
- << (MantissaWidth<long double>::VALUE + 1));
+ x_exp = ((x_exp >> 1) + LDBits::EXPONENT_BIAS);
+ y |= (static_cast<UIntType>(x_exp) << (LDBits::MANTISSA_WIDTH + 1));
switch (quick_get_round()) {
case FE_TONEAREST:
diff --git a/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h b/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
index f682f171bcab63..a31667528be2b0 100644
--- a/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
+++ b/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
@@ -41,13 +41,11 @@ template <> struct FPBits<long double> : private FloatProperties<long double> {
static constexpr int MAX_EXPONENT = 0x7FFF;
static constexpr UIntType MIN_SUBNORMAL = UIntType(1);
// Subnormal numbers include the implicit bit in x86 long double formats.
- static constexpr UIntType MAX_SUBNORMAL =
- (UIntType(1) << (MantissaWidth<long double>::VALUE)) - 1;
- static constexpr UIntType MIN_NORMAL =
- (UIntType(3) << MantissaWidth<long double>::VALUE);
+ static constexpr UIntType MAX_SUBNORMAL = (UIntType(1) << MANTISSA_WIDTH) - 1;
+ static constexpr UIntType MIN_NORMAL = (UIntType(3) << MANTISSA_WIDTH);
static constexpr UIntType MAX_NORMAL =
- (UIntType(MAX_EXPONENT - 1) << (MantissaWidth<long double>::VALUE + 1)) |
- (UIntType(1) << MantissaWidth<long double>::VALUE) | MAX_SUBNORMAL;
+ (UIntType(MAX_EXPONENT - 1) << (MANTISSA_WIDTH + 1)) |
+ (UIntType(1) << MANTISSA_WIDTH) | MAX_SUBNORMAL;
UIntType bits;
diff --git a/libc/src/__support/FPUtil/x86_64/NextAfterLongDouble.h b/libc/src/__support/FPUtil/x86_64/NextAfterLongDouble.h
index 5e32f766ad5863..653b6a48f3cc53 100644
--- a/libc/src/__support/FPUtil/x86_64/NextAfterLongDouble.h
+++ b/libc/src/__support/FPUtil/x86_64/NextAfterLongDouble.h
@@ -46,7 +46,7 @@ LIBC_INLINE long double nextafter(long double from, long double to) {
using UIntType = FPBits::UIntType;
constexpr UIntType SIGN_VAL = (UIntType(1) << 79);
constexpr UIntType MANTISSA_MASK =
- (UIntType(1) << MantissaWidth<long double>::VALUE) - 1;
+ (UIntType(1) << FPBits::MANTISSA_WIDTH) - 1;
UIntType int_val = from_bits.uintval();
if (from < 0.0l) {
if (from > to) {
@@ -117,8 +117,7 @@ LIBC_INLINE long double nextafter(long double from, long double to) {
}
}
- UIntType implicit_bit =
- int_val & (UIntType(1) << MantissaWidth<long double>::VALUE);
+ UIntType implicit_bit = int_val & (UIntType(1) << FPBits::MANTISSA_WIDTH);
if (implicit_bit == UIntType(0))
raise_except_if_required(FE_UNDERFLOW | FE_INEXACT);
diff --git a/libc/src/__support/float_to_string.h b/libc/src/__support/float_to_string.h
index be105830a91ac1..d53bb4b4c62b15 100644
--- a/libc/src/__support/float_to_string.h
+++ b/libc/src/__support/float_to_string.h
@@ -416,7 +416,7 @@ class FloatToString {
int exponent;
FloatProp::UIntType mantissa;
- static constexpr int MANT_WIDTH = fputil::MantissaWidth<T>::VALUE;
+ static constexpr int MANT_WIDTH = fputil::FPBits<T>::MANTISSA_WIDTH;
static constexpr int EXP_BIAS = fputil::FPBits<T>::EXPONENT_BIAS;
public:
diff --git a/libc/src/math/generic/asinf.cpp b/libc/src/math/generic/asinf.cpp
index f40a08e752ed39..5406e6660d7841 100644
--- a/libc/src/math/generic/asinf.cpp
+++ b/libc/src/math/generic/asinf.cpp
@@ -108,8 +108,7 @@ LLVM_LIBC_FUNCTION(float, asinf, (float x)) {
fputil::set_errno_if_required(EDOM);
fputil::raise_except_if_required(FE_INVALID);
}
- return x +
- FPBits::build_nan(1 << (fputil::MantissaWidth<float>::VALUE - 1));
+ return x + FPBits::build_nan(1 << (FPBits::MANTISSA_WIDTH - 1));
}
// Check for exceptional values
diff --git a/libc/src/math/generic/hypotf.cpp b/libc/src/math/generic/hypotf.cpp
index 389de3c450299d..5795291a043ae6 100644
--- a/libc/src/math/generic/hypotf.cpp
+++ b/libc/src/math/generic/hypotf.cpp
@@ -23,7 +23,7 @@ LLVM_LIBC_FUNCTION(float, hypotf, (float x, float y)) {
uint16_t y_exp = y_bits.get_biased_exponent();
uint16_t exp_diff = (x_exp > y_exp) ? (x_exp - y_exp) : (y_exp - x_exp);
- if (exp_diff >= fputil::MantissaWidth<float>::VALUE + 2) {
+ if (exp_diff >= FPBits::MANTISSA_WIDTH + 2) {
return fputil::abs(x) + fputil::abs(y);
}
diff --git a/libc/src/math/generic/log1pf.cpp b/libc/src/math/generic/log1pf.cpp
index fd3cf4647fd0da..ee11ae60f2013c 100644
--- a/libc/src/math/generic/log1pf.cpp
+++ b/libc/src/math/generic/log1pf.cpp
@@ -57,7 +57,7 @@ LIBC_INLINE float log(double x) {
// Get the 8 highest bits, use 7 bits (excluding the implicit hidden bit) for
// lookup tables.
int f_index = static_cast<int>(
- xbits.get_mantissa() >> 45); // fputil::MantissaWidth<double>::VALUE - 7
+ xbits.get_mantissa() >> 45); // fputil::FPBits<double>::MANTISSA_WIDTH - 7
// Set bits to 1.m
xbits.set_biased_exponent(0x3FF);
diff --git a/libc/src/math/generic/sincosf.cpp b/libc/src/math/generic/sincosf.cpp
index 7ed1c4381078b1..c5694fd1bea580 100644
--- a/libc/src/math/generic/sincosf.cpp
+++ b/libc/src/math/generic/sincosf.cpp
@@ -148,8 +148,7 @@ LLVM_LIBC_FUNCTION(void, sincosf, (float x, float *sinp, float *cosp)) {
fputil::set_errno_if_required(EDOM);
fputil::raise_except_if_required(FE_INVALID);
}
- *sinp =
- x + FPBits::build_nan(1 << (fputil::MantissaWidth<float>::VALUE - 1));
+ *sinp = x + FPBits::build_nan(1 << (FPBits::MANTISSA_WIDTH - 1));
*cosp = *sinp;
return;
}
diff --git a/libc/src/stdio/printf_core/float_dec_converter.h b/libc/src/stdio/printf_core/float_dec_converter.h
index ca522710040696..98b573646f7c31 100644
--- a/libc/src/stdio/printf_core/float_dec_converter.h
+++ b/libc/src/stdio/printf_core/float_dec_converter.h
@@ -478,7 +478,7 @@ LIBC_INLINE int convert_float_decimal_typed(Writer *writer,
const FormatSection &to_conv,
fputil::FPBits<T> float_bits) {
// signed because later we use -MANT_WIDTH
- constexpr int32_t MANT_WIDTH = fputil::MantissaWidth<T>::VALUE;
+ constexpr int32_t MANT_WIDTH = fputil::FloatProperties<T>::MANTISSA_WIDTH;
bool is_negative = float_bits.get_sign();
int exponent = float_bits.get_explicit_exponent();
@@ -591,7 +591,7 @@ LIBC_INLINE int convert_float_dec_exp_typed(Writer *writer,
const FormatSection &to_conv,
fputil::FPBits<T> float_bits) {
// signed because later we use -MANT_WIDTH
- constexpr int32_t MANT_WIDTH = fputil::MantissaWidth<T>::VALUE;
+ constexpr int32_t MANT_WIDTH = fputil::FloatProperties<T>::MANTISSA_WIDTH;
bool is_negative = float_bits.get_sign();
int exponent = float_bits.get_explicit_exponent();
MantissaInt mantissa = float_bits.get_explicit_mantissa();
@@ -754,7 +754,7 @@ LIBC_INLINE int convert_float_dec_auto_typed(Writer *writer,
const FormatSection &to_conv,
fputil::FPBits<T> float_bits) {
// signed because later we use -MANT_WIDTH
- constexpr int32_t MANT_WIDTH = fputil::MantissaWidth<T>::VALUE;
+ constexpr int32_t MANT_WIDTH = fputil::FloatProperties<T>::MANTISSA_WIDTH;
bool is_negative = float_bits.get_sign();
int exponent = float_bits.get_explicit_exponent();
MantissaInt mantissa = float_bits.get_explicit_mantissa();
diff --git a/libc/src/stdio/printf_core/float_hex_converter.h b/libc/src/stdio/printf_core/float_hex_converter.h
index 1bc07180295d98..cb10e219388be8 100644
--- a/libc/src/stdio/printf_core/float_hex_converter.h
+++ b/libc/src/stdio/printf_core/float_hex_converter.h
@@ -87,7 +87,7 @@ LIBC_INLINE int convert_float_hex_exp(Writer *writer,
// for the extra implicit bit. We use the larger of the two possible values
// since the size must be constant.
constexpr size_t MANT_BUFF_LEN =
- (fputil::MantissaWidth<long double>::VALUE / BITS_IN_HEX_DIGIT) + 1;
+ (LDBits::MANTISSA_WIDTH / BITS_IN_HEX_DIGIT) + 1;
char mant_buffer[MANT_BUFF_LEN];
size_t mant_len = (mantissa_width / BITS_IN_HEX_DIGIT) + 1;
diff --git a/libc/test/src/math/FrexpTest.h b/libc/test/src/math/FrexpTest.h
index 19c98872e04116..2f93987b1ea75f 100644
--- a/libc/test/src/math/FrexpTest.h
+++ b/libc/test/src/math/FrexpTest.h
@@ -20,7 +20,7 @@ template <typename T> class FrexpTest : public LIBC_NAMESPACE::testing::Test {
DECLARE_SPECIAL_CONSTANTS(T)
static constexpr UIntType HIDDEN_BIT =
- UIntType(1) << LIBC_NAMESPACE::fputil::MantissaWidth<T>::VALUE;
+ UIntType(1) << LIBC_NAMESPACE::fputil::FloatProperties<T>::MANTISSA_WIDTH;
public:
typedef T (*FrexpFunc)(T, int *);
diff --git a/libc/test/src/math/LdExpTest.h b/libc/test/src/math/LdExpTest.h
index ce0896ec2f4c25..5ebba62a3dbb8a 100644
--- a/libc/test/src/math/LdExpTest.h
+++ b/libc/test/src/math/LdExpTest.h
@@ -23,8 +23,7 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::Test {
using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
using NormalFloat = LIBC_NAMESPACE::fputil::NormalFloat<T>;
using UIntType = typename FPBits::UIntType;
- static constexpr UIntType MANTISSA_WIDTH =
- LIBC_NAMESPACE::fputil::MantissaWidth<T>::VALUE;
+ static constexpr UIntType MANTISSA_WIDTH = FPBits::MANTISSA_WIDTH;
// A normalized mantissa to be used with tests.
static constexpr UIntType MANTISSA = NormalFloat::ONE + 0x1234;
diff --git a/libc/test/src/math/LogbTest.h b/libc/test/src/math/LogbTest.h
index c2cf4f1f148333..3912385623ef19 100644
--- a/libc/test/src/math/LogbTest.h
+++ b/libc/test/src/math/LogbTest.h
@@ -20,7 +20,7 @@ template <typename T> class LogbTest : public LIBC_NAMESPACE::testing::Test {
DECLARE_SPECIAL_CONSTANTS(T)
static constexpr UIntType HIDDEN_BIT =
- UIntType(1) << LIBC_NAMESPACE::fputil::MantissaWidth<T>::VALUE;
+ UIntType(1) << LIBC_NAMESPACE::fputil::FloatProperties<T>::MANTISSA_WIDTH;
public:
typedef T (*LogbFunc)(T);
diff --git a/libc/test/src/math/NextAfterTest.h b/libc/test/src/math/NextAfterTest.h
index 57a801dfb28a31..ca34753871b960 100644
--- a/libc/test/src/math/NextAfterTest.h
+++ b/libc/test/src/math/NextAfterTest.h
@@ -20,7 +20,6 @@
template <typename T>
class NextAfterTestTemplate : public LIBC_NAMESPACE::testing::Test {
using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
- using MantissaWidth = LIBC_NAMESPACE::fputil::MantissaWidth<T>;
using UIntType = typename FPBits::UIntType;
static constexpr int BIT_WIDTH_OF_TYPE =
@@ -165,7 +164,7 @@ class NextAfterTestTemplate : public LIBC_NAMESPACE::testing::Test {
ASSERT_EQ(result_bits.get_biased_exponent(),
uint16_t(x_bits.get_biased_exponent() - 1));
ASSERT_EQ(result_bits.get_mantissa(),
- (UIntType(1) << MantissaWidth::VALUE) - 1);
+ (UIntType(1) << FPBits::MANTISSA_WIDTH) - 1);
result = func(x, T(33.0));
result_bits = FPBits(result);
@@ -179,7 +178,7 @@ class NextAfterTestTemplate : public LIBC_NAMESPACE::testing::Test {
ASSERT_EQ(result_bits.get_biased_exponent(),
uint16_t(x_bits.get_biased_exponent() - 1));
ASSERT_EQ(result_bits.get_mantissa(),
- (UIntType(1) << MantissaWidth::VALUE) - 1);
+ (UIntType(1) << FPBits::MANTISSA_WIDTH) - 1);
result = func(x, T(-33.0));
result_bits = FPBits(result);
diff --git a/libc/test/src/math/RoundToIntegerTest.h b/libc/test/src/math/RoundToIntegerTest.h
index 1a976e97359eb7..feee040c72f88d 100644
--- a/libc/test/src/math/RoundToIntegerTest.h
+++ b/libc/test/src/math/RoundToIntegerTest.h
@@ -32,11 +32,11 @@ class RoundToIntegerTestTemplate : public LIBC_NAMESPACE::testing::Test {
using FPBits = LIBC_NAMESPACE::fputil::FPBits<F>;
using UIntType = typename FPBits::UIntType;
- const F zero = F(LIBC_NAMESPACE::fputil::FPBits<F>::zero());
- const F neg_zero = F(LIBC_NAMESPACE::fputil::FPBits<F>::neg_zero());
- const F inf = F(LIBC_NAMESPACE::fputil::FPBits<F>::inf());
- const F neg_inf = F(LIBC_NAMESPACE::fputil::FPBits<F>::neg_inf());
- const F nan = F(LIBC_NAMESPACE::fputil::FPBits<F>::build_quiet_nan(1));
+ const F zero = F(FPBits::zero());
+ const F neg_zero = F(FPBits::neg_zero());
+ const F inf = F(FPBits::inf());
+ const F neg_inf = F(FPBits::neg_inf());
+ const F nan = F(FPBits::build_quiet_nan(1));
static constexpr I INTEGER_MIN = I(1) << (sizeof(I) * 8 - 1);
static constexpr I INTEGER_MAX = -(INTEGER_MIN + 1);
@@ -192,8 +192,7 @@ class RoundToIntegerTestTemplate : public LIBC_NAMESPACE::testing::Test {
FPBits bits(F(1.0));
bits.set_biased_exponent(EXPONENT_LIMIT + FPBits::EXPONENT_BIAS);
bits.set_sign(1);
- bits.set_mantissa(UIntType(0x1)
- << (LIBC_NAMESPACE::fputil::MantissaWidth<F>::VALUE - 1));
+ bits.set_mantissa(UIntType(0x1) << (FPBits::MANTISSA_WIDTH - 1));
F x = F(bits);
if (TestModes) {
diff --git a/libc/test/src/math/SqrtTest.h b/libc/test/src/math/SqrtTest.h
index 24f14b78d2f0f5..ab14e30d3ded6f 100644
--- a/libc/test/src/math/SqrtTest.h
+++ b/libc/test/src/math/SqrtTest.h
@@ -20,7 +20,7 @@ template <typename T> class SqrtTest : public LIBC_NAMESPACE::testing::Test {
DECLARE_SPECIAL_CONSTANTS(T)
static constexpr UIntType HIDDEN_BIT =
- UIntType(1) << LIBC_NAMESPACE::fputil::MantissaWidth<T>::VALUE;
+ UIntType(1) << LIBC_NAMESPACE::fputil::FloatProperties<T>::MANTISSA_WIDTH;
public:
typedef T (*SqrtFunc)(T);
diff --git a/libc/test/src/math/smoke/FrexpTest.h b/libc/test/src/math/smoke/FrexpTest.h
index 3ff169091cc9a3..643a151d89c7da 100644
--- a/libc/test/src/math/smoke/FrexpTest.h
+++ b/libc/test/src/math/smoke/FrexpTest.h
@@ -17,7 +17,7 @@ template <typename T> class FrexpTest : public LIBC_NAMESPACE::testing::Test {
DECLARE_SPECIAL_CONSTANTS(T)
static constexpr UIntType HIDDEN_BIT =
- UIntType(1) << LIBC_NAMESPACE::fputil::MantissaWidth<T>::VALUE;
+ UIntType(1) << LIBC_NAMESPACE::fputil::FloatProperties<T>::MANTISSA_WIDTH;
public:
typedef T (*FrexpFunc)(T, int *);
diff --git a/libc/test/src/math/smoke/LdExpTest.h b/libc/test/src/math/smoke/LdExpTest.h
index ce0896ec2f4c25..5ebba62a3dbb8a 100644
--- a/libc/test/src/math/smoke/LdExpTest.h
+++ b/libc/test/src/math/smoke/LdExpTest.h
@@ -23,8 +23,7 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::Test {
using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
using NormalFloat = LIBC_NAMESPACE::fputil::NormalFloat<T>;
using UIntType = typename FPBits::UIntType;
- static constexpr UIntType MANTISSA_WIDTH =
- LIBC_NAMESPACE::fputil::MantissaWidth<T>::VALUE;
+ static constexpr UIntType MANTISSA_WIDTH = FPBits::MANTISSA_WIDTH;
// A normalized mantissa to be used with tests.
static constexpr UIntType MANTISSA = NormalFloat::ONE + 0x1234;
diff --git a/libc/test/src/math/smoke/LogbTest.h b/libc/test/src/math/smoke/LogbTest.h
index 34cf92c19ed8b1..41b356fe0524e6 100644
--- a/libc/test/src/math/smoke/LogbTest.h
+++ b/libc/test/src/math/smoke/LogbTest.h
@@ -17,7 +17,7 @@ template <typename T> class LogbTest : public LIBC_NAMESPACE::testing::Test {
DECLARE_SPECIAL_CONSTANTS(T)
static constexpr UIntType HIDDEN_BIT =
- UIntType(1) << LIBC_NAMESPACE::fputil::MantissaWidth<T>::VALUE;
+ UIntType(1) << LIBC_NAMESPACE::fputil::FloatProperties<T>::MANTISSA_WIDTH;
public:
typedef T (*LogbFunc)(T);
diff --git a/libc/test/src/math/smoke/NextAfterTest.h b/libc/test/src/math/smoke/NextAfterTest.h
index 29098e0f49a457..90155a8b293f84 100644
--- a/libc/test/src/math/smoke/NextAfterTest.h
+++ b/libc/test/src/math/smoke/NextAfterTest.h
@@ -31,7 +31,6 @@
template <typename T>
class NextAfterTestTemplate : public LIBC_NAMESPACE::testing::Test {
using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
- using MantissaWidth = LIBC_NAMESPACE::fputil::MantissaWidth<T>;
using UIntType = typename FPBits::UIntType;
static constexpr int BIT_WIDTH_OF_TYPE =
@@ -176,7 +175,7 @@ class NextAfterTestTemplate : public LIBC_NAMESPACE::testing::Test {
ASSERT_EQ(result_bits.get_biased_exponent(),
uint16_t(x_bits.get_biased_exponent() - 1));
ASSERT_EQ(result_bits.get_mantissa(),
- (UIntType(1) << MantissaWidth::VALUE) - 1);
+ (UIntType(1) << FPBits::MANTISSA_WIDTH) - 1);
result = func(x, T(33.0));
result_bits = FPBits(result);
@@ -190,7 +189,7 @@ class NextAfterTestTemplate : public LIBC_NAMESPACE::testing::Test {
ASSERT_EQ(result_bits.get_biased_exponent(),
uint16_t(x_bits.get_biased_exponent() - 1));
ASSERT_EQ(result_bits.get_mantissa(),
- (UIntType(1) << MantissaWidth::VALUE) - 1);
+ (UIntType(1) << FPBits::MANTISSA_WIDTH) - 1);
result = func(x, T(-33.0));
result_bits = FPBits(result);
diff --git a/libc/test/src/math/smoke/NextTowardTest.h b/libc/test/src/math/smoke/NextTowardTest.h
index 111d8017e691d3..bd719275a41552 100644
--- a/libc/test/src/math/smoke/NextTowardTest.h
+++ b/libc/test/src/math/smoke/NextTowardTest.h
@@ -33,7 +33,6 @@ template <typename T>
class NextTowardTestTemplate : public LIBC_NAMESPACE::testing::Test {
using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
using ToFPBits = LIBC_NAMESPACE::fputil::FPBits<long double>;
- using MantissaWidth = LIBC_NAMESPACE::fputil::MantissaWidth<T>;
using UIntType = typename FPBits::UIntType;
static constexpr int BIT_WIDTH_OF_TYPE =
@@ -190,7 +189,7 @@ class NextTowardTestTemplate : public LIBC_NAMESPACE::testing::Test {
ASSERT_EQ(result_bits.get_biased_exponent(),
uint16_t(x_bits.get_biased_exponent() - 1));
ASSERT_EQ(result_bits.get_mantissa(),
- (UIntType(1) << MantissaWidth::VALUE) - 1);
+ (UIntType(1) << FPBits::MANTISSA_WIDTH) - 1);
result = func(x, 33.0);
result_bits = FPBits(result);
@@ -204,7 +203,7 @@ class NextTowardTestTemplate : public LIBC_NAMESPACE::testing::Test {
ASSERT_EQ(result_bits.get_biased_exponent(),
uint16_t(x_bits.get_biased_exponent() - 1));
ASSERT_EQ(result_bits.get_mantissa(),
- (UIntType(1) << MantissaWidth::VALUE) - 1);
+ (UIntType(1) << FPBits::MANTISSA_WIDTH) - 1);
result = func(x, -33.0);
result_bits = FPBits(result);
diff --git a/libc/test/src/math/smoke/SqrtTest.h b/libc/test/src/math/smoke/SqrtTest.h
index d4b2f9dd2624f1..dffff520cd69e8 100644
--- a/libc/test/src/math/smoke/SqrtTest.h
+++ b/libc/test/src/math/smoke/SqrtTest.h
@@ -17,7 +17,7 @@ template <typename T> class SqrtTest : public LIBC_NAMESPACE::testing::Test {
DECLARE_SPECIAL_CONSTANTS(T)
static constexpr UIntType HIDDEN_BIT =
- UIntType(1) << LIBC_NAMESPACE::fputil::MantissaWidth<T>::VALUE;
+ UIntType(1) << LIBC_NAMESPACE::fputil::FloatProperties<T>::MANTISSA_WIDTH;
public:
typedef T (*SqrtFunc)(T);
diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp
index e3dffadf4ed967..68146dff64c4e3 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.cpp
+++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp
@@ -468,7 +468,7 @@ class MPFRNumber {
mpfr_sub(inputMPFR.value, value, inputMPFR.value, MPFR_RNDN);
mpfr_abs(inputMPFR.value, inputMPFR.value, MPFR_RNDN);
mpfr_mul_2si(inputMPFR.value, inputMPFR.value,
- -thisExponent + int(fputil::MantissaWidth<T>::VALUE),
+ -thisExponent + int(fputil::FPBits<T>::MANTISSA_WIDTH),
MPFR_RNDN);
return inputMPFR;
}
@@ -496,12 +496,12 @@ class MPFRNumber {
mpfr_sub(minMPFR.value, pivot.value, minMPFR.value, MPFR_RNDN);
mpfr_mul_2si(minMPFR.value, minMPFR.value,
- -minExponent + int(fputil::MantissaWidth<T>::VALUE),
+ -minExponent + int(fputil::FPBits<T>::MANTISSA_WIDTH),
MPFR_RNDN);
mpfr_sub(maxMPFR.value, maxMPFR.value, pivot.value, MPFR_RNDN);
mpfr_mul_2si(maxMPFR.value, maxMPFR.value,
- -maxExponent + int(fputil::MantissaWidth<T>::VALUE),
+ -maxExponent + int(fputil::FPBits<T>::MANTISSA_WIDTH),
MPFR_RNDN);
mpfr_add(minMPFR.value, minMPFR.value, maxMPFR.value, MPFR_RNDN);
>From 7f76588f9d1d66119da14e3f5884a88399430f93 Mon Sep 17 00:00:00 2001
From: Guillaume Chatelet <gchatelet at google.com>
Date: Thu, 14 Dec 2023 13:59:51 +0000
Subject: [PATCH 2/2] replace computed constant with its definition
---
libc/src/math/generic/log1pf.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libc/src/math/generic/log1pf.cpp b/libc/src/math/generic/log1pf.cpp
index ee11ae60f2013c..fabd2bdc31bf3b 100644
--- a/libc/src/math/generic/log1pf.cpp
+++ b/libc/src/math/generic/log1pf.cpp
@@ -56,8 +56,8 @@ LIBC_INLINE float log(double x) {
// Get the 8 highest bits, use 7 bits (excluding the implicit hidden bit) for
// lookup tables.
- int f_index = static_cast<int>(
- xbits.get_mantissa() >> 45); // fputil::FPBits<double>::MANTISSA_WIDTH - 7
+ int f_index = static_cast<int>(xbits.get_mantissa() >>
+ (fputil::FPBits<double>::MANTISSA_WIDTH - 7));
// Set bits to 1.m
xbits.set_biased_exponent(0x3FF);
More information about the libc-commits
mailing list