-
Notifications
You must be signed in to change notification settings - Fork 49
/
function_registry.cpp
289 lines (214 loc) · 7.39 KB
/
function_registry.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include <string>
#include <vector>
#include <unordered_set>
#include <algorithm>
using function_ptr = std::uint64_t (*)(const uint8_t* data, const size_t size);
using function_64_ptr = std::uint64_t (*)(const uint64_t* data, int size);
class Function {
public:
bool is_trusted;
std::string name;
std::string help;
function_ptr function;
function_64_ptr function_64;
};
class FunctionRegistry final {
private:
using NameList = std::vector<std::string>;
using FunctionList = std::vector<Function>;
NameList available;
FunctionList functions;
int widest_name;
public:
FunctionRegistry() {
build();
widest_name = 0;
for (const auto& name: available) {
widest_name = std::max(widest_name, static_cast<int>(name.size()));
}
}
public:
bool has(const std::string& name) const;
const Function& get(const std::string& name) const;
const NameList& get_available() const {
return available;
}
const FunctionList& get_functions() const {
return functions;
}
public:
int get_widest_name() const {
return widest_name;
}
private:
void build();
void add(const std::string& name, const std::string& help, function_ptr fn);
void add(const std::string& name, const std::string& help, function_64_ptr fn);
void add_trusted(const std::string& name, const std::string& help, function_ptr fn);
};
bool FunctionRegistry::has(const std::string& name) const {
return std::find(available.begin(), available.end(), name) != available.end();
}
const Function& FunctionRegistry::get(const std::string& name) const {
auto pred = [&name](const Function& item) {
return item.name == name;
};
const auto& iter = std::find_if(functions.begin(), functions.end(), pred);
if (iter == functions.end()) {
fprintf(stderr, "function %s not found", name.c_str());
assert(false && "function not found");
}
return *iter;
}
void FunctionRegistry::build() {
// definition start
add_trusted("lookup-8",
"lookup in std::uint8_t[256] LUT",
popcnt_lookup_8bit);
add_trusted("lookup-64",
"lookup in std::uint64_t[256] LUT",
popcnt_lookup_64bit);
add("bit-parallel",
"naive bit parallel method",
popcnt_parallel_64bit_naive);
add("bit-parallel-optimized",
"a bit better bit parallel",
popcnt_parallel_64bit_optimized);
add("bit-parallel-optimized2",
"better utilization of 2- and 4-bit subwords",
popcnt_parallel_64bit_optimized2);
add("bit-parallel-mul",
"bit-parallel with fewer instructions",
popcnt_parallel_64bit_mul);
add("bit-parallel32",
"naive bit parallel method (32 bit)",
popcnt_parallel_32bit_naive);
add("bit-parallel-optimized32",
"a bit better bit parallel (32 bit)",
popcnt_parallel_32bit_optimized);
add("harley-seal",
"Harley-Seal popcount (4th iteration)",
popcnt_harley_seal);
#if defined(HAVE_SSE_INSTRUCTIONS)
add("sse-bit-parallel",
"SSE implementation of bit-parallel-optimized (unrolled)",
popcnt_SSE_bit_parallel);
add("sse-bit-parallel-original",
"SSE implementation of bit-parallel-optimized",
popcnt_SSE_bit_parallel_original);
add("sse-bit-parallel-better",
"SSE implementation of bit-parallel with fewer instructions",
popcnt_SSE_bit_parallel_better);
add("sse-harley-seal",
"SSE implementation of Harley-Seal",
popcnt_SSE_harley_seal);
add("sse-lookup",
"SSSE3 variant using pshufb instruction (unrolled)",
popcnt_SSE_lookup);
add("sse-lookup-original",
"SSSE3 variant using pshufb instruction",
popcnt_SSE_lookup_original);
#endif
#if defined(HAVE_AVX2_INSTRUCTIONS)
add("avx2-lookup",
"AVX2 variant using pshufb instruction (unrolled)",
popcnt_AVX2_lookup);
add("avx2-lookup-original",
"AVX2 variant using pshufb instruction",
popcnt_AVX2_lookup_original);
add("avx2-harley-seal",
"AVX2 implementation of Harley-Seal",
popcnt_AVX2_harley_seal);
#endif
#if defined(HAVE_POPCNT_INSTRUCTION)
add("cpu",
"CPU instruction popcnt (64-bit variant)",
popcnt_cpu_64bit);
add("sse-cpu",
"load data with SSE, then count bits using popcnt",
popcnt_cpu_64bit);
#endif
#if defined(HAVE_AVX2_INSTRUCTIONS)
add("avx2-cpu",
"load data with AVX2, then count bits using popcnt",
popcnt_AVX2_and_cpu);
#endif
#if defined(HAVE_AVX512BW_INSTRUCTIONS)
add("avx512-harley-seal",
"AVX512 implementation of Harley-Seal",
popcnt_AVX512_harley_seal);
add("avx512bw-shuf",
"AVX512BW implementation uses shuffle instruction",
popcnt_AVX512BW_lookup_original);
#endif
#if defined(HAVE_AVX512VBMI_INSTRUCTIONS)
add("avx512vbmi-shuf",
"AVX512VBMI implementation uses shuffle instruction",
popcnt_AVX512VBMI_lookup);
#endif
#if defined(HAVE_AVX512VPOPCNT_INSTRUCTIONS)
add("avx512-vpopcnt",
"AVX512 VPOPCNT",
popcnt_AVX512_vpopcnt);
#endif
add("builtin-popcnt",
"builtin for popcnt",
builtin_popcnt);
add("builtin-popcnt32",
"builtin for popcnt (32-bit variant)",
builtin_popcnt32);
add("builtin-popcnt-unrolled",
"unrolled builtin-popcnt",
builtin_popcnt_unrolled);
add("builtin-popcnt-unrolled32",
"unrolled builtin-popcnt32",
builtin_popcnt_unrolled32);
#if defined(HAVE_POPCNT_INSTRUCTION)
add("builtin-popcnt-unrolled-errata",
"unrolled builtin-popcnt avoiding false-dependency",
builtin_popcnt_unrolled_errata);
add("builtin-popcnt-unrolled-errata-manual",
"unrolled builtin-popcnt avoiding false-dependency (asembly code)",
builtin_popcnt_unrolled_errata_manual);
add("builtin-popcnt-movdq",
"builtin-popcnt where data is loaded via SSE registers",
builtin_popcnt_movdq);
add("builtin-popcnt-movdq-unrolled",
"builtin-popcnt-movdq unrolled",
builtin_popcnt_movdq_unrolled);
add("builtin-popcnt-movdq-unrolled_manual",
"builtin-popcnt-movdq unrolled (assembly code)",
builtin_popcnt_movdq_unrolled_manual);
#endif
#if defined(HAVE_NEON_INSTRUCTIONS)
add("neon-vcnt",
"ARM Neon using VCNT",
popcnt_neon_vcnt);
add("neon-HS",
"Harley-Seal using Neon VCNT",
popcnt_neon_harley_seal);
#endif
#if defined(HAVE_AARCH64_ARCHITECTURE)
add("aarch64-cnt",
"ARMv8 Neon using CNT",
popcnt_aarch64_cnt);
#endif
#if defined(HAVE_RVV_INSTRUCTIONS)
add("rvv-1",
"RVV generic implementation",
popcnt_rvv_lookup);
#endif
// definition end
}
void FunctionRegistry::add(const std::string& name, const std::string& help, function_ptr fn) {
available.push_back(name);
functions.push_back({false, name, help, fn, nullptr});
}
void FunctionRegistry::add(const std::string& name, const std::string& help, function_64_ptr fn) {
available.push_back(name);
functions.push_back({false, name, help, nullptr, fn});
}
void FunctionRegistry::add_trusted(const std::string& name, const std::string& help, function_ptr fn) {
available.push_back(name);
functions.push_back({true, name, help, fn, nullptr});
}