Copyright 2012 -> 2013
[folly.git] / folly / test / BitsTest.cpp
1 /*
2  * Copyright 2013 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 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");
31
32 namespace {
33
34 template <class INT>
35 void testFFS() {
36   EXPECT_EQ(0, findFirstSet(static_cast<INT>(0)));
37   size_t bits = std::numeric_limits<
38     typename std::make_unsigned<INT>::type>::digits;
39   for (size_t i = 0; i < bits; i++) {
40     INT v = (static_cast<INT>(1) << (bits - 1)) |
41             (static_cast<INT>(1) << i);
42     EXPECT_EQ(i+1, findFirstSet(v));
43   }
44 }
45
46 template <class INT>
47 void testFLS() {
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
55     INT v2 = (static_cast<UINT>(1) << i) - 1;
56     EXPECT_EQ(i, findLastSet(v2));
57   }
58 }
59
60 }  // namespace
61
62 TEST(Bits, FindFirstSet) {
63   testFFS<char>();
64   testFFS<signed char>();
65   testFFS<unsigned char>();
66   testFFS<short>();
67   testFFS<unsigned short>();
68   testFFS<int>();
69   testFFS<unsigned int>();
70   testFFS<long>();
71   testFFS<unsigned long>();
72   testFFS<long long>();
73   testFFS<unsigned long long>();
74 }
75
76 TEST(Bits, FindLastSet) {
77   testFLS<char>();
78   testFLS<signed char>();
79   testFLS<unsigned char>();
80   testFLS<short>();
81   testFLS<unsigned short>();
82   testFLS<int>();
83   testFLS<unsigned int>();
84   testFLS<long>();
85   testFLS<unsigned long>();
86   testFLS<long long>();
87   testFLS<unsigned long long>();
88 }
89
90 #define testPowTwo(nextPowTwoFunc) {                              \
91   EXPECT_EQ(1, nextPowTwoFunc(0u));                               \
92   EXPECT_EQ(1, nextPowTwoFunc(1u));                               \
93   EXPECT_EQ(2, nextPowTwoFunc(2u));                               \
94   EXPECT_EQ(4, nextPowTwoFunc(3u));                               \
95   EXPECT_EQ(4, nextPowTwoFunc(4u));                               \
96   EXPECT_EQ(8, nextPowTwoFunc(5u));                               \
97   EXPECT_EQ(8, nextPowTwoFunc(6u));                               \
98   EXPECT_EQ(8, nextPowTwoFunc(7u));                               \
99   EXPECT_EQ(8, nextPowTwoFunc(8u));                               \
100   EXPECT_EQ(16, nextPowTwoFunc(9u));                              \
101   EXPECT_EQ(16, nextPowTwoFunc(13u));                             \
102   EXPECT_EQ(16, nextPowTwoFunc(16u));                             \
103   EXPECT_EQ(512, nextPowTwoFunc(510u));                           \
104   EXPECT_EQ(512, nextPowTwoFunc(511u));                           \
105   EXPECT_EQ(512, nextPowTwoFunc(512u));                           \
106   EXPECT_EQ(1024, nextPowTwoFunc(513u));                          \
107   EXPECT_EQ(1024, nextPowTwoFunc(777u));                          \
108   EXPECT_EQ(1ul << 31, nextPowTwoFunc((1ul << 31) - 1));          \
109   EXPECT_EQ(1ul << 32, nextPowTwoFunc((1ul << 32) - 1));          \
110   EXPECT_EQ(1ull << 63, nextPowTwoFunc((1ull << 62) + 1));        \
111 }
112
113
114 TEST(Bits, nextPowTwoClz) {
115   testPowTwo(nextPowTwo);
116 }
117
118 BENCHMARK(nextPowTwoClz, iters) {
119   for (unsigned long i = 0; i < iters; ++i) {
120     auto x = folly::nextPowTwo(iters);
121     folly::doNotOptimizeAway(x);
122   }
123 }
124
125 TEST(Bits, isPowTwo) {
126   EXPECT_FALSE(isPowTwo(0u));
127   EXPECT_TRUE(isPowTwo(1ul));
128   EXPECT_TRUE(isPowTwo(2ull));
129   EXPECT_FALSE(isPowTwo(3ul));
130   EXPECT_TRUE(isPowTwo(4ul));
131   EXPECT_FALSE(isPowTwo(5ul));
132   EXPECT_TRUE(isPowTwo(8ul));
133   EXPECT_FALSE(isPowTwo(15u));
134   EXPECT_TRUE(isPowTwo(16u));
135   EXPECT_FALSE(isPowTwo(17u));
136   EXPECT_FALSE(isPowTwo(511ul));
137   EXPECT_TRUE(isPowTwo(512ul));
138   EXPECT_FALSE(isPowTwo(513ul));
139   EXPECT_FALSE(isPowTwo((1ul<<31) - 1));
140   EXPECT_TRUE(isPowTwo(1ul<<31));
141   EXPECT_FALSE(isPowTwo((1ul<<31) + 1));
142   EXPECT_FALSE(isPowTwo((1ull<<63) - 1));
143   EXPECT_TRUE(isPowTwo(1ull<<63));
144   EXPECT_FALSE(isPowTwo((1ull<<63) + 1));
145 }
146
147 BENCHMARK_DRAW_LINE();
148 BENCHMARK(isPowTwo, iters) {
149   bool b;
150   for (unsigned long i = 0; i < iters; ++i) {
151     b = folly::isPowTwo(i);
152     folly::doNotOptimizeAway(b);
153   }
154 }
155
156 TEST(Bits, popcount) {
157   EXPECT_EQ(0, popcount(0U));
158   EXPECT_EQ(1, popcount(1U));
159   EXPECT_EQ(32, popcount(uint32_t(-1)));
160   EXPECT_EQ(64, popcount(uint64_t(-1)));
161 }
162
163 int main(int argc, char** argv) {
164   testing::InitGoogleTest(&argc, argv);
165   google::ParseCommandLineFlags(&argc, &argv, true);
166   auto ret = RUN_ALL_TESTS();
167   if (!ret && FLAGS_benchmark) {
168     folly::runBenchmarks();
169   }
170   return ret;
171 }
172
173 /*
174 Benchmarks run on dual Xeon X5650's @ 2.67GHz w/hyperthreading enabled
175   (12 physical cores, 12 MB cache, 72 GB RAM)
176
177 Benchmark                               Iters   Total t    t/iter iter/sec
178 ------------------------------------------------------------------------------
179 *       nextPowTwoClz                 1000000  1.659 ms  1.659 ns  574.8 M
180 */