Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
52868 views
1
/*
2
* Assembly testing and benchmarking tool
3
* Copyright (c) 2015 Henrik Gramner
4
* Copyright (c) 2008 Loren Merritt
5
*
6
* This file is part of FFmpeg.
7
*
8
* FFmpeg is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2 of the License, or
11
* (at your option) any later version.
12
*
13
* FFmpeg is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
17
*
18
* You should have received a copy of the GNU General Public License along
19
* with FFmpeg; if not, write to the Free Software Foundation, Inc.,
20
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
*/
22
23
#include <stdarg.h>
24
#include <stdio.h>
25
#include <stdlib.h>
26
#include <string.h>
27
#include "checkasm.h"
28
#include "libavutil/common.h"
29
#include "libavutil/cpu.h"
30
#include "libavutil/intfloat.h"
31
#include "libavutil/random_seed.h"
32
33
#if HAVE_IO_H
34
#include <io.h>
35
#endif
36
37
#if HAVE_SETCONSOLETEXTATTRIBUTE
38
#include <windows.h>
39
#define COLOR_RED FOREGROUND_RED
40
#define COLOR_GREEN FOREGROUND_GREEN
41
#define COLOR_YELLOW (FOREGROUND_RED|FOREGROUND_GREEN)
42
#else
43
#define COLOR_RED 1
44
#define COLOR_GREEN 2
45
#define COLOR_YELLOW 3
46
#endif
47
48
#if HAVE_UNISTD_H
49
#include <unistd.h>
50
#endif
51
52
#if !HAVE_ISATTY
53
#define isatty(fd) 1
54
#endif
55
56
#if ARCH_ARM && HAVE_ARMV5TE_EXTERNAL
57
#include "libavutil/arm/cpu.h"
58
59
void (*checkasm_checked_call)(void *func, int dummy, ...) = checkasm_checked_call_novfp;
60
#endif
61
62
/* List of tests to invoke */
63
static const struct {
64
const char *name;
65
void (*func)(void);
66
} tests[] = {
67
#if CONFIG_AVCODEC
68
#if CONFIG_ALAC_DECODER
69
{ "alacdsp", checkasm_check_alacdsp },
70
#endif
71
#if CONFIG_BLEND_FILTER
72
{ "vf_blend", checkasm_check_blend },
73
#endif
74
#if CONFIG_BSWAPDSP
75
{ "bswapdsp", checkasm_check_bswapdsp },
76
#endif
77
#if CONFIG_DCA_DECODER
78
{ "synth_filter", checkasm_check_synth_filter },
79
#endif
80
#if CONFIG_FLACDSP
81
{ "flacdsp", checkasm_check_flacdsp },
82
#endif
83
#if CONFIG_FMTCONVERT
84
{ "fmtconvert", checkasm_check_fmtconvert },
85
#endif
86
#if CONFIG_H264PRED
87
{ "h264pred", checkasm_check_h264pred },
88
#endif
89
#if CONFIG_H264QPEL
90
{ "h264qpel", checkasm_check_h264qpel },
91
#endif
92
#if CONFIG_JPEG2000_DECODER
93
{ "jpeg2000dsp", checkasm_check_jpeg2000dsp },
94
#endif
95
#if CONFIG_PIXBLOCKDSP
96
{ "pixblockdsp", checkasm_check_pixblockdsp },
97
#endif
98
#if CONFIG_V210_ENCODER
99
{ "v210enc", checkasm_check_v210enc },
100
#endif
101
#if CONFIG_VP9_DECODER
102
{ "vp9dsp", checkasm_check_vp9dsp },
103
#endif
104
#if CONFIG_VIDEODSP
105
{ "videodsp", checkasm_check_videodsp },
106
#endif
107
#endif
108
{ NULL }
109
};
110
111
/* List of cpu flags to check */
112
static const struct {
113
const char *name;
114
const char *suffix;
115
int flag;
116
} cpus[] = {
117
#if ARCH_AARCH64
118
{ "ARMV8", "armv8", AV_CPU_FLAG_ARMV8 },
119
{ "NEON", "neon", AV_CPU_FLAG_NEON },
120
#elif ARCH_ARM
121
{ "ARMV5TE", "armv5te", AV_CPU_FLAG_ARMV5TE },
122
{ "ARMV6", "armv6", AV_CPU_FLAG_ARMV6 },
123
{ "ARMV6T2", "armv6t2", AV_CPU_FLAG_ARMV6T2 },
124
{ "VFP", "vfp", AV_CPU_FLAG_VFP },
125
{ "VFP_VM", "vfp_vm", AV_CPU_FLAG_VFP_VM },
126
{ "VFPV3", "vfp3", AV_CPU_FLAG_VFPV3 },
127
{ "NEON", "neon", AV_CPU_FLAG_NEON },
128
#elif ARCH_PPC
129
{ "ALTIVEC", "altivec", AV_CPU_FLAG_ALTIVEC },
130
{ "VSX", "vsx", AV_CPU_FLAG_VSX },
131
{ "POWER8", "power8", AV_CPU_FLAG_POWER8 },
132
#elif ARCH_X86
133
{ "MMX", "mmx", AV_CPU_FLAG_MMX|AV_CPU_FLAG_CMOV },
134
{ "MMXEXT", "mmxext", AV_CPU_FLAG_MMXEXT },
135
{ "3DNOW", "3dnow", AV_CPU_FLAG_3DNOW },
136
{ "3DNOWEXT", "3dnowext", AV_CPU_FLAG_3DNOWEXT },
137
{ "SSE", "sse", AV_CPU_FLAG_SSE },
138
{ "SSE2", "sse2", AV_CPU_FLAG_SSE2|AV_CPU_FLAG_SSE2SLOW },
139
{ "SSE3", "sse3", AV_CPU_FLAG_SSE3|AV_CPU_FLAG_SSE3SLOW },
140
{ "SSSE3", "ssse3", AV_CPU_FLAG_SSSE3|AV_CPU_FLAG_ATOM },
141
{ "SSE4.1", "sse4", AV_CPU_FLAG_SSE4 },
142
{ "SSE4.2", "sse42", AV_CPU_FLAG_SSE42 },
143
{ "AES-NI", "aesni", AV_CPU_FLAG_AESNI },
144
{ "AVX", "avx", AV_CPU_FLAG_AVX },
145
{ "XOP", "xop", AV_CPU_FLAG_XOP },
146
{ "FMA3", "fma3", AV_CPU_FLAG_FMA3 },
147
{ "FMA4", "fma4", AV_CPU_FLAG_FMA4 },
148
{ "AVX2", "avx2", AV_CPU_FLAG_AVX2 },
149
#endif
150
{ NULL }
151
};
152
153
typedef struct CheckasmFuncVersion {
154
struct CheckasmFuncVersion *next;
155
void *func;
156
int ok;
157
int cpu;
158
int iterations;
159
uint64_t cycles;
160
} CheckasmFuncVersion;
161
162
/* Binary search tree node */
163
typedef struct CheckasmFunc {
164
struct CheckasmFunc *child[2];
165
CheckasmFuncVersion versions;
166
uint8_t color; /* 0 = red, 1 = black */
167
char name[1];
168
} CheckasmFunc;
169
170
/* Internal state */
171
static struct {
172
CheckasmFunc *funcs;
173
CheckasmFunc *current_func;
174
CheckasmFuncVersion *current_func_ver;
175
const char *current_test_name;
176
const char *bench_pattern;
177
int bench_pattern_len;
178
int num_checked;
179
int num_failed;
180
int nop_time;
181
int cpu_flag;
182
const char *cpu_flag_name;
183
} state;
184
185
/* PRNG state */
186
AVLFG checkasm_lfg;
187
188
/* float compare support code */
189
static int is_negative(union av_intfloat32 u)
190
{
191
return u.i >> 31;
192
}
193
194
int float_near_ulp(float a, float b, unsigned max_ulp)
195
{
196
union av_intfloat32 x, y;
197
198
x.f = a;
199
y.f = b;
200
201
if (is_negative(x) != is_negative(y)) {
202
// handle -0.0 == +0.0
203
return a == b;
204
}
205
206
if (abs(x.i - y.i) <= max_ulp)
207
return 1;
208
209
return 0;
210
}
211
212
int float_near_ulp_array(const float *a, const float *b, unsigned max_ulp,
213
unsigned len)
214
{
215
unsigned i;
216
217
for (i = 0; i < len; i++) {
218
if (!float_near_ulp(a[i], b[i], max_ulp))
219
return 0;
220
}
221
return 1;
222
}
223
224
int float_near_abs_eps(float a, float b, float eps)
225
{
226
float abs_diff = fabsf(a - b);
227
228
return abs_diff < eps;
229
}
230
231
int float_near_abs_eps_array(const float *a, const float *b, float eps,
232
unsigned len)
233
{
234
unsigned i;
235
236
for (i = 0; i < len; i++) {
237
if (!float_near_abs_eps(a[i], b[i], eps))
238
return 0;
239
}
240
return 1;
241
}
242
243
int float_near_abs_eps_ulp(float a, float b, float eps, unsigned max_ulp)
244
{
245
return float_near_ulp(a, b, max_ulp) || float_near_abs_eps(a, b, eps);
246
}
247
248
int float_near_abs_eps_array_ulp(const float *a, const float *b, float eps,
249
unsigned max_ulp, unsigned len)
250
{
251
unsigned i;
252
253
for (i = 0; i < len; i++) {
254
if (!float_near_abs_eps_ulp(a[i], b[i], eps, max_ulp))
255
return 0;
256
}
257
return 1;
258
}
259
260
/* Print colored text to stderr if the terminal supports it */
261
static void color_printf(int color, const char *fmt, ...)
262
{
263
static int use_color = -1;
264
va_list arg;
265
266
#if HAVE_SETCONSOLETEXTATTRIBUTE
267
static HANDLE con;
268
static WORD org_attributes;
269
270
if (use_color < 0) {
271
CONSOLE_SCREEN_BUFFER_INFO con_info;
272
con = GetStdHandle(STD_ERROR_HANDLE);
273
if (con && con != INVALID_HANDLE_VALUE && GetConsoleScreenBufferInfo(con, &con_info)) {
274
org_attributes = con_info.wAttributes;
275
use_color = 1;
276
} else
277
use_color = 0;
278
}
279
if (use_color)
280
SetConsoleTextAttribute(con, (org_attributes & 0xfff0) | (color & 0x0f));
281
#else
282
if (use_color < 0) {
283
const char *term = getenv("TERM");
284
use_color = term && strcmp(term, "dumb") && isatty(2);
285
}
286
if (use_color)
287
fprintf(stderr, "\x1b[%d;3%dm", (color & 0x08) >> 3, color & 0x07);
288
#endif
289
290
va_start(arg, fmt);
291
vfprintf(stderr, fmt, arg);
292
va_end(arg);
293
294
if (use_color) {
295
#if HAVE_SETCONSOLETEXTATTRIBUTE
296
SetConsoleTextAttribute(con, org_attributes);
297
#else
298
fprintf(stderr, "\x1b[0m");
299
#endif
300
}
301
}
302
303
/* Deallocate a tree */
304
static void destroy_func_tree(CheckasmFunc *f)
305
{
306
if (f) {
307
CheckasmFuncVersion *v = f->versions.next;
308
while (v) {
309
CheckasmFuncVersion *next = v->next;
310
free(v);
311
v = next;
312
}
313
314
destroy_func_tree(f->child[0]);
315
destroy_func_tree(f->child[1]);
316
free(f);
317
}
318
}
319
320
/* Allocate a zero-initialized block, clean up and exit on failure */
321
static void *checkasm_malloc(size_t size)
322
{
323
void *ptr = calloc(1, size);
324
if (!ptr) {
325
fprintf(stderr, "checkasm: malloc failed\n");
326
destroy_func_tree(state.funcs);
327
exit(1);
328
}
329
return ptr;
330
}
331
332
/* Get the suffix of the specified cpu flag */
333
static const char *cpu_suffix(int cpu)
334
{
335
int i = FF_ARRAY_ELEMS(cpus);
336
337
while (--i >= 0)
338
if (cpu & cpus[i].flag)
339
return cpus[i].suffix;
340
341
return "c";
342
}
343
344
#ifdef AV_READ_TIME
345
static int cmp_nop(const void *a, const void *b)
346
{
347
return *(const uint16_t*)a - *(const uint16_t*)b;
348
}
349
350
/* Measure the overhead of the timing code (in decicycles) */
351
static int measure_nop_time(void)
352
{
353
uint16_t nops[10000];
354
int i, nop_sum = 0;
355
356
for (i = 0; i < 10000; i++) {
357
uint64_t t = AV_READ_TIME();
358
nops[i] = AV_READ_TIME() - t;
359
}
360
361
qsort(nops, 10000, sizeof(uint16_t), cmp_nop);
362
for (i = 2500; i < 7500; i++)
363
nop_sum += nops[i];
364
365
return nop_sum / 500;
366
}
367
368
/* Print benchmark results */
369
static void print_benchs(CheckasmFunc *f)
370
{
371
if (f) {
372
print_benchs(f->child[0]);
373
374
/* Only print functions with at least one assembly version */
375
if (f->versions.cpu || f->versions.next) {
376
CheckasmFuncVersion *v = &f->versions;
377
do {
378
if (v->iterations) {
379
int decicycles = (10*v->cycles/v->iterations - state.nop_time) / 4;
380
printf("%s_%s: %d.%d\n", f->name, cpu_suffix(v->cpu), decicycles/10, decicycles%10);
381
}
382
} while ((v = v->next));
383
}
384
385
print_benchs(f->child[1]);
386
}
387
}
388
#endif
389
390
/* ASCIIbetical sort except preserving natural order for numbers */
391
static int cmp_func_names(const char *a, const char *b)
392
{
393
const char *start = a;
394
int ascii_diff, digit_diff;
395
396
for (; !(ascii_diff = *(const unsigned char*)a - *(const unsigned char*)b) && *a; a++, b++);
397
for (; av_isdigit(*a) && av_isdigit(*b); a++, b++);
398
399
if (a > start && av_isdigit(a[-1]) && (digit_diff = av_isdigit(*a) - av_isdigit(*b)))
400
return digit_diff;
401
402
return ascii_diff;
403
}
404
405
/* Perform a tree rotation in the specified direction and return the new root */
406
static CheckasmFunc *rotate_tree(CheckasmFunc *f, int dir)
407
{
408
CheckasmFunc *r = f->child[dir^1];
409
f->child[dir^1] = r->child[dir];
410
r->child[dir] = f;
411
r->color = f->color;
412
f->color = 0;
413
return r;
414
}
415
416
#define is_red(f) ((f) && !(f)->color)
417
418
/* Balance a left-leaning red-black tree at the specified node */
419
static void balance_tree(CheckasmFunc **root)
420
{
421
CheckasmFunc *f = *root;
422
423
if (is_red(f->child[0]) && is_red(f->child[1])) {
424
f->color ^= 1;
425
f->child[0]->color = f->child[1]->color = 1;
426
}
427
428
if (!is_red(f->child[0]) && is_red(f->child[1]))
429
*root = rotate_tree(f, 0); /* Rotate left */
430
else if (is_red(f->child[0]) && is_red(f->child[0]->child[0]))
431
*root = rotate_tree(f, 1); /* Rotate right */
432
}
433
434
/* Get a node with the specified name, creating it if it doesn't exist */
435
static CheckasmFunc *get_func(CheckasmFunc **root, const char *name)
436
{
437
CheckasmFunc *f = *root;
438
439
if (f) {
440
/* Search the tree for a matching node */
441
int cmp = cmp_func_names(name, f->name);
442
if (cmp) {
443
f = get_func(&f->child[cmp > 0], name);
444
445
/* Rebalance the tree on the way up if a new node was inserted */
446
if (!f->versions.func)
447
balance_tree(root);
448
}
449
} else {
450
/* Allocate and insert a new node into the tree */
451
int name_length = strlen(name);
452
f = *root = checkasm_malloc(sizeof(CheckasmFunc) + name_length);
453
memcpy(f->name, name, name_length + 1);
454
}
455
456
return f;
457
}
458
459
/* Perform tests and benchmarks for the specified cpu flag if supported by the host */
460
static void check_cpu_flag(const char *name, int flag)
461
{
462
int old_cpu_flag = state.cpu_flag;
463
464
flag |= old_cpu_flag;
465
av_force_cpu_flags(-1);
466
state.cpu_flag = flag & av_get_cpu_flags();
467
av_force_cpu_flags(state.cpu_flag);
468
469
if (!flag || state.cpu_flag != old_cpu_flag) {
470
int i;
471
472
state.cpu_flag_name = name;
473
for (i = 0; tests[i].func; i++) {
474
state.current_test_name = tests[i].name;
475
tests[i].func();
476
}
477
}
478
}
479
480
/* Print the name of the current CPU flag, but only do it once */
481
static void print_cpu_name(void)
482
{
483
if (state.cpu_flag_name) {
484
color_printf(COLOR_YELLOW, "%s:\n", state.cpu_flag_name);
485
state.cpu_flag_name = NULL;
486
}
487
}
488
489
int main(int argc, char *argv[])
490
{
491
int i, seed, ret = 0;
492
493
#if ARCH_ARM && HAVE_ARMV5TE_EXTERNAL
494
if (have_vfp(av_get_cpu_flags()) || have_neon(av_get_cpu_flags()))
495
checkasm_checked_call = checkasm_checked_call_vfp;
496
#endif
497
498
if (!tests[0].func || !cpus[0].flag) {
499
fprintf(stderr, "checkasm: no tests to perform\n");
500
return 0;
501
}
502
503
if (argc > 1 && !strncmp(argv[1], "--bench", 7)) {
504
#ifndef AV_READ_TIME
505
fprintf(stderr, "checkasm: --bench is not supported on your system\n");
506
return 1;
507
#endif
508
if (argv[1][7] == '=') {
509
state.bench_pattern = argv[1] + 8;
510
state.bench_pattern_len = strlen(state.bench_pattern);
511
} else
512
state.bench_pattern = "";
513
514
argc--;
515
argv++;
516
}
517
518
seed = (argc > 1) ? atoi(argv[1]) : av_get_random_seed();
519
fprintf(stderr, "checkasm: using random seed %u\n", seed);
520
av_lfg_init(&checkasm_lfg, seed);
521
522
check_cpu_flag(NULL, 0);
523
for (i = 0; cpus[i].flag; i++)
524
check_cpu_flag(cpus[i].name, cpus[i].flag);
525
526
if (state.num_failed) {
527
fprintf(stderr, "checkasm: %d of %d tests have failed\n", state.num_failed, state.num_checked);
528
ret = 1;
529
} else {
530
fprintf(stderr, "checkasm: all %d tests passed\n", state.num_checked);
531
#ifdef AV_READ_TIME
532
if (state.bench_pattern) {
533
state.nop_time = measure_nop_time();
534
printf("nop: %d.%d\n", state.nop_time/10, state.nop_time%10);
535
print_benchs(state.funcs);
536
}
537
#endif
538
}
539
540
destroy_func_tree(state.funcs);
541
return ret;
542
}
543
544
/* Decide whether or not the specified function needs to be tested and
545
* allocate/initialize data structures if needed. Returns a pointer to a
546
* reference function if the function should be tested, otherwise NULL */
547
void *checkasm_check_func(void *func, const char *name, ...)
548
{
549
char name_buf[256];
550
void *ref = func;
551
CheckasmFuncVersion *v;
552
int name_length;
553
va_list arg;
554
555
va_start(arg, name);
556
name_length = vsnprintf(name_buf, sizeof(name_buf), name, arg);
557
va_end(arg);
558
559
if (!func || name_length <= 0 || name_length >= sizeof(name_buf))
560
return NULL;
561
562
state.current_func = get_func(&state.funcs, name_buf);
563
state.funcs->color = 1;
564
v = &state.current_func->versions;
565
566
if (v->func) {
567
CheckasmFuncVersion *prev;
568
do {
569
/* Only test functions that haven't already been tested */
570
if (v->func == func)
571
return NULL;
572
573
if (v->ok)
574
ref = v->func;
575
576
prev = v;
577
} while ((v = v->next));
578
579
v = prev->next = checkasm_malloc(sizeof(CheckasmFuncVersion));
580
}
581
582
v->func = func;
583
v->ok = 1;
584
v->cpu = state.cpu_flag;
585
state.current_func_ver = v;
586
587
if (state.cpu_flag)
588
state.num_checked++;
589
590
return ref;
591
}
592
593
/* Decide whether or not the current function needs to be benchmarked */
594
int checkasm_bench_func(void)
595
{
596
return !state.num_failed && state.bench_pattern &&
597
!strncmp(state.current_func->name, state.bench_pattern, state.bench_pattern_len);
598
}
599
600
/* Indicate that the current test has failed */
601
void checkasm_fail_func(const char *msg, ...)
602
{
603
if (state.current_func_ver->cpu && state.current_func_ver->ok) {
604
va_list arg;
605
606
print_cpu_name();
607
fprintf(stderr, " %s_%s (", state.current_func->name, cpu_suffix(state.current_func_ver->cpu));
608
va_start(arg, msg);
609
vfprintf(stderr, msg, arg);
610
va_end(arg);
611
fprintf(stderr, ")\n");
612
613
state.current_func_ver->ok = 0;
614
state.num_failed++;
615
}
616
}
617
618
/* Update benchmark results of the current function */
619
void checkasm_update_bench(int iterations, uint64_t cycles)
620
{
621
state.current_func_ver->iterations += iterations;
622
state.current_func_ver->cycles += cycles;
623
}
624
625
/* Print the outcome of all tests performed since the last time this function was called */
626
void checkasm_report(const char *name, ...)
627
{
628
static int prev_checked, prev_failed, max_length;
629
630
if (state.num_checked > prev_checked) {
631
int pad_length = max_length + 4;
632
va_list arg;
633
634
print_cpu_name();
635
pad_length -= fprintf(stderr, " - %s.", state.current_test_name);
636
va_start(arg, name);
637
pad_length -= vfprintf(stderr, name, arg);
638
va_end(arg);
639
fprintf(stderr, "%*c", FFMAX(pad_length, 0) + 2, '[');
640
641
if (state.num_failed == prev_failed)
642
color_printf(COLOR_GREEN, "OK");
643
else
644
color_printf(COLOR_RED, "FAILED");
645
fprintf(stderr, "]\n");
646
647
prev_checked = state.num_checked;
648
prev_failed = state.num_failed;
649
} else if (!state.cpu_flag) {
650
/* Calculate the amount of padding required to make the output vertically aligned */
651
int length = strlen(state.current_test_name);
652
va_list arg;
653
654
va_start(arg, name);
655
length += vsnprintf(NULL, 0, name, arg);
656
va_end(arg);
657
658
if (length > max_length)
659
max_length = length;
660
}
661
}
662
663