bits_test.cc
Go to the documentation of this file.
1 // Copyright 2018 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 
16 
17 #include "gtest/gtest.h"
18 
19 namespace {
20 
21 int CLZ64(uint64_t n) {
24  EXPECT_EQ(fast, slow) << n;
25  return fast;
26 }
27 
28 TEST(BitsTest, CountLeadingZeros64) {
29  EXPECT_EQ(64, CLZ64(uint64_t{}));
30  EXPECT_EQ(0, CLZ64(~uint64_t{}));
31 
32  for (int index = 0; index < 64; index++) {
33  uint64_t x = static_cast<uint64_t>(1) << index;
34  const auto cnt = 63 - index;
35  ASSERT_EQ(cnt, CLZ64(x)) << index;
36  ASSERT_EQ(cnt, CLZ64(x + x - 1)) << index;
37  }
38 }
39 
40 int CLZ32(uint32_t n) {
43  EXPECT_EQ(fast, slow) << n;
44  return fast;
45 }
46 
47 TEST(BitsTest, CountLeadingZeros32) {
48  EXPECT_EQ(32, CLZ32(uint32_t{}));
49  EXPECT_EQ(0, CLZ32(~uint32_t{}));
50 
51  for (int index = 0; index < 32; index++) {
52  uint32_t x = static_cast<uint32_t>(1) << index;
53  const auto cnt = 31 - index;
54  ASSERT_EQ(cnt, CLZ32(x)) << index;
55  ASSERT_EQ(cnt, CLZ32(x + x - 1)) << index;
56  ASSERT_EQ(CLZ64(x), CLZ32(x) + 32);
57  }
58 }
59 
60 int CTZ64(uint64_t n) {
63  EXPECT_EQ(fast, slow) << n;
64  return fast;
65 }
66 
68  EXPECT_EQ(0, CTZ64(~uint64_t{}));
69 
70  for (int index = 0; index < 64; index++) {
71  uint64_t x = static_cast<uint64_t>(1) << index;
72  const auto cnt = index;
73  ASSERT_EQ(cnt, CTZ64(x)) << index;
74  ASSERT_EQ(cnt, CTZ64(~(x - 1))) << index;
75  }
76 }
77 
78 int CTZ32(uint32_t n) {
81  EXPECT_EQ(fast, slow) << n;
82  return fast;
83 }
84 
86  EXPECT_EQ(0, CTZ32(~uint32_t{}));
87 
88  for (int index = 0; index < 32; index++) {
89  uint32_t x = static_cast<uint32_t>(1) << index;
90  const auto cnt = index;
91  ASSERT_EQ(cnt, CTZ32(x)) << index;
92  ASSERT_EQ(cnt, CTZ32(~(x - 1))) << index;
93  }
94 }
95 
96 
97 } // namespace
ABSL_BASE_INTERNAL_FORCEINLINE int CountLeadingZeros32(uint32_t n)
Definition: bits.h:104
ABSL_BASE_INTERNAL_FORCEINLINE int CountLeadingZeros64Slow(uint64_t n)
Definition: bits.h:51
ABSL_BASE_INTERNAL_FORCEINLINE int CountLeadingZeros64(uint64_t n)
Definition: bits.h:60
ABSL_BASE_INTERNAL_FORCEINLINE int CountTrailingZerosNonZero32(uint32_t n)
Definition: bits.h:174
ABSL_BASE_INTERNAL_FORCEINLINE int CountLeadingZeros32Slow(uint64_t n)
Definition: bits.h:96
ABSL_BASE_INTERNAL_FORCEINLINE int CountTrailingZerosNonZero64Slow(uint64_t n)
Definition: bits.h:129
ABSL_BASE_INTERNAL_FORCEINLINE int CountTrailingZerosNonZero32Slow(uint32_t n)
Definition: bits.h:163
TEST(Symbolize, Unimplemented)
ABSL_BASE_INTERNAL_FORCEINLINE int CountTrailingZerosNonZero64(uint64_t n)
Definition: bits.h:141


abseil_cpp
Author(s):
autogenerated on Mon Feb 28 2022 21:31:17