-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc][NFC] Use Sign in NormalFloat #78579
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
@llvm/pr-subscribers-libc Author: Guillaume Chatelet (gchatelet) ChangesFull diff: https://github.com/llvm/llvm-project/pull/78579.diff 4 Files Affected:
diff --git a/libc/src/__support/FPUtil/DivisionAndRemainderOperations.h b/libc/src/__support/FPUtil/DivisionAndRemainderOperations.h
index 1c09e7906165f5..1798310c3e31e3 100644
--- a/libc/src/__support/FPUtil/DivisionAndRemainderOperations.h
+++ b/libc/src/__support/FPUtil/DivisionAndRemainderOperations.h
@@ -78,7 +78,7 @@ LIBC_INLINE T remquo(T x, T y, int &q) {
}
}
- NormalFloat<T> remainder(exp + normaly.exponent, mx, 0);
+ NormalFloat<T> remainder(Sign::POS, exp + normaly.exponent, mx);
// Since NormalFloat to native type conversion is a truncation operation
// currently, the remainder value in the native type is correct as is.
diff --git a/libc/src/__support/FPUtil/NormalFloat.h b/libc/src/__support/FPUtil/NormalFloat.h
index 8b1612e1b47c61..1302f748286263 100644
--- a/libc/src/__support/FPUtil/NormalFloat.h
+++ b/libc/src/__support/FPUtil/NormalFloat.h
@@ -46,8 +46,8 @@ template <typename T> struct NormalFloat {
Sign sign = Sign::POS;
- LIBC_INLINE NormalFloat(int32_t e, StorageType m, bool s)
- : exponent(e), mantissa(m), sign(s ? Sign::NEG : Sign::POS) {
+ LIBC_INLINE NormalFloat(Sign s, int32_t e, StorageType m)
+ : exponent(e), mantissa(m), sign(s) {
if (mantissa >= ONE)
return;
diff --git a/libc/test/src/math/LdExpTest.h b/libc/test/src/math/LdExpTest.h
index b507bb4ec56e55..25120ba3646fda 100644
--- a/libc/test/src/math/LdExpTest.h
+++ b/libc/test/src/math/LdExpTest.h
@@ -60,8 +60,8 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::Test {
}
void testOverflow(LdExpFunc func) {
- NormalFloat x(FPBits::MAX_BIASED_EXPONENT - 10, NormalFloat::ONE + 0xF00BA,
- 0);
+ NormalFloat x(Sign::POS, FPBits::MAX_BIASED_EXPONENT - 10,
+ NormalFloat::ONE + 0xF00BA);
for (int32_t exp = 10; exp < 100; ++exp) {
ASSERT_FP_EQ(inf, func(T(x), exp));
ASSERT_FP_EQ(neg_inf, func(-T(x), exp));
@@ -75,7 +75,7 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::Test {
int32_t exp_array[] = {base_exponent + 5, base_exponent + 4,
base_exponent + 3, base_exponent + 2,
base_exponent + 1};
- T x = NormalFloat(0, MANTISSA, 0);
+ T x = NormalFloat(Sign::POS, 0, MANTISSA);
for (int32_t exp : exp_array) {
ASSERT_FP_EQ(func(x, -exp), x > 0 ? zero : neg_zero);
}
@@ -88,20 +88,21 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::Test {
int32_t exp_array[] = {base_exponent + 5, base_exponent + 4,
base_exponent + 3, base_exponent + 2,
base_exponent + 1};
- T x = NormalFloat(-FPBits::EXP_BIAS, MANTISSA, 0);
+ T x = NormalFloat(Sign::POS, -FPBits::EXP_BIAS, MANTISSA);
for (int32_t exp : exp_array) {
ASSERT_FP_EQ(func(x, -exp), x > 0 ? zero : neg_zero);
}
}
void testNormalOperation(LdExpFunc func) {
- T val_array[] = {
- // Normal numbers
- NormalFloat(100, MANTISSA, 0), NormalFloat(-100, MANTISSA, 0),
- NormalFloat(100, MANTISSA, 1), NormalFloat(-100, MANTISSA, 1),
- // Subnormal numbers
- NormalFloat(-FPBits::EXP_BIAS, MANTISSA, 0),
- NormalFloat(-FPBits::EXP_BIAS, MANTISSA, 1)};
+ T val_array[] = {// Normal numbers
+ NormalFloat(Sign::POS, 100, MANTISSA),
+ NormalFloat(Sign::POS, -100, MANTISSA),
+ NormalFloat(Sign::NEG, 100, MANTISSA),
+ NormalFloat(Sign::NEG, -100, MANTISSA),
+ // Subnormal numbers
+ NormalFloat(Sign::POS, -FPBits::EXP_BIAS, MANTISSA),
+ NormalFloat(Sign::NEG, -FPBits::EXP_BIAS, MANTISSA)};
for (int32_t exp = 0; exp <= FPBits::FRACTION_LEN; ++exp) {
for (T x : val_array) {
// We compare the result of ldexp with the result
@@ -120,14 +121,14 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::Test {
}
// Normal which trigger mantissa overflow.
- T x = NormalFloat(-FPBits::EXP_BIAS + 1,
- StorageType(2) * NormalFloat::ONE - StorageType(1), 0);
+ T x = NormalFloat(Sign::POS, -FPBits::EXP_BIAS + 1,
+ StorageType(2) * NormalFloat::ONE - StorageType(1));
ASSERT_FP_EQ(func(x, -1), x / 2);
ASSERT_FP_EQ(func(-x, -1), -x / 2);
// Start with a normal number high exponent but pass a very low number for
// exp. The result should be a subnormal number.
- x = NormalFloat(FPBits::EXP_BIAS, NormalFloat::ONE, 0);
+ x = NormalFloat(Sign::POS, FPBits::EXP_BIAS, NormalFloat::ONE);
int exp = -FPBits::MAX_BIASED_EXPONENT - 5;
T result = func(x, exp);
FPBits result_bits(result);
@@ -141,7 +142,7 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::Test {
// Start with a subnormal number but pass a very high number for exponent.
// The result should not be infinity.
- x = NormalFloat(-FPBits::EXP_BIAS + 1, NormalFloat::ONE >> 10, 0);
+ x = NormalFloat(Sign::POS, -FPBits::EXP_BIAS + 1, NormalFloat::ONE >> 10);
exp = FPBits::MAX_BIASED_EXPONENT + 5;
ASSERT_FALSE(FPBits(func(x, exp)).is_inf());
// But if the exp is large enough to oversome than the normalization shift,
diff --git a/libc/test/src/math/smoke/LdExpTest.h b/libc/test/src/math/smoke/LdExpTest.h
index b507bb4ec56e55..25120ba3646fda 100644
--- a/libc/test/src/math/smoke/LdExpTest.h
+++ b/libc/test/src/math/smoke/LdExpTest.h
@@ -60,8 +60,8 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::Test {
}
void testOverflow(LdExpFunc func) {
- NormalFloat x(FPBits::MAX_BIASED_EXPONENT - 10, NormalFloat::ONE + 0xF00BA,
- 0);
+ NormalFloat x(Sign::POS, FPBits::MAX_BIASED_EXPONENT - 10,
+ NormalFloat::ONE + 0xF00BA);
for (int32_t exp = 10; exp < 100; ++exp) {
ASSERT_FP_EQ(inf, func(T(x), exp));
ASSERT_FP_EQ(neg_inf, func(-T(x), exp));
@@ -75,7 +75,7 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::Test {
int32_t exp_array[] = {base_exponent + 5, base_exponent + 4,
base_exponent + 3, base_exponent + 2,
base_exponent + 1};
- T x = NormalFloat(0, MANTISSA, 0);
+ T x = NormalFloat(Sign::POS, 0, MANTISSA);
for (int32_t exp : exp_array) {
ASSERT_FP_EQ(func(x, -exp), x > 0 ? zero : neg_zero);
}
@@ -88,20 +88,21 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::Test {
int32_t exp_array[] = {base_exponent + 5, base_exponent + 4,
base_exponent + 3, base_exponent + 2,
base_exponent + 1};
- T x = NormalFloat(-FPBits::EXP_BIAS, MANTISSA, 0);
+ T x = NormalFloat(Sign::POS, -FPBits::EXP_BIAS, MANTISSA);
for (int32_t exp : exp_array) {
ASSERT_FP_EQ(func(x, -exp), x > 0 ? zero : neg_zero);
}
}
void testNormalOperation(LdExpFunc func) {
- T val_array[] = {
- // Normal numbers
- NormalFloat(100, MANTISSA, 0), NormalFloat(-100, MANTISSA, 0),
- NormalFloat(100, MANTISSA, 1), NormalFloat(-100, MANTISSA, 1),
- // Subnormal numbers
- NormalFloat(-FPBits::EXP_BIAS, MANTISSA, 0),
- NormalFloat(-FPBits::EXP_BIAS, MANTISSA, 1)};
+ T val_array[] = {// Normal numbers
+ NormalFloat(Sign::POS, 100, MANTISSA),
+ NormalFloat(Sign::POS, -100, MANTISSA),
+ NormalFloat(Sign::NEG, 100, MANTISSA),
+ NormalFloat(Sign::NEG, -100, MANTISSA),
+ // Subnormal numbers
+ NormalFloat(Sign::POS, -FPBits::EXP_BIAS, MANTISSA),
+ NormalFloat(Sign::NEG, -FPBits::EXP_BIAS, MANTISSA)};
for (int32_t exp = 0; exp <= FPBits::FRACTION_LEN; ++exp) {
for (T x : val_array) {
// We compare the result of ldexp with the result
@@ -120,14 +121,14 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::Test {
}
// Normal which trigger mantissa overflow.
- T x = NormalFloat(-FPBits::EXP_BIAS + 1,
- StorageType(2) * NormalFloat::ONE - StorageType(1), 0);
+ T x = NormalFloat(Sign::POS, -FPBits::EXP_BIAS + 1,
+ StorageType(2) * NormalFloat::ONE - StorageType(1));
ASSERT_FP_EQ(func(x, -1), x / 2);
ASSERT_FP_EQ(func(-x, -1), -x / 2);
// Start with a normal number high exponent but pass a very low number for
// exp. The result should be a subnormal number.
- x = NormalFloat(FPBits::EXP_BIAS, NormalFloat::ONE, 0);
+ x = NormalFloat(Sign::POS, FPBits::EXP_BIAS, NormalFloat::ONE);
int exp = -FPBits::MAX_BIASED_EXPONENT - 5;
T result = func(x, exp);
FPBits result_bits(result);
@@ -141,7 +142,7 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::Test {
// Start with a subnormal number but pass a very high number for exponent.
// The result should not be infinity.
- x = NormalFloat(-FPBits::EXP_BIAS + 1, NormalFloat::ONE >> 10, 0);
+ x = NormalFloat(Sign::POS, -FPBits::EXP_BIAS + 1, NormalFloat::ONE >> 10);
exp = FPBits::MAX_BIASED_EXPONENT + 5;
ASSERT_FALSE(FPBits(func(x, exp)).is_inf());
// But if the exp is large enough to oversome than the normalization shift,
|
legrosbuffle
approved these changes
Jan 19, 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.
No description provided.