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.

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