checkasm.c 29 KB

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