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 TEST(Bits, nextPowTwoClz) {
92 EXPECT_EQ(1, nextPowTwo(0u));
93 EXPECT_EQ(1, nextPowTwo(1u));
94 EXPECT_EQ(2, nextPowTwo(2u));
95 EXPECT_EQ(4, nextPowTwo(3u));
96 EXPECT_EQ(4, nextPowTwo(4u));
97 EXPECT_EQ(8, nextPowTwo(5u));
98 EXPECT_EQ(8, nextPowTwo(6u));
99 EXPECT_EQ(8, nextPowTwo(7u));
100 EXPECT_EQ(8, nextPowTwo(8u));
101 EXPECT_EQ(16, nextPowTwo(9u));
102 EXPECT_EQ(16, nextPowTwo(13u));
103 EXPECT_EQ(16, nextPowTwo(16u));
104 EXPECT_EQ(512, nextPowTwo(510u));
105 EXPECT_EQ(512, nextPowTwo(511u));
106 EXPECT_EQ(512, nextPowTwo(512u));
107 EXPECT_EQ(1024, nextPowTwo(513u));
108 EXPECT_EQ(1024, nextPowTwo(777u));
109 EXPECT_EQ(1ul << 31, nextPowTwo((1ul << 31) - 1));
110 EXPECT_EQ(1ull << 32, nextPowTwo((1ull << 32) - 1));
111 EXPECT_EQ(1ull << 63, nextPowTwo((1ull << 62) + 1));
114 TEST(Bits, isPowTwo) {
115 EXPECT_FALSE(isPowTwo(0u));
116 EXPECT_TRUE(isPowTwo(1ul));
117 EXPECT_TRUE(isPowTwo(2ull));
118 EXPECT_FALSE(isPowTwo(3ul));
119 EXPECT_TRUE(isPowTwo(4ul));
120 EXPECT_FALSE(isPowTwo(5ul));
121 EXPECT_TRUE(isPowTwo(8ul));
122 EXPECT_FALSE(isPowTwo(15u));
123 EXPECT_TRUE(isPowTwo(16u));
124 EXPECT_FALSE(isPowTwo(17u));
125 EXPECT_FALSE(isPowTwo(511ul));
126 EXPECT_TRUE(isPowTwo(512ul));
127 EXPECT_FALSE(isPowTwo(513ul));
128 EXPECT_FALSE(isPowTwo((1ul<<31) - 1));
129 EXPECT_TRUE(isPowTwo(1ul<<31));
130 EXPECT_FALSE(isPowTwo((1ul<<31) + 1));
131 EXPECT_FALSE(isPowTwo((1ull<<63) - 1));
132 EXPECT_TRUE(isPowTwo(1ull<<63));
133 EXPECT_FALSE(isPowTwo((1ull<<63) + 1));
136 TEST(Bits, popcount) {
137 EXPECT_EQ(0, popcount(0U));
138 EXPECT_EQ(1, popcount(1U));
139 EXPECT_EQ(32, popcount(uint32_t(-1)));
140 EXPECT_EQ(64, popcount(uint64_t(-1)));