-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc][NFC] Remove ExponentWidth traits #75362
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
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Is it redundant with FloatProperties::EXPONENT_WIDTH.
@llvm/pr-subscribers-libc Author: Guillaume Chatelet (gchatelet) ChangesIs it redundant with FloatProperties::EXPONENT_WIDTH. Full diff: https://github.com/llvm/llvm-project/pull/75362.diff 3 Files Affected:
diff --git a/libc/src/__support/FPUtil/FPBits.h b/libc/src/__support/FPUtil/FPBits.h
index bef166e14d72b2..a318c6a9fd235f 100644
--- a/libc/src/__support/FPUtil/FPBits.h
+++ b/libc/src/__support/FPUtil/FPBits.h
@@ -24,10 +24,6 @@ template <typename T> struct MantissaWidth {
static constexpr unsigned VALUE = FloatProperties<T>::MANTISSA_WIDTH;
};
-template <typename T> struct ExponentWidth {
- static constexpr unsigned VALUE = FloatProperties<T>::EXPONENT_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/NormalFloat.h b/libc/src/__support/FPUtil/NormalFloat.h
index d3236316a87995..77fcd85017735a 100644
--- a/libc/src/__support/FPUtil/NormalFloat.h
+++ b/libc/src/__support/FPUtil/NormalFloat.h
@@ -92,7 +92,7 @@ template <typename T> struct NormalFloat {
LIBC_INLINE operator T() const {
int biased_exponent = exponent + FPBits<T>::EXPONENT_BIAS;
// Max exponent is of the form 0xFF...E. That is why -2 and not -1.
- constexpr int MAX_EXPONENT_VALUE = (1 << ExponentWidth<T>::VALUE) - 2;
+ constexpr int MAX_EXPONENT_VALUE = (1 << FPBits<T>::EXPONENT_WIDTH) - 2;
if (biased_exponent > MAX_EXPONENT_VALUE) {
return sign ? T(FPBits<T>::neg_inf()) : T(FPBits<T>::inf());
}
@@ -208,18 +208,18 @@ NormalFloat<long double>::init_from_bits(FPBits<long double> bits) {
}
template <> LIBC_INLINE NormalFloat<long double>::operator long double() const {
- int biased_exponent = exponent + FPBits<long double>::EXPONENT_BIAS;
+ using LDBits = FPBits<long double>;
+ int biased_exponent = exponent + LDBits::EXPONENT_BIAS;
// Max exponent is of the form 0xFF...E. That is why -2 and not -1.
- constexpr int MAX_EXPONENT_VALUE =
- (1 << ExponentWidth<long double>::VALUE) - 2;
+ constexpr int MAX_EXPONENT_VALUE = (1 << LDBits::EXPONENT_WIDTH) - 2;
if (biased_exponent > MAX_EXPONENT_VALUE) {
- return sign ? FPBits<long double>::neg_inf() : FPBits<long double>::inf();
+ return sign ? LDBits::neg_inf() : LDBits::inf();
}
FPBits<long double> result(0.0l);
result.set_sign(sign);
- constexpr int SUBNORMAL_EXPONENT = -FPBits<long double>::EXPONENT_BIAS + 1;
+ constexpr int SUBNORMAL_EXPONENT = -LDBits::EXPONENT_BIAS + 1;
if (exponent < SUBNORMAL_EXPONENT) {
unsigned shift = SUBNORMAL_EXPONENT - exponent;
if (shift <= MantissaWidth<long double>::VALUE + 1) {
diff --git a/libc/src/stdio/printf_core/float_hex_converter.h b/libc/src/stdio/printf_core/float_hex_converter.h
index 1f105492e8e5a3..1bc07180295d98 100644
--- a/libc/src/stdio/printf_core/float_hex_converter.h
+++ b/libc/src/stdio/printf_core/float_hex_converter.h
@@ -25,10 +25,10 @@
namespace LIBC_NAMESPACE {
namespace printf_core {
-using MantissaInt = fputil::FPBits<long double>::UIntType;
-
LIBC_INLINE int convert_float_hex_exp(Writer *writer,
const FormatSection &to_conv) {
+ using LDBits = fputil::FPBits<long double>;
+ using MantissaInt = LDBits::UIntType;
// All of the letters will be defined relative to variable a, which will be
// the appropriate case based on the name of the conversion. This converts any
// conversion name into the letter 'a' with the appropriate case.
@@ -40,18 +40,19 @@ LIBC_INLINE int convert_float_hex_exp(Writer *writer,
bool is_inf_or_nan;
uint32_t mantissa_width;
if (to_conv.length_modifier == LengthModifier::L) {
- mantissa_width = fputil::MantissaWidth<long double>::VALUE;
- fputil::FPBits<long double>::UIntType float_raw = to_conv.conv_val_raw;
- fputil::FPBits<long double> float_bits(float_raw);
+ mantissa_width = LDBits::MANTISSA_WIDTH;
+ LDBits::UIntType float_raw = to_conv.conv_val_raw;
+ LDBits float_bits(float_raw);
is_negative = float_bits.get_sign();
exponent = float_bits.get_explicit_exponent();
mantissa = float_bits.get_explicit_mantissa();
is_inf_or_nan = float_bits.is_inf_or_nan();
} else {
- mantissa_width = fputil::MantissaWidth<double>::VALUE;
- fputil::FPBits<double>::UIntType float_raw =
- static_cast<fputil::FPBits<double>::UIntType>(to_conv.conv_val_raw);
- fputil::FPBits<double> float_bits(float_raw);
+ using LBits = fputil::FPBits<double>;
+ mantissa_width = LBits::MANTISSA_WIDTH;
+ LBits::UIntType float_raw =
+ static_cast<LBits::UIntType>(to_conv.conv_val_raw);
+ LBits float_bits(float_raw);
is_negative = float_bits.get_sign();
exponent = float_bits.get_explicit_exponent();
mantissa = float_bits.get_explicit_mantissa();
@@ -157,8 +158,7 @@ LIBC_INLINE int convert_float_hex_exp(Writer *writer,
// 15 -> 5
// 11 -> 4
// 8 -> 3
- constexpr size_t EXP_LEN =
- (((fputil::ExponentWidth<long double>::VALUE * 5) + 15) / 16) + 1;
+ constexpr size_t EXP_LEN = (((LDBits::EXPONENT_WIDTH * 5) + 15) / 16) + 1;
char exp_buffer[EXP_LEN];
bool exp_is_negative = false;
|
nickdesaulniers
approved these changes
Dec 13, 2023
lntue
approved these changes
Dec 13, 2023
michaelrj-google
approved these changes
Dec 13, 2023
gchatelet
added a commit
to gchatelet/llvm-project
that referenced
this pull request
Dec 14, 2023
Same as llvm#75362, the traits does not bring a lot of value over `FloatProperties::MANTISSA_WIDTH` (or `FPBits::MANTISSA_WIDTH`).
gchatelet
added a commit
that referenced
this pull request
Dec 14, 2023
Same as #75362, the traits does not bring a lot of value over `FloatProperties::MANTISSA_WIDTH` (or `FPBits::MANTISSA_WIDTH`).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Is it redundant with
FloatProperties::EXPONENT_WIDTH
and does bear its weight.