forked from mattsta/crcspeed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
169 lines (141 loc) · 5.62 KB
/
main.c
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
#include "crc16speed.h"
#include "crc64speed.h"
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
static long long ustime(void) {
struct timeval tv;
long long ust;
gettimeofday(&tv, NULL);
ust = ((long long)tv.tv_sec) * 1e6;
ust += tv.tv_usec;
return ust;
}
typedef uint64_t (*fns)(uint64_t, const void *, const uint64_t);
/* GCC 4.8 on Linux is dumb */
#ifndef ftello
extern off_t ftello(FILE *stream);
#endif
static inline uint64_t rdtsc() {
unsigned int lo = 0, hi = 0;
/* ask for something that can't be executed out-of-order
* to force the next rdtsc to not get re-ordered. */
__sync_synchronize();
__asm__ __volatile__("rdtsc" : "=a"(lo), "=d"(hi));
return ((uint64_t)hi << 32) | lo;
}
int main(int argc, char *argv[]) {
crc64speed_init();
crc16speed_init();
char li[] =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
"eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut "
"enim ad minim veniam, quis nostrud exercitation ullamco laboris "
"nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in "
"reprehenderit in voluptate velit esse cillum dolore eu fugiat "
"nulla pariatur. Excepteur sint occaecat cupidatat non proident, "
"sunt in culpa qui officia deserunt mollit anim id est laborum.";
if (argc == 1) {
/* REGULAR CHECK VALUES */
printf("[64calcu]: e9c6d914c4b8d9ca == %016" PRIx64 "\n",
(uint64_t)crc64(0, "123456789", 9));
printf("[64looku]: e9c6d914c4b8d9ca == %016" PRIx64 "\n",
(uint64_t)crc64_lookup(0, "123456789", 9));
printf("[64speed]: e9c6d914c4b8d9ca == %016" PRIx64 "\n",
(uint64_t)crc64speed(0, "123456789", 9));
printf("[16calcu]: 31c3 == %04" PRIx64 "\n",
(uint64_t)crc16(0, "123456789", 9));
printf("[16looku]: 31c3 == %04" PRIx64 "\n",
(uint64_t)crc16_lookup(0, "123456789", 9));
printf("[16speed]: 31c3 == %04" PRIx64 "\n",
(uint64_t)crc16speed(0, "123456789", 9));
/* LOREM IPSUM */
printf("[64calcu]: c7794709e69683b3 == %016" PRIx64 "\n",
(uint64_t)crc64(0, li, sizeof(li)));
printf("[64looku]: c7794709e69683b3 == %016" PRIx64 "\n",
(uint64_t)crc64_lookup(0, li, sizeof(li)));
printf("[64speed]: c7794709e69683b3 == %016" PRIx64 "\n",
(uint64_t)crc64speed(0, li, sizeof(li)));
printf("[16calcu]: 4b20 == %04" PRIx64 "\n",
(uint64_t)crc16(0, li, sizeof(li)));
printf("[16looku]: 4b20 == %04" PRIx64 "\n",
(uint64_t)crc16_lookup(0, li, sizeof(li)));
printf("[16speed]: 4b20 == %04" PRIx64 "\n",
(uint64_t)crc16speed(0, li, sizeof(li)));
return 0;
}
char *filename = argv[1];
FILE *fp = fopen(filename, "r");
if (fseek(fp, 0, SEEK_END) == -1) {
perror("Can't find file length");
return 1;
}
off_t sz = ftello(fp);
rewind(fp);
char *contents = malloc(sz); /* potentially very big */
if (fread(contents, sz, 1, fp) != 1) {
free(contents);
perror("Could not read entire file");
return 1;
}
fclose(fp);
fns compares[] = {crc64, crc64_lookup, crc64speed,
(fns)crc16, (fns)crc16_lookup, (fns)crc16speed};
size_t cc = sizeof(compares) / sizeof(*compares); /* compare count */
char *names[] = {"crc64 (no table)", "crc64 (lookup table)", "crc64speed",
"crc16 (no table)", "crc16 (lookup table)", "crc16speed"};
bool is16[] = {false, false, false, true, true, true};
double size_mb = sz / 1024.0 / 1024.0;
printf("Comparing CRCs against %0.2lf MB file...\n\n", size_mb);
bool error = false;
uint64_t accum_result = 0;
for (size_t i = 0; i < cc; i++) {
if (is16[i]) {
crc16speed_cache_table();
} else {
crc64speed_cache_table();
}
/* prime the code path with a dummy untimed call */
compares[i](0, li, sizeof(li));
long long start = ustime();
uint64_t start_c = rdtsc();
uint64_t result = compares[i](0, contents, sz);
uint64_t stop_c = rdtsc();
long long end = ustime();
/* Our test type returns 64 bits, but CRC16 only returns
* 16 bits, so let's ignore any upper-bit garbage. */
if (is16[i]) {
result &= 0xffff;
}
double total_time_seconds = (end - start) / 1e6;
double speed = size_mb / total_time_seconds; /* MB per second */
double cycles = (double)(stop_c - start_c) / sz;
if (argc > 2) { /* easier parsing for comparisons */
printf("%016" PRIx64 ":%lf\n", result, speed);
} else { /* boring human readable results */
printf("%s\n", names[i]);
printf("CRC = %016" PRIx64 "\n", result);
printf("%lf seconds at %0.2f MB/s (%0.2f CPU cycles per byte)\n",
total_time_seconds, speed, cycles);
}
/* We test outputs in pairs, so compare every 3 results for equality. */
if (i % 3 == 0) {
accum_result = result;
} else if (accum_result != result) {
printf("ERROR: CRC results don't match! (%016" PRIx64
" vs. %016" PRIx64 ")\n",
accum_result, result);
error = true;
}
printf("\n");
fflush(stdout);
}
free(contents);
return error;
}