]> git.sesse.net Git - ffmpeg/blob - tests/checkasm/checkasm.c
Merge commit '403ea4ac7289ac39229452b6b5e2f8ebcc00f2a1'
[ffmpeg] / tests / checkasm / checkasm.c
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/random_seed.h"
31
32 #if HAVE_IO_H
33 #include <io.h>
34 #endif
35
36 #if HAVE_SETCONSOLETEXTATTRIBUTE
37 #include <windows.h>
38 #define COLOR_RED    FOREGROUND_RED
39 #define COLOR_GREEN  FOREGROUND_GREEN
40 #define COLOR_YELLOW (FOREGROUND_RED|FOREGROUND_GREEN)
41 #else
42 #define COLOR_RED    1
43 #define COLOR_GREEN  2
44 #define COLOR_YELLOW 3
45 #endif
46
47 #if HAVE_UNISTD_H
48 #include <unistd.h>
49 #endif
50
51 #if !HAVE_ISATTY
52 #define isatty(fd) 1
53 #endif
54
55 /* List of tests to invoke */
56 static const struct {
57     const char *name;
58     void (*func)(void);
59 } tests[] = {
60 #if CONFIG_BSWAPDSP
61     { "bswapdsp", checkasm_check_bswapdsp },
62 #endif
63 #if CONFIG_H264PRED
64     { "h264pred", checkasm_check_h264pred },
65 #endif
66 #if CONFIG_H264QPEL
67     { "h264qpel", checkasm_check_h264qpel },
68 #endif
69     { NULL }
70 };
71
72 /* List of cpu flags to check */
73 static const struct {
74     const char *name;
75     const char *suffix;
76     int flag;
77 } cpus[] = {
78 #if   ARCH_AARCH64
79     { "ARMV8",    "armv8",    AV_CPU_FLAG_ARMV8 },
80     { "NEON",     "neon",     AV_CPU_FLAG_NEON },
81 #elif ARCH_ARM
82     { "ARMV5TE",  "armv5te",  AV_CPU_FLAG_ARMV5TE },
83     { "ARMV6",    "armv6",    AV_CPU_FLAG_ARMV6 },
84     { "ARMV6T2",  "armv6t2",  AV_CPU_FLAG_ARMV6T2 },
85     { "VFP",      "vfp",      AV_CPU_FLAG_VFP },
86     { "VFPV3",    "vfp3",     AV_CPU_FLAG_VFPV3 },
87     { "NEON",     "neon",     AV_CPU_FLAG_NEON },
88 #elif ARCH_PPC
89     { "ALTIVEC",  "altivec",  AV_CPU_FLAG_ALTIVEC },
90     { "VSX",      "vsx",      AV_CPU_FLAG_VSX },
91     { "POWER8",   "power8",   AV_CPU_FLAG_POWER8 },
92 #elif ARCH_X86
93     { "MMX",      "mmx",      AV_CPU_FLAG_MMX|AV_CPU_FLAG_CMOV },
94     { "MMXEXT",   "mmxext",   AV_CPU_FLAG_MMXEXT },
95     { "3DNOW",    "3dnow",    AV_CPU_FLAG_3DNOW },
96     { "3DNOWEXT", "3dnowext", AV_CPU_FLAG_3DNOWEXT },
97     { "SSE",      "sse",      AV_CPU_FLAG_SSE },
98     { "SSE2",     "sse2",     AV_CPU_FLAG_SSE2|AV_CPU_FLAG_SSE2SLOW },
99     { "SSE3",     "sse3",     AV_CPU_FLAG_SSE3|AV_CPU_FLAG_SSE3SLOW },
100     { "SSSE3",    "ssse3",    AV_CPU_FLAG_SSSE3|AV_CPU_FLAG_ATOM },
101     { "SSE4.1",   "sse4",     AV_CPU_FLAG_SSE4 },
102     { "SSE4.2",   "sse42",    AV_CPU_FLAG_SSE42 },
103     { "AVX",      "avx",      AV_CPU_FLAG_AVX },
104     { "XOP",      "xop",      AV_CPU_FLAG_XOP },
105     { "FMA3",     "fma3",     AV_CPU_FLAG_FMA3 },
106     { "FMA4",     "fma4",     AV_CPU_FLAG_FMA4 },
107     { "AVX2",     "avx2",     AV_CPU_FLAG_AVX2 },
108 #endif
109     { NULL }
110 };
111
112 typedef struct CheckasmFuncVersion {
113     struct CheckasmFuncVersion *next;
114     void *func;
115     int ok;
116     int cpu;
117     int iterations;
118     uint64_t cycles;
119 } CheckasmFuncVersion;
120
121 /* Binary search tree node */
122 typedef struct CheckasmFunc {
123     struct CheckasmFunc *child[2];
124     CheckasmFuncVersion versions;
125     char name[1];
126 } CheckasmFunc;
127
128 /* Internal state */
129 static struct {
130     CheckasmFunc *funcs;
131     CheckasmFunc *current_func;
132     CheckasmFuncVersion *current_func_ver;
133     const char *current_test_name;
134     const char *bench_pattern;
135     int bench_pattern_len;
136     int num_checked;
137     int num_failed;
138     int nop_time;
139     int cpu_flag;
140     const char *cpu_flag_name;
141 } state;
142
143 /* PRNG state */
144 AVLFG checkasm_lfg;
145
146 /* Print colored text to stderr if the terminal supports it */
147 static void color_printf(int color, const char *fmt, ...)
148 {
149     static int use_color = -1;
150     va_list arg;
151
152 #if HAVE_SETCONSOLETEXTATTRIBUTE
153     static HANDLE con;
154     static WORD org_attributes;
155
156     if (use_color < 0) {
157         CONSOLE_SCREEN_BUFFER_INFO con_info;
158         con = GetStdHandle(STD_ERROR_HANDLE);
159         if (con && con != INVALID_HANDLE_VALUE && GetConsoleScreenBufferInfo(con, &con_info)) {
160             org_attributes = con_info.wAttributes;
161             use_color = 1;
162         } else
163             use_color = 0;
164     }
165     if (use_color)
166         SetConsoleTextAttribute(con, (org_attributes & 0xfff0) | (color & 0x0f));
167 #else
168     if (use_color < 0) {
169         const char *term = getenv("TERM");
170         use_color = term && strcmp(term, "dumb") && isatty(2);
171     }
172     if (use_color)
173         fprintf(stderr, "\x1b[%d;3%dm", (color & 0x08) >> 3, color & 0x07);
174 #endif
175
176     va_start(arg, fmt);
177     vfprintf(stderr, fmt, arg);
178     va_end(arg);
179
180     if (use_color) {
181 #if HAVE_SETCONSOLETEXTATTRIBUTE
182         SetConsoleTextAttribute(con, org_attributes);
183 #else
184         fprintf(stderr, "\x1b[0m");
185 #endif
186     }
187 }
188
189 /* Deallocate a tree */
190 static void destroy_func_tree(CheckasmFunc *f)
191 {
192     if (f) {
193         CheckasmFuncVersion *v = f->versions.next;
194         while (v) {
195             CheckasmFuncVersion *next = v->next;
196             free(v);
197             v = next;
198         }
199
200         destroy_func_tree(f->child[0]);
201         destroy_func_tree(f->child[1]);
202         free(f);
203     }
204 }
205
206 /* Allocate a zero-initialized block, clean up and exit on failure */
207 static void *checkasm_malloc(size_t size)
208 {
209     void *ptr = calloc(1, size);
210     if (!ptr) {
211         fprintf(stderr, "checkasm: malloc failed\n");
212         destroy_func_tree(state.funcs);
213         exit(1);
214     }
215     return ptr;
216 }
217
218 /* Get the suffix of the specified cpu flag */
219 static const char *cpu_suffix(int cpu)
220 {
221     int i = FF_ARRAY_ELEMS(cpus);
222
223     while (--i >= 0)
224         if (cpu & cpus[i].flag)
225             return cpus[i].suffix;
226
227     return "c";
228 }
229
230 #ifdef AV_READ_TIME
231 static int cmp_nop(const void *a, const void *b)
232 {
233     return *(const uint16_t*)a - *(const uint16_t*)b;
234 }
235
236 /* Measure the overhead of the timing code (in decicycles) */
237 static int measure_nop_time(void)
238 {
239     uint16_t nops[10000];
240     int i, nop_sum = 0;
241
242     for (i = 0; i < 10000; i++) {
243         uint64_t t = AV_READ_TIME();
244         nops[i] = AV_READ_TIME() - t;
245     }
246
247     qsort(nops, 10000, sizeof(uint16_t), cmp_nop);
248     for (i = 2500; i < 7500; i++)
249         nop_sum += nops[i];
250
251     return nop_sum / 500;
252 }
253
254 /* Print benchmark results */
255 static void print_benchs(CheckasmFunc *f)
256 {
257     if (f) {
258         print_benchs(f->child[0]);
259
260         /* Only print functions with at least one assembly version */
261         if (f->versions.cpu || f->versions.next) {
262             CheckasmFuncVersion *v = &f->versions;
263             do {
264                 if (v->iterations) {
265                     int decicycles = (10*v->cycles/v->iterations - state.nop_time) / 4;
266                     printf("%s_%s: %d.%d\n", f->name, cpu_suffix(v->cpu), decicycles/10, decicycles%10);
267                 }
268             } while ((v = v->next));
269         }
270
271         print_benchs(f->child[1]);
272     }
273 }
274 #endif
275
276 /* ASCIIbetical sort except preserving natural order for numbers */
277 static int cmp_func_names(const char *a, const char *b)
278 {
279     int ascii_diff, digit_diff;
280
281     for (; !(ascii_diff = *a - *b) && *a; a++, b++);
282     for (; av_isdigit(*a) && av_isdigit(*b); a++, b++);
283
284     return (digit_diff = av_isdigit(*a) - av_isdigit(*b)) ? digit_diff : ascii_diff;
285 }
286
287 /* Get a node with the specified name, creating it if it doesn't exist */
288 static CheckasmFunc *get_func(const char *name, int length)
289 {
290     CheckasmFunc *f, **f_ptr = &state.funcs;
291
292     /* Search the tree for a matching node */
293     while ((f = *f_ptr)) {
294         int cmp = cmp_func_names(name, f->name);
295         if (!cmp)
296             return f;
297
298         f_ptr = &f->child[(cmp > 0)];
299     }
300
301     /* Allocate and insert a new node into the tree */
302     f = *f_ptr = checkasm_malloc(sizeof(CheckasmFunc) + length);
303     memcpy(f->name, name, length+1);
304
305     return f;
306 }
307
308 /* Perform tests and benchmarks for the specified cpu flag if supported by the host */
309 static void check_cpu_flag(const char *name, int flag)
310 {
311     int old_cpu_flag = state.cpu_flag;
312
313     flag |= old_cpu_flag;
314     av_set_cpu_flags_mask(flag);
315     state.cpu_flag = av_get_cpu_flags();
316
317     if (!flag || state.cpu_flag != old_cpu_flag) {
318         int i;
319
320         state.cpu_flag_name = name;
321         for (i = 0; tests[i].func; i++) {
322             state.current_test_name = tests[i].name;
323             tests[i].func();
324         }
325     }
326 }
327
328 /* Print the name of the current CPU flag, but only do it once */
329 static void print_cpu_name(void)
330 {
331     if (state.cpu_flag_name) {
332         color_printf(COLOR_YELLOW, "%s:\n", state.cpu_flag_name);
333         state.cpu_flag_name = NULL;
334     }
335 }
336
337 int main(int argc, char *argv[])
338 {
339     int i, seed, ret = 0;
340
341     if (!tests[0].func || !cpus[0].flag) {
342         fprintf(stderr, "checkasm: no tests to perform\n");
343         return 0;
344     }
345
346     if (argc > 1 && !strncmp(argv[1], "--bench", 7)) {
347 #ifndef AV_READ_TIME
348         fprintf(stderr, "checkasm: --bench is not supported on your system\n");
349         return 1;
350 #endif
351         if (argv[1][7] == '=') {
352             state.bench_pattern = argv[1] + 8;
353             state.bench_pattern_len = strlen(state.bench_pattern);
354         } else
355             state.bench_pattern = "";
356
357         argc--;
358         argv++;
359     }
360
361     seed = (argc > 1) ? atoi(argv[1]) : av_get_random_seed();
362     fprintf(stderr, "checkasm: using random seed %u\n", seed);
363     av_lfg_init(&checkasm_lfg, seed);
364
365     check_cpu_flag(NULL, 0);
366     for (i = 0; cpus[i].flag; i++)
367         check_cpu_flag(cpus[i].name, cpus[i].flag);
368
369     if (state.num_failed) {
370         fprintf(stderr, "checkasm: %d of %d tests have failed\n", state.num_failed, state.num_checked);
371         ret = 1;
372     } else {
373         fprintf(stderr, "checkasm: all %d tests passed\n", state.num_checked);
374 #ifdef AV_READ_TIME
375         if (state.bench_pattern) {
376             state.nop_time = measure_nop_time();
377             printf("nop: %d.%d\n", state.nop_time/10, state.nop_time%10);
378             print_benchs(state.funcs);
379         }
380 #endif
381     }
382
383     destroy_func_tree(state.funcs);
384     return ret;
385 }
386
387 /* Decide whether or not the specified function needs to be tested and
388  * allocate/initialize data structures if needed. Returns a pointer to a
389  * reference function if the function should be tested, otherwise NULL */
390 void *checkasm_check_func(void *func, const char *name, ...)
391 {
392     char name_buf[256];
393     void *ref = func;
394     CheckasmFuncVersion *v;
395     int name_length;
396     va_list arg;
397
398     va_start(arg, name);
399     name_length = vsnprintf(name_buf, sizeof(name_buf), name, arg);
400     va_end(arg);
401
402     if (!func || name_length <= 0 || name_length >= sizeof(name_buf))
403         return NULL;
404
405     state.current_func = get_func(name_buf, name_length);
406     v = &state.current_func->versions;
407
408     if (v->func) {
409         CheckasmFuncVersion *prev;
410         do {
411             /* Only test functions that haven't already been tested */
412             if (v->func == func)
413                 return NULL;
414
415             if (v->ok)
416                 ref = v->func;
417
418             prev = v;
419         } while ((v = v->next));
420
421         v = prev->next = checkasm_malloc(sizeof(CheckasmFuncVersion));
422     }
423
424     v->func = func;
425     v->ok = 1;
426     v->cpu = state.cpu_flag;
427     state.current_func_ver = v;
428
429     if (state.cpu_flag)
430         state.num_checked++;
431
432     return ref;
433 }
434
435 /* Decide whether or not the current function needs to be benchmarked */
436 int checkasm_bench_func(void)
437 {
438     return !state.num_failed && state.bench_pattern &&
439            !strncmp(state.current_func->name, state.bench_pattern, state.bench_pattern_len);
440 }
441
442 /* Indicate that the current test has failed */
443 void checkasm_fail_func(const char *msg, ...)
444 {
445     if (state.current_func_ver->cpu && state.current_func_ver->ok) {
446         va_list arg;
447
448         print_cpu_name();
449         fprintf(stderr, "   %s_%s (", state.current_func->name, cpu_suffix(state.current_func_ver->cpu));
450         va_start(arg, msg);
451         vfprintf(stderr, msg, arg);
452         va_end(arg);
453         fprintf(stderr, ")\n");
454
455         state.current_func_ver->ok = 0;
456         state.num_failed++;
457     }
458 }
459
460 /* Update benchmark results of the current function */
461 void checkasm_update_bench(int iterations, uint64_t cycles)
462 {
463     state.current_func_ver->iterations += iterations;
464     state.current_func_ver->cycles += cycles;
465 }
466
467 /* Print the outcome of all tests performed since the last time this function was called */
468 void checkasm_report(const char *name, ...)
469 {
470     static int prev_checked, prev_failed, max_length;
471
472     if (state.num_checked > prev_checked) {
473         int pad_length = max_length + 4;
474         va_list arg;
475
476         print_cpu_name();
477         pad_length -= fprintf(stderr, " - %s.", state.current_test_name);
478         va_start(arg, name);
479         pad_length -= vfprintf(stderr, name, arg);
480         va_end(arg);
481         fprintf(stderr, "%*c", FFMAX(pad_length, 0) + 2, '[');
482
483         if (state.num_failed == prev_failed)
484             color_printf(COLOR_GREEN, "OK");
485         else
486             color_printf(COLOR_RED, "FAILED");
487         fprintf(stderr, "]\n");
488
489         prev_checked = state.num_checked;
490         prev_failed  = state.num_failed;
491     } else if (!state.cpu_flag) {
492         /* Calculate the amount of padding required to make the output vertically aligned */
493         int length = strlen(state.current_test_name);
494         va_list arg;
495
496         va_start(arg, name);
497         length += vsnprintf(NULL, 0, name, arg);
498         va_end(arg);
499
500         if (length > max_length)
501             max_length = length;
502     }
503 }