Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "absl/base/internal/bits.h"
00016
00017 #include "gtest/gtest.h"
00018
00019 namespace {
00020
00021 int CLZ64(uint64_t n) {
00022 int fast = absl::base_internal::CountLeadingZeros64(n);
00023 int slow = absl::base_internal::CountLeadingZeros64Slow(n);
00024 EXPECT_EQ(fast, slow) << n;
00025 return fast;
00026 }
00027
00028 TEST(BitsTest, CountLeadingZeros64) {
00029 EXPECT_EQ(64, CLZ64(uint64_t{}));
00030 EXPECT_EQ(0, CLZ64(~uint64_t{}));
00031
00032 for (int index = 0; index < 64; index++) {
00033 uint64_t x = static_cast<uint64_t>(1) << index;
00034 const auto cnt = 63 - index;
00035 ASSERT_EQ(cnt, CLZ64(x)) << index;
00036 ASSERT_EQ(cnt, CLZ64(x + x - 1)) << index;
00037 }
00038 }
00039
00040 int CLZ32(uint32_t n) {
00041 int fast = absl::base_internal::CountLeadingZeros32(n);
00042 int slow = absl::base_internal::CountLeadingZeros32Slow(n);
00043 EXPECT_EQ(fast, slow) << n;
00044 return fast;
00045 }
00046
00047 TEST(BitsTest, CountLeadingZeros32) {
00048 EXPECT_EQ(32, CLZ32(uint32_t{}));
00049 EXPECT_EQ(0, CLZ32(~uint32_t{}));
00050
00051 for (int index = 0; index < 32; index++) {
00052 uint32_t x = static_cast<uint32_t>(1) << index;
00053 const auto cnt = 31 - index;
00054 ASSERT_EQ(cnt, CLZ32(x)) << index;
00055 ASSERT_EQ(cnt, CLZ32(x + x - 1)) << index;
00056 ASSERT_EQ(CLZ64(x), CLZ32(x) + 32);
00057 }
00058 }
00059
00060 int CTZ64(uint64_t n) {
00061 int fast = absl::base_internal::CountTrailingZerosNonZero64(n);
00062 int slow = absl::base_internal::CountTrailingZerosNonZero64Slow(n);
00063 EXPECT_EQ(fast, slow) << n;
00064 return fast;
00065 }
00066
00067 TEST(BitsTest, CountTrailingZerosNonZero64) {
00068 EXPECT_EQ(0, CTZ64(~uint64_t{}));
00069
00070 for (int index = 0; index < 64; index++) {
00071 uint64_t x = static_cast<uint64_t>(1) << index;
00072 const auto cnt = index;
00073 ASSERT_EQ(cnt, CTZ64(x)) << index;
00074 ASSERT_EQ(cnt, CTZ64(~(x - 1))) << index;
00075 }
00076 }
00077
00078 int CTZ32(uint32_t n) {
00079 int fast = absl::base_internal::CountTrailingZerosNonZero32(n);
00080 int slow = absl::base_internal::CountTrailingZerosNonZero32Slow(n);
00081 EXPECT_EQ(fast, slow) << n;
00082 return fast;
00083 }
00084
00085 TEST(BitsTest, CountTrailingZerosNonZero32) {
00086 EXPECT_EQ(0, CTZ32(~uint32_t{}));
00087
00088 for (int index = 0; index < 32; index++) {
00089 uint32_t x = static_cast<uint32_t>(1) << index;
00090 const auto cnt = index;
00091 ASSERT_EQ(cnt, CTZ32(x)) << index;
00092 ASSERT_EQ(cnt, CTZ32(~(x - 1))) << index;
00093 }
00094 }
00095
00096
00097 }