-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathbenchmark.cpp
More file actions
595 lines (537 loc) · 18 KB
/
Copy pathbenchmark.cpp
File metadata and controls
595 lines (537 loc) · 18 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
// Usage: benchmark [OPTIONS]
//
// You can pass the benchmark program one or more of the following
// options: u32, s32, u64, s64 to compare libdivide's speed against
// hardware division. If benchmark is run without any options u64
// is used as default option. benchmark tests a simple function that
// inputs an array of random numerators and a single divisor, and
// returns the sum of their quotients. It tests this using both
// hardware division, and the various division approaches supported
// by libdivide, including vector division.
// Silence MSVC sprintf unsafe warnings
#define _CRT_SECURE_NO_WARNINGS
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#if defined(_WIN32) || defined(WIN32)
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include <windows.h>
#include <mmsystem.h>
#define LIBDIVIDE_WINDOWS
#pragma comment(lib, "winmm")
#endif
#if !defined(LIBDIVIDE_WINDOWS)
#include <sys/time.h> // for gettimeofday()
#endif
#include "libdivide.h"
#if defined(__GNUC__)
#define NOINLINE __attribute__((__noinline__))
#elif defined(_MSC_VER)
#define NOINLINE __declspec(noinline)
#else
#define NOINLINE
#endif
#if defined(LIBDIVIDE_AVX512)
#define x86_VECTOR_TYPE __m512i
#define SETZERO_SI _mm512_setzero_si512
#define LOAD_SI _mm512_load_si512
#define ADD_EPI64 _mm512_add_epi64
#define ADD_EPI32 _mm512_add_epi32
#elif defined(LIBDIVIDE_AVX2)
#define x86_VECTOR_TYPE __m256i
#define SETZERO_SI _mm256_setzero_si256
#define LOAD_SI _mm256_load_si256
#define ADD_EPI64 _mm256_add_epi64
#define ADD_EPI32 _mm256_add_epi32
#elif defined(LIBDIVIDE_SSE2)
#define x86_VECTOR_TYPE __m128i
#define SETZERO_SI _mm_setzero_si128
#define LOAD_SI _mm_load_si128
#define ADD_EPI64 _mm_add_epi64
#define ADD_EPI32 _mm_add_epi32
#endif
#define NANOSEC_PER_SEC 1000000000ULL
#define NANOSEC_PER_USEC 1000ULL
#define NANOSEC_PER_MILLISEC 1000000ULL
#define SEED \
{ 2147483563, 2147483563 ^ 0x49616E42 }
using namespace libdivide;
struct random_state {
uint32_t hi;
uint32_t lo;
};
volatile uint64_t sGlobalUInt64;
size_t iters = 1 << 19;
size_t genIters = 1 << 16;
static uint32_t my_random(struct random_state *state) {
state->hi = (state->hi << 16) + (state->hi >> 16);
state->hi += state->lo;
state->lo += state->hi;
return state->hi;
}
#if defined(LIBDIVIDE_WINDOWS)
static LARGE_INTEGER gPerfCounterFreq;
#endif
#if !defined(LIBDIVIDE_WINDOWS)
static uint64_t nanoseconds(void) {
struct timeval now;
gettimeofday(&now, NULL);
return now.tv_sec * NANOSEC_PER_SEC + now.tv_usec * NANOSEC_PER_USEC;
}
#endif
template <typename IntT, typename Divisor>
NOINLINE uint64_t sum_quotients(const IntT *vals, Divisor div) {
IntT sum = 0;
for (size_t iter = 0; iter < iters; iter += 1) {
sum += vals[iter] / div;
}
return (uint64_t)sum;
}
#ifdef x86_VECTOR_TYPE
template <typename IntT, typename Divisor>
NOINLINE uint64_t sum_quotients_vec(const IntT *vals, Divisor div) {
size_t count = sizeof(x86_VECTOR_TYPE) / sizeof(IntT);
x86_VECTOR_TYPE sumX4 = SETZERO_SI();
for (size_t iter = 0; iter < iters; iter += count) {
x86_VECTOR_TYPE numers = LOAD_SI((const x86_VECTOR_TYPE *)&vals[iter]);
numers = numers / div;
if (sizeof(IntT) == 4) {
sumX4 = ADD_EPI32(sumX4, numers);
} else if (sizeof(IntT) == 8) {
sumX4 = ADD_EPI64(sumX4, numers);
} else {
abort();
}
}
const IntT *comps = (const IntT *)&sumX4;
IntT sum = 0;
for (size_t i = 0; i < count; i++) {
sum += comps[i];
}
return (uint64_t)sum;
}
#elif defined(LIBDIVIDE_NEON)
template <typename Divisor>
NOINLINE uint64_t sum_quotients_vec(const uint32_t *vals, Divisor div) {
typedef uint32_t IntT;
typedef typename NeonVecFor<IntT>::type NeonVectorType;
size_t count = sizeof(NeonVectorType) / sizeof(IntT);
NeonVectorType sumX4 = vdupq_n_u32(0);
for (size_t iter = 0; iter < iters; iter += count) {
NeonVectorType numers = *(NeonVectorType *)&vals[iter];
numers = numers / div;
sumX4 = vaddq_u32(sumX4, numers);
}
const IntT *comps = (const IntT *)&sumX4;
IntT sum = 0;
for (size_t i = 0; i < count; i++) {
sum += comps[i];
}
return (uint64_t)sum;
}
template <typename Divisor>
NOINLINE uint64_t sum_quotients_vec(const int32_t *vals, Divisor div) {
typedef int32_t IntT;
typedef typename NeonVecFor<IntT>::type NeonVectorType;
size_t count = sizeof(NeonVectorType) / sizeof(IntT);
NeonVectorType sumX4 = vdupq_n_s32(0);
for (size_t iter = 0; iter < iters; iter += count) {
NeonVectorType numers = *(NeonVectorType *)&vals[iter];
numers = numers / div;
sumX4 = vaddq_s32(sumX4, numers);
}
const IntT *comps = (const IntT *)&sumX4;
IntT sum = 0;
for (size_t i = 0; i < count; i++) {
sum += comps[i];
}
return (uint64_t)sum;
}
template <typename Divisor>
NOINLINE uint64_t sum_quotients_vec(const uint64_t *vals, Divisor div) {
typedef uint64_t IntT;
typedef typename NeonVecFor<IntT>::type NeonVectorType;
size_t count = sizeof(NeonVectorType) / sizeof(IntT);
NeonVectorType sumX4 = vdupq_n_u64(0);
for (size_t iter = 0; iter < iters; iter += count) {
NeonVectorType numers = *(NeonVectorType *)&vals[iter];
numers = numers / div;
sumX4 = vaddq_u64(sumX4, numers);
}
const IntT *comps = (const IntT *)&sumX4;
IntT sum = 0;
for (size_t i = 0; i < count; i++) {
sum += comps[i];
}
return (uint64_t)sum;
}
template <typename Divisor>
NOINLINE uint64_t sum_quotients_vec(const int64_t *vals, Divisor div) {
typedef int64_t IntT;
typedef typename NeonVecFor<IntT>::type NeonVectorType;
size_t count = sizeof(NeonVectorType) / sizeof(IntT);
const uint64x2_t zeros = vdupq_n_u64(0);
NeonVectorType sumX4 = *reinterpret_cast<const NeonVectorType *>(&zeros);
for (size_t iter = 0; iter < iters; iter += count) {
NeonVectorType numers = *(NeonVectorType *)&vals[iter];
numers = numers / div;
sumX4 = vaddq_s64(sumX4, numers);
}
const IntT *comps = (const IntT *)&sumX4;
IntT sum = 0;
for (size_t i = 0; i < count; i++) {
sum += comps[i];
}
return (uint64_t)sum;
}
#endif
// noinline to force compiler to emit this
template <typename IntT>
NOINLINE divider<IntT> generate_1_divisor(IntT d) {
return divider<IntT>(d);
}
template <typename IntT>
NOINLINE void generate_divisor(IntT denom) {
for (size_t iter = 0; iter < genIters; iter++) {
(void)generate_1_divisor(denom);
}
}
struct time_result_t {
uint64_t time; // in nanoseconds
uint64_t result;
};
enum which_function_t {
func_hardware,
func_scalar_branchfull,
func_scalar_branchfree,
func_vec_branchfull,
func_vec_branchfree,
func_generate
};
template <which_function_t Which, typename IntT>
NOINLINE static time_result_t time_function(const IntT *vals, IntT denom) {
time_result_t tresult;
uint64_t result;
uint64_t diff_nanos;
divider<IntT, BRANCHFULL> div_bfull(denom);
divider<IntT, BRANCHFREE> div_bfree(denom != 1 ? denom : 2);
#if defined(LIBDIVIDE_WINDOWS)
LARGE_INTEGER start, end;
QueryPerformanceCounter(&start);
#else
uint64_t start, end;
start = nanoseconds();
#endif
switch (Which) {
case func_hardware:
result = sum_quotients(vals, denom);
break;
case func_scalar_branchfull:
result = sum_quotients(vals, div_bfull);
break;
case func_scalar_branchfree:
result = sum_quotients(vals, div_bfree);
break;
#if defined(x86_VECTOR_TYPE) || defined(LIBDIVIDE_NEON)
case func_vec_branchfull:
result = sum_quotients_vec(vals, div_bfull);
break;
case func_vec_branchfree:
result = sum_quotients_vec(vals, div_bfree);
break;
#endif
case func_generate:
generate_divisor(denom);
result = 0;
break;
default:
abort();
}
#if defined(LIBDIVIDE_WINDOWS)
QueryPerformanceCounter(&end);
diff_nanos = ((end.QuadPart - start.QuadPart) * 1000000000ULL) / gPerfCounterFreq.QuadPart;
#else
end = nanoseconds();
diff_nanos = end - start;
#endif
tresult.time = diff_nanos;
sGlobalUInt64 += result;
tresult.result = result;
return tresult;
}
struct TestResult {
double hardware_time;
double base_time;
double branchfree_time;
double vector_time;
double vector_branchfree_time;
double gen_time;
int algo;
};
static uint64_t find_min(const uint64_t *vals, size_t cnt) {
uint64_t result = vals[0];
size_t i;
for (i = 1; i < cnt; i++) {
if (vals[i] < result) result = vals[i];
}
return result;
}
template <typename IntT>
NOINLINE struct TestResult test_one(const IntT *vals, IntT denom) {
#define TEST_COUNT 30
struct TestResult result;
memset(&result, 0, sizeof result);
const bool testBranchfree = (denom != 1);
#define CHECK(actual, expected) \
do { \
if ((actual) != (expected)) printf("Failure on line %lu\n", (unsigned long)__LINE__); \
} while (0)
uint64_t my_times[TEST_COUNT], my_times_branchfree[TEST_COUNT], my_times_vector[TEST_COUNT],
my_times_vector_branchfree[TEST_COUNT], his_times[TEST_COUNT], gen_times[TEST_COUNT];
time_result_t tresult;
for (size_t iter = 0; iter < TEST_COUNT; iter++) {
tresult = time_function<func_hardware>(vals, denom);
his_times[iter] = tresult.time;
const uint64_t expected = tresult.result;
tresult = time_function<func_scalar_branchfull>(vals, denom);
my_times[iter] = tresult.time;
CHECK(tresult.result, expected);
if (testBranchfree) {
tresult = time_function<func_scalar_branchfree>(vals, denom);
my_times_branchfree[iter] = tresult.time;
CHECK(tresult.result, expected);
}
#if defined(x86_VECTOR_TYPE) || defined(LIBDIVIDE_NEON)
tresult = time_function<func_vec_branchfull>(vals, denom);
my_times_vector[iter] = tresult.time;
CHECK(tresult.result, expected);
if (testBranchfree) {
tresult = time_function<func_vec_branchfree>(vals, denom);
my_times_vector_branchfree[iter] = tresult.time;
CHECK(tresult.result, expected);
}
#else
my_times_vector[iter] = 0;
my_times_vector_branchfree[iter] = 0;
#endif
tresult = time_function<func_generate>(vals, denom);
gen_times[iter] = tresult.time;
}
result.gen_time = find_min(gen_times, TEST_COUNT) / (double)genIters;
result.base_time = find_min(my_times, TEST_COUNT) / (double)iters;
result.branchfree_time =
testBranchfree ? find_min(my_times_branchfree, TEST_COUNT) / (double)iters : -1;
result.vector_time = find_min(my_times_vector, TEST_COUNT) / (double)iters;
result.vector_branchfree_time =
find_min(my_times_vector_branchfree, TEST_COUNT) / (double)iters;
result.hardware_time = find_min(his_times, TEST_COUNT) / (double)iters;
return result;
#undef TEST_COUNT
}
int libdivide_u32_get_algorithm(uint32_t d) {
const struct libdivide_u32_t denom = libdivide_u32_gen(d);
uint8_t more = denom.more;
if (!denom.magic)
return 0;
else if (!(more & LIBDIVIDE_ADD_MARKER))
return 1;
else
return 2;
}
NOINLINE struct TestResult test_one_u32(uint32_t d, const uint32_t *data) {
struct TestResult result = test_one(data, d);
result.algo = libdivide_u32_get_algorithm(d);
return result;
}
int libdivide_s32_get_algorithm(int32_t d) {
const struct libdivide_s32_t denom = libdivide_s32_gen(d);
uint8_t more = denom.more;
if (!denom.magic)
return 0;
else if (!(more & LIBDIVIDE_ADD_MARKER))
return 1;
else
return 2;
}
NOINLINE struct TestResult test_one_s32(int32_t d, const int32_t *data) {
struct TestResult result = test_one(data, d);
result.algo = libdivide_s32_get_algorithm(d);
return result;
}
int libdivide_u64_get_algorithm(uint64_t d) {
const struct libdivide_u64_t denom = libdivide_u64_gen(d);
uint8_t more = denom.more;
if (!denom.magic)
return 0;
else if (!(more & LIBDIVIDE_ADD_MARKER))
return 1;
else
return 2;
}
NOINLINE struct TestResult test_one_u64(uint64_t d, const uint64_t *data) {
struct TestResult result = test_one(data, d);
result.algo = libdivide_u64_get_algorithm(d);
return result;
}
int libdivide_s64_get_algorithm(int64_t d) {
const struct libdivide_s64_t denom = libdivide_s64_gen(d);
uint8_t more = denom.more;
if (!denom.magic)
return 0;
else if (!(more & LIBDIVIDE_ADD_MARKER))
return 1;
else
return 2;
}
NOINLINE struct TestResult test_one_s64(int64_t d, const int64_t *data) {
struct TestResult result = test_one(data, d);
result.algo = libdivide_s64_get_algorithm(d);
return result;
}
static void report_header(void) {
printf("%6s%9s%8s%8s%8s%8s%8s%7s\n", "#", "system", "scalar", "scl_bf", "vector", "vec_bf",
"gener", "algo");
}
static void report_result(const char *input, struct TestResult result) {
printf("%6s%8.3f%8.3f%8.3f%8.3f%8.3f%9.3f%4d\n", input, result.hardware_time, result.base_time,
result.branchfree_time, result.vector_time, result.vector_branchfree_time, result.gen_time,
result.algo);
}
static void test_many_u32(const uint32_t *data) {
printf("\n%50s", "=== libdivide u32 benchmark ===\n\n");
report_header();
uint32_t d;
for (d = 1; d > 0; d++) {
struct TestResult result = test_one_u32(d, data);
char input_buff[32];
sprintf(input_buff, "%u", d);
report_result(input_buff, result);
}
}
static void test_many_s32(const int32_t *data) {
printf("\n%50s", "=== libdivide s32 benchmark ===\n\n");
report_header();
int32_t d;
for (d = 1; d != 0;) {
struct TestResult result = test_one_s32(d, data);
char input_buff[32];
sprintf(input_buff, "%d", d);
report_result(input_buff, result);
d = -d;
if (d > 0) d++;
}
}
static void test_many_u64(const uint64_t *data) {
printf("\n%50s", "=== libdivide u64 benchmark ===\n\n");
report_header();
uint64_t d;
for (d = 1; d > 0; d++) {
struct TestResult result = test_one_u64(d, data);
char input_buff[32];
sprintf(input_buff, "%" PRIu64, d);
report_result(input_buff, result);
}
}
static void test_many_s64(const int64_t *data) {
printf("\n%50s", "=== libdivide s64 benchmark ===\n\n");
report_header();
int64_t d;
for (d = 1; d != 0;) {
struct TestResult result = test_one_s64(d, data);
char input_buff[32];
sprintf(input_buff, "%" PRId64, d);
report_result(input_buff, result);
d = -d;
if (d > 0) d++;
}
}
static const uint32_t *random_data(unsigned sizeOfType) {
#if defined(LIBDIVIDE_WINDOWS)
/* Align memory to 64 byte boundary for AVX512 */
uint32_t *data = (uint32_t *)_aligned_malloc(iters * sizeOfType, 64);
#else
/* Align memory to 64 byte boundary for AVX512 */
void *ptr = NULL;
int failed = posix_memalign(&ptr, 64, iters * sizeOfType);
if (failed) {
printf("Failed to align memory!\n");
exit(1);
}
uint32_t *data = (uint32_t *)ptr;
#endif
size_t size = (iters * sizeOfType) / sizeof(*data);
struct random_state state = SEED;
for (size_t i = 0; i < size; i++) {
data[i] = my_random(&state);
}
return data;
}
int main(int argc, char *argv[]) {
// Disable printf buffering.
// This is mainly required for Windows.
setbuf(stdout, NULL);
#if defined(LIBDIVIDE_WINDOWS)
QueryPerformanceFrequency(&gPerfCounterFreq);
#endif
int u32 = 0;
int s32 = 0;
int u64 = 0;
int s64 = 0;
if (argc == 1) {
// By default test only u64
u64 = 1;
} else {
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "u32"))
u32 = 1;
else if (!strcmp(argv[i], "u64"))
u64 = 1;
else if (!strcmp(argv[i], "s32"))
s32 = 1;
else if (!strcmp(argv[i], "s64"))
s64 = 1;
else {
printf(
"Usage: benchmark [OPTIONS]\n"
"\n"
"You can pass the benchmark program one or more of the following\n"
"options: u32, s32, u64, s64 to compare libdivide's speed against\n"
"hardware division. If benchmark is run without any options u64\n"
"is used as default option. benchmark tests a simple function that\n"
"inputs an array of random numerators and a single divisor, and\n"
"returns the sum of their quotients. It tests this using both\n"
"hardware division, and the various division approaches supported\n"
"by libdivide, including vector division.\n");
exit(1);
}
}
}
// Make sure that the number of iterations is not
// known at compile time to prevent the compiler
// from magically calculating results at compile
// time and hence falsifying the benchmark.
srand((unsigned)time(NULL));
iters += (rand() % 3) * (1 << 10);
genIters += (rand() % 3) * (1 << 10);
const uint32_t *data = random_data(sizeof(uint32_t));
if (u32) test_many_u32(data);
if (s32) test_many_s32((const int32_t *)data);
#if defined(LIBDIVIDE_WINDOWS)
_aligned_free((void *)data);
#else
free((void *)data);
#endif
data = random_data(sizeof(uint64_t));
if (u64) test_many_u64((const uint64_t *)data);
if (s64) test_many_s64((const int64_t *)data);
#if defined(LIBDIVIDE_WINDOWS)
_aligned_free((void *)data);
#else
free((void *)data);
#endif
return 0;
}