2 * Copyright 2016 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 // @author Tudor Bosman (tudorb@fb.com)
19 #include <folly/Bits.h>
21 #include <gtest/gtest.h>
23 using namespace folly;
25 // Test constexpr-ness.
27 static_assert(findFirstSet(2u) == 2, "findFirstSet");
28 static_assert(findLastSet(2u) == 2, "findLastSet");
29 static_assert(nextPowTwo(2u) == 2, "nextPowTwo");
30 static_assert(isPowTwo(2u), "isPowTwo");
37 EXPECT_EQ(0, findFirstSet(static_cast<INT>(0)));
38 size_t bits = std::numeric_limits<
39 typename std::make_unsigned<INT>::type>::digits;
40 for (size_t i = 0; i < bits; i++) {
41 INT v = (static_cast<INT>(1) << (bits - 1)) |
42 (static_cast<INT>(1) << i);
43 EXPECT_EQ(i+1, findFirstSet(v));
49 typedef typename std::make_unsigned<INT>::type UINT;
50 EXPECT_EQ(0, findLastSet(static_cast<INT>(0)));
51 size_t bits = std::numeric_limits<UINT>::digits;
52 for (size_t i = 0; i < bits; i++) {
53 INT v1 = static_cast<UINT>(1) << i;
54 EXPECT_EQ(i + 1, findLastSet(v1));
56 INT v2 = (static_cast<UINT>(1) << i) - 1;
57 EXPECT_EQ(i, findLastSet(v2));
63 TEST(Bits, FindFirstSet) {
65 testFFS<signed char>();
66 testFFS<unsigned char>();
68 testFFS<unsigned short>();
70 testFFS<unsigned int>();
72 testFFS<unsigned long>();
74 testFFS<unsigned long long>();
77 TEST(Bits, FindLastSet) {
79 testFLS<signed char>();
80 testFLS<unsigned char>();
82 testFLS<unsigned short>();
84 testFLS<unsigned int>();
86 testFLS<unsigned long>();
88 testFLS<unsigned long long>();
91 #define testPowTwo(nextPowTwoFunc) { \
92 EXPECT_EQ(1, nextPowTwoFunc(0u)); \
93 EXPECT_EQ(1, nextPowTwoFunc(1u)); \
94 EXPECT_EQ(2, nextPowTwoFunc(2u)); \
95 EXPECT_EQ(4, nextPowTwoFunc(3u)); \
96 EXPECT_EQ(4, nextPowTwoFunc(4u)); \
97 EXPECT_EQ(8, nextPowTwoFunc(5u)); \
98 EXPECT_EQ(8, nextPowTwoFunc(6u)); \
99 EXPECT_EQ(8, nextPowTwoFunc(7u)); \
100 EXPECT_EQ(8, nextPowTwoFunc(8u)); \
101 EXPECT_EQ(16, nextPowTwoFunc(9u)); \
102 EXPECT_EQ(16, nextPowTwoFunc(13u)); \
103 EXPECT_EQ(16, nextPowTwoFunc(16u)); \
104 EXPECT_EQ(512, nextPowTwoFunc(510u)); \
105 EXPECT_EQ(512, nextPowTwoFunc(511u)); \
106 EXPECT_EQ(512, nextPowTwoFunc(512u)); \
107 EXPECT_EQ(1024, nextPowTwoFunc(513u)); \
108 EXPECT_EQ(1024, nextPowTwoFunc(777u)); \
109 EXPECT_EQ(1ul << 31, nextPowTwoFunc((1ul << 31) - 1)); \
110 EXPECT_EQ(1ul << 32, nextPowTwoFunc((1ul << 32) - 1)); \
111 EXPECT_EQ(1ull << 63, nextPowTwoFunc((1ull << 62) + 1)); \
115 TEST(Bits, nextPowTwoClz) {
116 testPowTwo(nextPowTwo);
119 TEST(Bits, isPowTwo) {
120 EXPECT_FALSE(isPowTwo(0u));
121 EXPECT_TRUE(isPowTwo(1ul));
122 EXPECT_TRUE(isPowTwo(2ull));
123 EXPECT_FALSE(isPowTwo(3ul));
124 EXPECT_TRUE(isPowTwo(4ul));
125 EXPECT_FALSE(isPowTwo(5ul));
126 EXPECT_TRUE(isPowTwo(8ul));
127 EXPECT_FALSE(isPowTwo(15u));
128 EXPECT_TRUE(isPowTwo(16u));
129 EXPECT_FALSE(isPowTwo(17u));
130 EXPECT_FALSE(isPowTwo(511ul));
131 EXPECT_TRUE(isPowTwo(512ul));
132 EXPECT_FALSE(isPowTwo(513ul));
133 EXPECT_FALSE(isPowTwo((1ul<<31) - 1));
134 EXPECT_TRUE(isPowTwo(1ul<<31));
135 EXPECT_FALSE(isPowTwo((1ul<<31) + 1));
136 EXPECT_FALSE(isPowTwo((1ull<<63) - 1));
137 EXPECT_TRUE(isPowTwo(1ull<<63));
138 EXPECT_FALSE(isPowTwo((1ull<<63) + 1));
141 TEST(Bits, popcount) {
142 EXPECT_EQ(0, popcount(0U));
143 EXPECT_EQ(1, popcount(1U));
144 EXPECT_EQ(32, popcount(uint32_t(-1)));
145 EXPECT_EQ(64, popcount(uint64_t(-1)));