1 //===-- Unittests for fabsf -----------------------------------------------===// 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/fabsf.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<float>; 17 18 static const float zero = FPBits::zero(); 19 static const float negZero = FPBits::negZero(); 20 static const float nan = FPBits::buildNaN(1); 21 static const float inf = FPBits::inf(); 22 static const float negInf = FPBits::negInf(); 23 24 namespace mpfr = __llvm_libc::testing::mpfr; 25 26 TEST(FabsfTest, SpecialNumbers) { 27 EXPECT_FP_EQ(nan, __llvm_libc::fabsf(nan)); 28 29 EXPECT_FP_EQ(inf, __llvm_libc::fabsf(inf)); 30 EXPECT_FP_EQ(inf, __llvm_libc::fabsf(negInf)); 31 32 EXPECT_FP_EQ(zero, __llvm_libc::fabsf(zero)); 33 EXPECT_FP_EQ(zero, __llvm_libc::fabsf(negZero)); 34 } 35 36 TEST(FabsfTest, InFloatRange) { 37 using UIntType = FPBits::UIntType; 38 constexpr UIntType count = 1000000; 39 constexpr UIntType step = UIntType(-1) / count; 40 for (UIntType i = 0, v = 0; i <= count; ++i, v += step) { 41 float x = FPBits(v); 42 if (isnan(x) || isinf(x)) 43 continue; 44 ASSERT_MPFR_MATCH(mpfr::Operation::Abs, x, __llvm_libc::fabsf(x), 0.0); 45 } 46 } 47