1 //===-- Unittests for fabsl -----------------------------------------------===//
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 "src/math/fabsl.h"
10 #include "utils/FPUtil/FPBits.h"
11 #include "utils/FPUtil/TestHelpers.h"
12 #include "utils/MPFRWrapper/MPFRUtils.h"
13 #include "utils/UnitTest/Test.h"
14 #include <math.h>
15 
16 using FPBits = __llvm_libc::fputil::FPBits<long double>;
17 
18 DECLARE_SPECIAL_CONSTANTS(long double)
19 
20 namespace mpfr = __llvm_libc::testing::mpfr;
21 
TEST(FabslTest,SpecialNumbers)22 TEST(FabslTest, SpecialNumbers) {
23   EXPECT_FP_EQ(nan, __llvm_libc::fabsl(nan));
24 
25   EXPECT_FP_EQ(inf, __llvm_libc::fabsl(inf));
26   EXPECT_FP_EQ(inf, __llvm_libc::fabsl(negInf));
27 
28   EXPECT_FP_EQ(zero, __llvm_libc::fabsl(zero));
29   EXPECT_FP_EQ(zero, __llvm_libc::fabsl(negZero));
30 }
31 
TEST(FabslTest,InLongDoubleRange)32 TEST(FabslTest, InLongDoubleRange) {
33   using UIntType = FPBits::UIntType;
34   constexpr UIntType count = 10000000;
35   constexpr UIntType step = UIntType(-1) / count;
36   for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
37     long double x = FPBits(v);
38     if (isnan(x) || isinf(x))
39       continue;
40     ASSERT_MPFR_MATCH(mpfr::Operation::Abs, x, __llvm_libc::fabsl(x), 0.0);
41   }
42 }
43