• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- Utility class to test round[f|l] ------------------------*- 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_ROUNDTEST_H
10 #define LLVM_LIBC_TEST_SRC_MATH_ROUNDTEST_H
11 
12 #include "test/UnitTest/FEnvSafeTest.h"
13 #include "test/UnitTest/FPMatcher.h"
14 #include "test/UnitTest/Test.h"
15 #include "utils/MPFRWrapper/MPFRUtils.h"
16 
17 #include "hdr/math_macros.h"
18 
19 namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
20 
21 template <typename T>
22 class RoundTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
23 
24   DECLARE_SPECIAL_CONSTANTS(T)
25 
26 public:
27   typedef T (*RoundFunc)(T);
28 
testSpecialNumbers(RoundFunc func)29   void testSpecialNumbers(RoundFunc func) {
30     EXPECT_FP_EQ(zero, func(zero));
31     EXPECT_FP_EQ(neg_zero, func(neg_zero));
32 
33     EXPECT_FP_EQ(inf, func(inf));
34     EXPECT_FP_EQ(neg_inf, func(neg_inf));
35 
36     EXPECT_FP_EQ(aNaN, func(aNaN));
37   }
38 
testRoundedNumbers(RoundFunc func)39   void testRoundedNumbers(RoundFunc func) {
40     EXPECT_FP_EQ(T(1.0), func(T(1.0)));
41     EXPECT_FP_EQ(T(-1.0), func(T(-1.0)));
42     EXPECT_FP_EQ(T(10.0), func(T(10.0)));
43     EXPECT_FP_EQ(T(-10.0), func(T(-10.0)));
44     EXPECT_FP_EQ(T(1234.0), func(T(1234.0)));
45     EXPECT_FP_EQ(T(-1234.0), func(T(-1234.0)));
46   }
47 
testFractions(RoundFunc func)48   void testFractions(RoundFunc func) {
49     EXPECT_FP_EQ(T(1.0), func(T(0.5)));
50     EXPECT_FP_EQ(T(-1.0), func(T(-0.5)));
51     EXPECT_FP_EQ(T(0.0), func(T(0.115)));
52     EXPECT_FP_EQ(T(-0.0), func(T(-0.115)));
53     EXPECT_FP_EQ(T(1.0), func(T(0.715)));
54     EXPECT_FP_EQ(T(-1.0), func(T(-0.715)));
55     EXPECT_FP_EQ(T(1.0), func(T(1.3)));
56     EXPECT_FP_EQ(T(-1.0), func(T(-1.3)));
57     EXPECT_FP_EQ(T(2.0), func(T(1.5)));
58     EXPECT_FP_EQ(T(-2.0), func(T(-1.5)));
59     EXPECT_FP_EQ(T(2.0), func(T(1.75)));
60     EXPECT_FP_EQ(T(-2.0), func(T(-1.75)));
61     EXPECT_FP_EQ(T(10.0), func(T(10.32)));
62     EXPECT_FP_EQ(T(-10.0), func(T(-10.32)));
63     EXPECT_FP_EQ(T(11.0), func(T(10.65)));
64     EXPECT_FP_EQ(T(-11.0), func(T(-10.65)));
65     EXPECT_FP_EQ(T(1234.0), func(T(1234.38)));
66     EXPECT_FP_EQ(T(-1234.0), func(T(-1234.38)));
67     EXPECT_FP_EQ(T(1235.0), func(T(1234.96)));
68     EXPECT_FP_EQ(T(-1235.0), func(T(-1234.96)));
69   }
70 
testRange(RoundFunc func)71   void testRange(RoundFunc func) {
72     constexpr StorageType COUNT = 100'000;
73     constexpr StorageType STEP = STORAGE_MAX / COUNT;
74     for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
75       T x = FPBits(v).get_val();
76       if (isnan(x) || isinf(x))
77         continue;
78 
79       ASSERT_MPFR_MATCH(mpfr::Operation::Round, x, func(x), 0.0);
80     }
81   }
82 };
83 
84 #define LIST_ROUND_TESTS(T, func)                                              \
85   using LlvmLibcRoundTest = RoundTest<T>;                                      \
86   TEST_F(LlvmLibcRoundTest, SpecialNumbers) { testSpecialNumbers(&func); }     \
87   TEST_F(LlvmLibcRoundTest, RoundedNubmers) { testRoundedNumbers(&func); }     \
88   TEST_F(LlvmLibcRoundTest, Fractions) { testFractions(&func); }               \
89   TEST_F(LlvmLibcRoundTest, Range) { testRange(&func); }
90 
91 #endif // LLVM_LIBC_TEST_SRC_MATH_ROUNDTEST_H
92