#include "absl/random/internal/fastmath.h"
#include "gtest/gtest.h"
#if defined(__EMSCRIPTEN__)
#define ABSL_RANDOM_INACCURATE_LOG2
#endif
namespace {
TEST(FastMathTest, IntLog2FloorTest) {
using absl::random_internal::IntLog2Floor;
constexpr uint64_t kZero = 0;
EXPECT_EQ(0, IntLog2Floor(0)); EXPECT_EQ(0, IntLog2Floor(1));
EXPECT_EQ(1, IntLog2Floor(2));
EXPECT_EQ(63, IntLog2Floor(~kZero));
EXPECT_LT(IntLog2Floor(~kZero), static_cast<int>(std::log2(~kZero)));
for (int i = 0; i < 64; i++) {
const uint64_t i_pow_2 = static_cast<uint64_t>(1) << i;
EXPECT_EQ(i, IntLog2Floor(i_pow_2));
EXPECT_EQ(i, static_cast<int>(std::log2(i_pow_2)));
uint64_t y = i_pow_2;
for (int j = i - 1; j > 0; --j) {
y = y | (i_pow_2 >> j);
EXPECT_EQ(i, IntLog2Floor(y));
}
}
}
TEST(FastMathTest, IntLog2CeilTest) {
using absl::random_internal::IntLog2Ceil;
constexpr uint64_t kZero = 0;
EXPECT_EQ(0, IntLog2Ceil(0)); EXPECT_EQ(0, IntLog2Ceil(1));
EXPECT_EQ(1, IntLog2Ceil(2));
EXPECT_EQ(64, IntLog2Ceil(~kZero));
EXPECT_LE(IntLog2Ceil(~kZero), static_cast<int>(std::log2(~kZero)));
for (int i = 0; i < 64; i++) {
const uint64_t i_pow_2 = static_cast<uint64_t>(1) << i;
EXPECT_EQ(i, IntLog2Ceil(i_pow_2));
#ifndef ABSL_RANDOM_INACCURATE_LOG2
EXPECT_EQ(i, static_cast<int>(std::ceil(std::log2(i_pow_2))));
#endif
uint64_t y = i_pow_2;
for (int j = i - 1; j > 0; --j) {
y = y | (i_pow_2 >> j);
EXPECT_EQ(i + 1, IntLog2Ceil(y));
}
}
}
TEST(FastMathTest, StirlingLogFactorial) {
using absl::random_internal::StirlingLogFactorial;
EXPECT_NEAR(StirlingLogFactorial(1.0), 0, 1e-3);
EXPECT_NEAR(StirlingLogFactorial(1.50), 0.284683, 1e-3);
EXPECT_NEAR(StirlingLogFactorial(2.0), 0.69314718056, 1e-4);
for (int i = 2; i < 50; i++) {
double d = static_cast<double>(i);
EXPECT_NEAR(StirlingLogFactorial(d), std::lgamma(d + 1), 3e-5);
}
}
}