• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- Utility class to test different flavors of nearbyint ----*- 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_NEARBYINTTEST_H
10 #define LLVM_LIBC_TEST_SRC_MATH_NEARBYINTTEST_H
11 
12 #include "hdr/fenv_macros.h"
13 #include "src/__support/FPUtil/FEnvImpl.h"
14 #include "src/__support/FPUtil/FPBits.h"
15 #include "test/UnitTest/FPMatcher.h"
16 #include "test/UnitTest/Test.h"
17 
18 static constexpr int ROUNDING_MODES[4] = {FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO,
19                                           FE_TONEAREST};
20 
21 template <typename T>
22 class NearbyIntTestTemplate : public LIBC_NAMESPACE::testing::Test {
23 
24   DECLARE_SPECIAL_CONSTANTS(T)
25 
26 public:
27   typedef T (*NearbyIntFunc)(T);
28 
testNaN(NearbyIntFunc func)29   void testNaN(NearbyIntFunc func) {
30     EXPECT_FP_EQ_ALL_ROUNDING(func(aNaN), aNaN);
31   }
32 
testInfinities(NearbyIntFunc func)33   void testInfinities(NearbyIntFunc func) {
34     EXPECT_FP_EQ_ALL_ROUNDING(func(inf), inf);
35     EXPECT_FP_EQ_ALL_ROUNDING(func(neg_inf), neg_inf);
36   }
37 
testZeroes(NearbyIntFunc func)38   void testZeroes(NearbyIntFunc func) {
39     EXPECT_FP_EQ_ALL_ROUNDING(func(zero), zero);
40     EXPECT_FP_EQ_ALL_ROUNDING(func(neg_zero), neg_zero);
41   }
42 
testIntegers(NearbyIntFunc func)43   void testIntegers(NearbyIntFunc func) {
44     EXPECT_FP_EQ_ALL_ROUNDING(func(T(1.0)), T(1.0));
45     EXPECT_FP_EQ_ALL_ROUNDING(func(T(-1.0)), T(-1.0));
46 
47     EXPECT_FP_EQ_ALL_ROUNDING(func(T(1234.0)), T(1234.0));
48     EXPECT_FP_EQ_ALL_ROUNDING(func(T(-1234.0)), T(-1234.0));
49 
50     EXPECT_FP_EQ_ALL_ROUNDING(func(T(10.0)), T(10.0));
51     EXPECT_FP_EQ_ALL_ROUNDING(func(T(-10.0)), T(-10.0));
52 
53     FPBits ints_start(T(0));
54     ints_start.set_biased_exponent(FPBits::SIG_LEN + FPBits::EXP_BIAS);
55     T expected = ints_start.get_val();
56     EXPECT_FP_EQ_ALL_ROUNDING(func(expected), expected);
57   }
58 
testSubnormalToNearest(NearbyIntFunc func)59   void testSubnormalToNearest(NearbyIntFunc func) {
60     ASSERT_FP_EQ(func(min_denormal), zero);
61     ASSERT_FP_EQ(func(-min_denormal), neg_zero);
62   }
63 
testSubnormalTowardZero(NearbyIntFunc func)64   void testSubnormalTowardZero(NearbyIntFunc func) {
65     EXPECT_FP_EQ_ROUNDING_TOWARD_ZERO(func(min_denormal), zero);
66     EXPECT_FP_EQ_ROUNDING_TOWARD_ZERO(func(-min_denormal), neg_zero);
67   }
68 
testSubnormalToPosInf(NearbyIntFunc func)69   void testSubnormalToPosInf(NearbyIntFunc func) {
70     EXPECT_FP_EQ_ROUNDING_UPWARD(func(min_denormal), FPBits::one().get_val());
71     EXPECT_FP_EQ_ROUNDING_UPWARD(func(-min_denormal), neg_zero);
72   }
73 
testSubnormalToNegInf(NearbyIntFunc func)74   void testSubnormalToNegInf(NearbyIntFunc func) {
75     T negative_one = FPBits::one(Sign::NEG).get_val();
76     EXPECT_FP_EQ_ROUNDING_DOWNWARD(func(min_denormal), zero);
77     EXPECT_FP_EQ_ROUNDING_DOWNWARD(func(-min_denormal), negative_one);
78   }
79 };
80 
81 #define LIST_NEARBYINT_TESTS(T, func)                                          \
82   using LlvmLibcNearbyIntTest = NearbyIntTestTemplate<T>;                      \
83   TEST_F(LlvmLibcNearbyIntTest, TestNaN) { testNaN(&func); }                   \
84   TEST_F(LlvmLibcNearbyIntTest, TestInfinities) { testInfinities(&func); }     \
85   TEST_F(LlvmLibcNearbyIntTest, TestZeroes) { testZeroes(&func); }             \
86   TEST_F(LlvmLibcNearbyIntTest, TestIntegers) { testIntegers(&func); }         \
87   TEST_F(LlvmLibcNearbyIntTest, TestSubnormalToNearest) {                      \
88     testSubnormalToNearest(&func);                                             \
89   }                                                                            \
90   TEST_F(LlvmLibcNearbyIntTest, TestSubnormalTowardZero) {                     \
91     testSubnormalTowardZero(&func);                                            \
92   }                                                                            \
93   TEST_F(LlvmLibcNearbyIntTest, TestSubnormalToPosInf) {                       \
94     testSubnormalToPosInf(&func);                                              \
95   }                                                                            \
96   TEST_F(LlvmLibcNearbyIntTest, TestSubnormalToNegInf) {                       \
97     testSubnormalToNegInf(&func);                                              \
98   }
99 
100 #endif // LLVM_LIBC_TEST_SRC_MATH_NEARBYINTTEST_H
101