1 //===-- Unittests for log2 ------------------------------------------------===//
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/errno/libc_errno.h"
11 #include "src/math/log2.h"
12 #include "test/UnitTest/FPMatcher.h"
13 #include "test/UnitTest/Test.h"
14 #include "utils/MPFRWrapper/MPFRUtils.h"
20 namespace mpfr
= __llvm_libc::testing::mpfr
;
21 using __llvm_libc::testing::tlog
;
23 DECLARE_SPECIAL_CONSTANTS(double)
25 TEST(LlvmLibcLog2Test
, SpecialNumbers
) {
26 EXPECT_FP_EQ(aNaN
, __llvm_libc::log2(aNaN
));
27 EXPECT_FP_EQ(inf
, __llvm_libc::log2(inf
));
28 EXPECT_FP_IS_NAN_WITH_EXCEPTION(__llvm_libc::log2(neg_inf
), FE_INVALID
);
29 EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf
, __llvm_libc::log2(0.0), FE_DIVBYZERO
);
30 EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf
, __llvm_libc::log2(-0.0), FE_DIVBYZERO
);
31 EXPECT_FP_IS_NAN_WITH_EXCEPTION(__llvm_libc::log2(-1.0), FE_INVALID
);
32 EXPECT_FP_EQ_ALL_ROUNDING(zero
, __llvm_libc::log2(1.0));
35 TEST(LlvmLibcLog2Test
, TrickyInputs
) {
37 constexpr uint64_t INPUTS
[N
] = {
38 0x3ff0000000000000, // x = 1.0
39 0x4024000000000000, // x = 10.0
40 0x4059000000000000, // x = 10^2
41 0x408f400000000000, // x = 10^3
42 0x40c3880000000000, // x = 10^4
43 0x40f86a0000000000, // x = 10^5
44 0x412e848000000000, // x = 10^6
45 0x416312d000000000, // x = 10^7
46 0x4197d78400000000, // x = 10^8
47 0x41cdcd6500000000, // x = 10^9
48 0x4202a05f20000000, // x = 10^10
49 0x42374876e8000000, // x = 10^11
50 0x426d1a94a2000000, // x = 10^12
51 0x42a2309ce5400000, // x = 10^13
52 0x42d6bcc41e900000, // x = 10^14
53 0x430c6bf526340000, // x = 10^15
54 0x4341c37937e08000, // x = 10^16
55 0x4376345785d8a000, // x = 10^17
56 0x43abc16d674ec800, // x = 10^18
57 0x43e158e460913d00, // x = 10^19
58 0x4415af1d78b58c40, // x = 10^20
59 0x444b1ae4d6e2ef50, // x = 10^21
60 0x4480f0cf064dd592, // x = 10^22
61 0x3fefffffffef06ad, 0x3fefde0f22c7d0eb, 0x225e7812faadb32f,
62 0x3fee1076964c2903, 0x3fdfe93fff7fceb0, 0x3ff012631ad8df10,
65 for (int i
= 0; i
< N
; ++i
) {
66 double x
= double(FPBits(INPUTS
[i
]));
67 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log2
, x
,
68 __llvm_libc::log2(x
), 0.5);
72 TEST(LlvmLibcLog2Test
, AllExponents
) {
73 double x
= 0x1.0p
-1074;
74 for (int i
= -1074; i
< 1024; ++i
, x
*= 2.0) {
75 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log2
, x
,
76 __llvm_libc::log2(x
), 0.5);
80 TEST(LlvmLibcLog2Test
, InDoubleRange
) {
81 constexpr uint64_t COUNT
= 234561;
82 constexpr uint64_t START
= 0x3FD0'0000'0000'0000ULL
; // 0.25
83 constexpr uint64_t STOP
= 0x4010'0000'0000'0000ULL
; // 4.0
84 // constexpr uint64_t START = 0x3FF0'0000'0000'0000ULL; // 1.0
85 // constexpr uint64_t STOP = 0x4000'0000'0000'0000ULL; // 2.0
86 constexpr uint64_t STEP
= (STOP
- START
) / COUNT
;
88 auto test
= [&](mpfr::RoundingMode rounding_mode
) {
89 mpfr::ForceRoundingMode
__r(rounding_mode
);
96 for (uint64_t i
= 0, v
= START
; i
<= COUNT
; ++i
, v
+= STEP
) {
97 double x
= FPBits(v
).get_val();
98 if (isnan(x
) || isinf(x
) || x
< 0.0)
101 double result
= __llvm_libc::log2(x
);
103 if (isnan(result
) || isinf(result
))
107 // ASSERT_MPFR_MATCH(mpfr::Operation::Log2, x, result, 0.5);
108 if (!EXPECT_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Log2
, x
, result
,
109 0.5, rounding_mode
)) {
111 while (!EXPECT_MPFR_MATCH_ROUNDING_SILENTLY(
112 mpfr::Operation::Log2
, x
, result
, tol
, rounding_mode
)) {
119 tlog
<< " Log2 failed: " << fails
<< "/" << count
<< "/" << cc
121 tlog
<< " Max ULPs is at most: " << static_cast<uint64_t>(tol
) << ".\n";
123 EXPECT_MPFR_MATCH(mpfr::Operation::Log2
, mx
, mr
, 0.5, rounding_mode
);
127 tlog
<< " Test Rounding To Nearest...\n";
128 test(mpfr::RoundingMode::Nearest
);
130 tlog
<< " Test Rounding Downward...\n";
131 test(mpfr::RoundingMode::Downward
);
133 tlog
<< " Test Rounding Upward...\n";
134 test(mpfr::RoundingMode::Upward
);
136 tlog
<< " Test Rounding Toward Zero...\n";
137 test(mpfr::RoundingMode::TowardZero
);