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 TEST(FabsTest, SpecialNumbers) { 27 EXPECT_FP_EQ(nan, __llvm_libc::fabs(nan)); 28 29 EXPECT_FP_EQ(inf, __llvm_libc::fabs(inf)); 30 EXPECT_FP_EQ(inf, __llvm_libc::fabs(negInf)); 31 32 EXPECT_FP_EQ(zero, __llvm_libc::fabs(zero)); 33 EXPECT_FP_EQ(zero, __llvm_libc::fabs(negZero)); 34 } 35 36 TEST(FabsTest, InDoubleRange) { 37 using UIntType = FPBits::UIntType; 38 constexpr UIntType count = 10000000; 39 constexpr UIntType step = UIntType(-1) / count; 40 for (UIntType i = 0, v = 0; i <= count; ++i, v += step) { 41 double x = FPBits(v); 42 if (isnan(x) || isinf(x)) 43 continue; 44 ASSERT_MPFR_MATCH(mpfr::Operation::Abs, x, __llvm_libc::fabs(x), 0.0); 45 } 46 } 47