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 "src/math/fabs.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<double>; 17 18 DECLARE_SPECIAL_CONSTANTS(double) 19 20 namespace mpfr = __llvm_libc::testing::mpfr; 21 22 TEST(LlvmLibcFabsTest, SpecialNumbers) { 23 EXPECT_FP_EQ(aNaN, __llvm_libc::fabs(aNaN)); 24 25 EXPECT_FP_EQ(inf, __llvm_libc::fabs(inf)); 26 EXPECT_FP_EQ(inf, __llvm_libc::fabs(negInf)); 27 28 EXPECT_FP_EQ(zero, __llvm_libc::fabs(zero)); 29 EXPECT_FP_EQ(zero, __llvm_libc::fabs(negZero)); 30 } 31 32 TEST(LlvmLibcFabsTest, InDoubleRange) { 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 double x = FPBits(v); 38 if (isnan(x) || isinf(x)) 39 continue; 40 ASSERT_MPFR_MATCH(mpfr::Operation::Abs, x, __llvm_libc::fabs(x), 0.0); 41 } 42 } 43