Skip to content

Commit 508c4ef

Browse files
authored
[libc][NFC] Use Sign in NormalFloat (#78579)
1 parent ee9c9f3 commit 508c4ef

File tree

4 files changed

+35
-33
lines changed

4 files changed

+35
-33
lines changed

libc/src/__support/FPUtil/DivisionAndRemainderOperations.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ LIBC_INLINE T remquo(T x, T y, int &q) {
7878
}
7979
}
8080

81-
NormalFloat<T> remainder(exp + normaly.exponent, mx, 0);
81+
NormalFloat<T> remainder(Sign::POS, exp + normaly.exponent, mx);
8282

8383
// Since NormalFloat to native type conversion is a truncation operation
8484
// currently, the remainder value in the native type is correct as is.

libc/src/__support/FPUtil/NormalFloat.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ template <typename T> struct NormalFloat {
4646

4747
Sign sign = Sign::POS;
4848

49-
LIBC_INLINE NormalFloat(int32_t e, StorageType m, bool s)
50-
: exponent(e), mantissa(m), sign(s ? Sign::NEG : Sign::POS) {
49+
LIBC_INLINE NormalFloat(Sign s, int32_t e, StorageType m)
50+
: exponent(e), mantissa(m), sign(s) {
5151
if (mantissa >= ONE)
5252
return;
5353

libc/test/src/math/LdExpTest.h

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::Test {
6060
}
6161

6262
void testOverflow(LdExpFunc func) {
63-
NormalFloat x(FPBits::MAX_BIASED_EXPONENT - 10, NormalFloat::ONE + 0xF00BA,
64-
0);
63+
NormalFloat x(Sign::POS, FPBits::MAX_BIASED_EXPONENT - 10,
64+
NormalFloat::ONE + 0xF00BA);
6565
for (int32_t exp = 10; exp < 100; ++exp) {
6666
ASSERT_FP_EQ(inf, func(T(x), exp));
6767
ASSERT_FP_EQ(neg_inf, func(-T(x), exp));
@@ -75,7 +75,7 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::Test {
7575
int32_t exp_array[] = {base_exponent + 5, base_exponent + 4,
7676
base_exponent + 3, base_exponent + 2,
7777
base_exponent + 1};
78-
T x = NormalFloat(0, MANTISSA, 0);
78+
T x = NormalFloat(Sign::POS, 0, MANTISSA);
7979
for (int32_t exp : exp_array) {
8080
ASSERT_FP_EQ(func(x, -exp), x > 0 ? zero : neg_zero);
8181
}
@@ -88,20 +88,21 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::Test {
8888
int32_t exp_array[] = {base_exponent + 5, base_exponent + 4,
8989
base_exponent + 3, base_exponent + 2,
9090
base_exponent + 1};
91-
T x = NormalFloat(-FPBits::EXP_BIAS, MANTISSA, 0);
91+
T x = NormalFloat(Sign::POS, -FPBits::EXP_BIAS, MANTISSA);
9292
for (int32_t exp : exp_array) {
9393
ASSERT_FP_EQ(func(x, -exp), x > 0 ? zero : neg_zero);
9494
}
9595
}
9696

9797
void testNormalOperation(LdExpFunc func) {
98-
T val_array[] = {
99-
// Normal numbers
100-
NormalFloat(100, MANTISSA, 0), NormalFloat(-100, MANTISSA, 0),
101-
NormalFloat(100, MANTISSA, 1), NormalFloat(-100, MANTISSA, 1),
102-
// Subnormal numbers
103-
NormalFloat(-FPBits::EXP_BIAS, MANTISSA, 0),
104-
NormalFloat(-FPBits::EXP_BIAS, MANTISSA, 1)};
98+
T val_array[] = {// Normal numbers
99+
NormalFloat(Sign::POS, 100, MANTISSA),
100+
NormalFloat(Sign::POS, -100, MANTISSA),
101+
NormalFloat(Sign::NEG, 100, MANTISSA),
102+
NormalFloat(Sign::NEG, -100, MANTISSA),
103+
// Subnormal numbers
104+
NormalFloat(Sign::POS, -FPBits::EXP_BIAS, MANTISSA),
105+
NormalFloat(Sign::NEG, -FPBits::EXP_BIAS, MANTISSA)};
105106
for (int32_t exp = 0; exp <= FPBits::FRACTION_LEN; ++exp) {
106107
for (T x : val_array) {
107108
// We compare the result of ldexp with the result
@@ -120,14 +121,14 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::Test {
120121
}
121122

122123
// Normal which trigger mantissa overflow.
123-
T x = NormalFloat(-FPBits::EXP_BIAS + 1,
124-
StorageType(2) * NormalFloat::ONE - StorageType(1), 0);
124+
T x = NormalFloat(Sign::POS, -FPBits::EXP_BIAS + 1,
125+
StorageType(2) * NormalFloat::ONE - StorageType(1));
125126
ASSERT_FP_EQ(func(x, -1), x / 2);
126127
ASSERT_FP_EQ(func(-x, -1), -x / 2);
127128

128129
// Start with a normal number high exponent but pass a very low number for
129130
// exp. The result should be a subnormal number.
130-
x = NormalFloat(FPBits::EXP_BIAS, NormalFloat::ONE, 0);
131+
x = NormalFloat(Sign::POS, FPBits::EXP_BIAS, NormalFloat::ONE);
131132
int exp = -FPBits::MAX_BIASED_EXPONENT - 5;
132133
T result = func(x, exp);
133134
FPBits result_bits(result);
@@ -141,7 +142,7 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::Test {
141142

142143
// Start with a subnormal number but pass a very high number for exponent.
143144
// The result should not be infinity.
144-
x = NormalFloat(-FPBits::EXP_BIAS + 1, NormalFloat::ONE >> 10, 0);
145+
x = NormalFloat(Sign::POS, -FPBits::EXP_BIAS + 1, NormalFloat::ONE >> 10);
145146
exp = FPBits::MAX_BIASED_EXPONENT + 5;
146147
ASSERT_FALSE(FPBits(func(x, exp)).is_inf());
147148
// But if the exp is large enough to oversome than the normalization shift,

libc/test/src/math/smoke/LdExpTest.h

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::Test {
6060
}
6161

6262
void testOverflow(LdExpFunc func) {
63-
NormalFloat x(FPBits::MAX_BIASED_EXPONENT - 10, NormalFloat::ONE + 0xF00BA,
64-
0);
63+
NormalFloat x(Sign::POS, FPBits::MAX_BIASED_EXPONENT - 10,
64+
NormalFloat::ONE + 0xF00BA);
6565
for (int32_t exp = 10; exp < 100; ++exp) {
6666
ASSERT_FP_EQ(inf, func(T(x), exp));
6767
ASSERT_FP_EQ(neg_inf, func(-T(x), exp));
@@ -75,7 +75,7 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::Test {
7575
int32_t exp_array[] = {base_exponent + 5, base_exponent + 4,
7676
base_exponent + 3, base_exponent + 2,
7777
base_exponent + 1};
78-
T x = NormalFloat(0, MANTISSA, 0);
78+
T x = NormalFloat(Sign::POS, 0, MANTISSA);
7979
for (int32_t exp : exp_array) {
8080
ASSERT_FP_EQ(func(x, -exp), x > 0 ? zero : neg_zero);
8181
}
@@ -88,20 +88,21 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::Test {
8888
int32_t exp_array[] = {base_exponent + 5, base_exponent + 4,
8989
base_exponent + 3, base_exponent + 2,
9090
base_exponent + 1};
91-
T x = NormalFloat(-FPBits::EXP_BIAS, MANTISSA, 0);
91+
T x = NormalFloat(Sign::POS, -FPBits::EXP_BIAS, MANTISSA);
9292
for (int32_t exp : exp_array) {
9393
ASSERT_FP_EQ(func(x, -exp), x > 0 ? zero : neg_zero);
9494
}
9595
}
9696

9797
void testNormalOperation(LdExpFunc func) {
98-
T val_array[] = {
99-
// Normal numbers
100-
NormalFloat(100, MANTISSA, 0), NormalFloat(-100, MANTISSA, 0),
101-
NormalFloat(100, MANTISSA, 1), NormalFloat(-100, MANTISSA, 1),
102-
// Subnormal numbers
103-
NormalFloat(-FPBits::EXP_BIAS, MANTISSA, 0),
104-
NormalFloat(-FPBits::EXP_BIAS, MANTISSA, 1)};
98+
T val_array[] = {// Normal numbers
99+
NormalFloat(Sign::POS, 100, MANTISSA),
100+
NormalFloat(Sign::POS, -100, MANTISSA),
101+
NormalFloat(Sign::NEG, 100, MANTISSA),
102+
NormalFloat(Sign::NEG, -100, MANTISSA),
103+
// Subnormal numbers
104+
NormalFloat(Sign::POS, -FPBits::EXP_BIAS, MANTISSA),
105+
NormalFloat(Sign::NEG, -FPBits::EXP_BIAS, MANTISSA)};
105106
for (int32_t exp = 0; exp <= FPBits::FRACTION_LEN; ++exp) {
106107
for (T x : val_array) {
107108
// We compare the result of ldexp with the result
@@ -120,14 +121,14 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::Test {
120121
}
121122

122123
// Normal which trigger mantissa overflow.
123-
T x = NormalFloat(-FPBits::EXP_BIAS + 1,
124-
StorageType(2) * NormalFloat::ONE - StorageType(1), 0);
124+
T x = NormalFloat(Sign::POS, -FPBits::EXP_BIAS + 1,
125+
StorageType(2) * NormalFloat::ONE - StorageType(1));
125126
ASSERT_FP_EQ(func(x, -1), x / 2);
126127
ASSERT_FP_EQ(func(-x, -1), -x / 2);
127128

128129
// Start with a normal number high exponent but pass a very low number for
129130
// exp. The result should be a subnormal number.
130-
x = NormalFloat(FPBits::EXP_BIAS, NormalFloat::ONE, 0);
131+
x = NormalFloat(Sign::POS, FPBits::EXP_BIAS, NormalFloat::ONE);
131132
int exp = -FPBits::MAX_BIASED_EXPONENT - 5;
132133
T result = func(x, exp);
133134
FPBits result_bits(result);
@@ -141,7 +142,7 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::Test {
141142

142143
// Start with a subnormal number but pass a very high number for exponent.
143144
// The result should not be infinity.
144-
x = NormalFloat(-FPBits::EXP_BIAS + 1, NormalFloat::ONE >> 10, 0);
145+
x = NormalFloat(Sign::POS, -FPBits::EXP_BIAS + 1, NormalFloat::ONE >> 10);
145146
exp = FPBits::MAX_BIASED_EXPONENT + 5;
146147
ASSERT_FALSE(FPBits(func(x, exp)).is_inf());
147148
// But if the exp is large enough to oversome than the normalization shift,

0 commit comments

Comments
 (0)