forked from freebsd/freebsd-src
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
321 lines (286 loc) · 7.26 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
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
#include <sys/param.h>
#include <sys/mman.h>
#include <sys/signal.h>
#include <machine/cpufunc.h>
#include <machine/specialreg.h>
#include <err.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define SSE2_ALIGNED 0x0001
#define SSE2_UNALIGNED 0x0002
#define SSE42 0x0004
#define SSE42_2 0x0008
#define AVX2 0x0010
#define ERMS 0x0020
static struct name_table {
const char *name;
int value;
} variant_table[] = {
{ "sse2_aligned", SSE2_ALIGNED },
{ "sse2_unaligned", SSE2_UNALIGNED },
{ "sse2", SSE2_ALIGNED | SSE2_UNALIGNED },
{ "sse42", SSE42 | SSE42_2 },
{ "avx2", AVX2 },
{ "erms", ERMS },
};
static int variants;
extern int memcmp_sse2_aligned(void *dst, const void *src, size_t len);
extern int memcmp_sse2_unaligned(void *dst, const void *src, size_t len);
extern int memcmp_sse42(void *dst, const void *src, size_t len);
extern int memcmp_sse42_2(void *dst, const void *src, size_t len);
extern int memcmp_avx2(void *dst, const void *src, size_t len);
extern int memcmp_erms(void *dst, const void *src, size_t len);
extern int memcmp_stock(void *dst, const void *src, size_t len);
static void
set_variants(void)
{
u_int regs[4];
variants = SSE2_ALIGNED | SSE2_UNALIGNED | ERMS;
do_cpuid(1, regs);
if (regs[2] & CPUID2_SSE42)
variants |= SSE42 | SSE42_2;
if ((regs[2] & (CPUID2_XSAVE | CPUID2_OSXSAVE | CPUID2_AVX)) ==
(CPUID2_XSAVE | CPUID2_OSXSAVE | CPUID2_AVX)) {
do_cpuid(0, regs);
if (regs[0] >= 7) {
cpuid_count(7, 0, regs);
if (regs[1] & CPUID_STDEXT_AVX2)
variants |= AVX2;
}
}
}
#define TRIAL(fname, suffix, TEST) do { \
FILE *fp; \
int i; \
\
for (i = 0; i < trials; i++) { \
samples[i] = rdtsc(); \
TEST; \
} \
samples[trials] = rdtsc(); \
fp = fopen(fname suffix, "w"); \
for (i = 0; i < trials; i++) \
fprintf(fp, "%ld\n", samples[i + 1] - samples[i]); \
fclose(fp); \
} while (0)
#define TRIALS(suffix, args) do { \
TRIAL("builtin", suffix, memcmp_stock args); \
if (variants & SSE2_ALIGNED) \
TRIAL("sse2_aligned", suffix, \
memcmp_sse2_aligned args); \
if (variants & SSE2_UNALIGNED) \
TRIAL("sse2_unaligned", suffix, \
memcmp_sse2_unaligned args); \
if (variants & SSE42) \
TRIAL("sse42", suffix, memcmp_sse42 args); \
if (variants & SSE42_2) \
TRIAL("sse42_2", suffix, memcmp_sse42_2 args); \
if (variants & AVX2) \
TRIAL("avx2", suffix, memcmp_avx2 args); \
if (variants & ERMS) \
TRIAL("erms", suffix, memcmp_erms args); \
} while (0)
static void
benchmarks(void)
{
unsigned char *p1, *p2;
uint64_t *samples;
size_t size;
int trials;
size = getpagesize();
trials = 1000;
samples = calloc(trials + 1, sizeof(uint64_t));
p1 = calloc(size, 1);
p2 = calloc(size, 1);
TRIALS("_page", (p2, p1, size));
TRIALS("_short", (p2, p1, 15));
TRIALS("_short2", (p2, p1, 32));
TRIALS("_short3", (p2, p1, 48));
TRIALS("_offset", (p2 + 4, p1, 128));
TRIALS("_offset2", (p2 + 7, p1 + 7, 97));
p1[size - 6] = 1;
TRIALS("_dpage", (p2, p1, size));
TRIALS("_dshort", (p2 + size - 16, p1 + size - 16, 15));
TRIALS("_dshort2", (p2 + size - 32, p2 + size - 32, 32));
TRIALS("_dshort3", (p2 + size - 48, p2 + size - 48, 48));
TRIALS("_doffset", (p2 + size - 132, p2 + size - 128, 128));
TRIALS("_doffset2", (p2 + size - 131, p2 + size - 131, 131));
}
static sig_atomic_t info;
static void
handler(int sig)
{
info = 1;
}
/*
* Allocates a page with PROT_NONE pages before and after to catch any
* attempts to access memory outside of the page.
*/
static void *
alloc_guardedpage(void)
{
char *p;
p = mmap(NULL, 3 * getpagesize(), PROT_READ | PROT_WRITE, MAP_ANON, -1,
0);
if (p == MAP_FAILED)
err(1, "mmap(MAP_ANON)");
if ((uintptr_t)p % getpagesize() != 0)
errx(1, "guarded page is not page aligned");
if (mprotect(p, getpagesize(), PROT_NONE) == -1)
err(1, "mprotect(PROT_NONE)");
if (mprotect(p + getpagesize() * 2, getpagesize(), PROT_NONE) == -1)
err(1, "mprotect(PROT_NONE)");
return (p + getpagesize());
}
static void
run_test(unsigned char *p1, unsigned char *p2, size_t len, size_t same)
{
int i, todo, variant;
int control, test;
/* Always fill the raw data buffers with specific data. */
for (i = 0; i < len; i++)
p1[i] = i;
memcpy(p2, p1, same);
for (i = same; i < len; i++)
p2[i] = ~i;
/* Run plain memcmp first. */
control = memcmp(p1, p2, len);
if (same == len) {
if (control != 0) {
printf("memcmp: equal match %u vs 0\n", control);
abort();
}
} else {
if (control != p1[same] - p2[same]) {
printf("memcmp: control %u vs expected %u\n", control,
p1[same] - p2[same]);
abort();
}
}
todo = variants;
while (todo != 0) {
/* Test memcpy. */
if (todo & SSE2_ALIGNED) {
test = memcmp_sse2_aligned(p1, p2, len);
variant = SSE2_ALIGNED;
} else if (todo & SSE2_UNALIGNED) {
test = memcmp_sse2_unaligned(p1, p2, len);
variant = SSE2_UNALIGNED;
} else if (todo & SSE42) {
test = memcmp_sse42(p1, p2, len);
variant = SSE42;
} else if (todo & SSE42_2) {
test = memcmp_sse42_2(p1, p2, len);
variant = SSE42_2;
} else if (todo & AVX2) {
test = memcmp_avx2(p1, p2, len);
variant = AVX2;
} else if (todo & ERMS) {
test = memcmp_erms(p1, p2, len);
variant = ERMS;
}
/* Verify results are identical. */
if (test != control) {
printf("memcmp_%s: failed: same %zu len %zu: %u vs %u\n",
variant == SSE2_ALIGNED ? "sse2_aligned" :
variant == SSE2_UNALIGNED ? "sse2_unaligned" :
variant == SSE42 ? "sse42" :
variant == SSE42_2 ? "sse42_2" :
variant == AVX2 ? "avx2" :
variant == ERMS ? "erms" : "???",
same, len, test, control);
abort();
}
todo &= ~variant;
}
}
static void
tests(void)
{
unsigned char *p1, *p2;
int si, di, len, cap, same;
p1 = alloc_guardedpage();
p2 = alloc_guardedpage();
signal(SIGINFO, handler);
for (si = 0; si < 128; si++) {
for (di = 0; di < 128; di++) {
cap = getpagesize() - si;
if (getpagesize() - di < cap)
cap = getpagesize() - di;
for (len = 0; len < cap; len++) {
for (same = 0; same < len; same++) {
run_test(p1 + si, p2 + di, len, same);
if (info) {
printf(
"memcmp: si %d di %d len %d same %d\n",
si, di, len, same);
info = 0;
}
}
}
}
}
}
static void
usage(void)
{
fprintf(stderr, "Usage: memcmp [-t] [-v variant[,variant...]]\n");
exit(1);
}
static void
parse_variants(const char *arg)
{
char *str, *cp;
unsigned i;
str = strdup(arg);
cp = strtok(str, ",");
while (cp != NULL) {
for (i = 0; i < nitems(variant_table); i++) {
if (strcasecmp(cp, variant_table[i].name) == 0) {
variants |= variant_table[i].value;
goto next;
}
}
errx(1, "Invalid variant %s", cp);
next:
cp = strtok(NULL, ",");
}
free(str);
}
int
main(int ac, char **av)
{
enum { BENCHMARKS, TESTS } mode = BENCHMARKS;
int ch;
while ((ch = getopt(ac, av, "tv:")) != -1) {
switch (ch) {
case 't':
mode = TESTS;
break;
case 'v':
parse_variants(optarg);
break;
default:
usage();
}
}
ac -= optind;
av += optind;
if (ac != 0)
usage();
if (variants == 0)
set_variants();
switch (mode) {
case BENCHMARKS:
benchmarks();
break;
case TESTS:
tests();
break;
}
return (0);
}