1 | //===-- Utility class to test different flavors of fdim ---------*- 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 "src/__support/FPUtil/BasicOperations.h" |
10 | #include "src/__support/FPUtil/FPBits.h" |
11 | #include "test/UnitTest/FEnvSafeTest.h" |
12 | #include "test/UnitTest/FPMatcher.h" |
13 | #include "test/UnitTest/Test.h" |
14 | |
15 | template <typename T> |
16 | class FDimTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest { |
17 | public: |
18 | using FuncPtr = T (*)(T, T); |
19 | using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>; |
20 | using StorageType = typename FPBits::StorageType; |
21 | |
22 | const T inf = FPBits::inf(Sign::POS).get_val(); |
23 | const T neg_inf = FPBits::inf(Sign::NEG).get_val(); |
24 | const T zero = FPBits::zero(Sign::POS).get_val(); |
25 | const T neg_zero = FPBits::zero(Sign::NEG).get_val(); |
26 | const T nan = FPBits::quiet_nan().get_val(); |
27 | |
28 | void test_nan_arg(FuncPtr func) { |
29 | EXPECT_FP_EQ(nan, func(nan, inf)); |
30 | EXPECT_FP_EQ(nan, func(neg_inf, nan)); |
31 | EXPECT_FP_EQ(nan, func(nan, zero)); |
32 | EXPECT_FP_EQ(nan, func(neg_zero, nan)); |
33 | EXPECT_FP_EQ(nan, func(nan, T(-1.2345))); |
34 | EXPECT_FP_EQ(nan, func(T(1.2345), nan)); |
35 | EXPECT_FP_EQ(func(nan, nan), nan); |
36 | } |
37 | |
38 | void test_inf_arg(FuncPtr func) { |
39 | EXPECT_FP_EQ(zero, func(neg_inf, inf)); |
40 | EXPECT_FP_EQ(inf, func(inf, zero)); |
41 | EXPECT_FP_EQ(zero, func(neg_zero, inf)); |
42 | EXPECT_FP_EQ(inf, func(inf, T(1.2345))); |
43 | EXPECT_FP_EQ(zero, func(T(-1.2345), inf)); |
44 | } |
45 | |
46 | void test_neg_inf_arg(FuncPtr func) { |
47 | EXPECT_FP_EQ(inf, func(inf, neg_inf)); |
48 | EXPECT_FP_EQ(zero, func(neg_inf, zero)); |
49 | EXPECT_FP_EQ(inf, func(neg_zero, neg_inf)); |
50 | EXPECT_FP_EQ(zero, func(neg_inf, T(-1.2345))); |
51 | EXPECT_FP_EQ(inf, func(T(1.2345), neg_inf)); |
52 | } |
53 | |
54 | void test_both_zero(FuncPtr func) { |
55 | EXPECT_FP_EQ(zero, func(zero, zero)); |
56 | EXPECT_FP_EQ(zero, func(zero, neg_zero)); |
57 | EXPECT_FP_EQ(zero, func(neg_zero, zero)); |
58 | EXPECT_FP_EQ(zero, func(neg_zero, neg_zero)); |
59 | } |
60 | |
61 | void test_in_range(FuncPtr func) { |
62 | constexpr StorageType STORAGE_MAX = |
63 | LIBC_NAMESPACE::cpp::numeric_limits<StorageType>::max(); |
64 | constexpr StorageType COUNT = 100'001; |
65 | constexpr StorageType STEP = STORAGE_MAX / COUNT; |
66 | for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT; |
67 | ++i, v += STEP, w -= STEP) { |
68 | FPBits xbits(v), ybits(w); |
69 | if (xbits.is_inf_or_nan()) |
70 | continue; |
71 | if (ybits.is_inf_or_nan()) |
72 | continue; |
73 | |
74 | T x = xbits.get_val(); |
75 | T y = ybits.get_val(); |
76 | |
77 | if (x > y) { |
78 | EXPECT_FP_EQ(x - y, func(x, y)); |
79 | } else { |
80 | EXPECT_FP_EQ(zero, func(x, y)); |
81 | } |
82 | } |
83 | } |
84 | }; |
85 | |
86 | #define LIST_FDIM_TESTS(T, func) \ |
87 | using LlvmLibcFDimTest = FDimTestTemplate<T>; \ |
88 | TEST_F(LlvmLibcFDimTest, NaNArg) { test_nan_arg(&func); } \ |
89 | TEST_F(LlvmLibcFDimTest, InfArg) { test_inf_arg(&func); } \ |
90 | TEST_F(LlvmLibcFDimTest, NegInfArg) { test_neg_inf_arg(&func); } \ |
91 | TEST_F(LlvmLibcFDimTest, BothZero) { test_both_zero(&func); } \ |
92 | TEST_F(LlvmLibcFDimTest, InFloatRange) { test_in_range(&func); } \ |
93 | static_assert(true, "Require semicolon.") |
94 | |