1 //===-- Utility class to test copysign[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/FEnvSafeTest.h"
10 #include "test/UnitTest/FPMatcher.h"
11 #include "test/UnitTest/Test.h"
12 #include "utils/MPFRWrapper/MPFRUtils.h"
14 #include "hdr/math_macros.h"
16 namespace mpfr
= LIBC_NAMESPACE::testing::mpfr
;
19 class CopySignTest
: public LIBC_NAMESPACE::testing::FEnvSafeTest
{
21 DECLARE_SPECIAL_CONSTANTS(T
)
24 typedef T (*CopySignFunc
)(T
, T
);
26 void testSpecialNumbers(CopySignFunc func
) {
27 EXPECT_FP_EQ(aNaN
, func(aNaN
, -1.0));
28 EXPECT_FP_EQ(aNaN
, func(aNaN
, 1.0));
30 EXPECT_FP_EQ(neg_inf
, func(inf
, -1.0));
31 EXPECT_FP_EQ(inf
, func(neg_inf
, 1.0));
33 EXPECT_FP_EQ(neg_zero
, func(zero
, -1.0));
34 EXPECT_FP_EQ(zero
, func(neg_zero
, 1.0));
37 void testRange(CopySignFunc func
) {
38 constexpr StorageType COUNT
= 100'000;
39 constexpr StorageType STEP
= STORAGE_MAX
/ COUNT
;
40 for (StorageType i
= 0, v
= 0; i
<= COUNT
; ++i
, v
+= STEP
) {
41 T x
= FPBits(v
).get_val();
42 if (FPBits(v
).is_nan() || FPBits(v
).is_inf())
45 double res1
= func(x
, -x
);
46 ASSERT_FP_EQ(res1
, -x
);
48 double res2
= func(x
, x
);
49 ASSERT_FP_EQ(res2
, x
);
54 #define LIST_COPYSIGN_TESTS(T, func) \
55 using LlvmLibcCopySignTest = CopySignTest<T>; \
56 TEST_F(LlvmLibcCopySignTest, SpecialNumbers) { testSpecialNumbers(&func); } \
57 TEST_F(LlvmLibcCopySignTest, Range) { testRange(&func); }