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.

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