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