-
Notifications
You must be signed in to change notification settings - Fork 12k
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] Fix is_subnormal for Intel Extended Precision #78592
Merged
Merged
Conversation
This file contains 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
Also turn a set of `get_biased_exponent() == 0` into `is_subnormal()` which is clearer.
@llvm/pr-subscribers-libc Author: Guillaume Chatelet (gchatelet) ChangesAlso turn a set of Full diff: https://github.com/llvm/llvm-project/pull/78592.diff 7 Files Affected:
diff --git a/libc/src/__support/FPUtil/FPBits.h b/libc/src/__support/FPUtil/FPBits.h
index 3ee6289b749648..e6c0f91c878f75 100644
--- a/libc/src/__support/FPUtil/FPBits.h
+++ b/libc/src/__support/FPUtil/FPBits.h
@@ -559,8 +559,7 @@ struct FPRep<FPType::X86_Binary80> : public FPRepBase<FPType::X86_Binary80> {
}
LIBC_INLINE
constexpr bool is_subnormal() const {
- return exp_sig_bits() >
- encode(BiasedExponent::BITS_ALL_ZEROES(), Significand::ZERO());
+ return exp_bits() == encode(BiasedExponent::BITS_ALL_ZEROES());
}
LIBC_INLINE constexpr bool is_normal() const {
const auto exp = exp_bits();
diff --git a/libc/src/__support/FPUtil/NormalFloat.h b/libc/src/__support/FPUtil/NormalFloat.h
index 8b1612e1b47c61..e7abc53ce6129c 100644
--- a/libc/src/__support/FPUtil/NormalFloat.h
+++ b/libc/src/__support/FPUtil/NormalFloat.h
@@ -153,7 +153,7 @@ template <typename T> struct NormalFloat {
}
// Normalize subnormal numbers.
- if (bits.get_biased_exponent() == 0) {
+ if (bits.is_subnormal()) {
unsigned shift = evaluate_normalization_shift(bits.get_mantissa());
mantissa = StorageType(bits.get_mantissa()) << shift;
exponent = 1 - FPBits<T>::EXP_BIAS - shift;
@@ -186,7 +186,7 @@ NormalFloat<long double>::init_from_bits(FPBits<long double> bits) {
return;
}
- if (bits.get_biased_exponent() == 0) {
+ if (bits.is_subnormal()) {
if (bits.get_implicit_bit() == 0) {
// Since we ignore zero value, the mantissa in this case is non-zero.
int normalization_shift =
diff --git a/libc/src/__support/FPUtil/generic/FMA.h b/libc/src/__support/FPUtil/generic/FMA.h
index 9c67c645d5243d..6285cac1983d1e 100644
--- a/libc/src/__support/FPUtil/generic/FMA.h
+++ b/libc/src/__support/FPUtil/generic/FMA.h
@@ -104,15 +104,15 @@ template <> LIBC_INLINE double fma<double>(double x, double y, double z) {
int z_exp = 0;
// Normalize denormal inputs.
- if (LIBC_UNLIKELY(FPBits(x).get_biased_exponent() == 0)) {
+ if (LIBC_UNLIKELY(FPBits(x).is_subnormal())) {
x_exp -= 52;
x *= 0x1.0p+52;
}
- if (LIBC_UNLIKELY(FPBits(y).get_biased_exponent() == 0)) {
+ if (LIBC_UNLIKELY(FPBits(y).is_subnormal())) {
y_exp -= 52;
y *= 0x1.0p+52;
}
- if (LIBC_UNLIKELY(FPBits(z).get_biased_exponent() == 0)) {
+ if (LIBC_UNLIKELY(FPBits(z).is_subnormal())) {
z_exp -= 52;
z *= 0x1.0p+52;
}
diff --git a/libc/src/__support/FPUtil/generic/sqrt.h b/libc/src/__support/FPUtil/generic/sqrt.h
index f273b678edf527..21ae9d081d3f12 100644
--- a/libc/src/__support/FPUtil/generic/sqrt.h
+++ b/libc/src/__support/FPUtil/generic/sqrt.h
@@ -97,7 +97,7 @@ LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, T> sqrt(T x) {
StorageType x_mant = bits.get_mantissa();
// Step 1a: Normalize denormal input and append hidden bit to the mantissa
- if (bits.get_biased_exponent() == 0) {
+ if (bits.is_subnormal()) {
++x_exp; // let x_exp be the correct exponent of ONE bit.
internal::normalize<T>(x_exp, x_mant);
} else {
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 4fe9b49ff41cf0..4f8d136938f56e 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
@@ -65,7 +65,7 @@ LIBC_INLINE long double sqrt(long double x) {
// Step 1a: Normalize denormal input
if (bits.get_implicit_bit()) {
x_mant |= ONE;
- } else if (bits.get_biased_exponent() == 0) {
+ } else if (bits.is_subnormal()) {
normalize(x_exp, x_mant);
}
diff --git a/libc/src/__support/FPUtil/x86_64/NextAfterLongDouble.h b/libc/src/__support/FPUtil/x86_64/NextAfterLongDouble.h
index 5f15bac5df77f8..6e3cc1d1834d79 100644
--- a/libc/src/__support/FPUtil/x86_64/NextAfterLongDouble.h
+++ b/libc/src/__support/FPUtil/x86_64/NextAfterLongDouble.h
@@ -39,7 +39,7 @@ LIBC_INLINE long double nextafter(long double from, long double to) {
// Convert pseudo subnormal number to normal number.
if (from_bits.get_implicit_bit() == 1 &&
- from_bits.get_biased_exponent() == 0) {
+ from_bits.is_subnormal()) {
from_bits.set_biased_exponent(1);
}
diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp
index fca83c4cdc52f1..b6ca525db6cf74 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.cpp
+++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp
@@ -457,9 +457,9 @@ class MPFRNumber {
int thisExponent = FPBits<T>(thisAsT).get_exponent();
int inputExponent = FPBits<T>(input).get_exponent();
// Adjust the exponents for denormal numbers.
- if (FPBits<T>(thisAsT).get_biased_exponent() == 0)
+ if (FPBits<T>(thisAsT).is_subnormal())
++thisExponent;
- if (FPBits<T>(input).get_biased_exponent() == 0)
+ if (FPBits<T>(input).is_subnormal())
++inputExponent;
if (thisAsT * input < 0 || thisExponent == inputExponent) {
@@ -481,9 +481,9 @@ class MPFRNumber {
int minExponent = FPBits<T>(min).get_exponent();
int maxExponent = FPBits<T>(max).get_exponent();
// Adjust the exponents for denormal numbers.
- if (FPBits<T>(min).get_biased_exponent() == 0)
+ if (FPBits<T>(min).is_subnormal())
++minExponent;
- if (FPBits<T>(max).get_biased_exponent() == 0)
+ if (FPBits<T>(max).is_subnormal())
++maxExponent;
MPFRNumber minMPFR(min);
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
legrosbuffle
approved these changes
Jan 18, 2024
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.
Also turn a set of
get_biased_exponent() == 0
intois_subnormal()
which is clearer.