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