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