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.

812 lines
21KB

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