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.

836 lines
22KB

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