1 //===-- Utility class to test fmin[f|l] -------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "test/UnitTest/FPMatcher.h"
10 #include "test/UnitTest/Test.h"
11 #include "utils/MPFRWrapper/MPFRUtils.h"
15 namespace mpfr
= __llvm_libc::testing::mpfr
;
17 template <typename T
> class FMaxTest
: public __llvm_libc::testing::Test
{
19 DECLARE_SPECIAL_CONSTANTS(T
)
22 typedef T (*FMaxFunc
)(T
, T
);
24 void testNaN(FMaxFunc func
) {
25 EXPECT_FP_EQ(inf
, func(aNaN
, inf
));
26 EXPECT_FP_EQ(neg_inf
, func(neg_inf
, aNaN
));
27 EXPECT_FP_EQ(0.0, func(aNaN
, 0.0));
28 EXPECT_FP_EQ(-0.0, func(-0.0, aNaN
));
29 EXPECT_FP_EQ(T(-1.2345), func(aNaN
, T(-1.2345)));
30 EXPECT_FP_EQ(T(1.2345), func(T(1.2345), aNaN
));
31 EXPECT_FP_EQ(aNaN
, func(aNaN
, aNaN
));
34 void testInfArg(FMaxFunc func
) {
35 EXPECT_FP_EQ(inf
, func(neg_inf
, inf
));
36 EXPECT_FP_EQ(inf
, func(inf
, 0.0));
37 EXPECT_FP_EQ(inf
, func(-0.0, inf
));
38 EXPECT_FP_EQ(inf
, func(inf
, T(1.2345)));
39 EXPECT_FP_EQ(inf
, func(T(-1.2345), inf
));
42 void testNegInfArg(FMaxFunc func
) {
43 EXPECT_FP_EQ(inf
, func(inf
, neg_inf
));
44 EXPECT_FP_EQ(0.0, func(neg_inf
, 0.0));
45 EXPECT_FP_EQ(-0.0, func(-0.0, neg_inf
));
46 EXPECT_FP_EQ(T(-1.2345), func(neg_inf
, T(-1.2345)));
47 EXPECT_FP_EQ(T(1.2345), func(T(1.2345), neg_inf
));
50 void testBothZero(FMaxFunc func
) {
51 EXPECT_FP_EQ(0.0, func(0.0, 0.0));
52 EXPECT_FP_EQ(0.0, func(-0.0, 0.0));
53 EXPECT_FP_EQ(0.0, func(0.0, -0.0));
54 EXPECT_FP_EQ(-0.0, func(-0.0, -0.0));
57 void testRange(FMaxFunc func
) {
58 constexpr UIntType COUNT
= 10000001;
59 constexpr UIntType STEP
= UIntType(-1) / COUNT
;
60 for (UIntType i
= 0, v
= 0, w
= UIntType(-1); i
<= COUNT
;
61 ++i
, v
+= STEP
, w
-= STEP
) {
62 T x
= T(FPBits(v
)), y
= T(FPBits(w
));
63 if (isnan(x
) || isinf(x
))
65 if (isnan(y
) || isinf(y
))
67 if ((x
== 0) && (y
== 0))
71 EXPECT_FP_EQ(x
, func(x
, y
));
73 EXPECT_FP_EQ(y
, func(x
, y
));
79 #define LIST_FMAX_TESTS(T, func) \
80 using LlvmLibcFMaxTest = FMaxTest<T>; \
81 TEST_F(LlvmLibcFMaxTest, NaN) { testNaN(&func); } \
82 TEST_F(LlvmLibcFMaxTest, InfArg) { testInfArg(&func); } \
83 TEST_F(LlvmLibcFMaxTest, NegInfArg) { testNegInfArg(&func); } \
84 TEST_F(LlvmLibcFMaxTest, BothZero) { testBothZero(&func); } \
85 TEST_F(LlvmLibcFMaxTest, Range) { testRange(&func); }