-
Notifications
You must be signed in to change notification settings - Fork 6
/
testmem.cpp
396 lines (357 loc) · 14.8 KB
/
testmem.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
// TESTMEM.CPP Agner Fog 2011-07-04
// Test file for asmlib memcpy and memmove functions
// Instructions: Compile on any platform and link with the appropriate
// version of the asmlib library.
#include <stdio.h>
//#include <process.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include "asmlib.h"
// define function type
typedef void * memcpyF (void * dest, const void * src, size_t count);
typedef void * memsetF (void * dest, int c, size_t count);
extern "C" {
extern int IInstrSet;
// function prototypes for CPU specific function versions
memcpyF memcpy386, memcpySSE2, memcpySSSE3, memcpyU, memcpyU256;
memcpyF memmove386, memmoveSSE2, memmoveSSSE3, memmoveU, memmoveU256;
memsetF memset386, memsetSSE2, memsetAVX;
}
// Tables of function pointers
#if defined(_WIN64) || defined(_M_X64) || defined(__amd64)
const int NUMFUNC = 5;
memcpyF * memcpyTab[NUMFUNC] = {A_memcpy, memcpySSE2, memcpySSSE3, memcpyU, memcpyU256};
memcpyF * memmoveTab[NUMFUNC] = {A_memmove, memmoveSSE2, memmoveSSSE3, memmoveU, memmoveU256};
const char * DispatchNames[NUMFUNC] = {"Dispatched", "SSE2", "SSSE3", "Unalign", "U256"};
int isetreq [NUMFUNC] = {0, 4, 6, 4, 11}; // instruction set required
const int MEMSETFUNCS = 3;
memsetF * memsetTab[MEMSETFUNCS] = {A_memset, memsetSSE2, memsetAVX};
const char * memsetNames[MEMSETFUNCS] = {"Dispatched", "SSE2", "AVX"};
int memsetreq [NUMFUNC] = {0, 4, 11}; // instruction set required
#else
const int NUMFUNC = 6;
memcpyF * memcpyTab[NUMFUNC] = {A_memcpy, memcpy386, memcpySSE2, memcpySSSE3, memcpyU, memcpyU256};
memcpyF * memmoveTab[NUMFUNC] = {A_memmove, memmove386, memmoveSSE2, memmoveSSSE3, memmoveU, memmoveU256};
const char * DispatchNames[NUMFUNC] = {"Dispatched", "386", "SSE2", "SSSE3", "Unalign", "U256"};
int isetreq [NUMFUNC] = {0, 0, 4, 6, 4, 11}; // instruction set required
const int MEMSETFUNCS = 4;
memsetF * memsetTab[MEMSETFUNCS] = {A_memset, memset386, memsetSSE2, memsetAVX};
const char * memsetNames[MEMSETFUNCS] = {"Dispatched", "386", "SSE2", "AVX"};
int memsetreq [NUMFUNC] = {0, 0, 4, 11}; // instruction set required
#endif
void error(const char * s, int a, int b, int c) {
printf("\nError %s: %i %i %i\n", s, a, b, c);
exit (1);
}
void error(const char * s, int i, int a, int b, int c) {
printf("\nError %s: %i %i %i %i\n", s, i, a, b, c);
exit (1);
}
int main () {
int ao, bo, os, len;
int version;
const int pagesize = 0x1000; // 4 kbytes
const int n = 16*pagesize;
char a[n], b[n], c[n];
int instrset = InstructionSet();
// CacheBypassLimit = 5;
printf("\nmemcpy cache limit = 0x%X, memset cache limit 0x%X\n",
(int)GetMemcpyCacheLimit(), (int)GetMemsetCacheLimit());
printf("\nTest memcpy");
int i, x = 91;
for (i=0; i<n; i++) {
x += 23;
a[i] = (char)x;
}
A_memset(b, -1, n);
SetMemcpyCacheLimit(0); // default
#if 1
// Test memcpy for correctness
// Loop through versions
for (version = 0; version < NUMFUNC; version++) {
printf("\n%s", DispatchNames[version]);
if (instrset < isetreq[version]) {
// instruction set not supported
printf(" skipped"); continue;
}
for (len=0; len<514; len++) {
for (ao = 0; ao <=20; ao++) {
for (bo = 0; bo <=32; bo++) {
A_memset(b, -1, len+96);
(*memcpyTab[version])(b+bo, a+ao, len);
if (bo && b[bo-1] != -1) error("A", ao, bo, len);
if (b[bo+len] != -1) error("B", ao, bo, len);
if (len==0) continue;
if (b[bo] != a[ao]) error("C", ao, bo, len);
if (b[bo+len-1] != a[ao+len-1]) error("D", ao, bo, len);
if (memcmp(b+bo, a+ao, len)) error("E", ao, bo, len);
}
}
}
// check false memory dependence branches
len = 300;
A_memcpy(b, a, 3*pagesize);
for (ao = pagesize-300; ao < pagesize+200; ao++) {
for (bo = 3*pagesize; bo <=3*pagesize+33; bo++) {
A_memset(b+bo-64, -1, len+128);
(*memcpyTab[version])(b+bo, b+ao, len);
if (b[bo-1] != -1) error("A1", ao, bo, len);
if (b[bo+len] != -1) error("B1", ao, bo, len);
if (memcmp(b+bo, b+ao, len)) error("E1", ao, bo, len);
}
}
// check false memory dependence branches with overlap
// src > dest and overlap: must copy forwards
len = pagesize+1000;
for (ao = 2*pagesize; ao <=2*pagesize+33; ao++) {
for (bo = pagesize-200; bo < pagesize+300; bo++) {
A_memcpy(b, a, 4*pagesize);
A_memcpy(c, a, 4*pagesize);
(*memcpyTab[version])(b+bo, b+ao, len);
//memcpy(c+bo, c+ao, len); // Most library versions of memcpy are actually memmove
memcpySSE2(c+bo, c+ao, len);
if (memcmp(b, c, 4*pagesize)) {
error("E2", ao-pagesize, bo-2*pagesize, len);
}
}
}
// check false memory dependence branches with overlap
// dest > src and overlap: undefined behavior
#if 1
len = pagesize+1000;
for (ao = pagesize-200; ao < pagesize+200; ao++) {
for (bo = 2*pagesize; bo <=2*pagesize+33; bo++) {
A_memcpy(b, a, 4*pagesize);
A_memcpy(c, a, 4*pagesize);
(*memcpyTab[version])(b+bo, b+ao, len);
//memcpy(c+bo, c+ao, len); // MS Most library versions of memcpy are actually memmove
memcpySSE2(c+bo, c+ao, len);
if (memcmp(b, c, 4*pagesize)) {
error("E3", ao-pagesize, bo-2*pagesize, len);
}
}
}
#endif
}
printf("\n\nTest memmove");
// Test memmove for correctness
for (i=0; i<n; i++) {
x += 23;
a[i] = char(x);
}
// Loop through versions
for (version = 0; version < NUMFUNC; version++) {
printf("\n%s", DispatchNames[version]);
if (instrset < isetreq[version]) {
// instruction set not supported
printf(" skipped"); continue;
}
// move forward
for (len=0; len<400; len++) {
for (bo = 0; bo <=33; bo++) {
for (os = 0; os <= 33; os++) {
A_memcpy(b, a, len+100);
(*memmoveTab[version])(b+bo+os, b+bo, len);
for (i=0; i<bo+os; i++) if (b[i]!=a[i]) error("E", i, bo, os, len);
for (i=bo+os; i<bo+os+len; i++) if (b[i] != a[i-os]) error("F", i, bo, os, len);
for (;i < bo+os+len+20; i++) if (b[i]!=a[i]) error("G", i, bo, os, len);
}
}
}
// move backwards
for (len=0; len<400; len++) {
for (bo = 0; bo <=33; bo++) {
for (os = 0; os < 33; os++) {
A_memcpy(b, a, len+96);
(*memmoveTab[version])(b+bo, b+bo+os, len);
for (i=0; i<bo; i++) if (b[i]!=a[i]) error("H", i, bo, os, len);
for (i=bo; i<bo+len; i++) if (b[i] != a[i+os]) error("I", i, bo, os, len);
for (;i < bo+len+20; i++) if (b[i]!=a[i]) error("J", i, bo, os, len);
}
}
}
}
printf("\n\nSame, with non-temporal moves");
SetMemcpyCacheLimit(1); // bypass cache
// Loop through versions
for (version = 0; version < NUMFUNC; version++) {
printf("\n%s", DispatchNames[version]);
if (instrset < isetreq[version]) {
// instruction set not supported
printf(" skipped"); continue;
}
for (len=0; len<514; len++) {
for (ao = 0; ao <=20; ao++) {
for (bo = 0; bo <=32; bo++) {
A_memset(b, -1, len+96);
(*memcpyTab[version])(b+bo, a+ao, len);
if (bo && b[bo-1] != -1) error("A", ao, bo, len);
if (b[bo+len] != -1) error("B", ao, bo, len);
if (len==0) continue;
if (b[bo] != a[ao]) error("C", ao, bo, len);
if (b[bo+len-1] != a[ao+len-1]) error("D", ao, bo, len);
if (memcmp(b+bo, a+ao, len)) error("E", ao, bo, len);
}
}
}
// check false memory dependence branches
len = 300;
A_memcpy(b, a, 3*pagesize);
for (ao = pagesize-200; ao < pagesize+200; ao++) {
for (bo = 3*pagesize; bo <=3*pagesize+33; bo++) {
A_memset(b+bo-64, -1, len+128);
(*memcpyTab[version])(b+bo, b+ao, len);
if (b[bo-1] != -1) error("A1", ao, bo, len);
if (b[bo+len] != -1) error("B1", ao, bo, len);
if (memcmp(b+bo, b+ao, len)) error("E1", ao, bo, len);
}
}
// check false memory dependence branches with overlap
// src > dest and overlap: must copy forwards
len = pagesize+1000;
for (ao = 2*pagesize; ao <=2*pagesize+33; ao++) {
for (bo = pagesize-200; bo < pagesize+200; bo++) {
A_memcpy(b, a, 4*pagesize);
A_memcpy(c, a, 4*pagesize);
(*memcpyTab[version])(b+bo, b+ao, len);
//memcpy(c+bo, c+ao, len); // Most library versions of memcpy are actually memmove
memcpySSE2(c+bo, c+ao, len);
if (memcmp(b, c, 4*pagesize)) {
error("E2", ao-pagesize, bo-2*pagesize, len);
}
}
}
// (check false memory dependence branches with overlap. skipped)
}
printf("\n\nTest memmove");
// Test memmove for correctness
for (i=0; i<n; i++) {
x += 23;
a[i] = char(x);
}
// Loop through versions
for (version = 0; version < NUMFUNC; version++) {
printf("\n%s", DispatchNames[version]);
if (instrset < isetreq[version]) {
// instruction set not supported
printf(" skipped"); continue;
}
// move forward
for (len=0; len<400; len++) {
for (bo = 0; bo <=33; bo++) {
for (os = 0; os <= 33; os++) {
A_memcpy(b, a, len+100);
(*memmoveTab[version])(b+bo+os, b+bo, len);
for (i=0; i<bo+os; i++) if (b[i]!=a[i]) error("E", i, bo, os, len);
for (i=bo+os; i<bo+os+len; i++) if (b[i] != a[i-os]) error("F", i, bo, os, len);
for (;i < bo+os+len+20; i++) if (b[i]!=a[i]) error("G", i, bo, os, len);
}
}
}
// move backwards
for (len=0; len<400; len++) {
for (bo = 0; bo <=33; bo++) {
for (os = 0; os < 33; os++) {
A_memcpy(b, a, len+96);
(*memmoveTab[version])(b+bo, b+bo+os, len);
for (i=0; i<bo; i++) if (b[i]!=a[i]) error("H", i, bo, os, len);
for (i=bo; i<bo+len; i++) if (b[i] != a[i+os]) error("I", i, bo, os, len);
for (;i < bo+len+20; i++) if (b[i]!=a[i]) error("J", i, bo, os, len);
}
}
}
}
#endif
SetMemcpyCacheLimit(0); // back to default
SetMemsetCacheLimit(0);
printf("\n\nTest memset");
// test memset
const int val1 = 0x4C, val2 = 0xA2, len2 = 1024;
for (version = 0; version < MEMSETFUNCS; version++) {
memsetF * func = memsetTab[version];
printf("\n%s", memsetNames[version]);
if (instrset < memsetreq[version]) {
// instruction set not supported
printf(" skipped"); continue;
}
for (os = 0; os < 34; os++) {
for (len = 0; len < 500; len++) {
memset(a, val1, len2);
memset(a+os, val2, len);
(*func)(b, val1, len2);
(*func)(b+os, val2, len);
if (memcmp(a, b, len2)) {
error("MS", version, os, len);
}
}
}
for (len=0; len<200; len++) {
for (os = 0; os <= 33; os++) {
A_memcpy(b, a, len+64);
A_memset(b+os, 55, len);
for (i=0; i<os; i++) if (b[i] != a[i]) error("K", i, os, len);
for (; i<os+len; i++) if (b[i] != 55) error("L", i, os, len);
for (; i<os+len+17; i++) if (b[i] != a[i]) error("M", i, os, len);
}
}
}
printf("\n\nSame, with non-temporal moves");
SetMemsetCacheLimit(1); // bypass cache
for (version = 0; version < MEMSETFUNCS; version++) {
memsetF * func = memsetTab[version];
printf("\n%s", memsetNames[version]);
if (instrset < memsetreq[version]) {
// instruction set not supported
printf(" skipped"); continue;
}
for (os = 0; os < 34; os++) {
for (len = 0; len < 500; len++) {
memset(a, val1, len2);
memset(a+os, val2, len);
(*func)(b, val1, len2);
(*func)(b+os, val2, len);
if (memcmp(a, b, len2)) {
error("MS", version, os, len);
}
}
}
}
SetMemsetCacheLimit(0); // back to default
printf("\n\nTest strlen");
// test strlen
for (len=0; len<400; len++) {
for (os = 0; os <= 32; os++) {
A_memset(b, 0, len+64);
A_memset(b+os, 'a', len);
x = A_strlen(b+os);
if (x != len) error("N", 0, os, len);
A_memset(b, 1, len+64);
b[os+len] = 0;
x = A_strlen(b+os);
if (x != len) error("O", 0, os, len);
}
}
printf("\n\nTest strcpy and strcat");
// test strcpy and strcat
for (i=0; i<n; i++) {
x += 23;
a[i] = char(x) | 1;
}
for (len=0; len<400; len++) {
for (os = 0; os <= 16; os++) {
for (i=0; i<33; i++) {
A_memmove(b, a, len+64);
b[os+len] = 0;
A_strcpy(c+5, b+os);
if (A_strlen(c+5) != len) error("P", 0, os, len);
A_memmove(b+55, a, i+4);
b[55+i] = 0;
A_strcat(c+5, b+55);
if (A_strlen(c+5) != len+i) error("R", 0, os, len);
}
}
}
printf("\n\nSuccess\n");
return 0;
}