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 "hdr/math_macros.h" |
10 | #include "src/__support/FPUtil/BasicOperations.h" |
11 | #include "src/__support/FPUtil/FPBits.h" |
12 | #include "test/UnitTest/FEnvSafeTest.h" |
13 | #include "test/UnitTest/FPMatcher.h" |
14 | #include "test/UnitTest/Test.h" |
15 | |
16 | template <typename T> |
17 | class FDimTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest { |
18 | public: |
19 | using FuncPtr = T (*)(T, T); |
20 | using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>; |
21 | using StorageType = typename FPBits::StorageType; |
22 | |
23 | const T inf = FPBits::inf(Sign::POS).get_val(); |
24 | const T neg_inf = FPBits::inf(Sign::NEG).get_val(); |
25 | const T zero = FPBits::zero(Sign::POS).get_val(); |
26 | const T neg_zero = FPBits::zero(Sign::NEG).get_val(); |
27 | const T nan = FPBits::quiet_nan().get_val(); |
28 | |
29 | void test_na_n_arg(FuncPtr func) { |
30 | EXPECT_FP_EQ(nan, func(nan, inf)); |
31 | EXPECT_FP_EQ(nan, func(neg_inf, nan)); |
32 | EXPECT_FP_EQ(nan, func(nan, zero)); |
33 | EXPECT_FP_EQ(nan, func(neg_zero, nan)); |
34 | EXPECT_FP_EQ(nan, func(nan, T(-1.2345))); |
35 | EXPECT_FP_EQ(nan, func(T(1.2345), nan)); |
36 | EXPECT_FP_EQ(func(nan, nan), nan); |
37 | } |
38 | |
39 | void test_inf_arg(FuncPtr func) { |
40 | EXPECT_FP_EQ(zero, func(neg_inf, inf)); |
41 | EXPECT_FP_EQ(inf, func(inf, zero)); |
42 | EXPECT_FP_EQ(zero, func(neg_zero, inf)); |
43 | EXPECT_FP_EQ(inf, func(inf, T(1.2345))); |
44 | EXPECT_FP_EQ(zero, func(T(-1.2345), inf)); |
45 | } |
46 | |
47 | void test_neg_inf_arg(FuncPtr func) { |
48 | EXPECT_FP_EQ(inf, func(inf, neg_inf)); |
49 | EXPECT_FP_EQ(zero, func(neg_inf, zero)); |
50 | EXPECT_FP_EQ(inf, func(neg_zero, neg_inf)); |
51 | EXPECT_FP_EQ(zero, func(neg_inf, T(-1.2345))); |
52 | EXPECT_FP_EQ(inf, func(T(1.2345), neg_inf)); |
53 | } |
54 | |
55 | void test_both_zero(FuncPtr func) { |
56 | EXPECT_FP_EQ(zero, func(zero, zero)); |
57 | EXPECT_FP_EQ(zero, func(zero, neg_zero)); |
58 | EXPECT_FP_EQ(zero, func(neg_zero, zero)); |
59 | EXPECT_FP_EQ(zero, func(neg_zero, neg_zero)); |
60 | } |
61 | |
62 | void test_in_range(FuncPtr func) { |
63 | constexpr StorageType STORAGE_MAX = |
64 | LIBC_NAMESPACE::cpp::numeric_limits<StorageType>::max(); |
65 | constexpr StorageType COUNT = 100'001; |
66 | constexpr StorageType STEP = STORAGE_MAX / COUNT; |
67 | for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT; |
68 | ++i, v += STEP, w -= STEP) { |
69 | T x = FPBits(v).get_val(), y = FPBits(w).get_val(); |
70 | if (isnan(x) || isinf(x)) |
71 | continue; |
72 | if (isnan(y) || isinf(y)) |
73 | continue; |
74 | |
75 | if (x > y) { |
76 | EXPECT_FP_EQ(x - y, func(x, y)); |
77 | } else { |
78 | EXPECT_FP_EQ(zero, func(x, y)); |
79 | } |
80 | } |
81 | } |
82 | }; |
83 | |