1 //===-- Unittests for log2f -----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "src/__support/FPUtil/FPBits.h"
10 #include "src/math/log2f.h"
11 #include "test/UnitTest/FPMatcher.h"
12 #include "test/UnitTest/Test.h"
13 #include "utils/MPFRWrapper/MPFRUtils.h"
19 namespace mpfr
= __llvm_libc::testing::mpfr
;
21 DECLARE_SPECIAL_CONSTANTS(float)
23 TEST(LlvmLibcLog2fTest
, SpecialNumbers
) {
24 EXPECT_FP_EQ(aNaN
, __llvm_libc::log2f(aNaN
));
25 EXPECT_FP_EQ(inf
, __llvm_libc::log2f(inf
));
26 EXPECT_FP_IS_NAN_WITH_EXCEPTION(__llvm_libc::log2f(neg_inf
), FE_INVALID
);
27 EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf
, __llvm_libc::log2f(0.0f
), FE_DIVBYZERO
);
28 EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf
, __llvm_libc::log2f(-0.0f
), FE_DIVBYZERO
);
29 EXPECT_FP_IS_NAN_WITH_EXCEPTION(__llvm_libc::log2f(-1.0f
), FE_INVALID
);
30 EXPECT_FP_EQ(zero
, __llvm_libc::log2f(1.0f
));
33 TEST(LlvmLibcLog2fTest
, TrickyInputs
) {
35 constexpr uint32_t INPUTS
[N
] = {
36 0x3f7d57f5U
, 0x3f7e3274U
, 0x3f7ed848U
, 0x3f7fd6ccU
, 0x3f7fffffU
,
37 0x3f80079bU
, 0x3f81d0b5U
, 0x3f82e602U
, 0x3f83c98dU
, 0x3f8cba39U
};
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);
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
))
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
59 if (isnan(result
) || isinf(result
) || errno
!= 0)
61 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log2
, x
,
62 __llvm_libc::log2f(x
), 0.5);