forked from ssvb/tinymembench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asm-opt.c
428 lines (380 loc) · 14.9 KB
/
asm-opt.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
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
/*
* Copyright © 2011 Siarhei Siamashka <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "asm-opt.h"
#if defined(__linux__) || defined(ANDROID) || defined(__ANDROID__)
#define SOMEWHAT_SANE_PROC_CPUINFO_SIZE_LIMIT (1024 * 1024)
#if defined(__i386__) || defined(__amd64__)
#define FEATURES_ID "flags"
#elif defined(__arm__)
#define FEATURES_ID "Features"
#elif defined(__mips__)
#define FEATURES_ID "cpu model"
#else
#define FEATURES_ID "?"
#endif
static int check_feature (char *buffer, const char *feature)
{
char *p;
if (*feature == 0)
return 0;
if (strncmp(buffer, FEATURES_ID, strlen(FEATURES_ID)) != 0)
return 0;
buffer += strlen(FEATURES_ID);
while (isspace(*buffer))
buffer++;
/* Check if 'feature' is present in the buffer as a separate word */
while ((p = strstr(buffer, feature))) {
if (p > buffer && !isspace(*(p - 1))) {
buffer++;
continue;
}
p += strlen(feature);
if (*p != 0 && !isspace(*p)) {
buffer++;
continue;
}
return 1;
}
return 0;
}
static int parse_proc_cpuinfo(int bufsize, const char *feature)
{
char *buffer = (char *)malloc(bufsize);
FILE *fd;
int feature_support = 0;
if (!buffer)
return 0;
fd = fopen("/proc/cpuinfo", "r");
if (fd) {
while (fgets(buffer, bufsize, fd)) {
if (!strchr(buffer, '\n') && !feof(fd)) {
/* "impossible" happened - insufficient size of the buffer! */
fclose(fd);
free(buffer);
return -1;
}
if (check_feature(buffer, feature))
feature_support = 1;
}
fclose(fd);
}
free(buffer);
return feature_support;
}
#define SOMEWHAT_SANE_PROC_CPUINFO_SIZE_LIMIT (1024 * 1024)
int check_cpu_feature(const char *feature)
{
int bufsize = 1024;
int result;
while ((result = parse_proc_cpuinfo(bufsize, feature)) == -1)
{
bufsize *= 2;
if (bufsize > SOMEWHAT_SANE_PROC_CPUINFO_SIZE_LIMIT)
return 0;
}
return result;
}
#else
int check_cpu_feature(const char *feature)
{
return 0;
}
#endif
static bench_info empty[] = { { NULL, 0, NULL } };
#if defined(__i386__) || defined(__amd64__)
#include "x86-sse2.h"
static bench_info x86_sse2[] =
{
{ "MOVSB copy", 0, aligned_block_copy_movsb },
{ "MOVSD copy", 0, aligned_block_copy_movsd },
{ "STOSB fill", 0, aligned_block_fill_stosb },
{ "SSE2 copy", 0, aligned_block_copy_sse2 },
{ "SSE2 nontemporal copy", 0, aligned_block_copy_nt_sse2 },
{ "SSE2 copy prefetched (32 bytes step)", 0, aligned_block_copy_pf32_sse2 },
{ "SSE2 copy prefetched (64 bytes step)", 0, aligned_block_copy_pf64_sse2 },
{ "SSE2 nontemporal copy prefetched (32 bytes step)", 0, aligned_block_copy_nt_pf32_sse2 },
{ "SSE2 nontemporal copy prefetched (64 bytes step)", 0, aligned_block_copy_nt_pf64_sse2 },
{ "SSE2 2-pass copy", 1, aligned_block_copy_sse2 },
{ "SSE2 2-pass copy prefetched (32 bytes step)", 1, aligned_block_copy_pf32_sse2 },
{ "SSE2 2-pass copy prefetched (64 bytes step)", 1, aligned_block_copy_pf64_sse2 },
{ "SSE2 2-pass nontemporal copy", 1, aligned_block_copy_nt_sse2 },
{ "SSE2 fill", 0, aligned_block_fill_sse2 },
{ "SSE2 nontemporal fill", 0, aligned_block_fill_nt_sse2 },
{ NULL, 0, NULL }
};
static bench_info x86_sse2_fb[] =
{
{ "MOVSD copy (from framebuffer)", 0, aligned_block_copy_movsd },
{ "MOVSD 2-pass copy (from framebuffer)", 1, aligned_block_copy_movsd },
{ "SSE2 copy (from framebuffer)", 0, aligned_block_copy_sse2 },
{ "SSE2 2-pass copy (from framebuffer)", 1, aligned_block_copy_sse2 },
{ NULL, 0, NULL }
};
static int check_sse2_support(void)
{
#ifdef __amd64__
return 1; /* We assume that all 64-bit processors have SSE2 support */
#else
int cpuid_feature_information;
__asm__ volatile (
/* According to Intel manual, CPUID instruction is supported
* if the value of ID bit (bit 21) in EFLAGS can be modified */
"pushf\n"
"movl (%%esp), %0\n"
"xorl $0x200000, (%%esp)\n" /* try to modify ID bit */
"popf\n"
"pushf\n"
"xorl (%%esp), %0\n" /* check if ID bit changed */
"jz 1f\n"
"push %%eax\n"
"push %%ebx\n"
"push %%ecx\n"
"mov $1, %%eax\n"
"cpuid\n"
"pop %%ecx\n"
"pop %%ebx\n"
"pop %%eax\n"
"1:\n"
"popf\n"
: "=d" (cpuid_feature_information)
:
: "cc");
return cpuid_feature_information & (1 << 26);
#endif
}
bench_info *get_asm_benchmarks(void)
{
if (check_sse2_support())
return x86_sse2;
else
return empty;
}
bench_info *get_asm_framebuffer_benchmarks(void)
{
if (check_sse2_support())
return x86_sse2_fb;
else
return empty;
}
#elif defined(__arm__) && defined(__ARM_ARCH_ISA_ARM)
#include "arm-neon.h"
static bench_info arm_neon[] =
{
{ "NEON read", 0, aligned_block_read_neon },
{ "NEON read prefetched (32 bytes step)", 0, aligned_block_read_pf32_neon },
{ "NEON read prefetched (64 bytes step)", 0, aligned_block_read_pf64_neon },
{ "NEON read 2 data streams", 0, aligned_block_read2_neon },
{ "NEON read 2 data streams prefetched (32 bytes step)", 0, aligned_block_read2_pf32_neon },
{ "NEON read 2 data streams prefetched (64 bytes step)", 0, aligned_block_read2_pf64_neon },
{ "NEON copy", 0, aligned_block_copy_neon },
{ "NEON copy prefetched (32 bytes step)", 0, aligned_block_copy_pf32_neon },
{ "NEON copy prefetched (64 bytes step)", 0, aligned_block_copy_pf64_neon },
{ "NEON unrolled copy", 0, aligned_block_copy_unrolled_neon },
{ "NEON unrolled copy prefetched (32 bytes step)", 0, aligned_block_copy_unrolled_pf32_neon },
{ "NEON unrolled copy prefetched (64 bytes step)", 0, aligned_block_copy_unrolled_pf64_neon },
{ "NEON copy backwards", 0, aligned_block_copy_backwards_neon },
{ "NEON copy backwards prefetched (32 bytes step)", 0, aligned_block_copy_backwards_pf32_neon },
{ "NEON copy backwards prefetched (64 bytes step)", 0, aligned_block_copy_backwards_pf64_neon },
{ "NEON 2-pass copy", 1, aligned_block_copy_neon },
{ "NEON 2-pass copy prefetched (32 bytes step)", 1, aligned_block_copy_pf32_neon },
{ "NEON 2-pass copy prefetched (64 bytes step)", 1, aligned_block_copy_pf64_neon },
{ "NEON unrolled 2-pass copy", 1, aligned_block_copy_unrolled_neon },
{ "NEON unrolled 2-pass copy prefetched (32 bytes step)", 1, aligned_block_copy_unrolled_pf32_neon },
{ "NEON unrolled 2-pass copy prefetched (64 bytes step)", 1, aligned_block_copy_unrolled_pf64_neon },
{ "NEON fill", 0, aligned_block_fill_neon },
{ "NEON fill backwards", 0, aligned_block_fill_backwards_neon },
{ "VFP copy", 0, aligned_block_copy_vfp },
{ "VFP 2-pass copy", 1, aligned_block_copy_vfp },
{ "ARM fill (STRD)", 0, aligned_block_fill_strd_armv5te },
{ "ARM fill (STM with 8 registers)", 0, aligned_block_fill_stm8_armv4 },
{ "ARM fill (STM with 4 registers)", 0, aligned_block_fill_stm4_armv4 },
{ "ARM copy prefetched (incr pld)", 0, aligned_block_copy_incr_armv5te },
{ "ARM copy prefetched (wrap pld)", 0, aligned_block_copy_wrap_armv5te },
{ "ARM 2-pass copy prefetched (incr pld)", 1, aligned_block_copy_incr_armv5te },
{ "ARM 2-pass copy prefetched (wrap pld)", 1, aligned_block_copy_wrap_armv5te },
{ NULL, 0, NULL }
};
static bench_info arm_v5te_vfp[] =
{
{ "VFP copy", 0, aligned_block_copy_vfp },
{ "VFP 2-pass copy", 1, aligned_block_copy_vfp },
{ "ARM fill (STRD)", 0, aligned_block_fill_strd_armv5te },
{ "ARM fill (STM with 8 registers)", 0, aligned_block_fill_stm8_armv4 },
{ "ARM fill (STM with 4 registers)", 0, aligned_block_fill_stm4_armv4 },
{ "ARM copy prefetched (incr pld)", 0, aligned_block_copy_incr_armv5te },
{ "ARM copy prefetched (wrap pld)", 0, aligned_block_copy_wrap_armv5te },
{ "ARM 2-pass copy prefetched (incr pld)", 1, aligned_block_copy_incr_armv5te },
{ "ARM 2-pass copy prefetched (wrap pld)", 1, aligned_block_copy_wrap_armv5te },
{ NULL, 0, NULL }
};
static bench_info arm_v5te[] =
{
{ "ARM fill (STRD)", 0, aligned_block_fill_strd_armv5te },
{ "ARM fill (STM with 8 registers)", 0, aligned_block_fill_stm8_armv4 },
{ "ARM fill (STM with 4 registers)", 0, aligned_block_fill_stm4_armv4 },
{ "ARM copy prefetched (incr pld)", 0, aligned_block_copy_incr_armv5te },
{ "ARM copy prefetched (wrap pld)", 0, aligned_block_copy_wrap_armv5te },
{ "ARM 2-pass copy prefetched (incr pld)", 1, aligned_block_copy_incr_armv5te },
{ "ARM 2-pass copy prefetched (wrap pld)", 1, aligned_block_copy_wrap_armv5te },
{ NULL, 0, NULL }
};
static bench_info arm_v4[] =
{
{ "ARM fill (STM with 8 registers)", 0, aligned_block_fill_stm8_armv4 },
{ "ARM fill (STM with 4 registers)", 0, aligned_block_fill_stm4_armv4 },
{ NULL, 0, NULL }
};
bench_info *get_asm_benchmarks(void)
{
if (check_cpu_feature("neon") || check_cpu_feature("asimd"))
return arm_neon;
else if (check_cpu_feature("edsp") && check_cpu_feature("vfp"))
return arm_v5te_vfp;
else if (check_cpu_feature("edsp"))
return arm_v5te;
else
return arm_v4;
}
static bench_info arm_neon_fb[] =
{
{ "NEON read (from framebuffer)", 0, aligned_block_read_neon },
{ "NEON copy (from framebuffer)", 0, aligned_block_copy_neon },
{ "NEON 2-pass copy (from framebuffer)", 1, aligned_block_copy_neon },
{ "NEON unrolled copy (from framebuffer)", 0, aligned_block_copy_unrolled_neon },
{ "NEON 2-pass unrolled copy (from framebuffer)", 1, aligned_block_copy_unrolled_neon },
{ "VFP copy (from framebuffer)", 0, aligned_block_copy_vfp },
{ "VFP 2-pass copy (from framebuffer)", 1, aligned_block_copy_vfp },
{ "ARM copy (from framebuffer)", 0, aligned_block_copy_incr_armv5te },
{ "ARM 2-pass copy (from framebuffer)", 1, aligned_block_copy_incr_armv5te },
{ NULL, 0, NULL }
};
static bench_info arm_v5te_vfp_fb[] =
{
{ "VFP copy (from framebuffer)", 0, aligned_block_copy_vfp },
{ "VFP 2-pass copy (from framebuffer)", 1, aligned_block_copy_vfp },
{ "ARM copy (from framebuffer)", 0, aligned_block_copy_incr_armv5te },
{ "ARM 2-pass copy (from framebuffer)", 1, aligned_block_copy_incr_armv5te },
{ NULL, 0, NULL }
};
static bench_info arm_v5te_fb[] =
{
{ "ARM copy (from framebuffer)", 0, aligned_block_copy_incr_armv5te },
{ "ARM 2-pass copy (from framebuffer)", 1, aligned_block_copy_incr_armv5te },
{ NULL, 0, NULL }
};
bench_info *get_asm_framebuffer_benchmarks(void)
{
if (check_cpu_feature("neon") || check_cpu_feature("asimd"))
return arm_neon_fb;
else if (check_cpu_feature("edsp") && check_cpu_feature("vfp"))
return arm_v5te_vfp_fb;
else if (check_cpu_feature("edsp"))
return arm_v5te_fb;
else
return empty;
}
#elif defined(__aarch64__)
#include "aarch64-asm.h"
static bench_info aarch64_neon[] =
{
{ "NEON LDP/STP copy", 0, aligned_block_copy_ldpstp_q_aarch64 },
{ "NEON LDP/STP copy pldl2strm (32 bytes step)", 0, aligned_block_copy_ldpstp_q_pf32_l2strm_aarch64 },
{ "NEON LDP/STP copy pldl2strm (64 bytes step)", 0, aligned_block_copy_ldpstp_q_pf64_l2strm_aarch64 },
{ "NEON LDP/STP copy pldl1keep (32 bytes step)", 0, aligned_block_copy_ldpstp_q_pf32_l1keep_aarch64 },
{ "NEON LDP/STP copy pldl1keep (64 bytes step)", 0, aligned_block_copy_ldpstp_q_pf64_l1keep_aarch64 },
{ "NEON LD1/ST1 copy", 0, aligned_block_copy_ld1st1_aarch64 },
{ "NEON STP fill", 0, aligned_block_fill_stp_q_aarch64 },
{ "NEON STNP fill", 0, aligned_block_fill_stnp_q_aarch64 },
{ "ARM LDP/STP copy", 0, aligned_block_copy_ldpstp_x_aarch64 },
{ "ARM STP fill", 0, aligned_block_fill_stp_x_aarch64 },
{ "ARM STNP fill", 0, aligned_block_fill_stnp_x_aarch64 },
{ NULL, 0, NULL }
};
static bench_info aarch64_neon_fb[] =
{
{ "NEON LDP/STP copy (from framebuffer)", 0, aligned_block_copy_ldpstp_q_aarch64 },
{ "NEON LDP/STP 2-pass copy (from framebuffer)", 1, aligned_block_copy_ldpstp_q_aarch64 },
{ "NEON LD1/ST1 copy (from framebuffer)", 0, aligned_block_copy_ld1st1_aarch64 },
{ "NEON LD1/ST1 2-pass copy (from framebuffer)", 1, aligned_block_copy_ld1st1_aarch64 },
{ "ARM LDP/STP copy (from framebuffer)", 0, aligned_block_copy_ldpstp_x_aarch64 },
{ "ARM LDP/STP 2-pass copy (from framebuffer)", 1, aligned_block_copy_ldpstp_x_aarch64 },
{ NULL, 0, NULL }
};
bench_info *get_asm_benchmarks(void)
{
return aarch64_neon;
}
bench_info *get_asm_framebuffer_benchmarks(void)
{
return aarch64_neon_fb;
}
#elif defined(__mips__) && defined(_ABIO32)
#include "mips-32.h"
static bench_info mips_32[] =
{
{ "MIPS32 copy prefetched (32 bytes step)", 0, aligned_block_copy_pf32_mips32 },
{ "MIPS32 2-pass copy prefetched (32 bytes step)", 1, aligned_block_copy_pf32_mips32 },
{ "MIPS32 fill prefetched (32 bytes step)", 0, aligned_block_fill_pf32_mips32 },
{ NULL, 0, NULL }
};
bench_info *get_asm_benchmarks(void)
{
/* Enable only for MIPS32 processors which have 32 bytes cache line */
if (check_cpu_feature("MIPS 24K") ||
check_cpu_feature("MIPS 24Kc") ||
check_cpu_feature("MIPS 24Kf") ||
check_cpu_feature("MIPS 74K") ||
check_cpu_feature("MIPS 74Kc") ||
check_cpu_feature("MIPS 74Kf") ||
check_cpu_feature("MIPS 1004K") ||
check_cpu_feature("MIPS 1004Kc") ||
check_cpu_feature("MIPS 1004Kf") ||
check_cpu_feature("MIPS 1074K") ||
check_cpu_feature("MIPS 1074Kc") ||
check_cpu_feature("MIPS 1074Kf"))
{
return mips_32;
}
else
{
return empty;
}
}
bench_info *get_asm_framebuffer_benchmarks(void)
{
return empty;
}
#else
bench_info *get_asm_benchmarks(void)
{
return empty;
}
bench_info *get_asm_framebuffer_benchmarks(void)
{
return empty;
}
#endif