-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathbenchmarks.cpp
520 lines (447 loc) · 14.7 KB
/
benchmarks.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
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
// intx: extended precision integer library.
// Copyright 2019 Pawel Bylica.
// Licensed under the Apache License, Version 2.0.
#include "../experimental/addmod.hpp"
#include <benchmark/benchmark.h>
#include <intx/intx.hpp>
#include <test/utils/gmp.hpp>
#include <test/utils/random.hpp>
#if __clang_major__ >= 11 && !defined(__apple_build_version__)
#define INTX_HAS_EXTINT 1
#else
#define INTX_HAS_EXTINT 0
#endif
using namespace intx;
using namespace intx::test;
template <typename ArgT, div_result<ArgT> DivFn(const ArgT&, const ArgT&)>
static void div(benchmark::State& state) noexcept
{
const auto division_set_id = [&state]() noexcept {
switch (state.range(0))
{
case 64:
return x_64;
case 128:
return x_128;
case 192:
return x_192;
case 256:
return lt_256;
default:
state.SkipWithError("unexpected argument");
return x_64;
}
}();
const auto& xs = test::get_samples<ArgT>(sizeof(ArgT) == sizeof(uint256) ? x_256 : x_512);
const auto& ys = test::get_samples<ArgT>(division_set_id);
while (state.KeepRunningBatch(xs.size()))
{
for (size_t i = 0; i < xs.size(); ++i)
{
const auto _ = DivFn(xs[i], ys[i]);
benchmark::DoNotOptimize(_);
}
}
}
BENCHMARK_TEMPLATE(div, uint256, udivrem)->DenseRange(64, 256, 64);
BENCHMARK_TEMPLATE(div, uint256, gmp::udivrem)->DenseRange(64, 256, 64);
BENCHMARK_TEMPLATE(div, uint512, udivrem)->DenseRange(64, 256, 64);
BENCHMARK_TEMPLATE(div, uint512, gmp::udivrem)->DenseRange(64, 256, 64);
template <uint256 ModFn(const uint256&, const uint256&, const uint256&)>
static void mod(benchmark::State& state)
{
const auto mod_set_id = [&state]() noexcept {
switch (state.range(0))
{
case 64:
return x_64;
case 128:
return x_128;
case 192:
return x_192;
case 256:
return lt_256;
default:
state.SkipWithError("unexpected argument");
return x_64;
}
}();
const auto& xs = test::get_samples<uint256>(x_256);
const auto& ys = test::get_samples<uint256>(y_256);
const auto& ms = test::get_samples<uint256>(mod_set_id);
while (state.KeepRunningBatch(xs.size()))
{
for (size_t i = 0; i < xs.size(); ++i)
{
const auto _ = ModFn(xs[i], ys[i], ms[i]);
benchmark::DoNotOptimize(_);
}
}
}
#define ARGS DenseRange(64, 256, 64)
BENCHMARK_TEMPLATE(mod, addmod)->ARGS;
BENCHMARK_TEMPLATE(mod, addmod_public)->ARGS;
BENCHMARK_TEMPLATE(mod, addmod_simple)->ARGS;
BENCHMARK_TEMPLATE(mod, addmod_prenormalize)->ARGS;
BENCHMARK_TEMPLATE(mod, addmod_daosvik)->ARGS;
BENCHMARK_TEMPLATE(mod, mulmod)->ARGS;
#undef ARGS
template <uint256 ModFn(const uint256&, const uint256&, const uint256&)>
static void ecmod(benchmark::State& state)
{
// Samples such x <= m, y <= m.
const auto& xs = test::get_samples<uint256>(lt_x_256);
const auto& ys = test::get_samples<uint256>(lt_256);
const auto& ms = test::get_samples<uint256>(x_256);
while (state.KeepRunningBatch(xs.size()))
{
for (size_t i = 0; i < xs.size(); ++i)
{
const auto _ = ModFn(xs[i], ys[i], ms[i]);
benchmark::DoNotOptimize(_);
}
}
}
BENCHMARK_TEMPLATE(ecmod, addmod_public);
BENCHMARK_TEMPLATE(ecmod, addmod_simple);
BENCHMARK_TEMPLATE(ecmod, addmod_prenormalize);
BENCHMARK_TEMPLATE(ecmod, addmod_daosvik);
BENCHMARK_TEMPLATE(ecmod, mulmod);
template <unsigned N>
[[gnu::noinline]] static auto public_mul(const intx::uint<N>& x, const intx::uint<N>& y) noexcept
{
return x * y;
}
template <unsigned N>
[[gnu::noinline]] static auto umul_(const intx::uint<N>& x, const intx::uint<N>& y) noexcept
{
return intx::umul(x, y);
}
inline auto inline_add(const uint256& x, const uint256& y) noexcept
{
return x + y;
}
uint256 add(const uint256& x, const uint256& y) noexcept;
inline auto inline_sub(const uint256& x, const uint256& y) noexcept
{
return x - y;
}
uint256 sub(const uint256& x, const uint256& y) noexcept;
uint256 exp(const uint256& x, const uint256& y) noexcept;
inline auto inline_add(const uint512& x, const uint512& y) noexcept
{
return x + y;
}
uint512 add(const uint512& x, const uint512& y) noexcept;
inline auto inline_sub(const uint512& x, const uint512& y) noexcept
{
return x - y;
}
uint512 sub(const uint512& x, const uint512& y) noexcept;
template <typename ResultT, typename ArgT, ResultT BinOp(const ArgT&, const ArgT&)>
static void binop(benchmark::State& state)
{
const auto& xs = test::get_samples<ArgT>(sizeof(ArgT) == sizeof(uint256) ? x_256 : x_512);
const auto& ys = test::get_samples<ArgT>(sizeof(ArgT) == sizeof(uint256) ? y_256 : y_512);
while (state.KeepRunningBatch(xs.size()))
{
for (size_t i = 0; i < xs.size(); ++i)
{
const auto _ = BinOp(xs[i], ys[i]);
benchmark::DoNotOptimize(_);
}
}
}
BENCHMARK_TEMPLATE(binop, uint256, uint256, add);
BENCHMARK_TEMPLATE(binop, uint256, uint256, inline_add);
BENCHMARK_TEMPLATE(binop, uint256, uint256, sub);
BENCHMARK_TEMPLATE(binop, uint256, uint256, inline_sub);
BENCHMARK_TEMPLATE(binop, uint256, uint256, public_mul);
BENCHMARK_TEMPLATE(binop, uint256, uint256, gmp::mul);
BENCHMARK_TEMPLATE(binop, uint512, uint256, umul_);
BENCHMARK_TEMPLATE(binop, uint512, uint256, gmp::mul_full);
BENCHMARK_TEMPLATE(binop, uint512, uint512, add);
BENCHMARK_TEMPLATE(binop, uint512, uint512, inline_add);
BENCHMARK_TEMPLATE(binop, uint512, uint512, sub);
BENCHMARK_TEMPLATE(binop, uint512, uint512, inline_sub);
BENCHMARK_TEMPLATE(binop, uint512, uint512, public_mul);
BENCHMARK_TEMPLATE(binop, uint512, uint512, gmp::mul);
template <unsigned N>
[[gnu::noinline]] static intx::uint<N> shl_public(
const intx::uint<N>& x, const uint64_t& y) noexcept
{
return x << y;
}
template <unsigned N>
[[gnu::noinline]] static intx::uint<N> shl_public(
const intx::uint<N>& x, const intx::uint<N>& y) noexcept
{
return x << y;
}
[[gnu::noinline]] static intx::uint256 shl_halves(
const intx::uint256& x, const uint64_t& shift) noexcept
{
constexpr auto num_bits = 256;
constexpr auto half_bits = num_bits / 2;
const auto xlo = uint128{x[0], x[1]};
if (shift < half_bits)
{
const auto lo = xlo << shift;
const auto xhi = uint128{x[2], x[3]};
// Find the part moved from lo to hi.
// The shift right here can be invalid:
// for shift == 0 => lshift == half_bits.
// Split it into 2 valid shifts by (rshift - 1) and 1.
const auto rshift = half_bits - shift;
const auto lo_overflow = (xlo >> (rshift - 1)) >> 1;
const auto hi = (xhi << shift) | lo_overflow;
return {lo[0], lo[1], hi[0], hi[1]};
}
// This check is only needed if we want "defined" behavior for shifts
// larger than size of the Int.
if (shift < num_bits)
{
const auto hi = xlo << (shift - half_bits);
return {0, 0, hi[0], hi[1]};
}
return 0;
}
[[gnu::noinline]] static intx::uint256 shl_halves(
const intx::uint256& x, const uint256& big_shift) noexcept
{
if (INTX_UNLIKELY((big_shift[3] | big_shift[2] | big_shift[1]) != 0))
return 0;
const auto shift = big_shift[0];
constexpr auto num_bits = 256;
constexpr auto half_bits = num_bits / 2;
const auto xlo = uint128{x[0], x[1]};
if (shift < half_bits)
{
const auto lo = xlo << shift;
const auto xhi = uint128{x[2], x[3]};
// Find the part moved from lo to hi.
// The shift right here can be invalid:
// for shift == 0 => lshift == half_bits.
// Split it into 2 valid shifts by (rshift - 1) and 1.
const auto rshift = half_bits - shift;
const auto lo_overflow = (xlo >> (rshift - 1)) >> 1;
const auto hi = (xhi << shift) | lo_overflow;
return {lo[0], lo[1], hi[0], hi[1]};
}
// This check is only needed if we want "defined" behavior for shifts
// larger than size of the Int.
if (shift < num_bits)
{
const auto hi = xlo << (shift - half_bits);
return {0, 0, hi[0], hi[1]};
}
return 0;
}
#if INTX_HAS_EXTINT
[[gnu::noinline]] static intx::uint256 shl_llvm(const intx::uint256& x, const uint64_t& y) noexcept
{
unsigned _ExtInt(256) a; // NOLINT(cppcoreguidelines-init-variables)
std::memcpy(&a, &x, sizeof(a));
const auto b = a << y;
uint256 r;
std::memcpy(&r, &b, sizeof(r));
return r;
}
#endif
template <typename ArgT, typename ShiftT, ArgT ShiftFn(const ArgT&, const ShiftT&)>
static void shift(benchmark::State& state)
{
const auto& shift_samples_id = [&state]() noexcept {
switch (state.range(0))
{
case -1:
return shift_mixed;
case 0:
return shift_w0;
case 1:
return shift_w1;
case 2:
return shift_w2;
case 3:
return shift_w3;
default:
state.SkipWithError("unexpected argument");
return shift_mixed;
}
}();
const auto& xs = test::get_samples<ArgT>(sizeof(ArgT) == sizeof(uint256) ? x_256 : x_512);
const auto& raw_shifts = test::get_samples<uint64_t>(shift_samples_id);
std::array<ShiftT, test::num_samples> shifts{};
std::copy(std::cbegin(raw_shifts), std::cend(raw_shifts), std::begin(shifts));
while (state.KeepRunningBatch(xs.size()))
{
for (size_t i = 0; i < xs.size(); ++i)
{
const auto _ = ShiftFn(xs[i], shifts[i]);
benchmark::DoNotOptimize(_);
}
}
}
BENCHMARK_TEMPLATE(shift, uint256, uint256, shl_public)->DenseRange(-1, 3);
BENCHMARK_TEMPLATE(shift, uint256, uint256, shl_halves)->DenseRange(-1, 3);
BENCHMARK_TEMPLATE(shift, uint256, uint64_t, shl_public)->DenseRange(-1, 3);
BENCHMARK_TEMPLATE(shift, uint256, uint64_t, shl_halves)->DenseRange(-1, 3);
#if INTX_HAS_EXTINT
BENCHMARK_TEMPLATE(shift, uint256, uint64_t, shl_llvm)->DenseRange(-1, 3);
#endif
BENCHMARK_TEMPLATE(shift, uint512, uint512, shl_public)->DenseRange(-1, 3);
BENCHMARK_TEMPLATE(shift, uint512, uint64_t, shl_public)->DenseRange(-1, 3);
[[gnu::noinline]] static bool lt_public(const uint256& x, const uint256& y) noexcept
{
return x < y;
}
[[gnu::noinline]] static bool lt_sub(const uint256& x, const uint256& y) noexcept
{
return subc(x, y).carry;
}
[[gnu::noinline]] static bool lt_wordcmp(const uint256& x, const uint256& y) noexcept
{
for (size_t i = 3; i >= 1; --i)
{
if (x[i] < y[i])
return true;
if (x[i] > y[i])
return false;
}
return x[0] < y[0];
}
[[gnu::noinline]] static bool lt_halves(const uint256& x, const uint256& y) noexcept
{
const auto xhi = uint128{x[2], x[3]};
const auto xlo = uint128{x[0], x[1]};
const auto yhi = uint128{y[2], y[3]};
const auto ylo = uint128{y[0], y[1]};
return (xhi < yhi) | ((xhi == yhi) & (xlo < ylo));
}
#if INTX_HAS_EXTINT
[[gnu::noinline]] static bool lt_llvm(const uint256& x, const uint256& y) noexcept
{
unsigned _ExtInt(256) a; // NOLINT(cppcoreguidelines-init-variables)
unsigned _ExtInt(256) b; // NOLINT(cppcoreguidelines-init-variables)
std::memcpy(&a, &x, sizeof(a));
std::memcpy(&b, &y, sizeof(b));
return a < b;
}
#endif
template <bool CmpFn(const uint256&, const uint256&)>
static void compare(benchmark::State& state)
{
const auto set_id = [&state]() noexcept {
switch (state.range(0))
{
case 64:
return x_64;
case 128:
return x_128;
case 192:
return x_192;
case 256:
return x_256;
default:
state.SkipWithError("unexpected argument");
return x_64;
}
}();
const auto& xs = test::get_samples<uint256>(set_id);
uint256 z;
while (state.KeepRunningBatch(xs.size()))
{
for (size_t i = 0; i < xs.size(); ++i)
{
const auto x = xs[i];
const auto _ = CmpFn(z, x);
benchmark::DoNotOptimize(_);
z = x;
}
}
}
BENCHMARK_TEMPLATE(compare, lt_public)->DenseRange(64, 256, 64);
BENCHMARK_TEMPLATE(compare, lt_sub)->DenseRange(64, 256, 64);
BENCHMARK_TEMPLATE(compare, lt_wordcmp)->DenseRange(64, 256, 64);
BENCHMARK_TEMPLATE(compare, lt_halves)->DenseRange(64, 256, 64);
#if INTX_HAS_EXTINT
BENCHMARK_TEMPLATE(compare, lt_llvm)->DenseRange(64, 256, 64);
#endif
static void exponentiation(benchmark::State& state)
{
const auto exponent_set_id = [&state]() noexcept {
switch (state.range(0))
{
case 64:
return x_64;
case 128:
return x_128;
case 192:
return x_192;
case 256:
return x_256;
default:
state.SkipWithError("unexpected argument");
return x_64;
}
}();
const auto& bs = test::get_samples<uint256>(x_256);
const auto& es = test::get_samples<uint256>(exponent_set_id);
while (state.KeepRunningBatch(bs.size()))
{
for (size_t i = 0; i < bs.size(); ++i)
{
const auto _ = exp(bs[i], es[i]);
benchmark::DoNotOptimize(_);
}
}
}
BENCHMARK(exponentiation)->DenseRange(64, 256, 64);
static void exponentiation2(benchmark::State& state)
{
const auto base = uint256{2};
const auto exponent = static_cast<unsigned>(state.range(0));
for ([[maybe_unused]] auto _ : state)
{
const auto e = exp(base, exponent);
benchmark::DoNotOptimize(e);
}
}
BENCHMARK(exponentiation2)->Arg(0)->RangeMultiplier(2)->Range(64, 512);
static void count_sigificant_words_256(benchmark::State& state)
{
auto s = static_cast<unsigned>(state.range(0));
auto x = s != 0 ? uint256(0xff) << (s * 32 - 17) : uint256(0);
benchmark::DoNotOptimize(x);
benchmark::ClobberMemory();
for ([[maybe_unused]] auto _ : state)
{
benchmark::ClobberMemory();
auto w = count_significant_words(x);
benchmark::DoNotOptimize(w);
}
}
BENCHMARK(count_sigificant_words_256)->DenseRange(0, 8);
template <typename Int>
static void to_string(benchmark::State& state)
{
// Pick random operands. Keep the divisor small, because this is the worst
// case for most algorithms.
lcg<Int> rng(get_seed());
constexpr size_t size = 1000;
std::vector<Int> input(size);
for (auto& x : input)
x = rng();
while (state.KeepRunningBatch(size))
{
for (size_t i = 0; i < size; ++i)
{
auto s = intx::to_string(input[i]);
benchmark::DoNotOptimize(s.data());
}
}
}
BENCHMARK_TEMPLATE(to_string, uint128);
BENCHMARK_TEMPLATE(to_string, uint256);
BENCHMARK_TEMPLATE(to_string, uint512);
BENCHMARK_MAIN();