2 * Copyright 2014 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 <gflags/gflags.h>
20 #include "folly/Bits.h"
21 #include "folly/Benchmark.h"
22 #include <gtest/gtest.h>
24 using namespace folly;
26 // Test constexpr-ness.
28 static_assert(findFirstSet(2u) == 2, "findFirstSet");
29 static_assert(findLastSet(2u) == 2, "findLastSet");
30 static_assert(nextPowTwo(2u) == 2, "nextPowTwo");
31 static_assert(isPowTwo(2u), "isPowTwo");
38 EXPECT_EQ(0, findFirstSet(static_cast<INT>(0)));
39 size_t bits = std::numeric_limits<
40 typename std::make_unsigned<INT>::type>::digits;
41 for (size_t i = 0; i < bits; i++) {
42 INT v = (static_cast<INT>(1) << (bits - 1)) |
43 (static_cast<INT>(1) << i);
44 EXPECT_EQ(i+1, findFirstSet(v));
50 typedef typename std::make_unsigned<INT>::type UINT;
51 EXPECT_EQ(0, findLastSet(static_cast<INT>(0)));
52 size_t bits = std::numeric_limits<UINT>::digits;
53 for (size_t i = 0; i < bits; i++) {
54 INT v1 = static_cast<UINT>(1) << i;
55 EXPECT_EQ(i + 1, findLastSet(v1));
57 INT v2 = (static_cast<UINT>(1) << i) - 1;
58 EXPECT_EQ(i, findLastSet(v2));
64 TEST(Bits, FindFirstSet) {
66 testFFS<signed char>();
67 testFFS<unsigned char>();
69 testFFS<unsigned short>();
71 testFFS<unsigned int>();
73 testFFS<unsigned long>();
75 testFFS<unsigned long long>();
78 TEST(Bits, FindLastSet) {
80 testFLS<signed char>();
81 testFLS<unsigned char>();
83 testFLS<unsigned short>();
85 testFLS<unsigned int>();
87 testFLS<unsigned long>();
89 testFLS<unsigned long long>();
92 #define testPowTwo(nextPowTwoFunc) { \
93 EXPECT_EQ(1, nextPowTwoFunc(0u)); \
94 EXPECT_EQ(1, nextPowTwoFunc(1u)); \
95 EXPECT_EQ(2, nextPowTwoFunc(2u)); \
96 EXPECT_EQ(4, nextPowTwoFunc(3u)); \
97 EXPECT_EQ(4, nextPowTwoFunc(4u)); \
98 EXPECT_EQ(8, nextPowTwoFunc(5u)); \
99 EXPECT_EQ(8, nextPowTwoFunc(6u)); \
100 EXPECT_EQ(8, nextPowTwoFunc(7u)); \
101 EXPECT_EQ(8, nextPowTwoFunc(8u)); \
102 EXPECT_EQ(16, nextPowTwoFunc(9u)); \
103 EXPECT_EQ(16, nextPowTwoFunc(13u)); \
104 EXPECT_EQ(16, nextPowTwoFunc(16u)); \
105 EXPECT_EQ(512, nextPowTwoFunc(510u)); \
106 EXPECT_EQ(512, nextPowTwoFunc(511u)); \
107 EXPECT_EQ(512, nextPowTwoFunc(512u)); \
108 EXPECT_EQ(1024, nextPowTwoFunc(513u)); \
109 EXPECT_EQ(1024, nextPowTwoFunc(777u)); \
110 EXPECT_EQ(1ul << 31, nextPowTwoFunc((1ul << 31) - 1)); \
111 EXPECT_EQ(1ul << 32, nextPowTwoFunc((1ul << 32) - 1)); \
112 EXPECT_EQ(1ull << 63, nextPowTwoFunc((1ull << 62) + 1)); \
116 TEST(Bits, nextPowTwoClz) {
117 testPowTwo(nextPowTwo);
120 BENCHMARK(nextPowTwoClz, iters) {
121 for (unsigned long i = 0; i < iters; ++i) {
122 auto x = folly::nextPowTwo(iters);
123 folly::doNotOptimizeAway(x);
127 TEST(Bits, isPowTwo) {
128 EXPECT_FALSE(isPowTwo(0u));
129 EXPECT_TRUE(isPowTwo(1ul));
130 EXPECT_TRUE(isPowTwo(2ull));
131 EXPECT_FALSE(isPowTwo(3ul));
132 EXPECT_TRUE(isPowTwo(4ul));
133 EXPECT_FALSE(isPowTwo(5ul));
134 EXPECT_TRUE(isPowTwo(8ul));
135 EXPECT_FALSE(isPowTwo(15u));
136 EXPECT_TRUE(isPowTwo(16u));
137 EXPECT_FALSE(isPowTwo(17u));
138 EXPECT_FALSE(isPowTwo(511ul));
139 EXPECT_TRUE(isPowTwo(512ul));
140 EXPECT_FALSE(isPowTwo(513ul));
141 EXPECT_FALSE(isPowTwo((1ul<<31) - 1));
142 EXPECT_TRUE(isPowTwo(1ul<<31));
143 EXPECT_FALSE(isPowTwo((1ul<<31) + 1));
144 EXPECT_FALSE(isPowTwo((1ull<<63) - 1));
145 EXPECT_TRUE(isPowTwo(1ull<<63));
146 EXPECT_FALSE(isPowTwo((1ull<<63) + 1));
149 BENCHMARK_DRAW_LINE();
150 BENCHMARK(isPowTwo, iters) {
152 for (unsigned long i = 0; i < iters; ++i) {
153 b = folly::isPowTwo(i);
154 folly::doNotOptimizeAway(b);
158 TEST(Bits, popcount) {
159 EXPECT_EQ(0, popcount(0U));
160 EXPECT_EQ(1, popcount(1U));
161 EXPECT_EQ(32, popcount(uint32_t(-1)));
162 EXPECT_EQ(64, popcount(uint64_t(-1)));
165 int main(int argc, char** argv) {
166 testing::InitGoogleTest(&argc, argv);
167 google::ParseCommandLineFlags(&argc, &argv, true);
168 auto ret = RUN_ALL_TESTS();
169 if (!ret && FLAGS_benchmark) {
170 folly::runBenchmarks();
176 Benchmarks run on dual Xeon X5650's @ 2.67GHz w/hyperthreading enabled
177 (12 physical cores, 12 MB cache, 72 GB RAM)
179 Benchmark Iters Total t t/iter iter/sec
180 ------------------------------------------------------------------------------
181 * nextPowTwoClz 1000000 1.659 ms 1.659 ns 574.8 M