2 * Copyright 2012 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;
30 EXPECT_EQ(0, findFirstSet(static_cast<INT>(0)));
31 size_t bits = std::numeric_limits<
32 typename std::make_unsigned<INT>::type>::digits;
33 for (size_t i = 0; i < bits; i++) {
34 INT v = (static_cast<INT>(1) << (bits - 1)) |
35 (static_cast<INT>(1) << i);
36 EXPECT_EQ(i+1, findFirstSet(v));
41 unsigned int findLastSetPortable(INT x) {
42 return detail::findLastSetPortable(
43 static_cast<typename std::make_unsigned<INT>::type>(x));
48 typedef typename std::make_unsigned<INT>::type UINT;
49 EXPECT_EQ(0, findLastSet(static_cast<INT>(0)));
50 size_t bits = std::numeric_limits<UINT>::digits;
51 for (size_t i = 0; i < bits; i++) {
52 INT v1 = static_cast<UINT>(1) << i;
53 EXPECT_EQ(i + 1, findLastSet(v1));
54 EXPECT_EQ(i + 1, findLastSetPortable(v1));
56 INT v2 = (static_cast<UINT>(1) << i) - 1;
57 EXPECT_EQ(i, findLastSet(v2));
58 EXPECT_EQ(i, findLastSetPortable(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)); \
118 TEST(Bits, nextPowTwoClz) {
119 testPowTwo(nextPowTwo);
122 int x; // prevent the loop from getting optimized away
123 BENCHMARK(nextPowTwoClz, iters) {
124 x = folly::nextPowTwo(iters);
129 TEST(Bits, nextPowTwoPortable) {
130 testPowTwo(detail::nextPowTwoPortable);
133 BENCHMARK(nextPowTwoPortable, iters) {
134 x = detail::nextPowTwoPortable(iters);
137 BENCHMARK_DRAW_LINE();
139 int main(int argc, char** argv) {
140 testing::InitGoogleTest(&argc, argv);
141 google::ParseCommandLineFlags(&argc, &argv, true);
142 auto ret = RUN_ALL_TESTS();
143 if (!ret && FLAGS_benchmark) {
144 folly::runBenchmarks();
150 Benchmarks run on dual Xeon X5650's @ 2.67GHz w/hyperthreading enabled
151 (12 physical cores, 12 MB cache, 72 GB RAM)
153 Benchmark Iters Total t t/iter iter/sec
154 ------------------------------------------------------------------------------
155 * nextPowTwoClz 1000000 1.659 ms 1.659 ns 574.8 M
156 +66.8% nextPowTwoPortable 1000000 2.767 ms 2.767 ns 344.7 M