forked from skarupke/branchless_binary_search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
189 lines (174 loc) · 5.13 KB
/
test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <bit>
#include "binary_search.hpp"
struct Standard
{
template<typename It, typename T, typename C>
auto operator()(It begin, It end, const T & val, C && cmp)
{
// return std::lower_bound(begin, end, val, cmp);
return standard_lower_bound(begin, end, val, cmp);
}
};
struct Branchless
{
template<typename It, typename T, typename C>
auto operator()(It begin, It end, const T & val, C && cmp)
{
return branchless_lower_bound(begin, end, val, cmp);
}
};
#ifdef ENABLE_GTEST
#include <gtest/gtest.h>
template<typename BinarySearch, typename Cmp>
void test_all(Cmp && cmp, int limit = 64)
{
std::vector<int> sorted;
auto found = BinarySearch()(sorted.begin(), sorted.end(), 0, cmp);
ASSERT_EQ(sorted.end(), found);
for (int i = 0; i < limit; ++i)
{
sorted.push_back(i);
found = BinarySearch()(sorted.begin(), sorted.end(), -1, cmp);
ASSERT_EQ(0, *found) << "i: " << i;
for (int j = 0; j <= i; ++j)
{
found = BinarySearch()(sorted.begin(), sorted.end(), j, cmp);
ASSERT_EQ(j, *found) << "i: " << i << ", j: " << j;
}
found = BinarySearch()(sorted.begin(), sorted.end(), i + 1, cmp);
ASSERT_EQ(sorted.end(), found) << "i: " << i;
}
}
template<typename BinarySearch>
void test_all(int limit = 64)
{
test_all<Branchless>(std::less<>{}, limit);
}
struct CountingCompare
{
template<typename T, typename U>
bool operator()(T && l, U && r)
{
++count;
return std::less<>{}(l, r);
}
int & count;
};
template<typename BinarySearch>
void test_counts(int limit = 64)
{
int count = 0;
CountingCompare cmp{count};
std::vector<int> sorted;
auto found = BinarySearch()(sorted.begin(), sorted.end(), 0, cmp);
ASSERT_EQ(sorted.end(), found);
ASSERT_EQ(0, count);
for (int i = 0; i < limit; ++i)
{
sorted.push_back(i);
int compare_limit = 65 - std::countl_zero(std::bit_ceil(static_cast<unsigned>(i)));
count = 0;
found = BinarySearch()(sorted.begin(), sorted.end(), -1, cmp);
ASSERT_EQ(0, *found) << "i: " << i;
int actual_count = count;
count = 0;
Standard()(sorted.begin(), sorted.end(), -1, cmp);
ASSERT_LE(actual_count, compare_limit) << "count: " << count << ", actual_count: " << actual_count << ", i: " << i;
for (int j = 0; j <= i; ++j)
{
count = 0;
found = BinarySearch()(sorted.begin(), sorted.end(), j, cmp);
ASSERT_EQ(j, *found) << "i: " << i << ", j: " << j;
actual_count = count;
count = 0;
Standard()(sorted.begin(), sorted.end(), j, cmp);
ASSERT_LE(actual_count, compare_limit) << "count: " << count << ", actual_count: " << actual_count << ", i: " << i << ", j: " << j;
}
count = 0;
found = BinarySearch()(sorted.begin(), sorted.end(), i + 1, cmp);
ASSERT_EQ(sorted.end(), found) << "i: " << i;
actual_count = count;
count = 0;
Standard()(sorted.begin(), sorted.end(), i + 1, cmp);
ASSERT_LE(actual_count, compare_limit) << "count: " << count << ", actual_count: " << actual_count << ", i: " << i;
}
}
TEST(binary_search, all_shar_odd)
{
int count = 0;
CountingCompare cmp{count};
test_all<Branchless>(cmp);
ASSERT_EQ(14286, count);
}
TEST(binary_search, counts)
{
test_counts<Branchless>();
}
TEST(binary_search, half_shar_odd)
{
int count = 0;
CountingCompare cmp{count};
test_all<Branchless>(cmp, 1);
EXPECT_EQ(3, count);
count = 0;
test_all<Branchless>(cmp, 2);
EXPECT_EQ(11, count);
count = 0;
test_all<Branchless>(cmp, 3);
EXPECT_EQ(24, count);
count = 0;
test_all<Branchless>(cmp, 4);
EXPECT_EQ(42, count);
}
TEST(binary_search, all_lower_bound)
{
int count = 0;
CountingCompare cmp{count};
test_all<Standard>(cmp);
ASSERT_EQ(11835, count);
}
TEST(binary_search, half_lower_bound)
{
int count = 0;
CountingCompare cmp{count};
test_all<Standard>(cmp, 1);
EXPECT_EQ(3, count);
count = 0;
test_all<Standard>(cmp, 2);
EXPECT_EQ(10, count);
count = 0;
test_all<Standard>(cmp, 3);
EXPECT_EQ(20, count);
count = 0;
test_all<Standard>(cmp, 4);
EXPECT_EQ(35, count);
}
#endif
#include <iostream>
#ifdef ENABLE_GTEST
int main(int argc, char * argv[])
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
#else
int main()
{
std::vector<float> floats;
for (float f = 0.0f; f < 100.0f; f += 1.0f)
{
floats.push_back(f);
}
for (float f = -1.0f; f <= 100.0f; ++f)
{
auto found0 = standard_lower_bound(floats.begin(), floats.end(), f);
auto found1 = branchless_lower_bound(floats.begin(), floats.end(), f);
if (found0 != found1)
std::cout << "Error " << f << std::endl;
found0 = standard_lower_bound(floats.begin(), floats.end(), f + 0.5f);
found1 = branchless_lower_bound(floats.begin(), floats.end(), f + 0.5f);
if (found0 != found1)
std::cout << "Error " << (f + 0.5f) << std::endl;
}
}
#endif