fastmath_test.cc 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #include "absl/random/internal/fastmath.h"
  15. #include "gtest/gtest.h"
  16. #if defined(__native_client__) || defined(__EMSCRIPTEN__)
  17. // NACL has a less accurate implementation of std::log2 than most of
  18. // the other platforms. For some values which should have integral results,
  19. // sometimes NACL returns slightly larger values.
  20. //
  21. // The MUSL libc used by emscripten also has a similar bug.
  22. #define ABSL_RANDOM_INACCURATE_LOG2
  23. #endif
  24. namespace {
  25. TEST(FastMathTest, IntLog2FloorTest) {
  26. using absl::random_internal::IntLog2Floor;
  27. constexpr uint64_t kZero = 0;
  28. EXPECT_EQ(0, IntLog2Floor(0)); // boundary. return 0.
  29. EXPECT_EQ(0, IntLog2Floor(1));
  30. EXPECT_EQ(1, IntLog2Floor(2));
  31. EXPECT_EQ(63, IntLog2Floor(~kZero));
  32. // A boundary case: Converting 0xffffffffffffffff requires > 53
  33. // bits of precision, so the conversion to double rounds up,
  34. // and the result of std::log2(x) > IntLog2Floor(x).
  35. EXPECT_LT(IntLog2Floor(~kZero), static_cast<int>(std::log2(~kZero)));
  36. for (int i = 0; i < 64; i++) {
  37. const uint64_t i_pow_2 = static_cast<uint64_t>(1) << i;
  38. EXPECT_EQ(i, IntLog2Floor(i_pow_2));
  39. EXPECT_EQ(i, static_cast<int>(std::log2(i_pow_2)));
  40. uint64_t y = i_pow_2;
  41. for (int j = i - 1; j > 0; --j) {
  42. y = y | (i_pow_2 >> j);
  43. EXPECT_EQ(i, IntLog2Floor(y));
  44. }
  45. }
  46. }
  47. TEST(FastMathTest, IntLog2CeilTest) {
  48. using absl::random_internal::IntLog2Ceil;
  49. constexpr uint64_t kZero = 0;
  50. EXPECT_EQ(0, IntLog2Ceil(0)); // boundary. return 0.
  51. EXPECT_EQ(0, IntLog2Ceil(1));
  52. EXPECT_EQ(1, IntLog2Ceil(2));
  53. EXPECT_EQ(64, IntLog2Ceil(~kZero));
  54. // A boundary case: Converting 0xffffffffffffffff requires > 53
  55. // bits of precision, so the conversion to double rounds up,
  56. // and the result of std::log2(x) > IntLog2Floor(x).
  57. EXPECT_LE(IntLog2Ceil(~kZero), static_cast<int>(std::log2(~kZero)));
  58. for (int i = 0; i < 64; i++) {
  59. const uint64_t i_pow_2 = static_cast<uint64_t>(1) << i;
  60. EXPECT_EQ(i, IntLog2Ceil(i_pow_2));
  61. #ifndef ABSL_RANDOM_INACCURATE_LOG2
  62. EXPECT_EQ(i, static_cast<int>(std::ceil(std::log2(i_pow_2))));
  63. #endif
  64. uint64_t y = i_pow_2;
  65. for (int j = i - 1; j > 0; --j) {
  66. y = y | (i_pow_2 >> j);
  67. EXPECT_EQ(i + 1, IntLog2Ceil(y));
  68. }
  69. }
  70. }
  71. TEST(FastMathTest, StirlingLogFactorial) {
  72. using absl::random_internal::StirlingLogFactorial;
  73. EXPECT_NEAR(StirlingLogFactorial(1.0), 0, 1e-3);
  74. EXPECT_NEAR(StirlingLogFactorial(1.50), 0.284683, 1e-3);
  75. EXPECT_NEAR(StirlingLogFactorial(2.0), 0.69314718056, 1e-4);
  76. for (int i = 2; i < 50; i++) {
  77. double d = static_cast<double>(i);
  78. EXPECT_NEAR(StirlingLogFactorial(d), std::lgamma(d + 1), 3e-5);
  79. }
  80. }
  81. } // namespace