diff --git a/libc/src/__support/UInt.h b/libc/src/__support/UInt.h --- a/libc/src/__support/UInt.h +++ b/libc/src/__support/UInt.h @@ -137,7 +137,7 @@ constexpr uint64_t add(const BigInt &x) { SumCarry 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 @@ constexpr uint64_t sub(const BigInt &x) { DiffBorrow 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 --- a/libc/test/src/math/FmaTest.h +++ b/libc/test/src/math/FmaTest.h @@ -69,7 +69,8 @@ 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 @@ 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 --- a/libc/test/src/math/HypotTest.h +++ b/libc/test/src/math/HypotTest.h @@ -85,7 +85,8 @@ 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 --- a/libc/test/src/math/ILogbTest.h +++ b/libc/test/src/math/ILogbTest.h @@ -79,7 +79,8 @@ 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 @@ using FPBits = __llvm_libc::fputil::FPBits; 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 --- a/libc/test/src/math/LdExpTest.h +++ b/libc/test/src/math/LdExpTest.h @@ -70,7 +70,7 @@ 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 @@ 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 @@ 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(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 --- a/libc/test/src/math/RIntTest.h +++ b/libc/test/src/math/RIntTest.h @@ -95,7 +95,8 @@ 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 @@ 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 --- a/libc/test/src/math/RemQuoTest.h +++ b/libc/test/src/math/RemQuoTest.h @@ -97,7 +97,8 @@ 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 @@ 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 --- a/libc/test/src/math/RoundToIntegerTest.h +++ b/libc/test/src/math/RoundToIntegerTest.h @@ -216,7 +216,8 @@ 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 @@ 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