isPowTwo
[folly.git] / folly / test / BitsTest.cpp
1 /*
2  * Copyright 2012 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 // @author Tudor Bosman (tudorb@fb.com)
18
19 #include <gflags/gflags.h>
20 #include "folly/Bits.h"
21 #include "folly/Benchmark.h"
22 #include <gtest/gtest.h>
23
24 using namespace folly;
25
26 namespace {
27
28 template <class INT>
29 void testFFS() {
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));
37   }
38 }
39
40 template <class INT>
41 void testFLS() {
42   typedef typename std::make_unsigned<INT>::type UINT;
43   EXPECT_EQ(0, findLastSet(static_cast<INT>(0)));
44   size_t bits = std::numeric_limits<UINT>::digits;
45   for (size_t i = 0; i < bits; i++) {
46     INT v1 = static_cast<UINT>(1) << i;
47     EXPECT_EQ(i + 1, findLastSet(v1));
48
49     INT v2 = (static_cast<UINT>(1) << i) - 1;
50     EXPECT_EQ(i, findLastSet(v2));
51   }
52 }
53
54 }  // namespace
55
56 TEST(Bits, FindFirstSet) {
57   testFFS<char>();
58   testFFS<signed char>();
59   testFFS<unsigned char>();
60   testFFS<short>();
61   testFFS<unsigned short>();
62   testFFS<int>();
63   testFFS<unsigned int>();
64   testFFS<long>();
65   testFFS<unsigned long>();
66   testFFS<long long>();
67   testFFS<unsigned long long>();
68 }
69
70 TEST(Bits, FindLastSet) {
71   testFLS<char>();
72   testFLS<signed char>();
73   testFLS<unsigned char>();
74   testFLS<short>();
75   testFLS<unsigned short>();
76   testFLS<int>();
77   testFLS<unsigned int>();
78   testFLS<long>();
79   testFLS<unsigned long>();
80   testFLS<long long>();
81   testFLS<unsigned long long>();
82 }
83
84 #define testPowTwo(nextPowTwoFunc) {                              \
85   EXPECT_EQ(1, nextPowTwoFunc(0u));                               \
86   EXPECT_EQ(1, nextPowTwoFunc(1u));                               \
87   EXPECT_EQ(2, nextPowTwoFunc(2u));                               \
88   EXPECT_EQ(4, nextPowTwoFunc(3u));                               \
89   EXPECT_EQ(4, nextPowTwoFunc(4u));                               \
90   EXPECT_EQ(8, nextPowTwoFunc(5u));                               \
91   EXPECT_EQ(8, nextPowTwoFunc(6u));                               \
92   EXPECT_EQ(8, nextPowTwoFunc(7u));                               \
93   EXPECT_EQ(8, nextPowTwoFunc(8u));                               \
94   EXPECT_EQ(16, nextPowTwoFunc(9u));                              \
95   EXPECT_EQ(16, nextPowTwoFunc(13u));                             \
96   EXPECT_EQ(16, nextPowTwoFunc(16u));                             \
97   EXPECT_EQ(512, nextPowTwoFunc(510u));                           \
98   EXPECT_EQ(512, nextPowTwoFunc(511u));                           \
99   EXPECT_EQ(512, nextPowTwoFunc(512u));                           \
100   EXPECT_EQ(1024, nextPowTwoFunc(513u));                          \
101   EXPECT_EQ(1024, nextPowTwoFunc(777u));                          \
102   EXPECT_EQ(1ul << 31, nextPowTwoFunc((1ul << 31) - 1));          \
103   EXPECT_EQ(1ul << 32, nextPowTwoFunc((1ul << 32) - 1));          \
104   EXPECT_EQ(1ull << 63, nextPowTwoFunc((1ull << 62) + 1));        \
105 }
106
107
108 TEST(Bits, nextPowTwoClz) {
109   testPowTwo(nextPowTwo);
110 }
111
112 BENCHMARK(nextPowTwoClz, iters) {
113   for (unsigned long i = 0; i < iters; ++i) {
114     auto x = folly::nextPowTwo(iters);
115     folly::doNotOptimizeAway(x);
116   }
117 }
118
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));
139 }
140
141 BENCHMARK_DRAW_LINE();
142 BENCHMARK(isPowTwo, iters) {
143   bool b;
144   for (unsigned long i = 0; i < iters; ++i) {
145     b = folly::isPowTwo(i);
146     folly::doNotOptimizeAway(b);
147   }
148 }
149
150 TEST(Bits, popcount) {
151   EXPECT_EQ(0, popcount(0U));
152   EXPECT_EQ(1, popcount(1U));
153   EXPECT_EQ(32, popcount(uint32_t(-1)));
154   EXPECT_EQ(64, popcount(uint64_t(-1)));
155 }
156
157 int main(int argc, char** argv) {
158   testing::InitGoogleTest(&argc, argv);
159   google::ParseCommandLineFlags(&argc, &argv, true);
160   auto ret = RUN_ALL_TESTS();
161   if (!ret && FLAGS_benchmark) {
162     folly::runBenchmarks();
163   }
164   return ret;
165 }
166
167 /*
168 Benchmarks run on dual Xeon X5650's @ 2.67GHz w/hyperthreading enabled
169   (12 physical cores, 12 MB cache, 72 GB RAM)
170
171 Benchmark                               Iters   Total t    t/iter iter/sec
172 ------------------------------------------------------------------------------
173 *       nextPowTwoClz                 1000000  1.659 ms  1.659 ns  574.8 M
174 */