[libc-commits] [libc] 508c4ef - [libc][NFC] Use Sign in NormalFloat (#78579)
via libc-commits
libc-commits at lists.llvm.org
Fri Jan 19 00:56:05 PST 2024
Author: Guillaume Chatelet
Date: 2024-01-19T09:56:01+01:00
New Revision: 508c4efe1e9d95661b322818ae4d6a05b1913504
URL: https://github.com/llvm/llvm-project/commit/508c4efe1e9d95661b322818ae4d6a05b1913504
DIFF: https://github.com/llvm/llvm-project/commit/508c4efe1e9d95661b322818ae4d6a05b1913504.diff
LOG: [libc][NFC] Use Sign in NormalFloat (#78579)
Added:
Modified:
libc/src/__support/FPUtil/DivisionAndRemainderOperations.h
libc/src/__support/FPUtil/NormalFloat.h
libc/test/src/math/LdExpTest.h
libc/test/src/math/smoke/LdExpTest.h
Removed:
################################################################################
diff --git a/libc/src/__support/FPUtil/DivisionAndRemainderOperations.h b/libc/src/__support/FPUtil/DivisionAndRemainderOperations.h
index 1c09e7906165f51..1798310c3e31e30 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 e7abc53ce6129ce..cfa9e1417510559 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 b507bb4ec56e552..25120ba3646fda0 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 b507bb4ec56e552..25120ba3646fda0 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,
More information about the libc-commits
mailing list