Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / math / CopySignTest.h
blobaecd012d1d524a17a6f4436e0f5c700487b0f819
1 //===-- Utility class to test copysign[f|l] ---------------------*- 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 //===----------------------------------------------------------------------===//
9 #include "test/UnitTest/FPMatcher.h"
10 #include "test/UnitTest/Test.h"
11 #include "utils/MPFRWrapper/MPFRUtils.h"
13 #include <math.h>
15 namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
17 template <typename T>
18 class CopySignTest : public LIBC_NAMESPACE::testing::Test {
20 DECLARE_SPECIAL_CONSTANTS(T)
22 public:
23 typedef T (*CopySignFunc)(T, T);
25 void testSpecialNumbers(CopySignFunc func) {
26 EXPECT_FP_EQ(aNaN, func(aNaN, -1.0));
27 EXPECT_FP_EQ(aNaN, func(aNaN, 1.0));
29 EXPECT_FP_EQ(neg_inf, func(inf, -1.0));
30 EXPECT_FP_EQ(inf, func(neg_inf, 1.0));
32 EXPECT_FP_EQ(neg_zero, func(zero, -1.0));
33 EXPECT_FP_EQ(zero, func(neg_zero, 1.0));
36 void testRange(CopySignFunc func) {
37 constexpr UIntType COUNT = 100'000;
38 constexpr UIntType STEP = UIntType(-1) / COUNT;
39 for (UIntType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
40 T x = T(FPBits(v));
41 if (isnan(x) || isinf(x))
42 continue;
44 double res1 = func(x, -x);
45 ASSERT_FP_EQ(res1, -x);
47 double res2 = func(x, x);
48 ASSERT_FP_EQ(res2, x);
53 #define LIST_COPYSIGN_TESTS(T, func) \
54 using LlvmLibcCopySignTest = CopySignTest<T>; \
55 TEST_F(LlvmLibcCopySignTest, SpecialNumbers) { testSpecialNumbers(&func); } \
56 TEST_F(LlvmLibcCopySignTest, Range) { testRange(&func); }