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