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 // Zero tolerance; As in, exact match with MPFR result. 27 static constexpr mpfr::Tolerance tolerance{mpfr::Tolerance::floatPrecision, 0, 28 0}; 29 30 TEST(FabsfTest, SpecialNumbers) { 31 EXPECT_FP_EQ(nan, __llvm_libc::fabsf(nan)); 32 33 EXPECT_FP_EQ(inf, __llvm_libc::fabsf(inf)); 34 EXPECT_FP_EQ(inf, __llvm_libc::fabsf(negInf)); 35 36 EXPECT_FP_EQ(zero, __llvm_libc::fabsf(zero)); 37 EXPECT_FP_EQ(zero, __llvm_libc::fabsf(negZero)); 38 } 39 40 TEST(FabsfTest, InFloatRange) { 41 using UIntType = FPBits::UIntType; 42 constexpr UIntType count = 1000000; 43 constexpr UIntType step = UIntType(-1) / count; 44 for (UIntType i = 0, v = 0; i <= count; ++i, v += step) { 45 float x = FPBits(v); 46 if (isnan(x) || isinf(x)) 47 continue; 48 ASSERT_MPFR_MATCH(mpfr::Operation::Abs, x, __llvm_libc::fabsf(x), 49 tolerance); 50 } 51 } 52