gflags now likes namespace gflags, not google
[folly.git] / folly / test / BitsTest.cpp
1 /*
2  * Copyright 2014 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 // Test constexpr-ness.
27 #ifndef __clang__
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");
32 #endif  // __clang__
33
34 namespace {
35
36 template <class INT>
37 void testFFS() {
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));
45   }
46 }
47
48 template <class INT>
49 void testFLS() {
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));
56
57     INT v2 = (static_cast<UINT>(1) << i) - 1;
58     EXPECT_EQ(i, findLastSet(v2));
59   }
60 }
61
62 }  // namespace
63
64 TEST(Bits, FindFirstSet) {
65   testFFS<char>();
66   testFFS<signed char>();
67   testFFS<unsigned char>();
68   testFFS<short>();
69   testFFS<unsigned short>();
70   testFFS<int>();
71   testFFS<unsigned int>();
72   testFFS<long>();
73   testFFS<unsigned long>();
74   testFFS<long long>();
75   testFFS<unsigned long long>();
76 }
77
78 TEST(Bits, FindLastSet) {
79   testFLS<char>();
80   testFLS<signed char>();
81   testFLS<unsigned char>();
82   testFLS<short>();
83   testFLS<unsigned short>();
84   testFLS<int>();
85   testFLS<unsigned int>();
86   testFLS<long>();
87   testFLS<unsigned long>();
88   testFLS<long long>();
89   testFLS<unsigned long long>();
90 }
91
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));        \
113 }
114
115
116 TEST(Bits, nextPowTwoClz) {
117   testPowTwo(nextPowTwo);
118 }
119
120 BENCHMARK(nextPowTwoClz, iters) {
121   for (unsigned long i = 0; i < iters; ++i) {
122     auto x = folly::nextPowTwo(iters);
123     folly::doNotOptimizeAway(x);
124   }
125 }
126
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));
147 }
148
149 BENCHMARK_DRAW_LINE();
150 BENCHMARK(isPowTwo, iters) {
151   bool b;
152   for (unsigned long i = 0; i < iters; ++i) {
153     b = folly::isPowTwo(i);
154     folly::doNotOptimizeAway(b);
155   }
156 }
157
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)));
163 }
164
165 int main(int argc, char** argv) {
166   testing::InitGoogleTest(&argc, argv);
167   gflags::ParseCommandLineFlags(&argc, &argv, true);
168   auto ret = RUN_ALL_TESTS();
169   if (!ret && FLAGS_benchmark) {
170     folly::runBenchmarks();
171   }
172   return ret;
173 }
174
175 /*
176 Benchmarks run on dual Xeon X5650's @ 2.67GHz w/hyperthreading enabled
177   (12 physical cores, 12 MB cache, 72 GB RAM)
178
179 Benchmark                               Iters   Total t    t/iter iter/sec
180 ------------------------------------------------------------------------------
181 *       nextPowTwoClz                 1000000  1.659 ms  1.659 ns  574.8 M
182 */