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 | using LIBC_NAMESPACE::Sign; |
17 | |
18 | template <typename T> |
19 | class FDimTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest { |
20 | public: |
21 | using FuncPtr = T (*)(T, T); |
22 | using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>; |
23 | using StorageType = typename FPBits::StorageType; |
24 | |
25 | const T inf = FPBits::inf(Sign::POS).get_val(); |
26 | const T neg_inf = FPBits::inf(Sign::NEG).get_val(); |
27 | const T zero = FPBits::zero(Sign::POS).get_val(); |
28 | const T neg_zero = FPBits::zero(Sign::NEG).get_val(); |
29 | const T nan = FPBits::quiet_nan().get_val(); |
30 | |
31 | void test_na_n_arg(FuncPtr func) { |
32 | EXPECT_FP_EQ(nan, func(nan, inf)); |
33 | EXPECT_FP_EQ(nan, func(neg_inf, nan)); |
34 | EXPECT_FP_EQ(nan, func(nan, zero)); |
35 | EXPECT_FP_EQ(nan, func(neg_zero, nan)); |
36 | EXPECT_FP_EQ(nan, func(nan, T(-1.2345))); |
37 | EXPECT_FP_EQ(nan, func(T(1.2345), nan)); |
38 | EXPECT_FP_EQ(func(nan, nan), nan); |
39 | } |
40 | |
41 | void test_inf_arg(FuncPtr func) { |
42 | EXPECT_FP_EQ(zero, func(neg_inf, inf)); |
43 | EXPECT_FP_EQ(inf, func(inf, zero)); |
44 | EXPECT_FP_EQ(zero, func(neg_zero, inf)); |
45 | EXPECT_FP_EQ(inf, func(inf, T(1.2345))); |
46 | EXPECT_FP_EQ(zero, func(T(-1.2345), inf)); |
47 | } |
48 | |
49 | void test_neg_inf_arg(FuncPtr func) { |
50 | EXPECT_FP_EQ(inf, func(inf, neg_inf)); |
51 | EXPECT_FP_EQ(zero, func(neg_inf, zero)); |
52 | EXPECT_FP_EQ(inf, func(neg_zero, neg_inf)); |
53 | EXPECT_FP_EQ(zero, func(neg_inf, T(-1.2345))); |
54 | EXPECT_FP_EQ(inf, func(T(1.2345), neg_inf)); |
55 | } |
56 | |
57 | void test_both_zero(FuncPtr func) { |
58 | EXPECT_FP_EQ(zero, func(zero, zero)); |
59 | EXPECT_FP_EQ(zero, func(zero, neg_zero)); |
60 | EXPECT_FP_EQ(zero, func(neg_zero, zero)); |
61 | EXPECT_FP_EQ(zero, func(neg_zero, neg_zero)); |
62 | } |
63 | |
64 | void test_in_range(FuncPtr func) { |
65 | constexpr StorageType STORAGE_MAX = |
66 | LIBC_NAMESPACE::cpp::numeric_limits<StorageType>::max(); |
67 | constexpr StorageType COUNT = 100'001; |
68 | constexpr StorageType STEP = STORAGE_MAX / COUNT; |
69 | for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT; |
70 | ++i, v += STEP, w -= STEP) { |
71 | T x = FPBits(v).get_val(), y = FPBits(w).get_val(); |
72 | if (FPBits(v).is_nan() || FPBits(v).is_inf()) |
73 | continue; |
74 | if (FPBits(w).is_nan() || FPBits(w).is_inf()) |
75 | continue; |
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 | |