FFmpeg
checkasm.c
Go to the documentation of this file.
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 "config.h"
24 
25 #if CONFIG_LINUX_PERF
26 # ifndef _GNU_SOURCE
27 # define _GNU_SOURCE // for syscall (performance monitoring API)
28 # endif
29 #endif
30 
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include "checkasm.h"
36 #include "libavutil/common.h"
37 #include "libavutil/cpu.h"
38 #include "libavutil/intfloat.h"
39 #include "libavutil/random_seed.h"
40 
41 #if HAVE_IO_H
42 #include <io.h>
43 #endif
44 
45 #if HAVE_SETCONSOLETEXTATTRIBUTE && HAVE_GETSTDHANDLE
46 #include <windows.h>
47 #define COLOR_RED FOREGROUND_RED
48 #define COLOR_GREEN FOREGROUND_GREEN
49 #define COLOR_YELLOW (FOREGROUND_RED|FOREGROUND_GREEN)
50 #else
51 #define COLOR_RED 1
52 #define COLOR_GREEN 2
53 #define COLOR_YELLOW 3
54 #endif
55 
56 #if HAVE_UNISTD_H
57 #include <unistd.h>
58 #endif
59 
60 #if !HAVE_ISATTY
61 #define isatty(fd) 1
62 #endif
63 
64 #if ARCH_ARM && HAVE_ARMV5TE_EXTERNAL
65 #include "libavutil/arm/cpu.h"
66 
67 void (*checkasm_checked_call)(void *func, int dummy, ...) = checkasm_checked_call_novfp;
68 #endif
69 
70 /* List of tests to invoke */
71 static const struct {
72  const char *name;
73  void (*func)(void);
74 } tests[] = {
75 #if CONFIG_AVCODEC
76  #if CONFIG_AAC_DECODER
77  { "aacpsdsp", checkasm_check_aacpsdsp },
78  { "sbrdsp", checkasm_check_sbrdsp },
79  #endif
80  #if CONFIG_ALAC_DECODER
81  { "alacdsp", checkasm_check_alacdsp },
82  #endif
83  #if CONFIG_AUDIODSP
84  { "audiodsp", checkasm_check_audiodsp },
85  #endif
86  #if CONFIG_BLOCKDSP
87  { "blockdsp", checkasm_check_blockdsp },
88  #endif
89  #if CONFIG_BSWAPDSP
90  { "bswapdsp", checkasm_check_bswapdsp },
91  #endif
92  #if CONFIG_DCA_DECODER
93  { "synth_filter", checkasm_check_synth_filter },
94  #endif
95  #if CONFIG_EXR_DECODER
96  { "exrdsp", checkasm_check_exrdsp },
97  #endif
98  #if CONFIG_FLACDSP
99  { "flacdsp", checkasm_check_flacdsp },
100  #endif
101  #if CONFIG_FMTCONVERT
102  { "fmtconvert", checkasm_check_fmtconvert },
103  #endif
104  #if CONFIG_G722DSP
105  { "g722dsp", checkasm_check_g722dsp },
106  #endif
107  #if CONFIG_H264DSP
108  { "h264dsp", checkasm_check_h264dsp },
109  #endif
110  #if CONFIG_H264PRED
111  { "h264pred", checkasm_check_h264pred },
112  #endif
113  #if CONFIG_H264QPEL
114  { "h264qpel", checkasm_check_h264qpel },
115  #endif
116  #if CONFIG_HEVC_DECODER
117  { "hevc_add_res", checkasm_check_hevc_add_res },
118  { "hevc_idct", checkasm_check_hevc_idct },
119  { "hevc_sao", checkasm_check_hevc_sao },
120  #endif
121  #if CONFIG_HUFFYUV_DECODER
122  { "huffyuvdsp", checkasm_check_huffyuvdsp },
123  #endif
124  #if CONFIG_JPEG2000_DECODER
125  { "jpeg2000dsp", checkasm_check_jpeg2000dsp },
126  #endif
127  #if CONFIG_HUFFYUVDSP
128  { "llviddsp", checkasm_check_llviddsp },
129  #endif
130  #if CONFIG_LLVIDENCDSP
131  { "llviddspenc", checkasm_check_llviddspenc },
132  #endif
133  #if CONFIG_OPUS_DECODER
134  { "opusdsp", checkasm_check_opusdsp },
135  #endif
136  #if CONFIG_PIXBLOCKDSP
137  { "pixblockdsp", checkasm_check_pixblockdsp },
138  #endif
139  #if CONFIG_UTVIDEO_DECODER
140  { "utvideodsp", checkasm_check_utvideodsp },
141  #endif
142  #if CONFIG_V210_DECODER
143  { "v210dec", checkasm_check_v210dec },
144  #endif
145  #if CONFIG_V210_ENCODER
146  { "v210enc", checkasm_check_v210enc },
147  #endif
148  #if CONFIG_VP8DSP
149  { "vp8dsp", checkasm_check_vp8dsp },
150  #endif
151  #if CONFIG_VP9_DECODER
152  { "vp9dsp", checkasm_check_vp9dsp },
153  #endif
154  #if CONFIG_VIDEODSP
155  { "videodsp", checkasm_check_videodsp },
156  #endif
157 #endif
158 #if CONFIG_AVFILTER
159  #if CONFIG_AFIR_FILTER
160  { "af_afir", checkasm_check_afir },
161  #endif
162  #if CONFIG_BLEND_FILTER
163  { "vf_blend", checkasm_check_blend },
164  #endif
165  #if CONFIG_COLORSPACE_FILTER
166  { "vf_colorspace", checkasm_check_colorspace },
167  #endif
168  #if CONFIG_EQ_FILTER
169  { "vf_eq", checkasm_check_vf_eq },
170  #endif
171  #if CONFIG_GBLUR_FILTER
172  { "vf_gblur", checkasm_check_vf_gblur },
173  #endif
174  #if CONFIG_HFLIP_FILTER
175  { "vf_hflip", checkasm_check_vf_hflip },
176  #endif
177  #if CONFIG_NLMEANS_FILTER
178  { "vf_nlmeans", checkasm_check_nlmeans },
179  #endif
180  #if CONFIG_THRESHOLD_FILTER
181  { "vf_threshold", checkasm_check_vf_threshold },
182  #endif
183 #endif
184 #if CONFIG_SWSCALE
185  { "sw_rgb", checkasm_check_sw_rgb },
186  { "sw_scale", checkasm_check_sw_scale },
187 #endif
188 #if CONFIG_AVUTIL
189  { "fixed_dsp", checkasm_check_fixed_dsp },
190  { "float_dsp", checkasm_check_float_dsp },
191 #endif
192  { NULL }
193 };
194 
195 /* List of cpu flags to check */
196 static const struct {
197  const char *name;
198  const char *suffix;
199  int flag;
200 } cpus[] = {
201 #if ARCH_AARCH64
202  { "ARMV8", "armv8", AV_CPU_FLAG_ARMV8 },
203  { "NEON", "neon", AV_CPU_FLAG_NEON },
204 #elif ARCH_ARM
205  { "ARMV5TE", "armv5te", AV_CPU_FLAG_ARMV5TE },
206  { "ARMV6", "armv6", AV_CPU_FLAG_ARMV6 },
207  { "ARMV6T2", "armv6t2", AV_CPU_FLAG_ARMV6T2 },
208  { "VFP", "vfp", AV_CPU_FLAG_VFP },
209  { "VFP_VM", "vfp_vm", AV_CPU_FLAG_VFP_VM },
210  { "VFPV3", "vfp3", AV_CPU_FLAG_VFPV3 },
211  { "NEON", "neon", AV_CPU_FLAG_NEON },
212 #elif ARCH_PPC
213  { "ALTIVEC", "altivec", AV_CPU_FLAG_ALTIVEC },
214  { "VSX", "vsx", AV_CPU_FLAG_VSX },
215  { "POWER8", "power8", AV_CPU_FLAG_POWER8 },
216 #elif ARCH_MIPS
217  { "MMI", "mmi", AV_CPU_FLAG_MMI },
218  { "MSA", "msa", AV_CPU_FLAG_MSA },
219 #elif ARCH_X86
220  { "MMX", "mmx", AV_CPU_FLAG_MMX|AV_CPU_FLAG_CMOV },
221  { "MMXEXT", "mmxext", AV_CPU_FLAG_MMXEXT },
222  { "3DNOW", "3dnow", AV_CPU_FLAG_3DNOW },
223  { "3DNOWEXT", "3dnowext", AV_CPU_FLAG_3DNOWEXT },
224  { "SSE", "sse", AV_CPU_FLAG_SSE },
225  { "SSE2", "sse2", AV_CPU_FLAG_SSE2|AV_CPU_FLAG_SSE2SLOW },
226  { "SSE3", "sse3", AV_CPU_FLAG_SSE3|AV_CPU_FLAG_SSE3SLOW },
227  { "SSSE3", "ssse3", AV_CPU_FLAG_SSSE3|AV_CPU_FLAG_ATOM },
228  { "SSE4.1", "sse4", AV_CPU_FLAG_SSE4 },
229  { "SSE4.2", "sse42", AV_CPU_FLAG_SSE42 },
230  { "AES-NI", "aesni", AV_CPU_FLAG_AESNI },
231  { "AVX", "avx", AV_CPU_FLAG_AVX },
232  { "XOP", "xop", AV_CPU_FLAG_XOP },
233  { "FMA3", "fma3", AV_CPU_FLAG_FMA3 },
234  { "FMA4", "fma4", AV_CPU_FLAG_FMA4 },
235  { "AVX2", "avx2", AV_CPU_FLAG_AVX2 },
236  { "AVX-512", "avx512", AV_CPU_FLAG_AVX512 },
237 #endif
238  { NULL }
239 };
240 
241 typedef struct CheckasmFuncVersion {
243  void *func;
244  int ok;
245  int cpu;
248 
249 /* Binary search tree node */
250 typedef struct CheckasmFunc {
251  struct CheckasmFunc *child[2];
253  uint8_t color; /* 0 = red, 1 = black */
254  char name[1];
255 } CheckasmFunc;
256 
257 /* Internal state */
258 static struct {
262  const char *current_test_name;
263  const char *bench_pattern;
267 
268  /* perf */
269  int nop_time;
270  int sysfd;
271 
272  int cpu_flag;
273  const char *cpu_flag_name;
274  const char *test_name;
275  int verbose;
276 } state;
277 
278 /* PRNG state */
280 
281 /* float compare support code */
282 static int is_negative(union av_intfloat32 u)
283 {
284  return u.i >> 31;
285 }
286 
287 int float_near_ulp(float a, float b, unsigned max_ulp)
288 {
289  union av_intfloat32 x, y;
290 
291  x.f = a;
292  y.f = b;
293 
294  if (is_negative(x) != is_negative(y)) {
295  // handle -0.0 == +0.0
296  return a == b;
297  }
298 
299  if (llabs((int64_t)x.i - y.i) <= max_ulp)
300  return 1;
301 
302  return 0;
303 }
304 
305 int float_near_ulp_array(const float *a, const float *b, unsigned max_ulp,
306  unsigned len)
307 {
308  unsigned i;
309 
310  for (i = 0; i < len; i++) {
311  if (!float_near_ulp(a[i], b[i], max_ulp))
312  return 0;
313  }
314  return 1;
315 }
316 
317 int float_near_abs_eps(float a, float b, float eps)
318 {
319  float abs_diff = fabsf(a - b);
320  if (abs_diff < eps)
321  return 1;
322 
323  fprintf(stderr, "test failed comparing %g with %g (abs diff=%g with EPS=%g)\n", a, b, abs_diff, eps);
324 
325  return 0;
326 }
327 
328 int float_near_abs_eps_array(const float *a, const float *b, float eps,
329  unsigned len)
330 {
331  unsigned i;
332 
333  for (i = 0; i < len; i++) {
334  if (!float_near_abs_eps(a[i], b[i], eps))
335  return 0;
336  }
337  return 1;
338 }
339 
340 int float_near_abs_eps_ulp(float a, float b, float eps, unsigned max_ulp)
341 {
342  return float_near_ulp(a, b, max_ulp) || float_near_abs_eps(a, b, eps);
343 }
344 
345 int float_near_abs_eps_array_ulp(const float *a, const float *b, float eps,
346  unsigned max_ulp, unsigned len)
347 {
348  unsigned i;
349 
350  for (i = 0; i < len; i++) {
351  if (!float_near_abs_eps_ulp(a[i], b[i], eps, max_ulp))
352  return 0;
353  }
354  return 1;
355 }
356 
357 int double_near_abs_eps(double a, double b, double eps)
358 {
359  double abs_diff = fabs(a - b);
360 
361  return abs_diff < eps;
362 }
363 
364 int double_near_abs_eps_array(const double *a, const double *b, double eps,
365  unsigned len)
366 {
367  unsigned i;
368 
369  for (i = 0; i < len; i++) {
370  if (!double_near_abs_eps(a[i], b[i], eps))
371  return 0;
372  }
373  return 1;
374 }
375 
376 /* Print colored text to stderr if the terminal supports it */
377 static void color_printf(int color, const char *fmt, ...)
378 {
379  static int use_color = -1;
380  va_list arg;
381 
382 #if HAVE_SETCONSOLETEXTATTRIBUTE && HAVE_GETSTDHANDLE
383  static HANDLE con;
384  static WORD org_attributes;
385 
386  if (use_color < 0) {
387  CONSOLE_SCREEN_BUFFER_INFO con_info;
388  con = GetStdHandle(STD_ERROR_HANDLE);
389  if (con && con != INVALID_HANDLE_VALUE && GetConsoleScreenBufferInfo(con, &con_info)) {
390  org_attributes = con_info.wAttributes;
391  use_color = 1;
392  } else
393  use_color = 0;
394  }
395  if (use_color)
396  SetConsoleTextAttribute(con, (org_attributes & 0xfff0) | (color & 0x0f));
397 #else
398  if (use_color < 0) {
399  const char *term = getenv("TERM");
400  use_color = term && strcmp(term, "dumb") && isatty(2);
401  }
402  if (use_color)
403  fprintf(stderr, "\x1b[%d;3%dm", (color & 0x08) >> 3, color & 0x07);
404 #endif
405 
406  va_start(arg, fmt);
407  vfprintf(stderr, fmt, arg);
408  va_end(arg);
409 
410  if (use_color) {
411 #if HAVE_SETCONSOLETEXTATTRIBUTE && HAVE_GETSTDHANDLE
412  SetConsoleTextAttribute(con, org_attributes);
413 #else
414  fprintf(stderr, "\x1b[0m");
415 #endif
416  }
417 }
418 
419 /* Deallocate a tree */
421 {
422  if (f) {
424  while (v) {
425  CheckasmFuncVersion *next = v->next;
426  free(v);
427  v = next;
428  }
429 
430  destroy_func_tree(f->child[0]);
431  destroy_func_tree(f->child[1]);
432  free(f);
433  }
434 }
435 
436 /* Allocate a zero-initialized block, clean up and exit on failure */
437 static void *checkasm_malloc(size_t size)
438 {
439  void *ptr = calloc(1, size);
440  if (!ptr) {
441  fprintf(stderr, "checkasm: malloc failed\n");
442  destroy_func_tree(state.funcs);
443  exit(1);
444  }
445  return ptr;
446 }
447 
448 /* Get the suffix of the specified cpu flag */
449 static const char *cpu_suffix(int cpu)
450 {
451  int i = FF_ARRAY_ELEMS(cpus);
452 
453  while (--i >= 0)
454  if (cpu & cpus[i].flag)
455  return cpus[i].suffix;
456 
457  return "c";
458 }
459 
460 static int cmp_nop(const void *a, const void *b)
461 {
462  return *(const uint16_t*)a - *(const uint16_t*)b;
463 }
464 
465 /* Measure the overhead of the timing code (in decicycles) */
466 static int measure_nop_time(void)
467 {
468  uint16_t nops[10000];
469  int i, nop_sum = 0;
470  av_unused const int sysfd = state.sysfd;
471 
472  uint64_t t = 0;
473  for (i = 0; i < 10000; i++) {
474  PERF_START(t);
475  PERF_STOP(t);
476  nops[i] = t;
477  }
478 
479  qsort(nops, 10000, sizeof(uint16_t), cmp_nop);
480  for (i = 2500; i < 7500; i++)
481  nop_sum += nops[i];
482 
483  return nop_sum / 500;
484 }
485 
486 /* Print benchmark results */
488 {
489  if (f) {
490  print_benchs(f->child[0]);
491 
492  /* Only print functions with at least one assembly version */
493  if (f->versions.cpu || f->versions.next) {
494  CheckasmFuncVersion *v = &f->versions;
495  do {
496  CheckasmPerf *p = &v->perf;
497  if (p->iterations) {
498  int decicycles = (10*p->cycles/p->iterations - state.nop_time) / 4;
499  printf("%s_%s: %d.%d\n", f->name, cpu_suffix(v->cpu), decicycles/10, decicycles%10);
500  }
501  } while ((v = v->next));
502  }
503 
504  print_benchs(f->child[1]);
505  }
506 }
507 
508 /* ASCIIbetical sort except preserving natural order for numbers */
509 static int cmp_func_names(const char *a, const char *b)
510 {
511  const char *start = a;
512  int ascii_diff, digit_diff;
513 
514  for (; !(ascii_diff = *(const unsigned char*)a - *(const unsigned char*)b) && *a; a++, b++);
515  for (; av_isdigit(*a) && av_isdigit(*b); a++, b++);
516 
517  if (a > start && av_isdigit(a[-1]) && (digit_diff = av_isdigit(*a) - av_isdigit(*b)))
518  return digit_diff;
519 
520  return ascii_diff;
521 }
522 
523 /* Perform a tree rotation in the specified direction and return the new root */
525 {
526  CheckasmFunc *r = f->child[dir^1];
527  f->child[dir^1] = r->child[dir];
528  r->child[dir] = f;
529  r->color = f->color;
530  f->color = 0;
531  return r;
532 }
533 
534 #define is_red(f) ((f) && !(f)->color)
535 
536 /* Balance a left-leaning red-black tree at the specified node */
537 static void balance_tree(CheckasmFunc **root)
538 {
539  CheckasmFunc *f = *root;
540 
541  if (is_red(f->child[0]) && is_red(f->child[1])) {
542  f->color ^= 1;
543  f->child[0]->color = f->child[1]->color = 1;
544  }
545 
546  if (!is_red(f->child[0]) && is_red(f->child[1]))
547  *root = rotate_tree(f, 0); /* Rotate left */
548  else if (is_red(f->child[0]) && is_red(f->child[0]->child[0]))
549  *root = rotate_tree(f, 1); /* Rotate right */
550 }
551 
552 /* Get a node with the specified name, creating it if it doesn't exist */
553 static CheckasmFunc *get_func(CheckasmFunc **root, const char *name)
554 {
555  CheckasmFunc *f = *root;
556 
557  if (f) {
558  /* Search the tree for a matching node */
559  int cmp = cmp_func_names(name, f->name);
560  if (cmp) {
561  f = get_func(&f->child[cmp > 0], name);
562 
563  /* Rebalance the tree on the way up if a new node was inserted */
564  if (!f->versions.func)
565  balance_tree(root);
566  }
567  } else {
568  /* Allocate and insert a new node into the tree */
569  int name_length = strlen(name);
570  f = *root = checkasm_malloc(sizeof(CheckasmFunc) + name_length);
571  memcpy(f->name, name, name_length + 1);
572  }
573 
574  return f;
575 }
576 
577 /* Perform tests and benchmarks for the specified cpu flag if supported by the host */
578 static void check_cpu_flag(const char *name, int flag)
579 {
580  int old_cpu_flag = state.cpu_flag;
581 
582  flag |= old_cpu_flag;
583  av_force_cpu_flags(-1);
584  state.cpu_flag = flag & av_get_cpu_flags();
585  av_force_cpu_flags(state.cpu_flag);
586 
587  if (!flag || state.cpu_flag != old_cpu_flag) {
588  int i;
589 
590  state.cpu_flag_name = name;
591  for (i = 0; tests[i].func; i++) {
592  if (state.test_name && strcmp(tests[i].name, state.test_name))
593  continue;
594  state.current_test_name = tests[i].name;
595  tests[i].func();
596  }
597  }
598 }
599 
600 /* Print the name of the current CPU flag, but only do it once */
601 static void print_cpu_name(void)
602 {
603  if (state.cpu_flag_name) {
604  color_printf(COLOR_YELLOW, "%s:\n", state.cpu_flag_name);
605  state.cpu_flag_name = NULL;
606  }
607 }
608 
609 #if CONFIG_LINUX_PERF
610 static int bench_init_linux(void)
611 {
612  struct perf_event_attr attr = {
613  .type = PERF_TYPE_HARDWARE,
614  .size = sizeof(struct perf_event_attr),
615  .config = PERF_COUNT_HW_CPU_CYCLES,
616  .disabled = 1, // start counting only on demand
617  .exclude_kernel = 1,
618  .exclude_hv = 1,
619  };
620 
621  printf("benchmarking with Linux Perf Monitoring API\n");
622 
623  state.sysfd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0);
624  if (state.sysfd == -1) {
625  perror("syscall");
626  return -1;
627  }
628  return 0;
629 }
630 #endif
631 
632 #if !CONFIG_LINUX_PERF
633 static int bench_init_ffmpeg(void)
634 {
635 #ifdef AV_READ_TIME
636  printf("benchmarking with native FFmpeg timers\n");
637  return 0;
638 #else
639  fprintf(stderr, "checkasm: --bench is not supported on your system\n");
640  return -1;
641 #endif
642 }
643 #endif
644 
645 static int bench_init(void)
646 {
647 #if CONFIG_LINUX_PERF
648  int ret = bench_init_linux();
649 #else
650  int ret = bench_init_ffmpeg();
651 #endif
652  if (ret < 0)
653  return ret;
654 
655  state.nop_time = measure_nop_time();
656  printf("nop: %d.%d\n", state.nop_time/10, state.nop_time%10);
657  return 0;
658 }
659 
660 static void bench_uninit(void)
661 {
662 #if CONFIG_LINUX_PERF
663  if (state.sysfd > 0)
664  close(state.sysfd);
665 #endif
666 }
667 
668 int main(int argc, char *argv[])
669 {
670  unsigned int seed = av_get_random_seed();
671  int i, ret = 0;
672 
673 #if ARCH_ARM && HAVE_ARMV5TE_EXTERNAL
675  checkasm_checked_call = checkasm_checked_call_vfp;
676 #endif
677 
678  if (!tests[0].func || !cpus[0].flag) {
679  fprintf(stderr, "checkasm: no tests to perform\n");
680  return 0;
681  }
682 
683  while (argc > 1) {
684  if (!strncmp(argv[1], "--bench", 7)) {
685  if (bench_init() < 0)
686  return 1;
687  if (argv[1][7] == '=') {
688  state.bench_pattern = argv[1] + 8;
689  state.bench_pattern_len = strlen(state.bench_pattern);
690  } else
691  state.bench_pattern = "";
692  } else if (!strncmp(argv[1], "--test=", 7)) {
693  state.test_name = argv[1] + 7;
694  } else if (!strcmp(argv[1], "--verbose") || !strcmp(argv[1], "-v")) {
695  state.verbose = 1;
696  } else {
697  seed = strtoul(argv[1], NULL, 10);
698  }
699 
700  argc--;
701  argv++;
702  }
703 
704  fprintf(stderr, "checkasm: using random seed %u\n", seed);
705  av_lfg_init(&checkasm_lfg, seed);
706 
707  check_cpu_flag(NULL, 0);
708  for (i = 0; cpus[i].flag; i++)
709  check_cpu_flag(cpus[i].name, cpus[i].flag);
710 
711  if (state.num_failed) {
712  fprintf(stderr, "checkasm: %d of %d tests have failed\n", state.num_failed, state.num_checked);
713  ret = 1;
714  } else {
715  fprintf(stderr, "checkasm: all %d tests passed\n", state.num_checked);
716  if (state.bench_pattern) {
717  print_benchs(state.funcs);
718  }
719  }
720 
721  destroy_func_tree(state.funcs);
722  bench_uninit();
723  return ret;
724 }
725 
726 /* Decide whether or not the specified function needs to be tested and
727  * allocate/initialize data structures if needed. Returns a pointer to a
728  * reference function if the function should be tested, otherwise NULL */
729 void *checkasm_check_func(void *func, const char *name, ...)
730 {
731  char name_buf[256];
732  void *ref = func;
734  int name_length;
735  va_list arg;
736 
737  va_start(arg, name);
738  name_length = vsnprintf(name_buf, sizeof(name_buf), name, arg);
739  va_end(arg);
740 
741  if (!func || name_length <= 0 || name_length >= sizeof(name_buf))
742  return NULL;
743 
744  state.current_func = get_func(&state.funcs, name_buf);
745  state.funcs->color = 1;
746  v = &state.current_func->versions;
747 
748  if (v->func) {
749  CheckasmFuncVersion *prev;
750  do {
751  /* Only test functions that haven't already been tested */
752  if (v->func == func)
753  return NULL;
754 
755  if (v->ok)
756  ref = v->func;
757 
758  prev = v;
759  } while ((v = v->next));
760 
761  v = prev->next = checkasm_malloc(sizeof(CheckasmFuncVersion));
762  }
763 
764  v->func = func;
765  v->ok = 1;
766  v->cpu = state.cpu_flag;
767  state.current_func_ver = v;
768 
769  if (state.cpu_flag)
770  state.num_checked++;
771 
772  return ref;
773 }
774 
775 /* Decide whether or not the current function needs to be benchmarked */
777 {
778  return !state.num_failed && state.bench_pattern &&
779  !strncmp(state.current_func->name, state.bench_pattern, state.bench_pattern_len);
780 }
781 
782 /* Indicate that the current test has failed */
783 void checkasm_fail_func(const char *msg, ...)
784 {
785  if (state.current_func_ver->cpu && state.current_func_ver->ok) {
786  va_list arg;
787 
788  print_cpu_name();
789  fprintf(stderr, " %s_%s (", state.current_func->name, cpu_suffix(state.current_func_ver->cpu));
790  va_start(arg, msg);
791  vfprintf(stderr, msg, arg);
792  va_end(arg);
793  fprintf(stderr, ")\n");
794 
795  state.current_func_ver->ok = 0;
796  state.num_failed++;
797  }
798 }
799 
800 /* Get the benchmark context of the current function */
802 {
803  CheckasmPerf *perf = &state.current_func_ver->perf;
804  memset(perf, 0, sizeof(*perf));
805  perf->sysfd = state.sysfd;
806  return perf;
807 }
808 
809 /* Print the outcome of all tests performed since the last time this function was called */
810 void checkasm_report(const char *name, ...)
811 {
812  static int prev_checked, prev_failed, max_length;
813 
814  if (state.num_checked > prev_checked) {
815  int pad_length = max_length + 4;
816  va_list arg;
817 
818  print_cpu_name();
819  pad_length -= fprintf(stderr, " - %s.", state.current_test_name);
820  va_start(arg, name);
821  pad_length -= vfprintf(stderr, name, arg);
822  va_end(arg);
823  fprintf(stderr, "%*c", FFMAX(pad_length, 0) + 2, '[');
824 
825  if (state.num_failed == prev_failed)
826  color_printf(COLOR_GREEN, "OK");
827  else
828  color_printf(COLOR_RED, "FAILED");
829  fprintf(stderr, "]\n");
830 
831  prev_checked = state.num_checked;
832  prev_failed = state.num_failed;
833  } else if (!state.cpu_flag) {
834  /* Calculate the amount of padding required to make the output vertically aligned */
835  int length = strlen(state.current_test_name);
836  va_list arg;
837 
838  va_start(arg, name);
839  length += vsnprintf(NULL, 0, name, arg);
840  va_end(arg);
841 
842  if (length > max_length)
843  max_length = length;
844  }
845 }
846 
847 #define DEF_CHECKASM_CHECK_FUNC(type, fmt) \
848 int checkasm_check_##type(const char *const file, const int line, \
849  const type *buf1, ptrdiff_t stride1, \
850  const type *buf2, ptrdiff_t stride2, \
851  const int w, int h, const char *const name) \
852 { \
853  int y = 0; \
854  stride1 /= sizeof(*buf1); \
855  stride2 /= sizeof(*buf2); \
856  for (y = 0; y < h; y++) \
857  if (memcmp(&buf1[y*stride1], &buf2[y*stride2], w*sizeof(*buf1))) \
858  break; \
859  if (y == h) \
860  return 0; \
861  checkasm_fail_func("%s:%d", file, line); \
862  if (!state.verbose) \
863  return 1; \
864  fprintf(stderr, "%s:\n", name); \
865  while (h--) { \
866  for (int x = 0; x < w; x++) \
867  fprintf(stderr, " " fmt, buf1[x]); \
868  fprintf(stderr, " "); \
869  for (int x = 0; x < w; x++) \
870  fprintf(stderr, " " fmt, buf2[x]); \
871  fprintf(stderr, " "); \
872  for (int x = 0; x < w; x++) \
873  fprintf(stderr, "%c", buf1[x] != buf2[x] ? 'x' : '.'); \
874  buf1 += stride1; \
875  buf2 += stride2; \
876  fprintf(stderr, "\n"); \
877  } \
878  return 1; \
879 }
880 
882 DEF_CHECKASM_CHECK_FUNC(uint16_t, "%04x")
883 DEF_CHECKASM_CHECK_FUNC(int16_t, "%6d")
Context structure for the Lagged Fibonacci PRNG.
Definition: lfg.h:33
const char * name
Definition: checkasm.c:72
static int bench_init_ffmpeg(void)
Definition: checkasm.c:633
static int cmp_nop(const void *a, const void *b)
Definition: checkasm.c:460
#define AV_CPU_FLAG_AVX
AVX functions: requires OS support even if YMM registers aren&#39;t used.
Definition: cpu.h:49
#define AV_CPU_FLAG_MSA
Definition: cpu.h:75
#define NULL
Definition: coverity.c:32
#define AV_CPU_FLAG_ALTIVEC
standard
Definition: cpu.h:60
void checkasm_check_v210enc(void)
Definition: v210enc.c:81
static int measure_nop_time(void)
Definition: checkasm.c:466
static void bench_uninit(void)
Definition: checkasm.c:660
static av_const int av_isdigit(int c)
Locale-independent conversion of ASCII isdigit.
Definition: avstring.h:211
CheckasmFunc * current_func
Definition: checkasm.c:260
int float_near_abs_eps_array(const float *a, const float *b, float eps, unsigned len)
Definition: checkasm.c:328
static int is_negative(union av_intfloat32 u)
Definition: checkasm.c:282
uint64_t cycles
Definition: checkasm.h:213
CheckasmPerf * checkasm_get_perf_context(void)
Definition: checkasm.c:801
#define AV_CPU_FLAG_SSE
SSE functions.
Definition: cpu.h:35
void checkasm_check_vp9dsp(void)
Definition: vp9dsp.c:625
void checkasm_check_vf_threshold(void)
Definition: vf_threshold.c:78
#define vsnprintf
Definition: snprintf.h:36
void checkasm_check_flacdsp(void)
Definition: flacdsp.c:56
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:36
#define AV_CPU_FLAG_CMOV
supports cmov instruction
Definition: cpu.h:53
void checkasm_check_h264pred(void)
Definition: h264pred.c:232
#define AV_CPU_FLAG_VFP
Definition: cpu.h:67
static const char * cpu_suffix(int cpu)
Definition: checkasm.c:449
void checkasm_check_afir(void)
Definition: af_afir.c:79
CheckasmFuncVersion * current_func_ver
Definition: checkasm.c:261
const char * test_name
Definition: checkasm.c:274
void checkasm_check_alacdsp(void)
Definition: alacdsp.c:115
static int cmp_func_names(const char *a, const char *b)
Definition: checkasm.c:509
static const struct @321 cpus[]
int float_near_abs_eps(float a, float b, float eps)
Definition: checkasm.c:317
void * checkasm_check_func(void *func, const char *name,...)
Definition: checkasm.c:729
void checkasm_check_aacpsdsp(void)
Definition: aacpsdsp.c:233
CheckasmPerf perf
Definition: checkasm.c:246
void checkasm_check_colorspace(void)
void checkasm_check_vp8dsp(void)
Definition: vp8dsp.c:507
struct CheckasmFunc * child[2]
Definition: checkasm.c:251
uint8_t
const char * cpu_flag_name
Definition: checkasm.c:273
int checkasm_bench_func(void)
Definition: checkasm.c:776
#define AV_CPU_FLAG_FMA3
Haswell FMA3 functions.
Definition: cpu.h:55
char name[1]
Definition: checkasm.c:254
#define AV_CPU_FLAG_NEON
Definition: cpu.h:69
#define AV_CPU_FLAG_MMXEXT
SSE integer functions or AMD MMX ext.
Definition: cpu.h:32
int main(int argc, char *argv[])
Definition: checkasm.c:668
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:262
#define AV_CPU_FLAG_ATOM
Atom processor, some SSSE3 instructions are slower.
Definition: cpu.h:45
#define COLOR_YELLOW
Definition: checkasm.c:53
void checkasm_check_sbrdsp(void)
Definition: sbrdsp.c:252
#define AV_CPU_FLAG_AVX2
AVX2 functions: requires OS support even if YMM registers aren&#39;t used.
Definition: cpu.h:54
#define AV_CPU_FLAG_SSE2SLOW
SSE2 supported, but usually not faster.
Definition: cpu.h:37
void checkasm_check_h264qpel(void)
Definition: h264qpel.c:50
ptrdiff_t size
Definition: opengl_enc.c:100
CheckasmFunc * funcs
Definition: checkasm.c:259
void checkasm_check_jpeg2000dsp(void)
Definition: jpeg2000dsp.c:91
#define AV_CPU_FLAG_XOP
Bulldozer XOP functions.
Definition: cpu.h:51
void checkasm_check_llviddspenc(void)
Definition: llviddspenc.c:104
int flag
Definition: checkasm.c:199
#define AV_CPU_FLAG_SSE42
Nehalem SSE4.2 functions.
Definition: cpu.h:47
#define isatty(fd)
Definition: checkasm.c:61
static CheckasmFunc * get_func(CheckasmFunc **root, const char *name)
Definition: checkasm.c:553
CheckasmFuncVersion versions
Definition: checkasm.c:252
const char * suffix
Definition: checkasm.c:198
#define AV_CPU_FLAG_SSSE3
Conroe SSSE3 functions.
Definition: cpu.h:43
int float_near_ulp(float a, float b, unsigned max_ulp)
Definition: checkasm.c:287
void checkasm_check_h264dsp(void)
Definition: h264dsp.c:441
int num_checked
Definition: checkasm.c:265
#define COLOR_GREEN
Definition: checkasm.c:52
const char * r
Definition: vf_curves.c:114
const char * arg
Definition: jacosubdec.c:66
#define AV_CPU_FLAG_ARMV6T2
Definition: cpu.h:66
GLsizei GLsizei * length
Definition: opengl_enc.c:114
void checkasm_check_hevc_add_res(void)
Definition: hevc_add_res.c:85
static int bench_init(void)
Definition: checkasm.c:645
AVLFG checkasm_lfg
Definition: checkasm.c:279
#define AV_CPU_FLAG_AVX512
AVX-512 functions: requires OS support even if YMM/ZMM registers aren&#39;t used.
Definition: cpu.h:58
#define AV_CPU_FLAG_VFP_VM
VFPv2 vector mode, deprecated in ARMv7-A and unavailable in various CPUs implementations.
Definition: cpu.h:71
#define FFMAX(a, b)
Definition: common.h:94
void checkasm_check_videodsp(void)
Definition: videodsp.c:80
#define AV_CPU_FLAG_ARMV5TE
Definition: cpu.h:64
void checkasm_check_fixed_dsp(void)
Definition: fixed_dsp.c:132
#define b
Definition: input.c:41
static void print_benchs(CheckasmFunc *f)
Definition: checkasm.c:487
#define have_neon(flags)
Definition: cpu.h:26
void checkasm_check_utvideodsp(void)
Definition: utvideodsp.c:90
static struct @322 state
void checkasm_check_audiodsp(void)
Definition: audiodsp.c:51
#define AV_CPU_FLAG_SSE3
Prescott SSE3 functions.
Definition: cpu.h:40
void checkasm_check_nlmeans(void)
Definition: vf_nlmeans.c:31
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
void checkasm_check_sw_rgb(void)
Definition: sw_rgb.c:182
#define is_red(f)
Definition: checkasm.c:534
int32_t
void checkasm_check_exrdsp(void)
Definition: exrdsp.c:76
uint32_t i
Definition: intfloat.h:28
static av_always_inline int cmp(MpegEncContext *s, const int x, const int y, const int subx, const int suby, const int size, const int h, int ref_index, int src_index, me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags)
compares a block (either a full macroblock or a partition thereof) against a proposed motion-compensa...
Definition: motion_est.c:260
int dummy
Definition: motion.c:64
#define AV_CPU_FLAG_VFPV3
Definition: cpu.h:68
static void * checkasm_malloc(size_t size)
Definition: checkasm.c:437
const char * bench_pattern
Definition: checkasm.c:263
int iterations
Definition: checkasm.h:214
int bench_pattern_len
Definition: checkasm.c:264
void checkasm_report(const char *name,...)
Definition: checkasm.c:810
#define DEF_CHECKASM_CHECK_FUNC(type, fmt)
Definition: checkasm.c:847
void checkasm_check_hevc_idct(void)
Definition: hevc_idct.c:86
#define FF_ARRAY_ELEMS(a)
int cpu_flag
Definition: checkasm.c:272
int float_near_ulp_array(const float *a, const float *b, unsigned max_ulp, unsigned len)
Definition: checkasm.c:305
#define AV_CPU_FLAG_3DNOW
AMD 3DNOW.
Definition: cpu.h:34
int nop_time
Definition: checkasm.c:269
int double_near_abs_eps(double a, double b, double eps)
Definition: checkasm.c:357
void checkasm_check_vf_eq(void)
Definition: vf_eq.c:75
#define AV_CPU_FLAG_ARMV6
Definition: cpu.h:65
int num_failed
Definition: checkasm.c:266
#define AV_CPU_FLAG_MMI
Definition: cpu.h:74
#define AV_CPU_FLAG_SSE3SLOW
SSE3 supported, but usually not faster.
Definition: cpu.h:41
#define COLOR_RED
Definition: checkasm.c:51
void(* func)(void)
Definition: checkasm.c:73
#define AV_CPU_FLAG_MMX
standard MMX
Definition: cpu.h:31
static unsigned int seed
Definition: videogen.c:78
void checkasm_check_fmtconvert(void)
Definition: fmtconvert.c:44
#define PERF_START(t)
Definition: checkasm.h:262
static CheckasmFunc * rotate_tree(CheckasmFunc *f, int dir)
Definition: checkasm.c:524
int verbose
Definition: checkasm.c:275
#define AV_CPU_FLAG_FMA4
Bulldozer FMA4 functions.
Definition: cpu.h:52
#define AV_CPU_FLAG_SSE4
Penryn SSE4.1 functions.
Definition: cpu.h:46
#define AV_CPU_FLAG_AESNI
Advanced Encryption Standard functions.
Definition: cpu.h:48
#define AV_CPU_FLAG_VSX
ISA 2.06.
Definition: cpu.h:61
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:32
int sysfd
Definition: checkasm.c:270
void checkasm_check_llviddsp(void)
Definition: llviddsp.c:195
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition: cpu.c:95
int double_near_abs_eps_array(const double *a, const double *b, double eps, unsigned len)
Definition: checkasm.c:364
static const struct @320 tests[]
static void color_printf(int color, const char *fmt,...)
Definition: checkasm.c:377
#define AV_CPU_FLAG_3DNOWEXT
AMD 3DNowExt.
Definition: cpu.h:39
void checkasm_check_float_dsp(void)
Definition: float_dsp.c:280
#define AV_CPU_FLAG_POWER8
ISA 2.07.
Definition: cpu.h:62
void checkasm_check_huffyuvdsp(void)
Definition: huffyuvdsp.c:67
void checkasm_check_vf_gblur(void)
Definition: vf_gblur.c:36
void checkasm_check_opusdsp(void)
Definition: opusdsp.c:83
int float_near_abs_eps_ulp(float a, float b, float eps, unsigned max_ulp)
Definition: checkasm.c:340
static void destroy_func_tree(CheckasmFunc *f)
Definition: checkasm.c:420
common internal and external API header
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
struct CheckasmFuncVersion * next
Definition: checkasm.c:242
#define AV_CPU_FLAG_ARMV8
Definition: cpu.h:70
const char * current_test_name
Definition: checkasm.c:262
static void check_cpu_flag(const char *name, int flag)
Definition: checkasm.c:578
static int use_color
Definition: log.c:121
void checkasm_check_v210dec(void)
Definition: v210dec.c:47
#define have_vfp(flags)
Definition: cpu.h:27
int len
printf("static const uint8_t my_array[100] = {\n")
void checkasm_check_pixblockdsp(void)
Definition: pixblockdsp.c:81
void checkasm_check_hevc_sao(void)
Definition: hevc_sao.c:131
#define PERF_STOP(t)
Definition: checkasm.h:263
void checkasm_check_synth_filter(void)
Definition: synth_filter.c:44
int float_near_abs_eps_array_ulp(const float *a, const float *b, float eps, unsigned max_ulp, unsigned len)
Definition: checkasm.c:345
#define AV_CPU_FLAG_SSE2
PIV SSE2 functions.
Definition: cpu.h:36
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:120
void checkasm_fail_func(const char *msg,...)
Definition: checkasm.c:783
void checkasm_check_bswapdsp(void)
Definition: bswapdsp.c:59
void av_force_cpu_flags(int arg)
Disables cpu detection and forces the specified flags.
Definition: cpu.c:67
void checkasm_check_g722dsp(void)
Definition: g722dsp.c:53
void checkasm_check_sw_scale(void)
Definition: sw_scale.c:130
static void balance_tree(CheckasmFunc **root)
Definition: checkasm.c:537
void checkasm_check_vf_hflip(void)
Definition: vf_hflip.c:70
void checkasm_check_blend(void)
Definition: vf_blend.c:88
#define av_unused
Definition: attributes.h:131
const char * name
Definition: opengl_enc.c:102
static void print_cpu_name(void)
Definition: checkasm.c:601
void checkasm_check_blockdsp(void)
Definition: blockdsp.c:54
uint8_t color
Definition: checkasm.c:253