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