You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

885 lines
24KB

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