|
9 | 9 | #ifndef LLVM_LIBC_TEST_SRC_MATH_ADDTEST_H |
10 | 10 | #define LLVM_LIBC_TEST_SRC_MATH_ADDTEST_H |
11 | 11 |
|
| 12 | +#include "src/__support/CPP/algorithm.h" |
12 | 13 | #include "test/UnitTest/FEnvSafeTest.h" |
13 | 14 | #include "test/UnitTest/FPMatcher.h" |
14 | 15 | #include "test/UnitTest/Test.h" |
@@ -36,37 +37,47 @@ class AddTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { |
36 | 37 | InFPBits::min_subnormal().uintval(); |
37 | 38 |
|
38 | 39 | public: |
39 | | - typedef OutType (*AddFunc)(InType, InType); |
| 40 | + using AddFunc = OutType (*)(InType, InType); |
40 | 41 |
|
41 | 42 | 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(); |
49 | 53 | mpfr::BinaryInput<InType> input{x, y}; |
50 | 54 | EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Add, input, func(x, y), |
51 | 55 | 0.5); |
52 | 56 | } |
53 | 57 | } |
54 | 58 |
|
55 | 59 | 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(); |
62 | 68 | mpfr::BinaryInput<InType> input{x, y}; |
63 | 69 | EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Add, input, func(x, y), |
64 | 70 | 0.5); |
65 | 71 | } |
66 | 72 | } |
67 | 73 | }; |
68 | 74 |
|
69 | | -#define LIST_ADD_TESTS(suffix, OutType, InType, func) \ |
| 75 | +#define LIST_ADD_TESTS(OutType, InType, func) \ |
| 76 | + using LlvmLibcAddTest = AddTest<OutType, InType>; \ |
| 77 | + TEST_F(LlvmLibcAddTest, SubnormalRange) { test_subnormal_range(&func); } \ |
| 78 | + TEST_F(LlvmLibcAddTest, NormalRange) { test_normal_range(&func); } |
| 79 | + |
| 80 | +#define LIST_ADD_SAME_TYPE_TESTS(suffix, OutType, InType, func) \ |
70 | 81 | using LlvmLibcAddTest##suffix = AddTest<OutType, InType>; \ |
71 | 82 | TEST_F(LlvmLibcAddTest##suffix, SubnormalRange) { \ |
72 | 83 | test_subnormal_range(&func); \ |
|
0 commit comments