-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
70 lines (55 loc) · 2.39 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
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include "reference.h"
#include "avx2.h"
const size_t TEST_DATA_SIZE = 1024 * 1000;
int main() {
uint16_t* data = reinterpret_cast<uint16_t*>(malloc(TEST_DATA_SIZE * sizeof(uint16_t)));
assert(data);
// Initialize with random data
srand(time(NULL));
for (size_t i = 0; i < TEST_DATA_SIZE; i++) {
data[i] = uint16_t(rand());
}
// Speed test for AVX2 implementation
clock_t begin = clock();
for (int i = 0; i < 10000; i++) {
// Sum counters have legal values between 0 and 65535
uint32_t sum1_right = rand() % 65535;
uint32_t sum2_right = rand() % 65535;
fletcher32_avx2(data, TEST_DATA_SIZE, sum1_right, sum2_right);
}
clock_t end = clock();
std::cout<<"Elapsed "<<end - begin<<" clock cycles"<<std::endl;
// Iterate with random offset and length
for (int i = 0; i < 10000; i++) {
size_t offset = 4 + rand() % (TEST_DATA_SIZE / 2);
// Keep offset aligned
offset -= reinterpret_cast<uintptr_t>(offset + data) % 4;
size_t len = rand() % (TEST_DATA_SIZE / 2);
// Sum counters have legal values between 0 and 65535
uint32_t sum1_left = rand() % 65535;
uint32_t sum2_left = rand() % 65535;
// Sum counters are passed by reference, so make a copy for comparison
uint32_t sum1_right = sum1_left;
uint32_t sum2_right = sum2_left;
// Compare reference solution with naive C implementation
if (fletcher32_ref(data + offset, len, sum1_left, sum2_left) != fletcher32_naive(data + offset, len, sum1_right, sum2_right)) {
std::cout<<"Reference vs naive, failed at iteration "<<i + 1<<std::endl;
std::cout<<"Left: "<<sum1_left<<" "<<sum2_left<<std::endl;
std::cout<<"Right: "<<sum1_right<<" "<<sum2_right<<std::endl;
break;
}
// Compare reference solution with AVX2 accelerated solution
if (fletcher32_ref(data + offset, len, sum1_left, sum2_left) != fletcher32_avx2(data + offset, len, sum1_right, sum2_right)) {
std::cout<<"Reference vs AVX2, failed at iteration "<<i + 1<<std::endl;
std::cout<<"Left: "<<sum1_left<<" "<<sum2_left<<std::endl;
std::cout<<"Right: "<<sum1_right<<" "<<sum2_right<<std::endl;
break;
}
}
free(data);
return 0;
}