-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.cpp
479 lines (367 loc) · 9.96 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
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
#include "source/original/md5-original.h"
#include "source/simd/md5-simd.h"
#include <iostream>
#include <chrono>
#include <iomanip>
using namespace std;
using md5_simd::MD5_SIMD;
constexpr int TEST_STRING_LENGTH = 43;
constexpr int RUN_INIT_STRING_LENGTH = 6;
constexpr int RUN_ZERO_COUNT = 4;
constexpr int RUN_RESULT = 31556;
constexpr char char_map[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
inline int digit_count(int number)
{
if (number < 10) return 1;
if (number < 100) return 2;
if (number < 1000) return 3;
if (number < 10000) return 4;
if (number < 100000) return 5;
if (number < 1000000) return 6;
if (number < 10000000) return 7;
if (number < 100000000) return 8;
if (number < 1000000000) return 9;
return 10;
}
bool test_original()
{
const char* text = "The quick brown fox jumps over the lazy dog";
char buf[33];
buf[32] = '\0';
MD5 md5;
md5.calculate(text, TEST_STRING_LENGTH);
md5.hexdigest(buf);
if (strcmp(buf, "9e107d9d372bb6826bd81d3542a419d6") == 0)
{
return true;
}
else
{
return false;
}
}
template<bool length_check = true>
bool test_simd()
{
const char* text = "The quick brown fox jumps over the lazy dog";
MD5_SIMD md5;
const char* arr[4] = { text, text, text, text };
uint64_t lengths[4] = { TEST_STRING_LENGTH, TEST_STRING_LENGTH, TEST_STRING_LENGTH, TEST_STRING_LENGTH };
md5.calculate<4, length_check>(arr, lengths);
[[maybe_unused]] char buf[33];
buf[32] = '\0';
md5.hexdigest(buf, 0);
if (strcmp(buf, "9e107d9d372bb6826bd81d3542a419d6") == 0)
{
return true;
}
else
{
return false;
}
}
int run_original()
{
char input_buffer[100];
char output_buffer[32];
memcpy(input_buffer, "abcdef", RUN_INIT_STRING_LENGTH);
int i = 1;
MD5 md5;
while (true)
{
int digits = digit_count(i);
int index = digits + RUN_INIT_STRING_LENGTH - 1;
int num = i;
while (num > 0)
{
input_buffer[index] = char_map[num % 10];
num /= 10;
index--;
}
int count = 0;
md5.calculate(input_buffer, (MD5::size_type) (RUN_INIT_STRING_LENGTH + digits));
md5.hexdigest(output_buffer);
//cout << output_buffer << endl;
for (int idx = 0; idx < RUN_ZERO_COUNT; idx++)
{
if (output_buffer[idx] == '0')
{
count++;
if (count >= RUN_ZERO_COUNT)
{
return i;
}
}
}
i++;
}
}
template<bool length_check = true>
int run_simd_1x()
{
char* buffers[1];
buffers[0] = new char[128];
uint64_t lengths[1];
const char* buffer = "abcdef";
int n = 1;
MD5_SIMD md5;
while (true)
{
int num = n;
int digits = digit_count(num);
lengths[0] = (uint64_t) RUN_INIT_STRING_LENGTH + digits;
buffers[0][RUN_INIT_STRING_LENGTH + digits] = '\0';
memcpy(buffers[0], buffer, RUN_INIT_STRING_LENGTH);
int index = RUN_INIT_STRING_LENGTH + digits - 1;
while (num > 0)
{
buffers[0][index] = char_map[num % 10];
num /= 10;
index--;
}
md5.calculate<1, length_check>(buffers, lengths);
if (md5.check_zeroes<RUN_ZERO_COUNT>(0))
{
delete[] buffers[0];
return n;
}
n++;
}
delete[] buffers[0];
}
template<bool length_check = true>
int run_simd_2x()
{
char* buffers[2];
buffers[0] = new char[128];
buffers[1] = new char[128];
uint64_t lengths[2];
const char* buffer = "abcdef";
int n = 1;
MD5_SIMD md5;
while (true)
{
for (int i = 0; i < 2; i++)
{
int num = n + i;
int digits = digit_count(num);
lengths[i] = (uint64_t) RUN_INIT_STRING_LENGTH + digits;
buffers[i][RUN_INIT_STRING_LENGTH + digits] = '\0';
memcpy(buffers[i], buffer, RUN_INIT_STRING_LENGTH);
int index = RUN_INIT_STRING_LENGTH + digits - 1;
while (num > 0)
{
buffers[i][index] = char_map[num % 10];
num /= 10;
index--;
}
}
md5.calculate<2, length_check>(buffers, lengths);
for (int buffer_index = 0; buffer_index < 2; buffer_index++)
{
if (md5.check_zeroes<RUN_ZERO_COUNT>(buffer_index))
{
delete[] buffers[0];
delete[] buffers[1];
return n + buffer_index;
}
}
n += 2;
}
delete[] buffers[0];
delete[] buffers[1];
}
template<bool length_check = true>
int run_simd_4x()
{
char* buffers[4];
buffers[0] = new char[128];
buffers[1] = new char[128];
buffers[2] = new char[128];
buffers[3] = new char[128];
uint64_t lengths[4];
const char* buffer = "abcdef";
int n = 1;
MD5_SIMD md5;
while (true)
{
for (int i = 0; i < 4; i++)
{
int num = n + i;
int digits = digit_count(num);
lengths[i] = (uint64_t) RUN_INIT_STRING_LENGTH + digits;
buffers[i][RUN_INIT_STRING_LENGTH + digits] = '\0';
memcpy(buffers[i], buffer, RUN_INIT_STRING_LENGTH);
int index = RUN_INIT_STRING_LENGTH + digits - 1;
while (num > 0)
{
buffers[i][index] = char_map[num % 10];
num /= 10;
index--;
}
}
md5.calculate<4, length_check>(buffers, lengths);
for (int buffer_index = 0; buffer_index < 4; buffer_index++)
{
if (md5.check_zeroes<RUN_ZERO_COUNT>(buffer_index))
{
delete[] buffers[0];
delete[] buffers[1];
delete[] buffers[2];
delete[] buffers[3];
return n + buffer_index;
}
}
n += 4;
}
delete[] buffers[0];
delete[] buffers[1];
delete[] buffers[2];
delete[] buffers[3];
}
#ifdef USE_256_BITS
template<bool length_check = true>
int run_simd_8x()
{
char* buffers[8];
buffers[0] = new char[128];
buffers[1] = new char[128];
buffers[2] = new char[128];
buffers[3] = new char[128];
buffers[4] = new char[128];
buffers[5] = new char[128];
buffers[6] = new char[128];
buffers[7] = new char[128];
uint64_t lengths[8];
const char* buffer = "abcdef";
int n = 1;
MD5_SIMD md5;
while (true)
{
for (int i = 0; i < 8; i++)
{
int num = n + i;
int digits = digit_count(num);
lengths[i] = (uint64_t) RUN_INIT_STRING_LENGTH + digits;
buffers[i][RUN_INIT_STRING_LENGTH + digits] = '\0';
memcpy(buffers[i], buffer, RUN_INIT_STRING_LENGTH);
int index = RUN_INIT_STRING_LENGTH + digits - 1;
while (num > 0)
{
buffers[i][index] = char_map[num % 10];
num /= 10;
index--;
}
}
md5.calculate<8, length_check>(buffers, lengths);
for (int buffer_index = 0; buffer_index < 8; buffer_index++)
{
if (md5.check_zeroes<RUN_ZERO_COUNT>(buffer_index))
{
delete[] buffers[0];
delete[] buffers[1];
delete[] buffers[2];
delete[] buffers[3];
delete[] buffers[4];
delete[] buffers[5];
delete[] buffers[6];
delete[] buffers[7];
return n + buffer_index;
}
}
n += 8;
}
delete[] buffers[0];
delete[] buffers[1];
delete[] buffers[2];
delete[] buffers[3];
delete[] buffers[4];
delete[] buffers[5];
delete[] buffers[6];
delete[] buffers[7];
}
#endif
double time(int reps, int(*fn)())
{
auto st = chrono::steady_clock::now();
for (int i = 0; i < reps; i++)
{
[[maybe_unused]] auto r = fn();
}
auto et = chrono::steady_clock::now();
auto elapsed = et - st;
double tp = chrono::duration_cast<chrono::nanoseconds>(elapsed).count() / (double) reps;
double t = 1e-3 * tp;
return t;
}
template<bool length_check = true>
void do_tests()
{
if (length_check == true)
{
cout << "Running With Length Checks" << endl;
}
else
{
cout << "Running Without Length Checks" << endl;
}
// test for correct hashes
bool test_original_result = test_original();
bool test_simd_result = test_simd<length_check>();
cout << "test: original => " << (test_original_result ? "succeeded" : "failed") << endl;
cout << "test: simd => " << (test_simd_result ? "succeeded" : "failed") << endl;
if (!test_original_result || !test_simd_result)
{
return;
}
cout << endl;
// test for correct running
bool run_original_result = run_original() == RUN_RESULT;
bool run_simd_1x_result = run_simd_1x<length_check>() == RUN_RESULT;
bool run_simd_2x_result = run_simd_2x<length_check>() == RUN_RESULT;
bool run_simd_4x_result = run_simd_4x<length_check>() == RUN_RESULT;
#ifdef USE_256_BITS
bool run_simd_8x_result = run_simd_8x<length_check>() == RUN_RESULT;
#endif
cout << "run: original => " << (run_original_result ? "succeeded" : "failed") << endl;
cout << "run: simd 1x => " << (run_simd_1x_result ? "succeeded" : "failed") << endl;
cout << "run: simd 2x => " << (run_simd_2x_result ? "succeeded" : "failed") << endl;
cout << "run: simd 4x => " << (run_simd_4x_result ? "succeeded" : "failed") << endl;
#ifdef USE_256_BITS
cout << "run: simd 8x => " << (run_simd_8x_result ? "succeeded" : "failed") << endl;
#endif
#ifdef USE_256_BITS
if (!run_original_result || !run_simd_1x_result || !run_simd_2x_result || !run_simd_4x_result || !run_simd_8x_result)
#else
if (!run_original_result || !run_simd_1x_result || !run_simd_2x_result || !run_simd_4x_result)
#endif
{
return;
}
cout << endl;
// time the functions
int reps = 500;
double time_original = time(reps, run_original);
double time_simd_1x = time(reps, run_simd_1x<length_check>);
double time_simd_2x = time(reps, run_simd_2x<length_check>);
double time_simd_4x = time(reps, run_simd_4x<length_check>);
#ifdef USE_256_BITS
double time_simd_8x = time(reps, run_simd_8x<length_check>);
#endif
cout << "With Length Checks" << endl;
cout << "time: original => " << fixed << setprecision(3) << time_original << "us" << endl;
cout << "time: simd 1x => " << fixed << setprecision(3) << time_simd_1x << "us" << endl;
cout << "time: simd 2x => " << fixed << setprecision(3) << time_simd_2x << "us" << endl;
cout << "time: simd 4x => " << fixed << setprecision(3) << time_simd_4x << "us" << endl;
#ifdef USE_256_BITS
cout << "time: simd 8x => " << fixed << setprecision(3) << time_simd_8x << "us" << endl;
#endif
cout << endl;
}
int main()
{
do_tests<true>();
cout << endl;
//do_tests<false>();
return 0;
}