1 //===-- Utility class to test different flavors of [l|ll]round --*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_TEST_SRC_MATH_ROUNDTOINTEGERTEST_H
10 #define LLVM_LIBC_TEST_SRC_MATH_ROUNDTOINTEGERTEST_H
12 #include "src/__support/FPUtil/FEnvImpl.h"
13 #include "src/__support/FPUtil/FPBits.h"
14 #include "test/UnitTest/FPMatcher.h"
15 #include "test/UnitTest/Test.h"
16 #include "utils/MPFRWrapper/MPFRUtils.h"
21 namespace mpfr
= __llvm_libc::testing::mpfr
;
23 static constexpr int ROUNDING_MODES
[4] = {FE_UPWARD
, FE_DOWNWARD
, FE_TOWARDZERO
,
26 template <typename F
, typename I
, bool TestModes
= false>
27 class RoundToIntegerTestTemplate
: public __llvm_libc::testing::Test
{
29 typedef I (*RoundToIntegerFunc
)(F
);
32 using FPBits
= __llvm_libc::fputil::FPBits
<F
>;
33 using UIntType
= typename
FPBits::UIntType
;
35 const F zero
= F(__llvm_libc::fputil::FPBits
<F
>::zero());
36 const F neg_zero
= F(__llvm_libc::fputil::FPBits
<F
>::neg_zero());
37 const F inf
= F(__llvm_libc::fputil::FPBits
<F
>::inf());
38 const F neg_inf
= F(__llvm_libc::fputil::FPBits
<F
>::neg_inf());
39 const F nan
= F(__llvm_libc::fputil::FPBits
<F
>::build_quiet_nan(1));
40 static constexpr I INTEGER_MIN
= I(1) << (sizeof(I
) * 8 - 1);
41 static constexpr I INTEGER_MAX
= -(INTEGER_MIN
+ 1);
43 void test_one_input(RoundToIntegerFunc func
, F input
, I expected
,
46 __llvm_libc::fputil::clear_except(FE_ALL_EXCEPT
);
48 ASSERT_EQ(func(input
), expected
);
51 ASSERT_FP_EXCEPTION(FE_INVALID
);
52 ASSERT_MATH_ERRNO(EDOM
);
54 ASSERT_FP_EXCEPTION(0);
59 static inline mpfr::RoundingMode
to_mpfr_rounding_mode(int mode
) {
62 return mpfr::RoundingMode::Upward
;
64 return mpfr::RoundingMode::Downward
;
66 return mpfr::RoundingMode::TowardZero
;
68 return mpfr::RoundingMode::Nearest
;
70 __builtin_unreachable();
75 void SetUp() override
{
76 if (math_errhandling
& MATH_ERREXCEPT
) {
77 // We will disable all exceptions so that the test will not
78 // crash with SIGFPE. We can still use fetestexcept to check
79 // if the appropriate flag was raised.
80 __llvm_libc::fputil::disable_except(FE_ALL_EXCEPT
);
84 void do_infinity_and_na_n_test(RoundToIntegerFunc func
) {
85 test_one_input(func
, inf
, INTEGER_MAX
, true);
86 test_one_input(func
, neg_inf
, INTEGER_MIN
, true);
87 // This is currently never enabled, the
88 // LLVM_LIBC_IMPLEMENTATION_DEFINED_TEST_BEHAVIOR CMake option in
89 // libc/CMakeLists.txt is not forwarded to C++.
90 #if LIBC_COPT_IMPLEMENTATION_DEFINED_TEST_BEHAVIOR
91 // Result is not well-defined, we always returns INTEGER_MAX
92 test_one_input(func
, nan
, INTEGER_MAX
, true);
93 #endif // LIBC_COPT_IMPLEMENTATION_DEFINED_TEST_BEHAVIOR
96 void testInfinityAndNaN(RoundToIntegerFunc func
) {
98 for (int mode
: ROUNDING_MODES
) {
99 __llvm_libc::fputil::set_round(mode
);
100 do_infinity_and_na_n_test(func
);
103 do_infinity_and_na_n_test(func
);
107 void do_round_numbers_test(RoundToIntegerFunc func
) {
108 test_one_input(func
, zero
, I(0), false);
109 test_one_input(func
, neg_zero
, I(0), false);
110 test_one_input(func
, F(1.0), I(1), false);
111 test_one_input(func
, F(-1.0), I(-1), false);
112 test_one_input(func
, F(10.0), I(10), false);
113 test_one_input(func
, F(-10.0), I(-10), false);
114 test_one_input(func
, F(1234.0), I(1234), false);
115 test_one_input(func
, F(-1234.0), I(-1234), false);
117 // The rest of this this function compares with an equivalent MPFR function
118 // which rounds floating point numbers to long values. There is no MPFR
119 // function to round to long long or wider integer values. So, we will
120 // the remaining tests only if the width of I less than equal to that of
122 if (sizeof(I
) > sizeof(long))
125 constexpr int EXPONENT_LIMIT
= sizeof(I
) * 8 - 1;
126 // We start with 1.0 so that the implicit bit for x86 long doubles
129 bits
.set_unbiased_exponent(EXPONENT_LIMIT
+ FPBits::EXPONENT_BIAS
);
131 bits
.set_mantissa(0);
135 bool erangeflag
= mpfr::round_to_long(x
, mpfr_result
);
136 ASSERT_FALSE(erangeflag
);
137 test_one_input(func
, x
, mpfr_result
, false);
140 void testRoundNumbers(RoundToIntegerFunc func
) {
142 for (int mode
: ROUNDING_MODES
) {
143 __llvm_libc::fputil::set_round(mode
);
144 do_round_numbers_test(func
);
147 do_round_numbers_test(func
);
151 void do_fractions_test(RoundToIntegerFunc func
, int mode
) {
152 constexpr F FRACTIONS
[] = {0.5, -0.5, 0.115, -0.115, 0.715, -0.715};
153 for (F x
: FRACTIONS
) {
154 long mpfr_long_result
;
157 erangeflag
= mpfr::round_to_long(x
, to_mpfr_rounding_mode(mode
),
160 erangeflag
= mpfr::round_to_long(x
, mpfr_long_result
);
161 ASSERT_FALSE(erangeflag
);
162 I mpfr_result
= mpfr_long_result
;
163 test_one_input(func
, x
, mpfr_result
, false);
167 void testFractions(RoundToIntegerFunc func
) {
169 for (int mode
: ROUNDING_MODES
) {
170 __llvm_libc::fputil::set_round(mode
);
171 do_fractions_test(func
, mode
);
174 // Passing 0 for mode has no effect as it is not used in doFractionsTest
175 // when `TestModes` is false;
176 do_fractions_test(func
, 0);
180 void testIntegerOverflow(RoundToIntegerFunc func
) {
181 // This function compares with an equivalent MPFR function which rounds
182 // floating point numbers to long values. There is no MPFR function to
183 // round to long long or wider integer values. So, we will peform the
184 // comparisons in this function only if the width of I less than equal to
186 if (sizeof(I
) > sizeof(long))
189 constexpr int EXPONENT_LIMIT
= sizeof(I
) * 8 - 1;
190 // We start with 1.0 so that the implicit bit for x86 long doubles
193 bits
.set_unbiased_exponent(EXPONENT_LIMIT
+ FPBits::EXPONENT_BIAS
);
195 bits
.set_mantissa(UIntType(0x1)
196 << (__llvm_libc::fputil::MantissaWidth
<F
>::VALUE
- 1));
200 for (int m
: ROUNDING_MODES
) {
201 __llvm_libc::fputil::set_round(m
);
202 long mpfr_long_result
;
204 mpfr::round_to_long(x
, to_mpfr_rounding_mode(m
), mpfr_long_result
);
205 ASSERT_TRUE(erangeflag
);
206 test_one_input(func
, x
, INTEGER_MIN
, true);
209 long mpfr_long_result
;
210 bool erangeflag
= mpfr::round_to_long(x
, mpfr_long_result
);
211 ASSERT_TRUE(erangeflag
);
212 test_one_input(func
, x
, INTEGER_MIN
, true);
216 void testSubnormalRange(RoundToIntegerFunc func
) {
217 constexpr UIntType COUNT
= 1000001;
218 constexpr UIntType STEP
=
219 (FPBits::MAX_SUBNORMAL
- FPBits::MIN_SUBNORMAL
) / COUNT
;
220 for (UIntType i
= FPBits::MIN_SUBNORMAL
; i
<= FPBits::MAX_SUBNORMAL
;
225 // All subnormal numbers should round to zero.
228 __llvm_libc::fputil::set_round(FE_UPWARD
);
229 test_one_input(func
, x
, I(1), false);
230 __llvm_libc::fputil::set_round(FE_DOWNWARD
);
231 test_one_input(func
, x
, I(0), false);
232 __llvm_libc::fputil::set_round(FE_TOWARDZERO
);
233 test_one_input(func
, x
, I(0), false);
234 __llvm_libc::fputil::set_round(FE_TONEAREST
);
235 test_one_input(func
, x
, I(0), false);
237 __llvm_libc::fputil::set_round(FE_UPWARD
);
238 test_one_input(func
, x
, I(0), false);
239 __llvm_libc::fputil::set_round(FE_DOWNWARD
);
240 test_one_input(func
, x
, I(-1), false);
241 __llvm_libc::fputil::set_round(FE_TOWARDZERO
);
242 test_one_input(func
, x
, I(0), false);
243 __llvm_libc::fputil::set_round(FE_TONEAREST
);
244 test_one_input(func
, x
, I(0), false);
247 test_one_input(func
, x
, 0L, false);
252 void testNormalRange(RoundToIntegerFunc func
) {
253 // This function compares with an equivalent MPFR function which rounds
254 // floating point numbers to long values. There is no MPFR function to
255 // round to long long or wider integer values. So, we will peform the
256 // comparisons in this function only if the width of I less than equal to
258 if (sizeof(I
) > sizeof(long))
261 constexpr UIntType COUNT
= 1000001;
262 constexpr UIntType STEP
= (FPBits::MAX_NORMAL
- FPBits::MIN_NORMAL
) / COUNT
;
263 for (UIntType i
= FPBits::MIN_NORMAL
; i
<= FPBits::MAX_NORMAL
; i
+= STEP
) {
265 // In normal range on x86 platforms, the long double implicit 1 bit can be
266 // zero making the numbers NaN. We will skip them.
272 for (int m
: ROUNDING_MODES
) {
273 long mpfr_long_result
;
274 bool erangeflag
= mpfr::round_to_long(x
, to_mpfr_rounding_mode(m
),
276 I mpfr_result
= mpfr_long_result
;
277 __llvm_libc::fputil::set_round(m
);
279 test_one_input(func
, x
, x
> 0 ? INTEGER_MAX
: INTEGER_MIN
, true);
281 test_one_input(func
, x
, mpfr_result
, false);
284 long mpfr_long_result
;
285 bool erangeflag
= mpfr::round_to_long(x
, mpfr_long_result
);
286 I mpfr_result
= mpfr_long_result
;
288 test_one_input(func
, x
, x
> 0 ? INTEGER_MAX
: INTEGER_MIN
, true);
290 test_one_input(func
, x
, mpfr_result
, false);
296 #define LIST_ROUND_TO_INTEGER_TESTS_HELPER(F, I, func, TestModes) \
297 using LlvmLibcRoundToIntegerTest = \
298 RoundToIntegerTestTemplate<F, I, TestModes>; \
299 TEST_F(LlvmLibcRoundToIntegerTest, InfinityAndNaN) { \
300 testInfinityAndNaN(&func); \
302 TEST_F(LlvmLibcRoundToIntegerTest, RoundNumbers) { \
303 testRoundNumbers(&func); \
305 TEST_F(LlvmLibcRoundToIntegerTest, Fractions) { testFractions(&func); } \
306 TEST_F(LlvmLibcRoundToIntegerTest, IntegerOverflow) { \
307 testIntegerOverflow(&func); \
309 TEST_F(LlvmLibcRoundToIntegerTest, SubnormalRange) { \
310 testSubnormalRange(&func); \
312 TEST_F(LlvmLibcRoundToIntegerTest, NormalRange) { testNormalRange(&func); }
314 #define LIST_ROUND_TO_INTEGER_TESTS(F, I, func) \
315 LIST_ROUND_TO_INTEGER_TESTS_HELPER(F, I, func, false)
317 #define LIST_ROUND_TO_INTEGER_TESTS_WITH_MODES(F, I, func) \
318 LIST_ROUND_TO_INTEGER_TESTS_HELPER(F, I, func, true)
320 #endif // LLVM_LIBC_TEST_SRC_MATH_ROUNDTOINTEGERTEST_H