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.

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