Pull from FB rev 63ce89e2f2301e6bba44a111cc7d4218022156f6
[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 unsigned int findLastSetPortable(INT x) {
42   return detail::findLastSetPortable(
43       static_cast<typename std::make_unsigned<INT>::type>(x));
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     EXPECT_EQ(i + 1, findLastSetPortable(v1));
55
56     INT v2 = (static_cast<UINT>(1) << i) - 1;
57     EXPECT_EQ(i, findLastSet(v2));
58     EXPECT_EQ(i, findLastSetPortable(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 #ifdef __GNUC__
117
118 TEST(Bits, nextPowTwoClz) {
119   testPowTwo(nextPowTwo);
120 }
121
122 int x; // prevent the loop from getting optimized away
123 BENCHMARK(nextPowTwoClz, iters) {
124   x = folly::nextPowTwo(iters);
125 }
126
127 #endif
128
129 TEST(Bits, nextPowTwoPortable) {
130   testPowTwo(detail::nextPowTwoPortable);
131 }
132
133 BENCHMARK(nextPowTwoPortable, iters) {
134   x = detail::nextPowTwoPortable(iters);
135 }
136
137 BENCHMARK_DRAW_LINE();
138
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();
145   }
146   return ret;
147 }
148
149 /*
150 Benchmarks run on dual Xeon X5650's @ 2.67GHz w/hyperthreading enabled
151   (12 physical cores, 12 MB cache, 72 GB RAM)
152
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
157 */