xref: /aosp_15_r20/external/llvm-libc/test/src/math/RIntTest.h (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1*71db0c75SAndroid Build Coastguard Worker //===-- Utility class to test different flavors of rint ---------*- 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_RINTTEST_H
10*71db0c75SAndroid Build Coastguard Worker #define LLVM_LIBC_TEST_SRC_MATH_RINTTEST_H
11*71db0c75SAndroid Build Coastguard Worker 
12*71db0c75SAndroid Build Coastguard Worker #include "src/__support/CPP/algorithm.h"
13*71db0c75SAndroid Build Coastguard Worker #include "src/__support/FPUtil/FEnvImpl.h"
14*71db0c75SAndroid Build Coastguard Worker #include "src/__support/FPUtil/FPBits.h"
15*71db0c75SAndroid Build Coastguard Worker #include "test/UnitTest/FEnvSafeTest.h"
16*71db0c75SAndroid Build Coastguard Worker #include "test/UnitTest/FPMatcher.h"
17*71db0c75SAndroid Build Coastguard Worker #include "test/UnitTest/Test.h"
18*71db0c75SAndroid Build Coastguard Worker #include "utils/MPFRWrapper/MPFRUtils.h"
19*71db0c75SAndroid Build Coastguard Worker 
20*71db0c75SAndroid Build Coastguard Worker #include "hdr/fenv_macros.h"
21*71db0c75SAndroid Build Coastguard Worker #include "hdr/math_macros.h"
22*71db0c75SAndroid Build Coastguard Worker 
23*71db0c75SAndroid Build Coastguard Worker namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
24*71db0c75SAndroid Build Coastguard Worker using LIBC_NAMESPACE::Sign;
25*71db0c75SAndroid Build Coastguard Worker 
26*71db0c75SAndroid Build Coastguard Worker static constexpr int ROUNDING_MODES[4] = {FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO,
27*71db0c75SAndroid Build Coastguard Worker                                           FE_TONEAREST};
28*71db0c75SAndroid Build Coastguard Worker 
29*71db0c75SAndroid Build Coastguard Worker template <typename T>
30*71db0c75SAndroid Build Coastguard Worker class RIntTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
31*71db0c75SAndroid Build Coastguard Worker public:
32*71db0c75SAndroid Build Coastguard Worker   typedef T (*RIntFunc)(T);
33*71db0c75SAndroid Build Coastguard Worker 
34*71db0c75SAndroid Build Coastguard Worker private:
35*71db0c75SAndroid Build Coastguard Worker   using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
36*71db0c75SAndroid Build Coastguard Worker   using StorageType = typename FPBits::StorageType;
37*71db0c75SAndroid Build Coastguard Worker 
38*71db0c75SAndroid Build Coastguard Worker   const T inf = FPBits::inf(Sign::POS).get_val();
39*71db0c75SAndroid Build Coastguard Worker   const T neg_inf = FPBits::inf(Sign::NEG).get_val();
40*71db0c75SAndroid Build Coastguard Worker   const T zero = FPBits::zero(Sign::POS).get_val();
41*71db0c75SAndroid Build Coastguard Worker   const T neg_zero = FPBits::zero(Sign::NEG).get_val();
42*71db0c75SAndroid Build Coastguard Worker   const T nan = FPBits::quiet_nan().get_val();
43*71db0c75SAndroid Build Coastguard Worker 
44*71db0c75SAndroid Build Coastguard Worker   static constexpr StorageType MIN_SUBNORMAL =
45*71db0c75SAndroid Build Coastguard Worker       FPBits::min_subnormal().uintval();
46*71db0c75SAndroid Build Coastguard Worker   static constexpr StorageType MAX_SUBNORMAL =
47*71db0c75SAndroid Build Coastguard Worker       FPBits::max_subnormal().uintval();
48*71db0c75SAndroid Build Coastguard Worker   static constexpr StorageType MIN_NORMAL = FPBits::min_normal().uintval();
49*71db0c75SAndroid Build Coastguard Worker   static constexpr StorageType MAX_NORMAL = FPBits::max_normal().uintval();
50*71db0c75SAndroid Build Coastguard Worker 
to_mpfr_rounding_mode(int mode)51*71db0c75SAndroid Build Coastguard Worker   static inline mpfr::RoundingMode to_mpfr_rounding_mode(int mode) {
52*71db0c75SAndroid Build Coastguard Worker     switch (mode) {
53*71db0c75SAndroid Build Coastguard Worker     case FE_UPWARD:
54*71db0c75SAndroid Build Coastguard Worker       return mpfr::RoundingMode::Upward;
55*71db0c75SAndroid Build Coastguard Worker     case FE_DOWNWARD:
56*71db0c75SAndroid Build Coastguard Worker       return mpfr::RoundingMode::Downward;
57*71db0c75SAndroid Build Coastguard Worker     case FE_TOWARDZERO:
58*71db0c75SAndroid Build Coastguard Worker       return mpfr::RoundingMode::TowardZero;
59*71db0c75SAndroid Build Coastguard Worker     case FE_TONEAREST:
60*71db0c75SAndroid Build Coastguard Worker       return mpfr::RoundingMode::Nearest;
61*71db0c75SAndroid Build Coastguard Worker     default:
62*71db0c75SAndroid Build Coastguard Worker       __builtin_unreachable();
63*71db0c75SAndroid Build Coastguard Worker     }
64*71db0c75SAndroid Build Coastguard Worker   }
65*71db0c75SAndroid Build Coastguard Worker 
66*71db0c75SAndroid Build Coastguard Worker public:
testSpecialNumbers(RIntFunc func)67*71db0c75SAndroid Build Coastguard Worker   void testSpecialNumbers(RIntFunc func) {
68*71db0c75SAndroid Build Coastguard Worker     for (int mode : ROUNDING_MODES) {
69*71db0c75SAndroid Build Coastguard Worker       LIBC_NAMESPACE::fputil::set_round(mode);
70*71db0c75SAndroid Build Coastguard Worker       ASSERT_FP_EQ(inf, func(inf));
71*71db0c75SAndroid Build Coastguard Worker       ASSERT_FP_EQ(neg_inf, func(neg_inf));
72*71db0c75SAndroid Build Coastguard Worker       ASSERT_FP_EQ(nan, func(nan));
73*71db0c75SAndroid Build Coastguard Worker       ASSERT_FP_EQ(zero, func(zero));
74*71db0c75SAndroid Build Coastguard Worker       ASSERT_FP_EQ(neg_zero, func(neg_zero));
75*71db0c75SAndroid Build Coastguard Worker     }
76*71db0c75SAndroid Build Coastguard Worker   }
77*71db0c75SAndroid Build Coastguard Worker 
testRoundNumbers(RIntFunc func)78*71db0c75SAndroid Build Coastguard Worker   void testRoundNumbers(RIntFunc func) {
79*71db0c75SAndroid Build Coastguard Worker     for (int mode : ROUNDING_MODES) {
80*71db0c75SAndroid Build Coastguard Worker       LIBC_NAMESPACE::fputil::set_round(mode);
81*71db0c75SAndroid Build Coastguard Worker       mpfr::RoundingMode mpfr_mode = to_mpfr_rounding_mode(mode);
82*71db0c75SAndroid Build Coastguard Worker       ASSERT_FP_EQ(func(T(1.0)), mpfr::round(T(1.0), mpfr_mode));
83*71db0c75SAndroid Build Coastguard Worker       ASSERT_FP_EQ(func(T(-1.0)), mpfr::round(T(-1.0), mpfr_mode));
84*71db0c75SAndroid Build Coastguard Worker       ASSERT_FP_EQ(func(T(10.0)), mpfr::round(T(10.0), mpfr_mode));
85*71db0c75SAndroid Build Coastguard Worker       ASSERT_FP_EQ(func(T(-10.0)), mpfr::round(T(-10.0), mpfr_mode));
86*71db0c75SAndroid Build Coastguard Worker       ASSERT_FP_EQ(func(T(1234.0)), mpfr::round(T(1234.0), mpfr_mode));
87*71db0c75SAndroid Build Coastguard Worker       ASSERT_FP_EQ(func(T(-1234.0)), mpfr::round(T(-1234.0), mpfr_mode));
88*71db0c75SAndroid Build Coastguard Worker     }
89*71db0c75SAndroid Build Coastguard Worker   }
90*71db0c75SAndroid Build Coastguard Worker 
testFractions(RIntFunc func)91*71db0c75SAndroid Build Coastguard Worker   void testFractions(RIntFunc func) {
92*71db0c75SAndroid Build Coastguard Worker     for (int mode : ROUNDING_MODES) {
93*71db0c75SAndroid Build Coastguard Worker       LIBC_NAMESPACE::fputil::set_round(mode);
94*71db0c75SAndroid Build Coastguard Worker       mpfr::RoundingMode mpfr_mode = to_mpfr_rounding_mode(mode);
95*71db0c75SAndroid Build Coastguard Worker       ASSERT_FP_EQ(func(T(0.5)), mpfr::round(T(0.5), mpfr_mode));
96*71db0c75SAndroid Build Coastguard Worker       ASSERT_FP_EQ(func(T(-0.5)), mpfr::round(T(-0.5), mpfr_mode));
97*71db0c75SAndroid Build Coastguard Worker       ASSERT_FP_EQ(func(T(0.115)), mpfr::round(T(0.115), mpfr_mode));
98*71db0c75SAndroid Build Coastguard Worker       ASSERT_FP_EQ(func(T(-0.115)), mpfr::round(T(-0.115), mpfr_mode));
99*71db0c75SAndroid Build Coastguard Worker       ASSERT_FP_EQ(func(T(0.715)), mpfr::round(T(0.715), mpfr_mode));
100*71db0c75SAndroid Build Coastguard Worker       ASSERT_FP_EQ(func(T(-0.715)), mpfr::round(T(-0.715), mpfr_mode));
101*71db0c75SAndroid Build Coastguard Worker     }
102*71db0c75SAndroid Build Coastguard Worker   }
103*71db0c75SAndroid Build Coastguard Worker 
testSubnormalRange(RIntFunc func)104*71db0c75SAndroid Build Coastguard Worker   void testSubnormalRange(RIntFunc func) {
105*71db0c75SAndroid Build Coastguard Worker     constexpr int COUNT = 100'001;
106*71db0c75SAndroid Build Coastguard Worker     constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max(
107*71db0c75SAndroid Build Coastguard Worker         static_cast<StorageType>((MAX_SUBNORMAL - MIN_SUBNORMAL) / COUNT),
108*71db0c75SAndroid Build Coastguard Worker         StorageType(1));
109*71db0c75SAndroid Build Coastguard Worker     for (StorageType i = MIN_SUBNORMAL; i <= MAX_SUBNORMAL; i += STEP) {
110*71db0c75SAndroid Build Coastguard Worker       T x = FPBits(i).get_val();
111*71db0c75SAndroid Build Coastguard Worker       for (int mode : ROUNDING_MODES) {
112*71db0c75SAndroid Build Coastguard Worker         LIBC_NAMESPACE::fputil::set_round(mode);
113*71db0c75SAndroid Build Coastguard Worker         mpfr::RoundingMode mpfr_mode = to_mpfr_rounding_mode(mode);
114*71db0c75SAndroid Build Coastguard Worker         ASSERT_FP_EQ(func(x), mpfr::round(x, mpfr_mode));
115*71db0c75SAndroid Build Coastguard Worker       }
116*71db0c75SAndroid Build Coastguard Worker     }
117*71db0c75SAndroid Build Coastguard Worker   }
118*71db0c75SAndroid Build Coastguard Worker 
119*71db0c75SAndroid Build Coastguard Worker   void testNormalRange(RIntFunc func) {
120*71db0c75SAndroid Build Coastguard Worker     constexpr int COUNT = 100'001;
121*71db0c75SAndroid Build Coastguard Worker     constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max(
122*71db0c75SAndroid Build Coastguard Worker         static_cast<StorageType>((MAX_NORMAL - MIN_NORMAL) / COUNT),
123*71db0c75SAndroid Build Coastguard Worker         StorageType(1));
124*71db0c75SAndroid Build Coastguard Worker     for (StorageType i = MIN_NORMAL; i <= MAX_NORMAL; i += STEP) {
125*71db0c75SAndroid Build Coastguard Worker       FPBits xbits(i);
126*71db0c75SAndroid Build Coastguard Worker       T x = xbits.get_val();
127*71db0c75SAndroid Build Coastguard Worker       // In normal range on x86 platforms, the long double implicit 1 bit can be
128*71db0c75SAndroid Build Coastguard Worker       // zero making the numbers NaN. We will skip them.
129*71db0c75SAndroid Build Coastguard Worker       if (xbits.is_nan())
130*71db0c75SAndroid Build Coastguard Worker         continue;
131*71db0c75SAndroid Build Coastguard Worker 
132*71db0c75SAndroid Build Coastguard Worker       for (int mode : ROUNDING_MODES) {
133*71db0c75SAndroid Build Coastguard Worker         LIBC_NAMESPACE::fputil::set_round(mode);
134*71db0c75SAndroid Build Coastguard Worker         mpfr::RoundingMode mpfr_mode = to_mpfr_rounding_mode(mode);
135*71db0c75SAndroid Build Coastguard Worker         ASSERT_FP_EQ(func(x), mpfr::round(x, mpfr_mode));
136*71db0c75SAndroid Build Coastguard Worker       }
137*71db0c75SAndroid Build Coastguard Worker     }
138*71db0c75SAndroid Build Coastguard Worker   }
139*71db0c75SAndroid Build Coastguard Worker };
140*71db0c75SAndroid Build Coastguard Worker 
141*71db0c75SAndroid Build Coastguard Worker #define LIST_RINT_TESTS(F, func)                                               \
142*71db0c75SAndroid Build Coastguard Worker   using LlvmLibcRIntTest = RIntTestTemplate<F>;                                \
143*71db0c75SAndroid Build Coastguard Worker   TEST_F(LlvmLibcRIntTest, specialNumbers) { testSpecialNumbers(&func); }      \
144*71db0c75SAndroid Build Coastguard Worker   TEST_F(LlvmLibcRIntTest, RoundNumbers) { testRoundNumbers(&func); }          \
145*71db0c75SAndroid Build Coastguard Worker   TEST_F(LlvmLibcRIntTest, Fractions) { testFractions(&func); }                \
146*71db0c75SAndroid Build Coastguard Worker   TEST_F(LlvmLibcRIntTest, SubnormalRange) { testSubnormalRange(&func); }      \
147*71db0c75SAndroid Build Coastguard Worker   TEST_F(LlvmLibcRIntTest, NormalRange) { testNormalRange(&func); }
148*71db0c75SAndroid Build Coastguard Worker 
149*71db0c75SAndroid Build Coastguard Worker #endif // LLVM_LIBC_TEST_SRC_MATH_RINTTEST_H
150