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_SMOKE_RINTTEST_H |
10 | #define LLVM_LIBC_TEST_SRC_MATH_SMOKE_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 | |
18 | #include "hdr/fenv_macros.h" |
19 | #include "hdr/math_macros.h" |
20 | #include <stdio.h> |
21 | |
22 | static constexpr int ROUNDING_MODES[4] = {FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO, |
23 | FE_TONEAREST}; |
24 | |
25 | template <typename T> |
26 | class RIntTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest { |
27 | public: |
28 | typedef T (*RIntFunc)(T); |
29 | |
30 | private: |
31 | using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>; |
32 | using StorageType = typename FPBits::StorageType; |
33 | |
34 | const T inf = FPBits::inf(Sign::POS).get_val(); |
35 | const T neg_inf = FPBits::inf(Sign::NEG).get_val(); |
36 | const T zero = FPBits::zero(Sign::POS).get_val(); |
37 | const T neg_zero = FPBits::zero(Sign::NEG).get_val(); |
38 | const T nan = FPBits::quiet_nan().get_val(); |
39 | |
40 | public: |
41 | void testSpecialNumbers(RIntFunc func) { |
42 | for (int mode : ROUNDING_MODES) { |
43 | LIBC_NAMESPACE::fputil::set_round(mode); |
44 | ASSERT_FP_EQ(inf, func(inf)); |
45 | ASSERT_FP_EQ(neg_inf, func(neg_inf)); |
46 | ASSERT_FP_EQ(nan, func(nan)); |
47 | ASSERT_FP_EQ(zero, func(zero)); |
48 | ASSERT_FP_EQ(neg_zero, func(neg_zero)); |
49 | } |
50 | } |
51 | }; |
52 | |
53 | #define LIST_RINT_TESTS(F, func) \ |
54 | using LlvmLibcRIntTest = RIntTestTemplate<F>; \ |
55 | TEST_F(LlvmLibcRIntTest, specialNumbers) { testSpecialNumbers(&func); } |
56 | |
57 | #endif // LLVM_LIBC_TEST_SRC_MATH_SMOKE_RINTTEST_H |
58 | |