2 * Assembly testing and benchmarking tool
3 * Copyright (c) 2015 Henrik Gramner
4 * Copyright (c) 2008 Loren Merritt
6 * This file is part of FFmpeg.
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.
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.
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.
28 #include "libavutil/common.h"
29 #include "libavutil/cpu.h"
30 #include "libavutil/random_seed.h"
36 #if HAVE_SETCONSOLETEXTATTRIBUTE
38 #define COLOR_RED FOREGROUND_RED
39 #define COLOR_GREEN FOREGROUND_GREEN
40 #define COLOR_YELLOW (FOREGROUND_RED|FOREGROUND_GREEN)
44 #define COLOR_YELLOW 3
55 /* List of tests to invoke */
61 { "bswapdsp", checkasm_check_bswapdsp },
64 { "h264pred", checkasm_check_h264pred },
67 { "h264qpel", checkasm_check_h264qpel },
72 /* List of cpu flags to check */
79 { "ARMV8", "armv8", AV_CPU_FLAG_ARMV8 },
80 { "NEON", "neon", AV_CPU_FLAG_NEON },
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 },
89 { "ALTIVEC", "altivec", AV_CPU_FLAG_ALTIVEC },
90 { "VSX", "vsx", AV_CPU_FLAG_VSX },
91 { "POWER8", "power8", AV_CPU_FLAG_POWER8 },
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 },
112 typedef struct CheckasmFuncVersion {
113 struct CheckasmFuncVersion *next;
119 } CheckasmFuncVersion;
121 /* Binary search tree node */
122 typedef struct CheckasmFunc {
123 struct CheckasmFunc *child[2];
124 CheckasmFuncVersion versions;
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;
140 const char *cpu_flag_name;
146 /* Print colored text to stderr if the terminal supports it */
147 static void color_printf(int color, const char *fmt, ...)
149 static int use_color = -1;
152 #if HAVE_SETCONSOLETEXTATTRIBUTE
154 static WORD org_attributes;
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;
166 SetConsoleTextAttribute(con, (org_attributes & 0xfff0) | (color & 0x0f));
169 const char *term = getenv("TERM");
170 use_color = term && strcmp(term, "dumb") && isatty(2);
173 fprintf(stderr, "\x1b[%d;3%dm", (color & 0x08) >> 3, color & 0x07);
177 vfprintf(stderr, fmt, arg);
181 #if HAVE_SETCONSOLETEXTATTRIBUTE
182 SetConsoleTextAttribute(con, org_attributes);
184 fprintf(stderr, "\x1b[0m");
189 /* Deallocate a tree */
190 static void destroy_func_tree(CheckasmFunc *f)
193 CheckasmFuncVersion *v = f->versions.next;
195 CheckasmFuncVersion *next = v->next;
200 destroy_func_tree(f->child[0]);
201 destroy_func_tree(f->child[1]);
206 /* Allocate a zero-initialized block, clean up and exit on failure */
207 static void *checkasm_malloc(size_t size)
209 void *ptr = calloc(1, size);
211 fprintf(stderr, "checkasm: malloc failed\n");
212 destroy_func_tree(state.funcs);
218 /* Get the suffix of the specified cpu flag */
219 static const char *cpu_suffix(int cpu)
221 int i = FF_ARRAY_ELEMS(cpus);
224 if (cpu & cpus[i].flag)
225 return cpus[i].suffix;
231 static int cmp_nop(const void *a, const void *b)
233 return *(const uint16_t*)a - *(const uint16_t*)b;
236 /* Measure the overhead of the timing code (in decicycles) */
237 static int measure_nop_time(void)
239 uint16_t nops[10000];
242 for (i = 0; i < 10000; i++) {
243 uint64_t t = AV_READ_TIME();
244 nops[i] = AV_READ_TIME() - t;
247 qsort(nops, 10000, sizeof(uint16_t), cmp_nop);
248 for (i = 2500; i < 7500; i++)
251 return nop_sum / 500;
254 /* Print benchmark results */
255 static void print_benchs(CheckasmFunc *f)
258 print_benchs(f->child[0]);
260 /* Only print functions with at least one assembly version */
261 if (f->versions.cpu || f->versions.next) {
262 CheckasmFuncVersion *v = &f->versions;
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);
268 } while ((v = v->next));
271 print_benchs(f->child[1]);
276 /* ASCIIbetical sort except preserving natural order for numbers */
277 static int cmp_func_names(const char *a, const char *b)
279 int ascii_diff, digit_diff;
281 for (; !(ascii_diff = *a - *b) && *a; a++, b++);
282 for (; av_isdigit(*a) && av_isdigit(*b); a++, b++);
284 return (digit_diff = av_isdigit(*a) - av_isdigit(*b)) ? digit_diff : ascii_diff;
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)
290 CheckasmFunc *f, **f_ptr = &state.funcs;
292 /* Search the tree for a matching node */
293 while ((f = *f_ptr)) {
294 int cmp = cmp_func_names(name, f->name);
298 f_ptr = &f->child[(cmp > 0)];
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);
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)
311 int old_cpu_flag = state.cpu_flag;
313 flag |= old_cpu_flag;
314 av_set_cpu_flags_mask(flag);
315 state.cpu_flag = av_get_cpu_flags();
317 if (!flag || state.cpu_flag != old_cpu_flag) {
320 state.cpu_flag_name = name;
321 for (i = 0; tests[i].func; i++) {
322 state.current_test_name = tests[i].name;
328 /* Print the name of the current CPU flag, but only do it once */
329 static void print_cpu_name(void)
331 if (state.cpu_flag_name) {
332 color_printf(COLOR_YELLOW, "%s:\n", state.cpu_flag_name);
333 state.cpu_flag_name = NULL;
337 int main(int argc, char *argv[])
339 int i, seed, ret = 0;
341 if (!tests[0].func || !cpus[0].flag) {
342 fprintf(stderr, "checkasm: no tests to perform\n");
346 if (argc > 1 && !strncmp(argv[1], "--bench", 7)) {
348 fprintf(stderr, "checkasm: --bench is not supported on your system\n");
351 if (argv[1][7] == '=') {
352 state.bench_pattern = argv[1] + 8;
353 state.bench_pattern_len = strlen(state.bench_pattern);
355 state.bench_pattern = "";
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);
365 check_cpu_flag(NULL, 0);
366 for (i = 0; cpus[i].flag; i++)
367 check_cpu_flag(cpus[i].name, cpus[i].flag);
369 if (state.num_failed) {
370 fprintf(stderr, "checkasm: %d of %d tests have failed\n", state.num_failed, state.num_checked);
373 fprintf(stderr, "checkasm: all %d tests passed\n", state.num_checked);
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);
383 destroy_func_tree(state.funcs);
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, ...)
394 CheckasmFuncVersion *v;
399 name_length = vsnprintf(name_buf, sizeof(name_buf), name, arg);
402 if (!func || name_length <= 0 || name_length >= sizeof(name_buf))
405 state.current_func = get_func(name_buf, name_length);
406 v = &state.current_func->versions;
409 CheckasmFuncVersion *prev;
411 /* Only test functions that haven't already been tested */
419 } while ((v = v->next));
421 v = prev->next = checkasm_malloc(sizeof(CheckasmFuncVersion));
426 v->cpu = state.cpu_flag;
427 state.current_func_ver = v;
435 /* Decide whether or not the current function needs to be benchmarked */
436 int checkasm_bench_func(void)
438 return !state.num_failed && state.bench_pattern &&
439 !strncmp(state.current_func->name, state.bench_pattern, state.bench_pattern_len);
442 /* Indicate that the current test has failed */
443 void checkasm_fail_func(const char *msg, ...)
445 if (state.current_func_ver->cpu && state.current_func_ver->ok) {
449 fprintf(stderr, " %s_%s (", state.current_func->name, cpu_suffix(state.current_func_ver->cpu));
451 vfprintf(stderr, msg, arg);
453 fprintf(stderr, ")\n");
455 state.current_func_ver->ok = 0;
460 /* Update benchmark results of the current function */
461 void checkasm_update_bench(int iterations, uint64_t cycles)
463 state.current_func_ver->iterations += iterations;
464 state.current_func_ver->cycles += cycles;
467 /* Print the outcome of all tests performed since the last time this function was called */
468 void checkasm_report(const char *name, ...)
470 static int prev_checked, prev_failed, max_length;
472 if (state.num_checked > prev_checked) {
473 int pad_length = max_length + 4;
477 pad_length -= fprintf(stderr, " - %s.", state.current_test_name);
479 pad_length -= vfprintf(stderr, name, arg);
481 fprintf(stderr, "%*c", FFMAX(pad_length, 0) + 2, '[');
483 if (state.num_failed == prev_failed)
484 color_printf(COLOR_GREEN, "OK");
486 color_printf(COLOR_RED, "FAILED");
487 fprintf(stderr, "]\n");
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);
497 length += vsnprintf(NULL, 0, name, arg);
500 if (length > max_length)