avfilter/avfiltergraph: fix constant string comparision
[ffmpeg.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 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 * Copyright © 2018, VideoLAN and dav1d authors
23 * Copyright © 2018, Two Orioles, LLC
24 * All rights reserved.
25 *
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions are met:
28 *
29 * 1. Redistributions of source code must retain the above copyright notice, this
30 * list of conditions and the following disclaimer.
31 *
32 * 2. Redistributions in binary form must reproduce the above copyright notice,
33 * this list of conditions and the following disclaimer in the documentation
34 * and/or other materials provided with the distribution.
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
38 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
40 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
41 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
45 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 */
47
48 #include "config.h"
49 #include "config_components.h"
50
51 #ifndef _GNU_SOURCE
52 # define _GNU_SOURCE // for syscall (performance monitoring API), strsignal()
53 #endif
54
55 #include <signal.h>
56 #include <stdarg.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include "checkasm.h"
61 #include "libavutil/avassert.h"
62 #include "libavutil/common.h"
63 #include "libavutil/cpu.h"
64 #include "libavutil/intfloat.h"
65 #include "libavutil/random_seed.h"
66
67 #if HAVE_IO_H
68 #include <io.h>
69 #endif
70 #if HAVE_PRCTL
71 #include <sys/prctl.h>
72 #endif
73
74 #if defined(_WIN32) && !defined(SIGBUS)
75 /* non-standard, use the same value as mingw-w64 */
76 #define SIGBUS 10
77 #endif
78
79 #if HAVE_SETCONSOLETEXTATTRIBUTE && HAVE_GETSTDHANDLE
80 #include <windows.h>
81 #define COLOR_RED FOREGROUND_RED
82 #define COLOR_GREEN FOREGROUND_GREEN
83 #define COLOR_YELLOW (FOREGROUND_RED|FOREGROUND_GREEN)
84 #else
85 #define COLOR_RED 1
86 #define COLOR_GREEN 2
87 #define COLOR_YELLOW 3
88 #endif
89
90 #if HAVE_UNISTD_H
91 #include <unistd.h>
92 #endif
93
94 #if !HAVE_ISATTY
95 #define isatty(fd) 1
96 #endif
97
98 #if ARCH_AARCH64
99 #include "libavutil/aarch64/cpu.h"
100 #elif ARCH_RISCV
101 #include "libavutil/riscv/cpu.h"
102 #endif
103
104 #if ARCH_ARM && HAVE_ARMV5TE_EXTERNAL
105 #include "libavutil/arm/cpu.h"
106
107 void (*checkasm_checked_call)(void *func, int dummy, ...) = checkasm_checked_call_novfp;
108 #endif
109
110 /* Trade-off between speed and accuracy */
111 uint64_t bench_runs = 1U << 10;
112
113 /* List of tests to invoke */
114 static const struct {
115 const char *name;
116 void (*func)(void);
117 } tests[] = {
118 #if CONFIG_AVCODEC
119 #if CONFIG_AAC_DECODER
120 { "aacpsdsp", checkasm_check_aacpsdsp },
121 { "sbrdsp", checkasm_check_sbrdsp },
122 #endif
123 #if CONFIG_AAC_ENCODER
124 { "aacencdsp", checkasm_check_aacencdsp },
125 #endif
126 #if CONFIG_AC3DSP
127 { "ac3dsp", checkasm_check_ac3dsp },
128 #endif
129 #if CONFIG_ALAC_DECODER
130 { "alacdsp", checkasm_check_alacdsp },
131 #endif
132 #if CONFIG_APV_DECODER
133 { "apv_dsp", checkasm_check_apv_dsp },
134 #endif
135 #if CONFIG_AUDIODSP
136 { "audiodsp", checkasm_check_audiodsp },
137 #endif
138 #if CONFIG_BLOCKDSP
139 { "blockdsp", checkasm_check_blockdsp },
140 #endif
141 #if CONFIG_BSWAPDSP
142 { "bswapdsp", checkasm_check_bswapdsp },
143 #endif
144 #if CONFIG_CAVS_DECODER
145 { "cavsdsp", checkasm_check_cavsdsp },
146 #endif
147 #if CONFIG_DCA_DECODER
148 { "dcadsp", checkasm_check_dcadsp },
149 { "synth_filter", checkasm_check_synth_filter },
150 #endif
151 #if CONFIG_DIRAC_DECODER
152 { "diracdsp", checkasm_check_diracdsp },
153 #endif
154 #if CONFIG_EXR_DECODER
155 { "exrdsp", checkasm_check_exrdsp },
156 #endif
157 #if CONFIG_FDCTDSP
158 { "fdctdsp", checkasm_check_fdctdsp },
159 #endif
160 #if CONFIG_FLAC_DECODER
161 { "flacdsp", checkasm_check_flacdsp },
162 #endif
163 #if CONFIG_FMTCONVERT
164 { "fmtconvert", checkasm_check_fmtconvert },
165 #endif
166 #if CONFIG_G722DSP
167 { "g722dsp", checkasm_check_g722dsp },
168 #endif
169 #if CONFIG_H263DSP
170 { "h263dsp", checkasm_check_h263dsp },
171 #endif
172 #if CONFIG_H264CHROMA
173 { "h264chroma", checkasm_check_h264chroma },
174 #endif
175 #if CONFIG_H264DSP
176 { "h264dsp", checkasm_check_h264dsp },
177 #endif
178 #if CONFIG_H264PRED
179 { "h264pred", checkasm_check_h264pred },
180 #endif
181 #if CONFIG_H264QPEL
182 { "h264qpel", checkasm_check_h264qpel },
183 #endif
184 #if CONFIG_HEVC_DECODER
185 { "hevc_add_res", checkasm_check_hevc_add_res },
186 { "hevc_deblock", checkasm_check_hevc_deblock },
187 { "hevc_idct", checkasm_check_hevc_idct },
188 { "hevc_pel", checkasm_check_hevc_pel },
189 { "hevc_sao", checkasm_check_hevc_sao },
190 #endif
191 #if CONFIG_HPELDSP
192 { "hpeldsp", checkasm_check_hpeldsp },
193 #endif
194 #if CONFIG_HUFFYUV_DECODER
195 { "huffyuvdsp", checkasm_check_huffyuvdsp },
196 #endif
197 #if CONFIG_IDCTDSP
198 { "idctdsp", checkasm_check_idctdsp },
199 #endif
200 #if CONFIG_JPEG2000_DECODER
201 { "jpeg2000dsp", checkasm_check_jpeg2000dsp },
202 #endif
203 #if CONFIG_LLAUDDSP
204 { "llauddsp", checkasm_check_llauddsp },
205 #endif
206 #if CONFIG_HUFFYUVDSP
207 { "llviddsp", checkasm_check_llviddsp },
208 #endif
209 #if CONFIG_LLVIDENCDSP
210 { "llviddspenc", checkasm_check_llviddspenc },
211 #endif
212 #if CONFIG_LPC
213 { "lpc", checkasm_check_lpc },
214 #endif
215 #if CONFIG_ME_CMP
216 { "motion", checkasm_check_motion },
217 #endif
218 #if CONFIG_MPEGVIDEOENCDSP
219 { "mpegvideoencdsp", checkasm_check_mpegvideoencdsp },
220 #endif
221 #if CONFIG_OPUS_DECODER
222 { "opusdsp", checkasm_check_opusdsp },
223 #endif
224 #if CONFIG_PIXBLOCKDSP
225 { "pixblockdsp", checkasm_check_pixblockdsp },
226 #endif
227 #if CONFIG_QPELDSP
228 { "qpeldsp", checkasm_check_qpeldsp },
229 #endif
230 #if CONFIG_RV34DSP
231 { "rv34dsp", checkasm_check_rv34dsp },
232 #endif
233 #if CONFIG_RV40_DECODER
234 { "rv40dsp", checkasm_check_rv40dsp },
235 #endif
236 #if CONFIG_SVQ1_ENCODER
237 { "svq1enc", checkasm_check_svq1enc },
238 #endif
239 #if CONFIG_TAK_DECODER
240 { "takdsp", checkasm_check_takdsp },
241 #endif
242 #if CONFIG_UTVIDEO_DECODER
243 { "utvideodsp", checkasm_check_utvideodsp },
244 #endif
245 #if CONFIG_V210_DECODER
246 { "v210dec", checkasm_check_v210dec },
247 #endif
248 #if CONFIG_V210_ENCODER
249 { "v210enc", checkasm_check_v210enc },
250 #endif
251 #if CONFIG_VC1DSP
252 { "vc1dsp", checkasm_check_vc1dsp },
253 #endif
254 #if CONFIG_VP3DSP
255 { "vp3dsp", checkasm_check_vp3dsp },
256 #endif
257 #if CONFIG_VP6_DECODER
258 { "vp6dsp", checkasm_check_vp6dsp },
259 #endif
260 #if CONFIG_VP8DSP
261 { "vp8dsp", checkasm_check_vp8dsp },
262 #endif
263 #if CONFIG_VP9_DECODER
264 { "vp9dsp", checkasm_check_vp9dsp },
265 #endif
266 #if CONFIG_VIDEODSP
267 { "videodsp", checkasm_check_videodsp },
268 #endif
269 #if CONFIG_VORBIS_DECODER
270 { "vorbisdsp", checkasm_check_vorbisdsp },
271 #endif
272 #if CONFIG_VVC_DECODER
273 { "vvc_alf", checkasm_check_vvc_alf },
274 { "vvc_mc", checkasm_check_vvc_mc },
275 { "vvc_sao", checkasm_check_vvc_sao },
276 #endif
277 #endif
278 #if CONFIG_AVFILTER
279 #if CONFIG_SCENE_SAD
280 { "scene_sad", checkasm_check_scene_sad },
281 #endif
282 #if CONFIG_AFIR_FILTER
283 { "af_afir", checkasm_check_afir },
284 #endif
285 #if CONFIG_BLACKDETECT_FILTER
286 { "vf_blackdetect", checkasm_check_blackdetect },
287 #endif
288 #if CONFIG_BLEND_FILTER
289 { "vf_blend", checkasm_check_blend },
290 #endif
291 #if CONFIG_BWDIF_FILTER
292 { "vf_bwdif", checkasm_check_vf_bwdif },
293 #endif
294 #if CONFIG_COLORDETECT_FILTER
295 { "vf_colordetect", checkasm_check_colordetect },
296 #endif
297 #if CONFIG_COLORSPACE_FILTER
298 { "vf_colorspace", checkasm_check_colorspace },
299 #endif
300 #if CONFIG_EQ_FILTER
301 { "vf_eq", checkasm_check_vf_eq },
302 #endif
303 #if CONFIG_FSPP_FILTER
304 { "vf_fspp", checkasm_check_vf_fspp },
305 #endif
306 #if CONFIG_GBLUR_FILTER
307 { "vf_gblur", checkasm_check_vf_gblur },
308 #endif
309 #if CONFIG_HFLIP_FILTER
310 { "vf_hflip", checkasm_check_vf_hflip },
311 #endif
312 #if CONFIG_IDET_FILTER
313 { "vf_idet", checkasm_check_idet },
314 #endif
315 #if CONFIG_NLMEANS_FILTER
316 { "vf_nlmeans", checkasm_check_nlmeans },
317 #endif
318 #if CONFIG_THRESHOLD_FILTER
319 { "vf_threshold", checkasm_check_vf_threshold },
320 #endif
321 #if CONFIG_SOBEL_FILTER
322 { "vf_sobel", checkasm_check_vf_sobel },
323 #endif
324 #endif
325 #if CONFIG_SWSCALE
326 { "sw_gbrp", checkasm_check_sw_gbrp },
327 { "sw_range_convert", checkasm_check_sw_range_convert },
328 { "sw_rgb", checkasm_check_sw_rgb },
329 { "sw_scale", checkasm_check_sw_scale },
330 { "sw_yuv2rgb", checkasm_check_sw_yuv2rgb },
331 { "sw_yuv2yuv", checkasm_check_sw_yuv2yuv },
332 { "sw_ops", checkasm_check_sw_ops },
333 #endif
334 #if CONFIG_AVUTIL
335 { "aes", checkasm_check_aes },
336 { "fixed_dsp", checkasm_check_fixed_dsp },
337 { "float_dsp", checkasm_check_float_dsp },
338 { "lls", checkasm_check_lls },
339 { "av_tx", checkasm_check_av_tx },
340 #endif
341 { NULL }
342 };
343
344 /* List of cpu flags to check */
345 static const struct {
346 const char *name;
347 const char *suffix;
348 int flag;
349 } cpus[] = {
350 #if ARCH_AARCH64
351 { "ARMV8", "armv8", AV_CPU_FLAG_ARMV8 },
352 { "NEON", "neon", AV_CPU_FLAG_NEON },
353 { "DOTPROD", "dotprod", AV_CPU_FLAG_DOTPROD },
354 { "I8MM", "i8mm", AV_CPU_FLAG_I8MM },
355 { "SVE", "sve", AV_CPU_FLAG_SVE },
356 { "SVE2", "sve2", AV_CPU_FLAG_SVE2 },
357 #elif ARCH_ARM
358 { "ARMV5TE", "armv5te", AV_CPU_FLAG_ARMV5TE },
359 { "ARMV6", "armv6", AV_CPU_FLAG_ARMV6 },
360 { "ARMV6T2", "armv6t2", AV_CPU_FLAG_ARMV6T2 },
361 { "VFP", "vfp", AV_CPU_FLAG_VFP },
362 { "VFP_VM", "vfp_vm", AV_CPU_FLAG_VFP_VM },
363 { "VFPV3", "vfp3", AV_CPU_FLAG_VFPV3 },
364 { "NEON", "neon", AV_CPU_FLAG_NEON },
365 #elif ARCH_PPC
366 { "ALTIVEC", "altivec", AV_CPU_FLAG_ALTIVEC },
367 { "VSX", "vsx", AV_CPU_FLAG_VSX },
368 { "POWER8", "power8", AV_CPU_FLAG_POWER8 },
369 #elif ARCH_RISCV
370 { "RVI", "rvi", AV_CPU_FLAG_RVI },
371 { "misaligned", "misaligned", AV_CPU_FLAG_RV_MISALIGNED },
372 { "RV_zbb", "rvb_b", AV_CPU_FLAG_RVB_BASIC },
373 { "RVB", "rvb", AV_CPU_FLAG_RVB },
374 { "RV_zve32x","rvv_i32", AV_CPU_FLAG_RVV_I32 },
375 { "RV_zve32f","rvv_f32", AV_CPU_FLAG_RVV_F32 },
376 { "RV_zve64x","rvv_i64", AV_CPU_FLAG_RVV_I64 },
377 { "RV_zve64d","rvv_f64", AV_CPU_FLAG_RVV_F64 },
378 { "RV_zvbb", "rv_zvbb", AV_CPU_FLAG_RV_ZVBB },
379 #elif ARCH_MIPS
380 { "MMI", "mmi", AV_CPU_FLAG_MMI },
381 { "MSA", "msa", AV_CPU_FLAG_MSA },
382 #elif ARCH_X86
383 { "MMX", "mmx", AV_CPU_FLAG_MMX|AV_CPU_FLAG_CMOV },
384 { "MMXEXT", "mmxext", AV_CPU_FLAG_MMXEXT },
385 { "3DNOW", "3dnow", AV_CPU_FLAG_3DNOW },
386 { "3DNOWEXT", "3dnowext", AV_CPU_FLAG_3DNOWEXT },
387 { "SSE", "sse", AV_CPU_FLAG_SSE },
388 { "SSE2", "sse2", AV_CPU_FLAG_SSE2|AV_CPU_FLAG_SSE2SLOW },
389 { "SSE3", "sse3", AV_CPU_FLAG_SSE3|AV_CPU_FLAG_SSE3SLOW },
390 { "SSSE3", "ssse3", AV_CPU_FLAG_SSSE3|AV_CPU_FLAG_ATOM },
391 { "SSE4.1", "sse4", AV_CPU_FLAG_SSE4 },
392 { "SSE4.2", "sse42", AV_CPU_FLAG_SSE42 },
393 { "AES-NI", "aesni", AV_CPU_FLAG_AESNI },
394 { "AVX", "avx", AV_CPU_FLAG_AVX },
395 { "XOP", "xop", AV_CPU_FLAG_XOP },
396 { "FMA3", "fma3", AV_CPU_FLAG_FMA3 },
397 { "FMA4", "fma4", AV_CPU_FLAG_FMA4 },
398 { "AVX2", "avx2", AV_CPU_FLAG_AVX2 },
399 { "AVX-512", "avx512", AV_CPU_FLAG_AVX512 },
400 { "AVX-512ICL", "avx512icl", AV_CPU_FLAG_AVX512ICL },
401 #elif ARCH_LOONGARCH
402 { "LSX", "lsx", AV_CPU_FLAG_LSX },
403 { "LASX", "lasx", AV_CPU_FLAG_LASX },
404 #elif ARCH_WASM
405 { "SIMD128", "simd128", AV_CPU_FLAG_SIMD128 },
406 #endif
407 { NULL }
408 };
409
410 typedef struct CheckasmFuncVersion {
411 struct CheckasmFuncVersion *next;
412 void *func;
413 int ok;
414 int cpu;
415 CheckasmPerf perf;
416 } CheckasmFuncVersion;
417
418 /* Binary search tree node */
419 typedef struct CheckasmFunc {
420 struct CheckasmFunc *child[2];
421 CheckasmFuncVersion versions;
422 uint8_t color; /* 0 = red, 1 = black */
423 char name[1];
424 } CheckasmFunc;
425
426 /* Internal state */
427 static struct {
428 CheckasmFunc *funcs;
429 CheckasmFunc *current_func;
430 CheckasmFuncVersion *current_func_ver;
431 const char *current_test_name;
432 const char *bench_pattern;
433 int bench_pattern_len;
434 int num_checked;
435 int num_failed;
436
437 /* perf */
438 int nop_time;
439 int sysfd;
440
441 int cpu_flag;
442 const char *cpu_flag_name;
443 const char *test_pattern;
444 int verbose;
445 int csv;
446 int tsv;
447 volatile sig_atomic_t catch_signals;
448 } state;
449
450 /* PRNG state */
451 AVLFG checkasm_lfg;
452
453 /* float compare support code */
454 static int is_negative(union av_intfloat32 u)
455 {
456 return u.i >> 31;
457 }
458
459 int float_near_ulp(float a, float b, unsigned max_ulp)
460 {
461 union av_intfloat32 x, y;
462
463 x.f = a;
464 y.f = b;
465
466 if (is_negative(x) != is_negative(y)) {
467 // handle -0.0 == +0.0
468 return a == b;
469 }
470
471 if (llabs((int64_t)x.i - y.i) <= max_ulp)
472 return 1;
473
474 return 0;
475 }
476
477 int float_near_ulp_array(const float *a, const float *b, unsigned max_ulp,
478 unsigned len)
479 {
480 unsigned i;
481
482 for (i = 0; i < len; i++) {
483 if (!float_near_ulp(a[i], b[i], max_ulp))
484 return 0;
485 }
486 return 1;
487 }
488
489 int float_near_abs_eps(float a, float b, float eps)
490 {
491 float abs_diff = fabsf(a - b);
492 if (abs_diff < eps)
493 return 1;
494
495 fprintf(stderr, "test failed comparing %g with %g (abs diff=%g with EPS=%g)\n", a, b, abs_diff, eps);
496
497 return 0;
498 }
499
500 int float_near_abs_eps_array(const float *a, const float *b, float eps,
501 unsigned len)
502 {
503 unsigned i;
504
505 for (i = 0; i < len; i++) {
506 if (!float_near_abs_eps(a[i], b[i], eps))
507 return 0;
508 }
509 return 1;
510 }
511
512 int float_near_abs_eps_ulp(float a, float b, float eps, unsigned max_ulp)
513 {
514 return float_near_ulp(a, b, max_ulp) || float_near_abs_eps(a, b, eps);
515 }
516
517 int float_near_abs_eps_array_ulp(const float *a, const float *b, float eps,
518 unsigned max_ulp, unsigned len)
519 {
520 unsigned i;
521
522 for (i = 0; i < len; i++) {
523 if (!float_near_abs_eps_ulp(a[i], b[i], eps, max_ulp))
524 return 0;
525 }
526 return 1;
527 }
528
529 int double_near_abs_eps(double a, double b, double eps)
530 {
531 double abs_diff = fabs(a - b);
532
533 return abs_diff < eps;
534 }
535
536 int double_near_abs_eps_array(const double *a, const double *b, double eps,
537 unsigned len)
538 {
539 unsigned i;
540
541 for (i = 0; i < len; i++) {
542 if (!double_near_abs_eps(a[i], b[i], eps))
543 return 0;
544 }
545 return 1;
546 }
547
548 /* Print colored text to stderr if the terminal supports it */
549 static void color_printf(int color, const char *fmt, ...)
550 {
551 static int use_color = -1;
552 va_list arg;
553
554 #if HAVE_SETCONSOLETEXTATTRIBUTE && HAVE_GETSTDHANDLE
555 static HANDLE con;
556 static WORD org_attributes;
557
558 if (use_color < 0) {
559 CONSOLE_SCREEN_BUFFER_INFO con_info;
560 con = GetStdHandle(STD_ERROR_HANDLE);
561 if (con && con != INVALID_HANDLE_VALUE && GetConsoleScreenBufferInfo(con, &con_info)) {
562 org_attributes = con_info.wAttributes;
563 use_color = 1;
564 } else
565 use_color = 0;
566 }
567 if (use_color)
568 SetConsoleTextAttribute(con, (org_attributes & 0xfff0) | (color & 0x0f));
569 #else
570 if (use_color < 0) {
571 const char *term = getenv("TERM");
572 use_color = term && strcmp(term, "dumb") && isatty(2);
573 }
574 if (use_color)
575 fprintf(stderr, "\x1b[%d;3%dm", (color & 0x08) >> 3, color & 0x07);
576 #endif
577
578 va_start(arg, fmt);
579 vfprintf(stderr, fmt, arg);
580 va_end(arg);
581
582 if (use_color) {
583 #if HAVE_SETCONSOLETEXTATTRIBUTE && HAVE_GETSTDHANDLE
584 SetConsoleTextAttribute(con, org_attributes);
585 #else
586 fprintf(stderr, "\x1b[0m");
587 #endif
588 }
589 }
590
591 /* Deallocate a tree */
592 static void destroy_func_tree(CheckasmFunc *f)
593 {
594 if (f) {
595 CheckasmFuncVersion *v = f->versions.next;
596 while (v) {
597 CheckasmFuncVersion *next = v->next;
598 free(v);
599 v = next;
600 }
601
602 destroy_func_tree(f->child[0]);
603 destroy_func_tree(f->child[1]);
604 free(f);
605 }
606 }
607
608 /* Allocate a zero-initialized block, clean up and exit on failure */
609 static void *checkasm_malloc(size_t size)
610 {
611 void *ptr = calloc(1, size);
612 if (!ptr) {
613 fprintf(stderr, "checkasm: malloc failed\n");
614 destroy_func_tree(state.funcs);
615 exit(1);
616 }
617 return ptr;
618 }
619
620 /* Get the suffix of the specified cpu flag */
621 static const char *cpu_suffix(int cpu)
622 {
623 int i = FF_ARRAY_ELEMS(cpus);
624
625 while (--i >= 0)
626 if (cpu & cpus[i].flag)
627 return cpus[i].suffix;
628
629 return "c";
630 }
631
632 static int cmp_nop(const void *a, const void *b)
633 {
634 return *(const uint16_t*)a - *(const uint16_t*)b;
635 }
636
637 /* Measure the overhead of the timing code (in decicycles) */
638 static int measure_nop_time(void)
639 {
640 uint16_t nops[10000];
641 int i, nop_sum = 0;
642 av_unused const int sysfd = state.sysfd;
643
644 uint64_t t = 0;
645 for (i = 0; i < 10000; i++) {
646 PERF_START(t);
647 PERF_STOP(t);
648 nops[i] = t;
649 }
650
651 qsort(nops, 10000, sizeof(uint16_t), cmp_nop);
652 for (i = 2500; i < 7500; i++)
653 nop_sum += nops[i];
654
655 return nop_sum / 500;
656 }
657
658 static inline double avg_cycles_per_call(const CheckasmPerf *const p)
659 {
660 if (p->iterations) {
661 const double cycles = (double)(10 * p->cycles) / p->iterations - state.nop_time;
662 if (cycles > 0.0)
663 return cycles / 32.0; /* 32 calls per iteration */
664 }
665 return 0.0;
666 }
667
668 /* Print benchmark results */
669 static void print_benchs(CheckasmFunc *f)
670 {
671 if (f) {
672 CheckasmFuncVersion *v = &f->versions;
673 const CheckasmPerf *p = &v->perf;
674 const double baseline = avg_cycles_per_call(p);
675 double decicycles;
676
677 print_benchs(f->child[0]);
678
679 do {
680 if (p->iterations) {
681 p = &v->perf;
682 decicycles = avg_cycles_per_call(p);
683 if (state.csv || state.tsv) {
684 const char sep = state.csv ? ',' : '\t';
685 printf("%s%c%s%c%.1f\n", f->name, sep,
686 cpu_suffix(v->cpu), sep,
687 decicycles / 10.0);
688 } else {
689 const int pad_length = 10 + 50 -
690 printf("%s_%s:", f->name, cpu_suffix(v->cpu));
691 const double ratio = decicycles ?
692 baseline / decicycles : 0.0;
693 printf("%*.1f (%5.2fx)\n", FFMAX(pad_length, 0),
694 decicycles / 10.0, ratio);
695 }
696 }
697 } while ((v = v->next));
698
699 print_benchs(f->child[1]);
700 }
701 }
702
703 /* ASCIIbetical sort except preserving natural order for numbers */
704 static int cmp_func_names(const char *a, const char *b)
705 {
706 const char *start = a;
707 int ascii_diff, digit_diff;
708
709 for (; !(ascii_diff = *(const unsigned char*)a - *(const unsigned char*)b) && *a; a++, b++);
710 for (; av_isdigit(*a) && av_isdigit(*b); a++, b++);
711
712 if (a > start && av_isdigit(a[-1]) && (digit_diff = av_isdigit(*a) - av_isdigit(*b)))
713 return digit_diff;
714
715 return ascii_diff;
716 }
717
718 /* Perform a tree rotation in the specified direction and return the new root */
719 static CheckasmFunc *rotate_tree(CheckasmFunc *f, int dir)
720 {
721 CheckasmFunc *r = f->child[dir^1];
722 f->child[dir^1] = r->child[dir];
723 r->child[dir] = f;
724 r->color = f->color;
725 f->color = 0;
726 return r;
727 }
728
729 #define is_red(f) ((f) && !(f)->color)
730
731 /* Balance a left-leaning red-black tree at the specified node */
732 static void balance_tree(CheckasmFunc **root)
733 {
734 CheckasmFunc *f = *root;
735
736 if (is_red(f->child[0]) && is_red(f->child[1])) {
737 f->color ^= 1;
738 f->child[0]->color = f->child[1]->color = 1;
739 }
740
741 if (!is_red(f->child[0]) && is_red(f->child[1]))
742 *root = rotate_tree(f, 0); /* Rotate left */
743 else if (is_red(f->child[0]) && is_red(f->child[0]->child[0]))
744 *root = rotate_tree(f, 1); /* Rotate right */
745 }
746
747 /* Get a node with the specified name, creating it if it doesn't exist */
748 static CheckasmFunc *get_func(CheckasmFunc **root, const char *name)
749 {
750 CheckasmFunc *f = *root;
751
752 if (f) {
753 /* Search the tree for a matching node */
754 int cmp = cmp_func_names(name, f->name);
755 if (cmp) {
756 f = get_func(&f->child[cmp > 0], name);
757
758 /* Rebalance the tree on the way up if a new node was inserted */
759 if (!f->versions.func)
760 balance_tree(root);
761 }
762 } else {
763 /* Allocate and insert a new node into the tree */
764 int name_length = strlen(name);
765 f = *root = checkasm_malloc(sizeof(CheckasmFunc) + name_length);
766 memcpy(f->name, name, name_length + 1);
767 }
768
769 return f;
770 }
771
772 checkasm_context checkasm_context_buf;
773
774 /* Crash handling: attempt to catch crashes and handle them
775 * gracefully instead of just aborting abruptly. */
776 #ifdef _WIN32
777 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
778 static LONG NTAPI signal_handler(EXCEPTION_POINTERS *e) {
779 int s;
780
781 if (!state.catch_signals)
782 return EXCEPTION_CONTINUE_SEARCH;
783
784 switch (e->ExceptionRecord->ExceptionCode) {
785 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
786 case EXCEPTION_INT_DIVIDE_BY_ZERO:
787 s = SIGFPE;
788 break;
789 case EXCEPTION_ILLEGAL_INSTRUCTION:
790 case EXCEPTION_PRIV_INSTRUCTION:
791 s = SIGILL;
792 break;
793 case EXCEPTION_ACCESS_VIOLATION:
794 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
795 case EXCEPTION_DATATYPE_MISALIGNMENT:
796 case EXCEPTION_STACK_OVERFLOW:
797 s = SIGSEGV;
798 break;
799 case EXCEPTION_IN_PAGE_ERROR:
800 s = SIGBUS;
801 break;
802 default:
803 return EXCEPTION_CONTINUE_SEARCH;
804 }
805 state.catch_signals = 0;
806 checkasm_load_context(s);
807 return EXCEPTION_CONTINUE_EXECUTION; /* never reached, but shuts up gcc */
808 }
809 #endif
810 #elif !defined(_WASI_EMULATED_SIGNAL)
811 static void signal_handler(int s);
812
813 static const struct sigaction signal_handler_act = {
814 .sa_handler = signal_handler,
815 .sa_flags = SA_RESETHAND,
816 };
817
818 static void signal_handler(int s) {
819 if (state.catch_signals) {
820 state.catch_signals = 0;
821 sigaction(s, &signal_handler_act, NULL);
822 checkasm_load_context(s);
823 }
824 }
825 #endif
826
827 /* Compares a string with a wildcard pattern. */
828 static int wildstrcmp(const char *str, const char *pattern)
829 {
830 const char *wild = strchr(pattern, '*');
831 if (wild) {
832 const size_t len = wild - pattern;
833 if (strncmp(str, pattern, len)) return 1;
834 while (*++wild == '*');
835 if (!*wild) return 0;
836 str += len;
837 while (*str && wildstrcmp(str, wild)) str++;
838 return !*str;
839 }
840 return strcmp(str, pattern);
841 }
842
843 /* Perform tests and benchmarks for the specified cpu flag if supported by the host */
844 static void check_cpu_flag(const char *name, int flag)
845 {
846 int old_cpu_flag = state.cpu_flag;
847
848 flag |= old_cpu_flag;
849 av_force_cpu_flags(-1);
850 state.cpu_flag = flag & av_get_cpu_flags();
851 av_force_cpu_flags(state.cpu_flag);
852
853 if (!flag || state.cpu_flag != old_cpu_flag) {
854 int i;
855
856 state.cpu_flag_name = name;
857 for (i = 0; tests[i].func; i++) {
858 if (state.test_pattern && wildstrcmp(tests[i].name, state.test_pattern))
859 continue;
860 state.current_test_name = tests[i].name;
861 tests[i].func();
862 }
863 }
864 }
865
866 /* Print the name of the current CPU flag, but only do it once */
867 static void print_cpu_name(void)
868 {
869 if (state.cpu_flag_name) {
870 color_printf(COLOR_YELLOW, "%s:\n", state.cpu_flag_name);
871 state.cpu_flag_name = NULL;
872 }
873 }
874
875 #if CONFIG_LINUX_PERF
876 static int bench_init_linux(void)
877 {
878 struct perf_event_attr attr = {
879 .type = PERF_TYPE_HARDWARE,
880 .size = sizeof(struct perf_event_attr),
881 .config = PERF_COUNT_HW_CPU_CYCLES,
882 .disabled = 1, // start counting only on demand
883 .exclude_kernel = 1,
884 .exclude_hv = 1,
885 #if !ARCH_X86
886 .exclude_guest = 1,
887 #endif
888 };
889
890 fprintf(stderr, "benchmarking with Linux Perf Monitoring API\n");
891
892 state.sysfd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0);
893 if (state.sysfd == -1) {
894 perror("perf_event_open");
895 return -1;
896 }
897 return 0;
898 }
899 #elif CONFIG_MACOS_KPERF
900 static int bench_init_kperf(void)
901 {
902 ff_kperf_init();
903 return 0;
904 }
905 #else
906 static int bench_init_ffmpeg(void)
907 {
908 #ifdef AV_READ_TIME
909 if (!checkasm_save_context()) {
910 checkasm_set_signal_handler_state(1);
911 AV_READ_TIME();
912 checkasm_set_signal_handler_state(0);
913 } else {
914 fprintf(stderr, "checkasm: unable to execute platform specific timer\n");
915 return -1;
916 }
917 fprintf(stderr, "benchmarking with native FFmpeg timers\n");
918 return 0;
919 #else
920 fprintf(stderr, "checkasm: --bench is not supported on your system\n");
921 return -1;
922 #endif
923 }
924 #endif
925
926 static int bench_init(void)
927 {
928 #if CONFIG_LINUX_PERF
929 int ret = bench_init_linux();
930 #elif CONFIG_MACOS_KPERF
931 int ret = bench_init_kperf();
932 #else
933 int ret = bench_init_ffmpeg();
934 #endif
935 if (ret < 0)
936 return ret;
937
938 state.nop_time = measure_nop_time();
939 fprintf(stderr, "nop: %d.%d\n", state.nop_time/10, state.nop_time%10);
940 return 0;
941 }
942
943 static void bench_uninit(void)
944 {
945 #if CONFIG_LINUX_PERF
946 close(state.sysfd);
947 #endif
948 }
949
950 static int usage(const char *path)
951 {
952 fprintf(stderr,
953 "Usage: %s [options...] [seed]\n"
954 " --test=<pattern> Run specific test.\n"
955 " --bench Run benchmark.\n"
956 " --csv, --tsv Output results in rows of comma or tab separated values.\n"
957 " --runs=<ptwo> Manual number of benchmark iterations to run 2**<ptwo>.\n"
958 " --verbose Increase verbosity.\n",
959 path);
960 return 1;
961 }
962
963 int main(int argc, char *argv[])
964 {
965 unsigned int seed = av_get_random_seed();
966 int i, ret = 0;
967 char arch_info_buf[50] = "";
968
969 #ifdef _WIN32
970 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
971 AddVectoredExceptionHandler(0, signal_handler);
972 #endif
973 #elif !defined(_WASI_EMULATED_SIGNAL)
974 sigaction(SIGBUS, &signal_handler_act, NULL);
975 sigaction(SIGFPE, &signal_handler_act, NULL);
976 sigaction(SIGILL, &signal_handler_act, NULL);
977 sigaction(SIGSEGV, &signal_handler_act, NULL);
978 #endif
979 #if HAVE_PRCTL && defined(PR_SET_UNALIGN)
980 prctl(PR_SET_UNALIGN, PR_UNALIGN_SIGBUS);
981 #endif
982 #if ARCH_ARM && HAVE_ARMV5TE_EXTERNAL
983 if (have_vfp(av_get_cpu_flags()) || have_neon(av_get_cpu_flags()))
984 checkasm_checked_call = checkasm_checked_call_vfp;
985 #endif
986
987 if (!tests[0].func || !cpus[0].flag) {
988 fprintf(stderr, "checkasm: no tests to perform\n");
989 return 0;
990 }
991
992 for (i = 1; i < argc; i++) {
993 const char *arg = argv[i];
994 unsigned long l;
995 char *end;
996
997 if (!strncmp(arg, "--bench", 7)) {
998 if (bench_init() < 0)
999 return 1;
1000 if (arg[7] == '=') {
1001 state.bench_pattern = arg + 8;
1002 state.bench_pattern_len = strlen(state.bench_pattern);
1003 } else
1004 state.bench_pattern = "*";
1005 } else if (!strncmp(arg, "--test=", 7)) {
1006 state.test_pattern = arg + 7;
1007 } else if (!strcmp(arg, "--csv")) {
1008 state.csv = 1; state.tsv = 0;
1009 } else if (!strcmp(arg, "--tsv")) {
1010 state.csv = 0; state.tsv = 1;
1011 } else if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
1012 state.verbose = 1;
1013 } else if (!strncmp(arg, "--runs=", 7)) {
1014 l = strtoul(arg + 7, &end, 10);
1015 if (*end == '\0') {
1016 if (l > 30) {
1017 fprintf(stderr, "checkasm: error: runs exponent must be within the range 0 <= 30\n");
1018 usage(argv[0]);
1019 }
1020 bench_runs = 1U << l;
1021 } else {
1022 return usage(argv[0]);
1023 }
1024 } else if ((l = strtoul(arg, &end, 10)) <= UINT_MAX &&
1025 *end == '\0') {
1026 seed = l;
1027 } else {
1028 return usage(argv[0]);
1029 }
1030 }
1031
1032 #if ARCH_AARCH64 && HAVE_SVE
1033 if (have_sve(av_get_cpu_flags()))
1034 snprintf(arch_info_buf, sizeof(arch_info_buf),
1035 "SVE %d bits, ", 8 * ff_aarch64_sve_length());
1036 #elif ARCH_RISCV && HAVE_RVV
1037 if (av_get_cpu_flags() & AV_CPU_FLAG_RVV_I32)
1038 snprintf(arch_info_buf, sizeof (arch_info_buf),
1039 "%zu-bit vectors, ", 8 * ff_get_rv_vlenb());
1040 #endif
1041 fprintf(stderr, "checkasm: %susing random seed %u\n", arch_info_buf, seed);
1042 av_lfg_init(&checkasm_lfg, seed);
1043
1044 if (state.bench_pattern)
1045 fprintf(stderr, "checkasm: bench runs %" PRIu64 " (1 << %i)\n", bench_runs, av_log2(bench_runs));
1046
1047 check_cpu_flag(NULL, 0);
1048 for (i = 0; cpus[i].flag; i++)
1049 check_cpu_flag(cpus[i].name, cpus[i].flag);
1050
1051 if (state.num_failed) {
1052 fprintf(stderr, "checkasm: %d of %d tests have failed\n", state.num_failed, state.num_checked);
1053 ret = 1;
1054 } else {
1055 fprintf(stderr, "checkasm: all %d tests passed\n", state.num_checked);
1056 if (state.bench_pattern) {
1057 print_benchs(state.funcs);
1058 }
1059 }
1060
1061 destroy_func_tree(state.funcs);
1062 bench_uninit();
1063 return ret;
1064 }
1065
1066 /* Decide whether or not the specified function needs to be tested and
1067 * allocate/initialize data structures if needed. Returns a pointer to a
1068 * reference function if the function should be tested, otherwise NULL */
1069 void *checkasm_check_func(void *func, const char *name, ...)
1070 {
1071 char name_buf[256];
1072 void *ref = func;
1073 CheckasmFuncVersion *v;
1074 int name_length;
1075 va_list arg;
1076
1077 va_start(arg, name);
1078 name_length = vsnprintf(name_buf, sizeof(name_buf), name, arg);
1079 va_end(arg);
1080
1081 if (!func || name_length <= 0 || name_length >= sizeof(name_buf))
1082 return NULL;
1083
1084 state.current_func = get_func(&state.funcs, name_buf);
1085 state.funcs->color = 1;
1086 v = &state.current_func->versions;
1087
1088 if (v->func) {
1089 CheckasmFuncVersion *prev;
1090 do {
1091 /* Only test functions that haven't already been tested */
1092 if (v->func == func)
1093 return NULL;
1094
1095 if (v->ok)
1096 ref = v->func;
1097
1098 prev = v;
1099 } while ((v = v->next));
1100
1101 v = prev->next = checkasm_malloc(sizeof(CheckasmFuncVersion));
1102 }
1103
1104 v->func = func;
1105 v->ok = 1;
1106 v->cpu = state.cpu_flag;
1107 state.current_func_ver = v;
1108
1109 if (state.cpu_flag)
1110 state.num_checked++;
1111
1112 return ref;
1113 }
1114
1115 /* Decide whether or not the current function needs to be benchmarked */
1116 int checkasm_bench_func(void)
1117 {
1118 return !state.num_failed && state.bench_pattern &&
1119 !wildstrcmp(state.current_func->name, state.bench_pattern);
1120 }
1121
1122 /* Indicate that the current test has failed, return whether verbose printing
1123 * is requested. */
1124 int checkasm_fail_func(const char *msg, ...)
1125 {
1126 if (state.current_func_ver && state.current_func_ver->cpu &&
1127 state.current_func_ver->ok)
1128 {
1129 va_list arg;
1130
1131 print_cpu_name();
1132 fprintf(stderr, " %s_%s (", state.current_func->name, cpu_suffix(state.current_func_ver->cpu));
1133 va_start(arg, msg);
1134 vfprintf(stderr, msg, arg);
1135 va_end(arg);
1136 fprintf(stderr, ")\n");
1137
1138 state.current_func_ver->ok = 0;
1139 state.num_failed++;
1140 }
1141 return state.verbose;
1142 }
1143
1144 void checkasm_set_signal_handler_state(int enabled) {
1145 state.catch_signals = enabled;
1146 }
1147
1148 int checkasm_handle_signal(int s) {
1149 if (s) {
1150 #ifdef __GLIBC__
1151 checkasm_fail_func("fatal signal %d: %s", s, strsignal(s));
1152 #else
1153 checkasm_fail_func(s == SIGFPE ? "fatal arithmetic error" :
1154 s == SIGILL ? "illegal instruction" :
1155 s == SIGBUS ? "bus error" :
1156 "segmentation fault");
1157 #endif
1158 }
1159 return s;
1160 }
1161
1162 /* Get the benchmark context of the current function */
1163 CheckasmPerf *checkasm_get_perf_context(void)
1164 {
1165 CheckasmPerf *perf = &state.current_func_ver->perf;
1166 memset(perf, 0, sizeof(*perf));
1167 perf->sysfd = state.sysfd;
1168 return perf;
1169 }
1170
1171 /* Print the outcome of all tests performed since the last time this function was called */
1172 void checkasm_report(const char *name, ...)
1173 {
1174 static int prev_checked, prev_failed, max_length;
1175
1176 if (state.num_checked > prev_checked) {
1177 int pad_length = max_length + 4;
1178 va_list arg;
1179
1180 print_cpu_name();
1181 pad_length -= fprintf(stderr, " - %s.", state.current_test_name);
1182 va_start(arg, name);
1183 pad_length -= vfprintf(stderr, name, arg);
1184 va_end(arg);
1185 fprintf(stderr, "%*c", FFMAX(pad_length, 0) + 2, '[');
1186
1187 if (state.num_failed == prev_failed)
1188 color_printf(COLOR_GREEN, "OK");
1189 else
1190 color_printf(COLOR_RED, "FAILED");
1191 fprintf(stderr, "]\n");
1192
1193 prev_checked = state.num_checked;
1194 prev_failed = state.num_failed;
1195 } else if (!state.cpu_flag) {
1196 /* Calculate the amount of padding required to make the output vertically aligned */
1197 int length = strlen(state.current_test_name);
1198 va_list arg;
1199
1200 va_start(arg, name);
1201 length += vsnprintf(NULL, 0, name, arg);
1202 va_end(arg);
1203
1204 if (length > max_length)
1205 max_length = length;
1206 }
1207 }
1208
1209 static int check_err(const char *file, int line,
1210 const char *name, int w, int h,
1211 int *err)
1212 {
1213 if (*err)
1214 return 0;
1215 if (!checkasm_fail_func("%s:%d", file, line))
1216 return 1;
1217 *err = 1;
1218 fprintf(stderr, "%s (%dx%d):\n", name, w, h);
1219 return 0;
1220 }
1221
1222 #define DEF_CHECKASM_CHECK_BODY(compare, type, fmt) \
1223 do { \
1224 int64_t aligned_w = (w - 1LL + align_w) & ~(align_w - 1); \
1225 int64_t aligned_h = (h - 1LL + align_h) & ~(align_h - 1); \
1226 int err = 0; \
1227 int y = 0; \
1228 av_assert0(aligned_w == (int32_t)aligned_w);\
1229 av_assert0(aligned_h == (int32_t)aligned_h);\
1230 stride1 /= sizeof(*buf1); \
1231 stride2 /= sizeof(*buf2); \
1232 for (y = 0; y < h; y++) \
1233 if (!compare(&buf1[y*stride1], &buf2[y*stride2], w)) \
1234 break; \
1235 if (y != h) { \
1236 if (check_err(file, line, name, w, h, &err)) \
1237 return 1; \
1238 for (y = 0; y < h; y++) { \
1239 for (int x = 0; x < w; x++) \
1240 fprintf(stderr, " " fmt, buf1[x]); \
1241 fprintf(stderr, " "); \
1242 for (int x = 0; x < w; x++) \
1243 fprintf(stderr, " " fmt, buf2[x]); \
1244 fprintf(stderr, " "); \
1245 for (int x = 0; x < w; x++) \
1246 fprintf(stderr, "%c", buf1[x] != buf2[x] ? 'x' : '.'); \
1247 buf1 += stride1; \
1248 buf2 += stride2; \
1249 fprintf(stderr, "\n"); \
1250 } \
1251 buf1 -= h*stride1; \
1252 buf2 -= h*stride2; \
1253 } \
1254 for (y = -padding; y < 0; y++) \
1255 if (!compare(&buf1[y*stride1 - padding], &buf2[y*stride2 - padding], \
1256 w + 2*padding)) { \
1257 if (check_err(file, line, name, w, h, &err)) \
1258 return 1; \
1259 fprintf(stderr, " overwrite above\n"); \
1260 break; \
1261 } \
1262 for (y = aligned_h; y < aligned_h + padding; y++) \
1263 if (!compare(&buf1[y*stride1 - padding], &buf2[y*stride2 - padding], \
1264 w + 2*padding)) { \
1265 if (check_err(file, line, name, w, h, &err)) \
1266 return 1; \
1267 fprintf(stderr, " overwrite below\n"); \
1268 break; \
1269 } \
1270 for (y = 0; y < h; y++) \
1271 if (!compare(&buf1[y*stride1 - padding], &buf2[y*stride2 - padding], \
1272 padding)) { \
1273 if (check_err(file, line, name, w, h, &err)) \
1274 return 1; \
1275 fprintf(stderr, " overwrite left\n"); \
1276 break; \
1277 } \
1278 for (y = 0; y < h; y++) \
1279 if (!compare(&buf1[y*stride1 + aligned_w], &buf2[y*stride2 + aligned_w], \
1280 padding)) { \
1281 if (check_err(file, line, name, w, h, &err)) \
1282 return 1; \
1283 fprintf(stderr, " overwrite right\n"); \
1284 break; \
1285 } \
1286 return err; \
1287 } while (0)
1288
1289 #define cmp_int(a, b, len) (!memcmp(a, b, (len) * sizeof(*(a))))
1290 #define DEF_CHECKASM_CHECK_FUNC(type, fmt) \
1291 int checkasm_check_##type(const char *file, int line, \
1292 const type *buf1, ptrdiff_t stride1, \
1293 const type *buf2, ptrdiff_t stride2, \
1294 int w, int h, const char *name, \
1295 int align_w, int align_h, \
1296 int padding) \
1297 { \
1298 DEF_CHECKASM_CHECK_BODY(cmp_int, type, fmt); \
1299 }
1300
1301 DEF_CHECKASM_CHECK_FUNC(uint8_t, "%02x")
1302 DEF_CHECKASM_CHECK_FUNC(uint16_t, "%04x")
1303 DEF_CHECKASM_CHECK_FUNC(uint32_t, "%08x")
1304 DEF_CHECKASM_CHECK_FUNC(int16_t, "%6d")
1305 DEF_CHECKASM_CHECK_FUNC(int32_t, "%9d")
1306
1307 int checkasm_check_float_ulp(const char *file, int line,
1308 const float *buf1, ptrdiff_t stride1,
1309 const float *buf2, ptrdiff_t stride2,
1310 int w, int h, const char *name,
1311 unsigned max_ulp, int align_w, int align_h,
1312 int padding)
1313 {
1314 #define cmp_float(a, b, len) float_near_ulp_array(a, b, max_ulp, len)
1315 DEF_CHECKASM_CHECK_BODY(cmp_float, float, "%g");
1316 #undef cmp_float
1317 }