1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "absl/random/internal/fastmath.h"
16
17 #include "gtest/gtest.h"
18
19 #if defined(__native_client__) || defined(__EMSCRIPTEN__)
20 // NACL has a less accurate implementation of std::log2 than most of
21 // the other platforms. For some values which should have integral results,
22 // sometimes NACL returns slightly larger values.
23 //
24 // The MUSL libc used by emscripten also has a similar bug.
25 #define ABSL_RANDOM_INACCURATE_LOG2
26 #endif
27
28 namespace {
29
TEST(DistributionImplTest,LeadingSetBit)30 TEST(DistributionImplTest, LeadingSetBit) {
31 using absl::random_internal::LeadingSetBit;
32 constexpr uint64_t kZero = 0;
33 EXPECT_EQ(0, LeadingSetBit(kZero));
34 EXPECT_EQ(64, LeadingSetBit(~kZero));
35
36 for (int index = 0; index < 64; index++) {
37 uint64_t x = static_cast<uint64_t>(1) << index;
38 EXPECT_EQ(index + 1, LeadingSetBit(x)) << index;
39 EXPECT_EQ(index + 1, LeadingSetBit(x + x - 1)) << index;
40 }
41 }
42
TEST(FastMathTest,IntLog2FloorTest)43 TEST(FastMathTest, IntLog2FloorTest) {
44 using absl::random_internal::IntLog2Floor;
45 constexpr uint64_t kZero = 0;
46 EXPECT_EQ(0, IntLog2Floor(0)); // boundary. return 0.
47 EXPECT_EQ(0, IntLog2Floor(1));
48 EXPECT_EQ(1, IntLog2Floor(2));
49 EXPECT_EQ(63, IntLog2Floor(~kZero));
50
51 // A boundary case: Converting 0xffffffffffffffff requires > 53
52 // bits of precision, so the conversion to double rounds up,
53 // and the result of std::log2(x) > IntLog2Floor(x).
54 EXPECT_LT(IntLog2Floor(~kZero), static_cast<int>(std::log2(~kZero)));
55
56 for (int i = 0; i < 64; i++) {
57 const uint64_t i_pow_2 = static_cast<uint64_t>(1) << i;
58 EXPECT_EQ(i, IntLog2Floor(i_pow_2));
59 EXPECT_EQ(i, static_cast<int>(std::log2(i_pow_2)));
60
61 uint64_t y = i_pow_2;
62 for (int j = i - 1; j > 0; --j) {
63 y = y | (i_pow_2 >> j);
64 EXPECT_EQ(i, IntLog2Floor(y));
65 }
66 }
67 }
68
TEST(FastMathTest,IntLog2CeilTest)69 TEST(FastMathTest, IntLog2CeilTest) {
70 using absl::random_internal::IntLog2Ceil;
71 constexpr uint64_t kZero = 0;
72 EXPECT_EQ(0, IntLog2Ceil(0)); // boundary. return 0.
73 EXPECT_EQ(0, IntLog2Ceil(1));
74 EXPECT_EQ(1, IntLog2Ceil(2));
75 EXPECT_EQ(64, IntLog2Ceil(~kZero));
76
77 // A boundary case: Converting 0xffffffffffffffff requires > 53
78 // bits of precision, so the conversion to double rounds up,
79 // and the result of std::log2(x) > IntLog2Floor(x).
80 EXPECT_LE(IntLog2Ceil(~kZero), static_cast<int>(std::log2(~kZero)));
81
82 for (int i = 0; i < 64; i++) {
83 const uint64_t i_pow_2 = static_cast<uint64_t>(1) << i;
84 EXPECT_EQ(i, IntLog2Ceil(i_pow_2));
85 #ifndef ABSL_RANDOM_INACCURATE_LOG2
86 EXPECT_EQ(i, static_cast<int>(std::ceil(std::log2(i_pow_2))));
87 #endif
88
89 uint64_t y = i_pow_2;
90 for (int j = i - 1; j > 0; --j) {
91 y = y | (i_pow_2 >> j);
92 EXPECT_EQ(i + 1, IntLog2Ceil(y));
93 }
94 }
95 }
96
TEST(FastMathTest,StirlingLogFactorial)97 TEST(FastMathTest, StirlingLogFactorial) {
98 using absl::random_internal::StirlingLogFactorial;
99
100 EXPECT_NEAR(StirlingLogFactorial(1.0), 0, 1e-3);
101 EXPECT_NEAR(StirlingLogFactorial(1.50), 0.284683, 1e-3);
102 EXPECT_NEAR(StirlingLogFactorial(2.0), 0.69314718056, 1e-4);
103
104 for (int i = 2; i < 50; i++) {
105 double d = static_cast<double>(i);
106 EXPECT_NEAR(StirlingLogFactorial(d), std::lgamma(d + 1), 3e-5);
107 }
108 }
109
110 } // namespace
111