99#ifndef LLVM_LIBC_TEST_SRC_MATH_ADDTEST_H
1010#define LLVM_LIBC_TEST_SRC_MATH_ADDTEST_H
1111
12+ #include " src/__support/CPP/algorithm.h"
1213#include " test/UnitTest/FEnvSafeTest.h"
1314#include " test/UnitTest/FPMatcher.h"
1415#include " test/UnitTest/Test.h"
@@ -36,29 +37,34 @@ class AddTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
3637 InFPBits::min_subnormal ().uintval();
3738
3839public:
39- typedef OutType (*AddFunc )(InType, InType);
40+ using AddFunc = OutType (*)(InType, InType);
4041
4142 void test_subnormal_range (AddFunc func) {
42- constexpr InStorageType COUNT = 100'001 ;
43- constexpr InStorageType STEP =
44- (IN_MAX_SUBNORMAL_U - IN_MIN_SUBNORMAL_U) / COUNT;
45- for (InStorageType i = 0 , v = 0 , w = IN_MAX_SUBNORMAL_U; i <= COUNT;
46- ++i, v += STEP, w -= STEP) {
47- InType x = InFPBits (v).get_val ();
48- InType y = InFPBits (w).get_val ();
43+ constexpr int COUNT = 100'001 ;
44+ constexpr InStorageType STEP = LIBC_NAMESPACE::cpp::max (
45+ static_cast <InStorageType>((IN_MAX_SUBNORMAL_U - IN_MIN_SUBNORMAL_U) /
46+ COUNT),
47+ InStorageType (1 ));
48+ for (InStorageType i = IN_MIN_SUBNORMAL_U; i <= IN_MAX_SUBNORMAL_U;
49+ i += STEP) {
50+ InType x = InFPBits (i).get_val ();
51+ InType y = InFPBits (static_cast <InStorageType>(IN_MAX_SUBNORMAL_U - i))
52+ .get_val ();
4953 mpfr::BinaryInput<InType> input{x, y};
5054 EXPECT_MPFR_MATCH_ALL_ROUNDING (mpfr::Operation::Add, input, func (x, y),
5155 0.5 );
5256 }
5357 }
5458
5559 void test_normal_range (AddFunc func) {
56- constexpr InStorageType COUNT = 100'001 ;
57- constexpr InStorageType STEP = (IN_MAX_NORMAL_U - IN_MIN_NORMAL_U) / COUNT;
58- for (InStorageType i = 0 , v = 0 , w = IN_MAX_NORMAL_U; i <= COUNT;
59- ++i, v += STEP, w -= STEP) {
60- InType x = InFPBits (v).get_val ();
61- InType y = InFPBits (w).get_val ();
60+ constexpr int COUNT = 100'001 ;
61+ constexpr InStorageType STEP = LIBC_NAMESPACE::cpp::max (
62+ static_cast <InStorageType>((IN_MAX_NORMAL_U - IN_MIN_NORMAL_U) / COUNT),
63+ InStorageType (1 ));
64+ for (InStorageType i = IN_MIN_NORMAL_U; i <= IN_MAX_NORMAL_U; i += STEP) {
65+ InType x = InFPBits (i).get_val ();
66+ InType y =
67+ InFPBits (static_cast <InStorageType>(IN_MAX_NORMAL_U - i)).get_val ();
6268 mpfr::BinaryInput<InType> input{x, y};
6369 EXPECT_MPFR_MATCH_ALL_ROUNDING (mpfr::Operation::Add, input, func (x, y),
6470 0.5 );
@@ -71,4 +77,11 @@ class AddTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
7177 TEST_F (LlvmLibcAddTest, SubnormalRange) { test_subnormal_range (&func); } \
7278 TEST_F (LlvmLibcAddTest, NormalRange) { test_normal_range (&func); }
7379
80+ #define LIST_ADD_SAME_TYPE_TESTS (suffix, OutType, InType, func ) \
81+ using LlvmLibcAddTest##suffix = AddTest<OutType, InType>; \
82+ TEST_F (LlvmLibcAddTest##suffix, SubnormalRange) { \
83+ test_subnormal_range (&func); \
84+ } \
85+ TEST_F (LlvmLibcAddTest##suffix, NormalRange) { test_normal_range (&func); }
86+
7487#endif // LLVM_LIBC_TEST_SRC_MATH_ADDTEST_H
0 commit comments