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 //===----------------------------------------------------------------------===//
8 
9 #include "utils/MPFRWrapper/MPFRUtils.h"
10 #include "utils/UnitTest/FPMatcher.h"
11 #include "utils/UnitTest/Test.h"
12 
13 #include <math.h>
14 
15 namespace mpfr = __llvm_libc::testing::mpfr;
16 
17 template <typename T> class FAbsTest : public __llvm_libc::testing::Test {
18 
19   DECLARE_SPECIAL_CONSTANTS(T)
20 
21 public:
22   typedef T (*FabsFunc)(T);
23 
testSpecialNumbers(FabsFunc func)24   void testSpecialNumbers(FabsFunc func) {
25     EXPECT_FP_EQ(aNaN, func(aNaN));
26 
27     EXPECT_FP_EQ(inf, func(inf));
28     EXPECT_FP_EQ(inf, func(neg_inf));
29 
30     EXPECT_FP_EQ(zero, func(zero));
31     EXPECT_FP_EQ(zero, func(neg_zero));
32   }
33 
testRange(FabsFunc func)34   void testRange(FabsFunc func) {
35     constexpr UIntType COUNT = 10000000;
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);
42     }
43   }
44 };
45 
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); }
50