1 //===-- Unittests for log2f -----------------------------------------------===// 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/__support/FPUtil/FPBits.h" 10 #include "src/math/log2f.h" 11 #include "utils/MPFRWrapper/MPFRUtils.h" 12 #include "utils/UnitTest/FPMatcher.h" 13 #include "utils/UnitTest/Test.h" 14 #include <math.h> 15 16 #include <errno.h> 17 #include <stdint.h> 18 19 namespace mpfr = __llvm_libc::testing::mpfr; 20 21 DECLARE_SPECIAL_CONSTANTS(float) 22 23 TEST(LlvmLibcLog2fTest, SpecialNumbers) { 24 EXPECT_FP_EQ(aNaN, __llvm_libc::log2f(aNaN)); 25 EXPECT_FP_EQ(inf, __llvm_libc::log2f(inf)); 26 EXPECT_TRUE(FPBits(__llvm_libc::log2f(neg_inf)).is_nan()); 27 EXPECT_FP_EQ(neg_inf, __llvm_libc::log2f(0.0f)); 28 EXPECT_FP_EQ(neg_inf, __llvm_libc::log2f(-0.0f)); 29 EXPECT_TRUE(FPBits(__llvm_libc::log2f(-1.0f)).is_nan()); 30 EXPECT_FP_EQ(zero, __llvm_libc::log2f(1.0f)); 31 } 32 33 TEST(LlvmLibcLog2fTest, TrickyInputs) { 34 constexpr int N = 10; 35 constexpr uint32_t INPUTS[N] = { 36 0x3f7d57f5U, 0x3f7e3274U, 0x3f7ed848U, 0x3f7fd6ccU, 0x3f7fffffU, 37 0x3f80079bU, 0x3f81d0b5U, 0x3f82e602U, 0x3f83c98dU, 0x3f8cba39U}; 38 39 for (int i = 0; i < N; ++i) { 40 float x = float(FPBits(INPUTS[i])); 41 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log2, x, 42 __llvm_libc::log2f(x), 0.5); 43 } 44 } 45 46 TEST(LlvmLibcLog2fTest, InFloatRange) { 47 constexpr uint32_t COUNT = 1000000; 48 constexpr uint32_t STEP = UINT32_MAX / COUNT; 49 for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) { 50 float x = float(FPBits(v)); 51 if (isnan(x) || isinf(x)) 52 continue; 53 errno = 0; 54 float result = __llvm_libc::log2f(x); 55 // If the computation resulted in an error or did not produce valid result 56 // in the single-precision floating point range, then ignore comparing with 57 // MPFR result as MPFR can still produce valid results because of its 58 // wider precision. 59 if (isnan(result) || isinf(result) || errno != 0) 60 continue; 61 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log2, x, 62 __llvm_libc::log2f(x), 0.5); 63 } 64 } 65