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.

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