[libc-commits] [libc] a2df87c - [libc] Fix libmath test compilation when using UInt<T>

Mikhail R. Gadelha via libc-commits libc-commits at lists.llvm.org
Tue Jun 20 11:45:43 PDT 2023


Author: Mikhail R. Gadelha
Date: 2023-06-20T15:41:18-03:00
New Revision: a2df87c2b0ea7eef6b124a41f818212173071560

URL: https://github.com/llvm/llvm-project/commit/a2df87c2b0ea7eef6b124a41f818212173071560
DIFF: https://github.com/llvm/llvm-project/commit/a2df87c2b0ea7eef6b124a41f818212173071560.diff

LOG: [libc] Fix libmath test compilation when using UInt<T>

This patch:
(1) adds the add_with_carry_const and sub_with_borrow_const constexpr calls
to add and sub, respectively. Both add and sub are constexpr calls and
were call the non-constexpr version of add/sub_with_borrow.
(2) adds explicit UIntType construct calls in some fp tests.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D150223

Added: 
    

Modified: 
    libc/src/__support/UInt.h
    libc/test/src/math/FmaTest.h
    libc/test/src/math/HypotTest.h
    libc/test/src/math/ILogbTest.h
    libc/test/src/math/LdExpTest.h
    libc/test/src/math/RIntTest.h
    libc/test/src/math/RemQuoTest.h
    libc/test/src/math/RoundToIntegerTest.h

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/UInt.h b/libc/src/__support/UInt.h
index 22ee2426fd533..74b72b59e1ddd 100644
--- a/libc/src/__support/UInt.h
+++ b/libc/src/__support/UInt.h
@@ -137,7 +137,7 @@ template <size_t Bits, bool Signed> struct BigInt {
   constexpr uint64_t add(const BigInt<Bits, Signed> &x) {
     SumCarry<uint64_t> s{0, 0};
     for (size_t i = 0; i < WORDCOUNT; ++i) {
-      s = add_with_carry(val[i], x.val[i], s.carry);
+      s = add_with_carry_const(val[i], x.val[i], s.carry);
       val[i] = s.sum;
     }
     return s.carry;
@@ -176,7 +176,7 @@ template <size_t Bits, bool Signed> struct BigInt {
   constexpr uint64_t sub(const BigInt<Bits, Signed> &x) {
     DiffBorrow<uint64_t> d{0, 0};
     for (size_t i = 0; i < WORDCOUNT; ++i) {
-      d = sub_with_borrow(val[i], x.val[i], d.borrow);
+      d = sub_with_borrow_const(val[i], x.val[i], d.borrow);
       val[i] = d.
diff ;
     }
     return d.borrow;

diff  --git a/libc/test/src/math/FmaTest.h b/libc/test/src/math/FmaTest.h
index f648ac05333c3..3a5ed2bf3e53b 100644
--- a/libc/test/src/math/FmaTest.h
+++ b/libc/test/src/math/FmaTest.h
@@ -69,7 +69,8 @@ class FmaTestTemplate : public __llvm_libc::testing::Test {
   void test_subnormal_range(Func func) {
     constexpr UIntType COUNT = 100'001;
     constexpr UIntType STEP =
-        (FPBits::MAX_SUBNORMAL - FPBits::MIN_SUBNORMAL) / COUNT;
+        (UIntType(FPBits::MAX_SUBNORMAL) - UIntType(FPBits::MIN_SUBNORMAL)) /
+        COUNT;
     for (UIntType v = FPBits::MIN_SUBNORMAL, w = FPBits::MAX_SUBNORMAL;
          v <= FPBits::MAX_SUBNORMAL && w >= FPBits::MIN_SUBNORMAL;
          v += STEP, w -= STEP) {
@@ -83,7 +84,8 @@ class FmaTestTemplate : public __llvm_libc::testing::Test {
 
   void test_normal_range(Func func) {
     constexpr UIntType COUNT = 100'001;
-    constexpr UIntType STEP = (FPBits::MAX_NORMAL - FPBits::MIN_NORMAL) / COUNT;
+    constexpr UIntType STEP =
+        (UIntType(FPBits::MAX_NORMAL) - UIntType(FPBits::MIN_NORMAL)) / COUNT;
     for (UIntType v = FPBits::MIN_NORMAL, w = FPBits::MAX_NORMAL;
          v <= FPBits::MAX_NORMAL && w >= FPBits::MIN_NORMAL;
          v += STEP, w -= STEP) {

diff  --git a/libc/test/src/math/HypotTest.h b/libc/test/src/math/HypotTest.h
index 5bee260c70f0e..d1409265c6709 100644
--- a/libc/test/src/math/HypotTest.h
+++ b/libc/test/src/math/HypotTest.h
@@ -85,7 +85,8 @@ class HypotTestTemplate : public __llvm_libc::testing::Test {
 
   void test_normal_range(Func func) {
     constexpr UIntType COUNT = 10'001;
-    constexpr UIntType STEP = (FPBits::MAX_NORMAL - FPBits::MIN_NORMAL) / COUNT;
+    constexpr UIntType STEP =
+        (UIntType(FPBits::MAX_NORMAL) - UIntType(FPBits::MIN_NORMAL)) / COUNT;
     for (int signs = 0; signs < 4; ++signs) {
       for (UIntType v = FPBits::MIN_NORMAL, w = FPBits::MAX_NORMAL;
            v <= FPBits::MAX_NORMAL && w >= FPBits::MIN_NORMAL;

diff  --git a/libc/test/src/math/ILogbTest.h b/libc/test/src/math/ILogbTest.h
index 45e997427c834..6ad5eb065c8aa 100644
--- a/libc/test/src/math/ILogbTest.h
+++ b/libc/test/src/math/ILogbTest.h
@@ -79,7 +79,8 @@ class LlvmLibcILogbTest : public __llvm_libc::testing::Test {
     using UIntType = typename FPBits::UIntType;
     constexpr UIntType COUNT = 10'001;
     constexpr UIntType STEP =
-        (FPBits::MAX_SUBNORMAL - FPBits::MIN_SUBNORMAL) / COUNT;
+        (UIntType(FPBits::MAX_SUBNORMAL) - UIntType(FPBits::MIN_SUBNORMAL)) /
+        COUNT;
     for (UIntType v = FPBits::MIN_SUBNORMAL; v <= FPBits::MAX_SUBNORMAL;
          v += STEP) {
       T x = T(FPBits(v));
@@ -97,7 +98,8 @@ class LlvmLibcILogbTest : public __llvm_libc::testing::Test {
     using FPBits = __llvm_libc::fputil::FPBits<T>;
     using UIntType = typename FPBits::UIntType;
     constexpr UIntType COUNT = 10'001;
-    constexpr UIntType STEP = (FPBits::MAX_NORMAL - FPBits::MIN_NORMAL) / COUNT;
+    constexpr UIntType STEP =
+        (UIntType(FPBits::MAX_NORMAL) - UIntType(FPBits::MIN_NORMAL)) / COUNT;
     for (UIntType v = FPBits::MIN_NORMAL; v <= FPBits::MAX_NORMAL; v += STEP) {
       T x = T(FPBits(v));
       if (isnan(x) || isinf(x) || x == 0.0)

diff  --git a/libc/test/src/math/LdExpTest.h b/libc/test/src/math/LdExpTest.h
index 18755aa2d8455..2e592995ae792 100644
--- a/libc/test/src/math/LdExpTest.h
+++ b/libc/test/src/math/LdExpTest.h
@@ -70,7 +70,7 @@ class LdExpTestTemplate : public __llvm_libc::testing::Test {
   void testUnderflowToZeroOnNormal(LdExpFunc func) {
     // In this test, we pass a normal nubmer to func and expect zero
     // to be returned due to underflow.
-    int32_t base_exponent = FPBits::EXPONENT_BIAS + MANTISSA_WIDTH;
+    int32_t base_exponent = FPBits::EXPONENT_BIAS + int32_t(MANTISSA_WIDTH);
     int32_t exp_array[] = {base_exponent + 5, base_exponent + 4,
                            base_exponent + 3, base_exponent + 2,
                            base_exponent + 1};
@@ -83,7 +83,7 @@ class LdExpTestTemplate : public __llvm_libc::testing::Test {
   void testUnderflowToZeroOnSubnormal(LdExpFunc func) {
     // In this test, we pass a normal nubmer to func and expect zero
     // to be returned due to underflow.
-    int32_t base_exponent = FPBits::EXPONENT_BIAS + MANTISSA_WIDTH;
+    int32_t base_exponent = FPBits::EXPONENT_BIAS + int32_t(MANTISSA_WIDTH);
     int32_t exp_array[] = {base_exponent + 5, base_exponent + 4,
                            base_exponent + 3, base_exponent + 2,
                            base_exponent + 1};
@@ -105,13 +105,22 @@ class LdExpTestTemplate : public __llvm_libc::testing::Test {
       for (T x : val_array) {
         // We compare the result of ldexp with the result
         // of the native multiplication/division instruction.
-        ASSERT_FP_EQ(func(x, exp), x * (UIntType(1) << exp));
-        ASSERT_FP_EQ(func(x, -exp), x / (UIntType(1) << exp));
+
+        // We need to use a NormalFloat here (instead of 1 << exp), because
+        // there are 32 bit systems that don't support 128bit long ints but
+        // support long doubles. This test can do 1 << 64, which would fail
+        // in these systems.
+        NormalFloat two_to_exp = NormalFloat(static_cast<T>(1.L));
+        two_to_exp = two_to_exp.mul2(exp);
+
+        ASSERT_FP_EQ(func(x, exp), x * two_to_exp);
+        ASSERT_FP_EQ(func(x, -exp), x / two_to_exp);
       }
     }
 
     // Normal which trigger mantissa overflow.
-    T x = NormalFloat(-FPBits::EXPONENT_BIAS + 1, 2 * NormalFloat::ONE - 1, 0);
+    T x = NormalFloat(-FPBits::EXPONENT_BIAS + 1,
+                      UIntType(2) * NormalFloat::ONE - UIntType(1), 0);
     ASSERT_FP_EQ(func(x, -1), x / 2);
     ASSERT_FP_EQ(func(-x, -1), -x / 2);
 

diff  --git a/libc/test/src/math/RIntTest.h b/libc/test/src/math/RIntTest.h
index caa62e743f266..b18123139d8ca 100644
--- a/libc/test/src/math/RIntTest.h
+++ b/libc/test/src/math/RIntTest.h
@@ -95,7 +95,8 @@ class RIntTestTemplate : public __llvm_libc::testing::Test {
   void testSubnormalRange(RIntFunc func) {
     constexpr UIntType COUNT = 100'001;
     constexpr UIntType STEP =
-        (FPBits::MAX_SUBNORMAL - FPBits::MIN_SUBNORMAL) / COUNT;
+        (UIntType(FPBits::MAX_SUBNORMAL) - UIntType(FPBits::MIN_SUBNORMAL)) /
+        COUNT;
     for (UIntType i = FPBits::MIN_SUBNORMAL; i <= FPBits::MAX_SUBNORMAL;
          i += STEP) {
       T x = T(FPBits(i));
@@ -109,7 +110,8 @@ class RIntTestTemplate : public __llvm_libc::testing::Test {
 
   void testNormalRange(RIntFunc func) {
     constexpr UIntType COUNT = 100'001;
-    constexpr UIntType STEP = (FPBits::MAX_NORMAL - FPBits::MIN_NORMAL) / COUNT;
+    constexpr UIntType STEP =
+        (UIntType(FPBits::MAX_NORMAL) - UIntType(FPBits::MIN_NORMAL)) / COUNT;
     for (UIntType i = FPBits::MIN_NORMAL; i <= FPBits::MAX_NORMAL; i += STEP) {
       T x = T(FPBits(i));
       // In normal range on x86 platforms, the long double implicit 1 bit can be

diff  --git a/libc/test/src/math/RemQuoTest.h b/libc/test/src/math/RemQuoTest.h
index 636a19dafcd36..5766beff2ceba 100644
--- a/libc/test/src/math/RemQuoTest.h
+++ b/libc/test/src/math/RemQuoTest.h
@@ -97,7 +97,8 @@ class RemQuoTestTemplate : public __llvm_libc::testing::Test {
   void testSubnormalRange(RemQuoFunc func) {
     constexpr UIntType COUNT = 100'001;
     constexpr UIntType STEP =
-        (FPBits::MAX_SUBNORMAL - FPBits::MIN_SUBNORMAL) / COUNT;
+        (UIntType(FPBits::MAX_SUBNORMAL) - UIntType(FPBits::MIN_SUBNORMAL)) /
+        COUNT;
     for (UIntType v = FPBits::MIN_SUBNORMAL, w = FPBits::MAX_SUBNORMAL;
          v <= FPBits::MAX_SUBNORMAL && w >= FPBits::MIN_SUBNORMAL;
          v += STEP, w -= STEP) {
@@ -111,7 +112,8 @@ class RemQuoTestTemplate : public __llvm_libc::testing::Test {
 
   void testNormalRange(RemQuoFunc func) {
     constexpr UIntType COUNT = 1'001;
-    constexpr UIntType STEP = (FPBits::MAX_NORMAL - FPBits::MIN_NORMAL) / COUNT;
+    constexpr UIntType STEP =
+        (UIntType(FPBits::MAX_NORMAL) - UIntType(FPBits::MIN_NORMAL)) / COUNT;
     for (UIntType v = FPBits::MIN_NORMAL, w = FPBits::MAX_NORMAL;
          v <= FPBits::MAX_NORMAL && w >= FPBits::MIN_NORMAL;
          v += STEP, w -= STEP) {

diff  --git a/libc/test/src/math/RoundToIntegerTest.h b/libc/test/src/math/RoundToIntegerTest.h
index d0f0ee23ada8d..4564e9885b3eb 100644
--- a/libc/test/src/math/RoundToIntegerTest.h
+++ b/libc/test/src/math/RoundToIntegerTest.h
@@ -216,7 +216,8 @@ class RoundToIntegerTestTemplate : public __llvm_libc::testing::Test {
   void testSubnormalRange(RoundToIntegerFunc func) {
     constexpr UIntType COUNT = 1'000'001;
     constexpr UIntType STEP =
-        (FPBits::MAX_SUBNORMAL - FPBits::MIN_SUBNORMAL) / COUNT;
+        (UIntType(FPBits::MAX_SUBNORMAL) - UIntType(FPBits::MIN_SUBNORMAL)) /
+        COUNT;
     for (UIntType i = FPBits::MIN_SUBNORMAL; i <= FPBits::MAX_SUBNORMAL;
          i += STEP) {
       F x = F(FPBits(i));
@@ -259,7 +260,8 @@ class RoundToIntegerTestTemplate : public __llvm_libc::testing::Test {
       return;
 
     constexpr UIntType COUNT = 1'000'001;
-    constexpr UIntType STEP = (FPBits::MAX_NORMAL - FPBits::MIN_NORMAL) / COUNT;
+    constexpr UIntType STEP =
+        (UIntType(FPBits::MAX_NORMAL) - UIntType(FPBits::MIN_NORMAL)) / COUNT;
     for (UIntType i = FPBits::MIN_NORMAL; i <= FPBits::MAX_NORMAL; i += STEP) {
       F x = F(FPBits(i));
       // In normal range on x86 platforms, the long double implicit 1 bit can be


        


More information about the libc-commits mailing list