xref: /aosp_15_r20/external/llvm-libc/test/src/math/SubTest.h (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1*71db0c75SAndroid Build Coastguard Worker //===-- Utility class to test different flavors of float sub ----*- C++ -*-===//
2*71db0c75SAndroid Build Coastguard Worker //
3*71db0c75SAndroid Build Coastguard Worker // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*71db0c75SAndroid Build Coastguard Worker // See https://llvm.org/LICENSE.txt for license information.
5*71db0c75SAndroid Build Coastguard Worker // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*71db0c75SAndroid Build Coastguard Worker //
7*71db0c75SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
8*71db0c75SAndroid Build Coastguard Worker 
9*71db0c75SAndroid Build Coastguard Worker #ifndef LLVM_LIBC_TEST_SRC_MATH_SUBTEST_H
10*71db0c75SAndroid Build Coastguard Worker #define LLVM_LIBC_TEST_SRC_MATH_SUBTEST_H
11*71db0c75SAndroid Build Coastguard Worker 
12*71db0c75SAndroid Build Coastguard Worker #include "test/UnitTest/FEnvSafeTest.h"
13*71db0c75SAndroid Build Coastguard Worker #include "test/UnitTest/FPMatcher.h"
14*71db0c75SAndroid Build Coastguard Worker #include "test/UnitTest/Test.h"
15*71db0c75SAndroid Build Coastguard Worker #include "utils/MPFRWrapper/MPFRUtils.h"
16*71db0c75SAndroid Build Coastguard Worker 
17*71db0c75SAndroid Build Coastguard Worker namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
18*71db0c75SAndroid Build Coastguard Worker 
19*71db0c75SAndroid Build Coastguard Worker template <typename OutType, typename InType>
20*71db0c75SAndroid Build Coastguard Worker class SubTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
21*71db0c75SAndroid Build Coastguard Worker 
22*71db0c75SAndroid Build Coastguard Worker   struct InConstants {
23*71db0c75SAndroid Build Coastguard Worker     DECLARE_SPECIAL_CONSTANTS(InType)
24*71db0c75SAndroid Build Coastguard Worker   };
25*71db0c75SAndroid Build Coastguard Worker 
26*71db0c75SAndroid Build Coastguard Worker   using InFPBits = typename InConstants::FPBits;
27*71db0c75SAndroid Build Coastguard Worker   using InStorageType = typename InConstants::StorageType;
28*71db0c75SAndroid Build Coastguard Worker 
29*71db0c75SAndroid Build Coastguard Worker   static constexpr InStorageType IN_MAX_NORMAL_U =
30*71db0c75SAndroid Build Coastguard Worker       InFPBits::max_normal().uintval();
31*71db0c75SAndroid Build Coastguard Worker   static constexpr InStorageType IN_MIN_NORMAL_U =
32*71db0c75SAndroid Build Coastguard Worker       InFPBits::min_normal().uintval();
33*71db0c75SAndroid Build Coastguard Worker   static constexpr InStorageType IN_MAX_SUBNORMAL_U =
34*71db0c75SAndroid Build Coastguard Worker       InFPBits::max_subnormal().uintval();
35*71db0c75SAndroid Build Coastguard Worker   static constexpr InStorageType IN_MIN_SUBNORMAL_U =
36*71db0c75SAndroid Build Coastguard Worker       InFPBits::min_subnormal().uintval();
37*71db0c75SAndroid Build Coastguard Worker 
38*71db0c75SAndroid Build Coastguard Worker public:
39*71db0c75SAndroid Build Coastguard Worker   using SubFunc = OutType (*)(InType, InType);
40*71db0c75SAndroid Build Coastguard Worker 
test_subnormal_range(SubFunc func)41*71db0c75SAndroid Build Coastguard Worker   void test_subnormal_range(SubFunc func) {
42*71db0c75SAndroid Build Coastguard Worker     constexpr InStorageType COUNT = 100'001;
43*71db0c75SAndroid Build Coastguard Worker     constexpr InStorageType STEP =
44*71db0c75SAndroid Build Coastguard Worker         (IN_MAX_SUBNORMAL_U - IN_MIN_SUBNORMAL_U) / COUNT;
45*71db0c75SAndroid Build Coastguard Worker     for (InStorageType i = 0, v = 0, w = IN_MAX_SUBNORMAL_U; i <= COUNT;
46*71db0c75SAndroid Build Coastguard Worker          ++i, v += STEP, w -= STEP) {
47*71db0c75SAndroid Build Coastguard Worker       InType x = InFPBits(v).get_val();
48*71db0c75SAndroid Build Coastguard Worker       InType y = InFPBits(w).get_val();
49*71db0c75SAndroid Build Coastguard Worker       mpfr::BinaryInput<InType> input{x, y};
50*71db0c75SAndroid Build Coastguard Worker       EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sub, input, func(x, y),
51*71db0c75SAndroid Build Coastguard Worker                                      0.5);
52*71db0c75SAndroid Build Coastguard Worker     }
53*71db0c75SAndroid Build Coastguard Worker   }
54*71db0c75SAndroid Build Coastguard Worker 
55*71db0c75SAndroid Build Coastguard Worker   void test_normal_range(SubFunc func) {
56*71db0c75SAndroid Build Coastguard Worker     constexpr InStorageType COUNT = 100'001;
57*71db0c75SAndroid Build Coastguard Worker     constexpr InStorageType STEP = (IN_MAX_NORMAL_U - IN_MIN_NORMAL_U) / COUNT;
58*71db0c75SAndroid Build Coastguard Worker     for (InStorageType i = 0, v = 0, w = IN_MAX_NORMAL_U; i <= COUNT;
59*71db0c75SAndroid Build Coastguard Worker          ++i, v += STEP, w -= STEP) {
60*71db0c75SAndroid Build Coastguard Worker       InType x = InFPBits(v).get_val();
61*71db0c75SAndroid Build Coastguard Worker       InType y = InFPBits(w).get_val();
62*71db0c75SAndroid Build Coastguard Worker       mpfr::BinaryInput<InType> input{x, y};
63*71db0c75SAndroid Build Coastguard Worker       EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sub, input, func(x, y),
64*71db0c75SAndroid Build Coastguard Worker                                      0.5);
65*71db0c75SAndroid Build Coastguard Worker     }
66*71db0c75SAndroid Build Coastguard Worker   }
67*71db0c75SAndroid Build Coastguard Worker };
68*71db0c75SAndroid Build Coastguard Worker 
69*71db0c75SAndroid Build Coastguard Worker #define LIST_SUB_TESTS(OutType, InType, func)                                  \
70*71db0c75SAndroid Build Coastguard Worker   using LlvmLibcSubTest = SubTest<OutType, InType>;                            \
71*71db0c75SAndroid Build Coastguard Worker   TEST_F(LlvmLibcSubTest, SubnormalRange) { test_subnormal_range(&func); }     \
72*71db0c75SAndroid Build Coastguard Worker   TEST_F(LlvmLibcSubTest, NormalRange) { test_normal_range(&func); }
73*71db0c75SAndroid Build Coastguard Worker 
74*71db0c75SAndroid Build Coastguard Worker #endif // LLVM_LIBC_TEST_SRC_MATH_SUBTEST_H
75