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 #include "utils/FPUtil/TestHelpers.h"
10 #include "utils/MPFRWrapper/MPFRUtils.h"
11 #include "utils/UnitTest/Test.h"
12 
13 #include <math.h>
14 
15 namespace mpfr = __llvm_libc::testing::mpfr;
16 
17 template <typename T> class RoundTest : public __llvm_libc::testing::Test {
18 
19   DECLARE_SPECIAL_CONSTANTS(T)
20 
21 public:
22   typedef T (*RoundFunc)(T);
23 
testSpecialNumbers(RoundFunc func)24   void testSpecialNumbers(RoundFunc func) {
25     EXPECT_FP_EQ(zero, func(zero));
26     EXPECT_FP_EQ(negZero, func(negZero));
27 
28     EXPECT_FP_EQ(inf, func(inf));
29     EXPECT_FP_EQ(negInf, func(negInf));
30 
31     EXPECT_FP_EQ(aNaN, func(aNaN));
32   }
33 
testRoundedNumbers(RoundFunc func)34   void testRoundedNumbers(RoundFunc func) {
35     EXPECT_FP_EQ(T(1.0), func(T(1.0)));
36     EXPECT_FP_EQ(T(-1.0), func(T(-1.0)));
37     EXPECT_FP_EQ(T(10.0), func(T(10.0)));
38     EXPECT_FP_EQ(T(-10.0), func(T(-10.0)));
39     EXPECT_FP_EQ(T(1234.0), func(T(1234.0)));
40     EXPECT_FP_EQ(T(-1234.0), func(T(-1234.0)));
41   }
42 
testFractions(RoundFunc func)43   void testFractions(RoundFunc func) {
44     EXPECT_FP_EQ(T(1.0), func(T(0.5)));
45     EXPECT_FP_EQ(T(-1.0), func(T(-0.5)));
46     EXPECT_FP_EQ(T(0.0), func(T(0.115)));
47     EXPECT_FP_EQ(T(-0.0), func(T(-0.115)));
48     EXPECT_FP_EQ(T(1.0), func(T(0.715)));
49     EXPECT_FP_EQ(T(-1.0), func(T(-0.715)));
50     EXPECT_FP_EQ(T(1.0), func(T(1.3)));
51     EXPECT_FP_EQ(T(-1.0), func(T(-1.3)));
52     EXPECT_FP_EQ(T(2.0), func(T(1.5)));
53     EXPECT_FP_EQ(T(-2.0), func(T(-1.5)));
54     EXPECT_FP_EQ(T(2.0), func(T(1.75)));
55     EXPECT_FP_EQ(T(-2.0), func(T(-1.75)));
56     EXPECT_FP_EQ(T(10.0), func(T(10.32)));
57     EXPECT_FP_EQ(T(-10.0), func(T(-10.32)));
58     EXPECT_FP_EQ(T(11.0), func(T(10.65)));
59     EXPECT_FP_EQ(T(-11.0), func(T(-10.65)));
60     EXPECT_FP_EQ(T(1234.0), func(T(1234.38)));
61     EXPECT_FP_EQ(T(-1234.0), func(T(-1234.38)));
62     EXPECT_FP_EQ(T(1235.0), func(T(1234.96)));
63     EXPECT_FP_EQ(T(-1235.0), func(T(-1234.96)));
64   }
65 
testRange(RoundFunc func)66   void testRange(RoundFunc func) {
67     constexpr UIntType count = 10000000;
68     constexpr UIntType step = UIntType(-1) / count;
69     for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
70       T x = T(FPBits(v));
71       if (isnan(x) || isinf(x))
72         continue;
73 
74       ASSERT_MPFR_MATCH(mpfr::Operation::Round, x, func(x), 0.0);
75     }
76   }
77 };
78 
79 #define LIST_ROUND_TESTS(T, func)                                              \
80   using LlvmLibcRoundTest = RoundTest<T>;                                      \
81   TEST_F(LlvmLibcRoundTest, SpecialNumbers) { testSpecialNumbers(&func); }     \
82   TEST_F(LlvmLibcRoundTest, RoundedNubmers) { testRoundedNumbers(&func); }     \
83   TEST_F(LlvmLibcRoundTest, Fractions) { testFractions(&func); }               \
84   TEST_F(LlvmLibcRoundTest, Range) { testRange(&func); }
85