checkasm.c 35 KB

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