Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / math / FAbsTest.h
blob3342926142ff1fb2e889c2689f261a63ab55010f
1 //===-- Utility class to test fabs[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> class FAbsTest : public LIBC_NAMESPACE::testing::Test {
19 DECLARE_SPECIAL_CONSTANTS(T)
21 public:
22 typedef T (*FabsFunc)(T);
24 void testSpecialNumbers(FabsFunc func) {
25 EXPECT_FP_EQ(aNaN, func(aNaN));
27 EXPECT_FP_EQ(inf, func(inf));
28 EXPECT_FP_EQ(inf, func(neg_inf));
30 EXPECT_FP_EQ(zero, func(zero));
31 EXPECT_FP_EQ(zero, func(neg_zero));
34 void testRange(FabsFunc func) {
35 constexpr UIntType COUNT = 100'000;
36 constexpr UIntType STEP = UIntType(-1) / COUNT;
37 for (UIntType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
38 T x = T(FPBits(v));
39 if (isnan(x) || isinf(x))
40 continue;
41 ASSERT_MPFR_MATCH(mpfr::Operation::Abs, x, func(x), 0.0);
46 #define LIST_FABS_TESTS(T, func) \
47 using LlvmLibcFAbsTest = FAbsTest<T>; \
48 TEST_F(LlvmLibcFAbsTest, SpecialNumbers) { testSpecialNumbers(&func); } \
49 TEST_F(LlvmLibcFAbsTest, Range) { testRange(&func); }