Skip to content

[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 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libc/src/__support/FPUtil/DivisionAndRemainderOperations.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions libc/src/__support/FPUtil/NormalFloat.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
31 changes: 16 additions & 15 deletions libc/test/src/math/LdExpTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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);
}
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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,
Expand Down
31 changes: 16 additions & 15 deletions libc/test/src/math/smoke/LdExpTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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);
}
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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,
Expand Down