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

Conversation

gchatelet
Copy link
Contributor

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Jan 18, 2024

@llvm/pr-subscribers-libc

Author: Guillaume Chatelet (gchatelet)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/78579.diff

4 Files Affected:

  • (modified) libc/src/__support/FPUtil/DivisionAndRemainderOperations.h (+1-1)
  • (modified) libc/src/__support/FPUtil/NormalFloat.h (+2-2)
  • (modified) libc/test/src/math/LdExpTest.h (+16-15)
  • (modified) libc/test/src/math/smoke/LdExpTest.h (+16-15)
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,

@gchatelet gchatelet merged commit 508c4ef into llvm:main Jan 19, 2024
@gchatelet gchatelet deleted the use_sign_in_normal_float branch January 19, 2024 08:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants