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.

819 lines
22KB

  1. /*
  2. * Assembly testing and benchmarking tool
  3. * Copyright (c) 2015 Henrik Gramner
  4. * Copyright (c) 2008 Loren Merritt
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include "config.h"
  23. #if CONFIG_LINUX_PERF
  24. # ifndef _GNU_SOURCE
  25. # define _GNU_SOURCE // for syscall (performance monitoring API)
  26. # endif
  27. #endif
  28. #include <stdarg.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include "checkasm.h"
  33. #include "libavutil/common.h"
  34. #include "libavutil/cpu.h"
  35. #include "libavutil/intfloat.h"
  36. #include "libavutil/random_seed.h"
  37. #if HAVE_IO_H
  38. #include <io.h>
  39. #endif
  40. #if HAVE_SETCONSOLETEXTATTRIBUTE
  41. #include <windows.h>
  42. #define COLOR_RED FOREGROUND_RED
  43. #define COLOR_GREEN FOREGROUND_GREEN
  44. #define COLOR_YELLOW (FOREGROUND_RED|FOREGROUND_GREEN)
  45. #else
  46. #define COLOR_RED 1
  47. #define COLOR_GREEN 2
  48. #define COLOR_YELLOW 3
  49. #endif
  50. #if HAVE_UNISTD_H
  51. #include <unistd.h>
  52. #endif
  53. #if !HAVE_ISATTY
  54. #define isatty(fd) 1
  55. #endif
  56. #if ARCH_ARM && HAVE_ARMV5TE_EXTERNAL
  57. #include "libavutil/arm/cpu.h"
  58. void (*checkasm_checked_call)(void *func, int dummy, ...) = checkasm_checked_call_novfp;
  59. #endif
  60. /* List of tests to invoke */
  61. static const struct {
  62. const char *name;
  63. void (*func)(void);
  64. } tests[] = {
  65. #if CONFIG_AVCODEC
  66. #if CONFIG_AAC_DECODER
  67. { "aacpsdsp", checkasm_check_aacpsdsp },
  68. { "sbrdsp", checkasm_check_sbrdsp },
  69. #endif
  70. #if CONFIG_ALAC_DECODER
  71. { "alacdsp", checkasm_check_alacdsp },
  72. #endif
  73. #if CONFIG_AUDIODSP
  74. { "audiodsp", checkasm_check_audiodsp },
  75. #endif
  76. #if CONFIG_BLOCKDSP
  77. { "blockdsp", checkasm_check_blockdsp },
  78. #endif
  79. #if CONFIG_BSWAPDSP
  80. { "bswapdsp", checkasm_check_bswapdsp },
  81. #endif
  82. #if CONFIG_DCA_DECODER
  83. { "synth_filter", checkasm_check_synth_filter },
  84. #endif
  85. #if CONFIG_EXR_DECODER
  86. { "exrdsp", checkasm_check_exrdsp },
  87. #endif
  88. #if CONFIG_FLACDSP
  89. { "flacdsp", checkasm_check_flacdsp },
  90. #endif
  91. #if CONFIG_FMTCONVERT
  92. { "fmtconvert", checkasm_check_fmtconvert },
  93. #endif
  94. #if CONFIG_G722DSP
  95. { "g722dsp", checkasm_check_g722dsp },
  96. #endif
  97. #if CONFIG_H264DSP
  98. { "h264dsp", checkasm_check_h264dsp },
  99. #endif
  100. #if CONFIG_H264PRED
  101. { "h264pred", checkasm_check_h264pred },
  102. #endif
  103. #if CONFIG_H264QPEL
  104. { "h264qpel", checkasm_check_h264qpel },
  105. #endif
  106. #if CONFIG_HEVC_DECODER
  107. { "hevc_add_res", checkasm_check_hevc_add_res },
  108. { "hevc_idct", checkasm_check_hevc_idct },
  109. { "hevc_sao", checkasm_check_hevc_sao },
  110. #endif
  111. #if CONFIG_HUFFYUV_DECODER
  112. { "huffyuvdsp", checkasm_check_huffyuvdsp },
  113. #endif
  114. #if CONFIG_JPEG2000_DECODER
  115. { "jpeg2000dsp", checkasm_check_jpeg2000dsp },
  116. #endif
  117. #if CONFIG_HUFFYUVDSP
  118. { "llviddsp", checkasm_check_llviddsp },
  119. #endif
  120. #if CONFIG_LLVIDENCDSP
  121. { "llviddspenc", checkasm_check_llviddspenc },
  122. #endif
  123. #if CONFIG_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_SWSCALE
  157. { "sw_rgb", checkasm_check_sw_rgb },
  158. #endif
  159. #if CONFIG_AVUTIL
  160. { "fixed_dsp", checkasm_check_fixed_dsp },
  161. { "float_dsp", checkasm_check_float_dsp },
  162. #endif
  163. { NULL }
  164. };
  165. /* List of cpu flags to check */
  166. static const struct {
  167. const char *name;
  168. const char *suffix;
  169. int flag;
  170. } cpus[] = {
  171. #if ARCH_AARCH64
  172. { "ARMV8", "armv8", AV_CPU_FLAG_ARMV8 },
  173. { "NEON", "neon", AV_CPU_FLAG_NEON },
  174. #elif ARCH_ARM
  175. { "ARMV5TE", "armv5te", AV_CPU_FLAG_ARMV5TE },
  176. { "ARMV6", "armv6", AV_CPU_FLAG_ARMV6 },
  177. { "ARMV6T2", "armv6t2", AV_CPU_FLAG_ARMV6T2 },
  178. { "VFP", "vfp", AV_CPU_FLAG_VFP },
  179. { "VFP_VM", "vfp_vm", AV_CPU_FLAG_VFP_VM },
  180. { "VFPV3", "vfp3", AV_CPU_FLAG_VFPV3 },
  181. { "NEON", "neon", AV_CPU_FLAG_NEON },
  182. #elif ARCH_PPC
  183. { "ALTIVEC", "altivec", AV_CPU_FLAG_ALTIVEC },
  184. { "VSX", "vsx", AV_CPU_FLAG_VSX },
  185. { "POWER8", "power8", AV_CPU_FLAG_POWER8 },
  186. #elif ARCH_X86
  187. { "MMX", "mmx", AV_CPU_FLAG_MMX|AV_CPU_FLAG_CMOV },
  188. { "MMXEXT", "mmxext", AV_CPU_FLAG_MMXEXT },
  189. { "3DNOW", "3dnow", AV_CPU_FLAG_3DNOW },
  190. { "3DNOWEXT", "3dnowext", AV_CPU_FLAG_3DNOWEXT },
  191. { "SSE", "sse", AV_CPU_FLAG_SSE },
  192. { "SSE2", "sse2", AV_CPU_FLAG_SSE2|AV_CPU_FLAG_SSE2SLOW },
  193. { "SSE3", "sse3", AV_CPU_FLAG_SSE3|AV_CPU_FLAG_SSE3SLOW },
  194. { "SSSE3", "ssse3", AV_CPU_FLAG_SSSE3|AV_CPU_FLAG_ATOM },
  195. { "SSE4.1", "sse4", AV_CPU_FLAG_SSE4 },
  196. { "SSE4.2", "sse42", AV_CPU_FLAG_SSE42 },
  197. { "AES-NI", "aesni", AV_CPU_FLAG_AESNI },
  198. { "AVX", "avx", AV_CPU_FLAG_AVX },
  199. { "XOP", "xop", AV_CPU_FLAG_XOP },
  200. { "FMA3", "fma3", AV_CPU_FLAG_FMA3 },
  201. { "FMA4", "fma4", AV_CPU_FLAG_FMA4 },
  202. { "AVX2", "avx2", AV_CPU_FLAG_AVX2 },
  203. { "AVX-512", "avx512", AV_CPU_FLAG_AVX512 },
  204. #endif
  205. { NULL }
  206. };
  207. typedef struct CheckasmFuncVersion {
  208. struct CheckasmFuncVersion *next;
  209. void *func;
  210. int ok;
  211. int cpu;
  212. CheckasmPerf perf;
  213. } CheckasmFuncVersion;
  214. /* Binary search tree node */
  215. typedef struct CheckasmFunc {
  216. struct CheckasmFunc *child[2];
  217. CheckasmFuncVersion versions;
  218. uint8_t color; /* 0 = red, 1 = black */
  219. char name[1];
  220. } CheckasmFunc;
  221. /* Internal state */
  222. static struct {
  223. CheckasmFunc *funcs;
  224. CheckasmFunc *current_func;
  225. CheckasmFuncVersion *current_func_ver;
  226. const char *current_test_name;
  227. const char *bench_pattern;
  228. int bench_pattern_len;
  229. int num_checked;
  230. int num_failed;
  231. /* perf */
  232. int nop_time;
  233. int sysfd;
  234. int cpu_flag;
  235. const char *cpu_flag_name;
  236. const char *test_name;
  237. } state;
  238. /* PRNG state */
  239. AVLFG checkasm_lfg;
  240. /* float compare support code */
  241. static int is_negative(union av_intfloat32 u)
  242. {
  243. return u.i >> 31;
  244. }
  245. int float_near_ulp(float a, float b, unsigned max_ulp)
  246. {
  247. union av_intfloat32 x, y;
  248. x.f = a;
  249. y.f = b;
  250. if (is_negative(x) != is_negative(y)) {
  251. // handle -0.0 == +0.0
  252. return a == b;
  253. }
  254. if (llabs((int64_t)x.i - y.i) <= max_ulp)
  255. return 1;
  256. return 0;
  257. }
  258. int float_near_ulp_array(const float *a, const float *b, unsigned max_ulp,
  259. unsigned len)
  260. {
  261. unsigned i;
  262. for (i = 0; i < len; i++) {
  263. if (!float_near_ulp(a[i], b[i], max_ulp))
  264. return 0;
  265. }
  266. return 1;
  267. }
  268. int float_near_abs_eps(float a, float b, float eps)
  269. {
  270. float abs_diff = fabsf(a - b);
  271. if (abs_diff < eps)
  272. return 1;
  273. fprintf(stderr, "test failed comparing %g with %g (abs diff=%g with EPS=%g)\n", a, b, abs_diff, eps);
  274. return 0;
  275. }
  276. int float_near_abs_eps_array(const float *a, const float *b, float eps,
  277. unsigned len)
  278. {
  279. unsigned i;
  280. for (i = 0; i < len; i++) {
  281. if (!float_near_abs_eps(a[i], b[i], eps))
  282. return 0;
  283. }
  284. return 1;
  285. }
  286. int float_near_abs_eps_ulp(float a, float b, float eps, unsigned max_ulp)
  287. {
  288. return float_near_ulp(a, b, max_ulp) || float_near_abs_eps(a, b, eps);
  289. }
  290. int float_near_abs_eps_array_ulp(const float *a, const float *b, float eps,
  291. unsigned max_ulp, unsigned len)
  292. {
  293. unsigned i;
  294. for (i = 0; i < len; i++) {
  295. if (!float_near_abs_eps_ulp(a[i], b[i], eps, max_ulp))
  296. return 0;
  297. }
  298. return 1;
  299. }
  300. int double_near_abs_eps(double a, double b, double eps)
  301. {
  302. double abs_diff = fabs(a - b);
  303. return abs_diff < eps;
  304. }
  305. int double_near_abs_eps_array(const double *a, const double *b, double eps,
  306. unsigned len)
  307. {
  308. unsigned i;
  309. for (i = 0; i < len; i++) {
  310. if (!double_near_abs_eps(a[i], b[i], eps))
  311. return 0;
  312. }
  313. return 1;
  314. }
  315. /* Print colored text to stderr if the terminal supports it */
  316. static void color_printf(int color, const char *fmt, ...)
  317. {
  318. static int use_color = -1;
  319. va_list arg;
  320. #if HAVE_SETCONSOLETEXTATTRIBUTE
  321. static HANDLE con;
  322. static WORD org_attributes;
  323. if (use_color < 0) {
  324. CONSOLE_SCREEN_BUFFER_INFO con_info;
  325. con = GetStdHandle(STD_ERROR_HANDLE);
  326. if (con && con != INVALID_HANDLE_VALUE && GetConsoleScreenBufferInfo(con, &con_info)) {
  327. org_attributes = con_info.wAttributes;
  328. use_color = 1;
  329. } else
  330. use_color = 0;
  331. }
  332. if (use_color)
  333. SetConsoleTextAttribute(con, (org_attributes & 0xfff0) | (color & 0x0f));
  334. #else
  335. if (use_color < 0) {
  336. const char *term = getenv("TERM");
  337. use_color = term && strcmp(term, "dumb") && isatty(2);
  338. }
  339. if (use_color)
  340. fprintf(stderr, "\x1b[%d;3%dm", (color & 0x08) >> 3, color & 0x07);
  341. #endif
  342. va_start(arg, fmt);
  343. vfprintf(stderr, fmt, arg);
  344. va_end(arg);
  345. if (use_color) {
  346. #if HAVE_SETCONSOLETEXTATTRIBUTE
  347. SetConsoleTextAttribute(con, org_attributes);
  348. #else
  349. fprintf(stderr, "\x1b[0m");
  350. #endif
  351. }
  352. }
  353. /* Deallocate a tree */
  354. static void destroy_func_tree(CheckasmFunc *f)
  355. {
  356. if (f) {
  357. CheckasmFuncVersion *v = f->versions.next;
  358. while (v) {
  359. CheckasmFuncVersion *next = v->next;
  360. free(v);
  361. v = next;
  362. }
  363. destroy_func_tree(f->child[0]);
  364. destroy_func_tree(f->child[1]);
  365. free(f);
  366. }
  367. }
  368. /* Allocate a zero-initialized block, clean up and exit on failure */
  369. static void *checkasm_malloc(size_t size)
  370. {
  371. void *ptr = calloc(1, size);
  372. if (!ptr) {
  373. fprintf(stderr, "checkasm: malloc failed\n");
  374. destroy_func_tree(state.funcs);
  375. exit(1);
  376. }
  377. return ptr;
  378. }
  379. /* Get the suffix of the specified cpu flag */
  380. static const char *cpu_suffix(int cpu)
  381. {
  382. int i = FF_ARRAY_ELEMS(cpus);
  383. while (--i >= 0)
  384. if (cpu & cpus[i].flag)
  385. return cpus[i].suffix;
  386. return "c";
  387. }
  388. static int cmp_nop(const void *a, const void *b)
  389. {
  390. return *(const uint16_t*)a - *(const uint16_t*)b;
  391. }
  392. /* Measure the overhead of the timing code (in decicycles) */
  393. static int measure_nop_time(void)
  394. {
  395. uint16_t nops[10000];
  396. int i, nop_sum = 0;
  397. av_unused const int sysfd = state.sysfd;
  398. uint64_t t = 0;
  399. for (i = 0; i < 10000; i++) {
  400. PERF_START(t);
  401. PERF_STOP(t);
  402. nops[i] = t;
  403. }
  404. qsort(nops, 10000, sizeof(uint16_t), cmp_nop);
  405. for (i = 2500; i < 7500; i++)
  406. nop_sum += nops[i];
  407. return nop_sum / 500;
  408. }
  409. /* Print benchmark results */
  410. static void print_benchs(CheckasmFunc *f)
  411. {
  412. if (f) {
  413. print_benchs(f->child[0]);
  414. /* Only print functions with at least one assembly version */
  415. if (f->versions.cpu || f->versions.next) {
  416. CheckasmFuncVersion *v = &f->versions;
  417. do {
  418. CheckasmPerf *p = &v->perf;
  419. if (p->iterations) {
  420. int decicycles = (10*p->cycles/p->iterations - state.nop_time) / 4;
  421. printf("%s_%s: %d.%d\n", f->name, cpu_suffix(v->cpu), decicycles/10, decicycles%10);
  422. }
  423. } while ((v = v->next));
  424. }
  425. print_benchs(f->child[1]);
  426. }
  427. }
  428. /* ASCIIbetical sort except preserving natural order for numbers */
  429. static int cmp_func_names(const char *a, const char *b)
  430. {
  431. const char *start = a;
  432. int ascii_diff, digit_diff;
  433. for (; !(ascii_diff = *(const unsigned char*)a - *(const unsigned char*)b) && *a; a++, b++);
  434. for (; av_isdigit(*a) && av_isdigit(*b); a++, b++);
  435. if (a > start && av_isdigit(a[-1]) && (digit_diff = av_isdigit(*a) - av_isdigit(*b)))
  436. return digit_diff;
  437. return ascii_diff;
  438. }
  439. /* Perform a tree rotation in the specified direction and return the new root */
  440. static CheckasmFunc *rotate_tree(CheckasmFunc *f, int dir)
  441. {
  442. CheckasmFunc *r = f->child[dir^1];
  443. f->child[dir^1] = r->child[dir];
  444. r->child[dir] = f;
  445. r->color = f->color;
  446. f->color = 0;
  447. return r;
  448. }
  449. #define is_red(f) ((f) && !(f)->color)
  450. /* Balance a left-leaning red-black tree at the specified node */
  451. static void balance_tree(CheckasmFunc **root)
  452. {
  453. CheckasmFunc *f = *root;
  454. if (is_red(f->child[0]) && is_red(f->child[1])) {
  455. f->color ^= 1;
  456. f->child[0]->color = f->child[1]->color = 1;
  457. }
  458. if (!is_red(f->child[0]) && is_red(f->child[1]))
  459. *root = rotate_tree(f, 0); /* Rotate left */
  460. else if (is_red(f->child[0]) && is_red(f->child[0]->child[0]))
  461. *root = rotate_tree(f, 1); /* Rotate right */
  462. }
  463. /* Get a node with the specified name, creating it if it doesn't exist */
  464. static CheckasmFunc *get_func(CheckasmFunc **root, const char *name)
  465. {
  466. CheckasmFunc *f = *root;
  467. if (f) {
  468. /* Search the tree for a matching node */
  469. int cmp = cmp_func_names(name, f->name);
  470. if (cmp) {
  471. f = get_func(&f->child[cmp > 0], name);
  472. /* Rebalance the tree on the way up if a new node was inserted */
  473. if (!f->versions.func)
  474. balance_tree(root);
  475. }
  476. } else {
  477. /* Allocate and insert a new node into the tree */
  478. int name_length = strlen(name);
  479. f = *root = checkasm_malloc(sizeof(CheckasmFunc) + name_length);
  480. memcpy(f->name, name, name_length + 1);
  481. }
  482. return f;
  483. }
  484. /* Perform tests and benchmarks for the specified cpu flag if supported by the host */
  485. static void check_cpu_flag(const char *name, int flag)
  486. {
  487. int old_cpu_flag = state.cpu_flag;
  488. flag |= old_cpu_flag;
  489. av_force_cpu_flags(-1);
  490. state.cpu_flag = flag & av_get_cpu_flags();
  491. av_force_cpu_flags(state.cpu_flag);
  492. if (!flag || state.cpu_flag != old_cpu_flag) {
  493. int i;
  494. state.cpu_flag_name = name;
  495. for (i = 0; tests[i].func; i++) {
  496. if (state.test_name && strcmp(tests[i].name, state.test_name))
  497. continue;
  498. state.current_test_name = tests[i].name;
  499. tests[i].func();
  500. }
  501. }
  502. }
  503. /* Print the name of the current CPU flag, but only do it once */
  504. static void print_cpu_name(void)
  505. {
  506. if (state.cpu_flag_name) {
  507. color_printf(COLOR_YELLOW, "%s:\n", state.cpu_flag_name);
  508. state.cpu_flag_name = NULL;
  509. }
  510. }
  511. #if CONFIG_LINUX_PERF
  512. static int bench_init_linux(void)
  513. {
  514. struct perf_event_attr attr = {
  515. .type = PERF_TYPE_HARDWARE,
  516. .size = sizeof(struct perf_event_attr),
  517. .config = PERF_COUNT_HW_CPU_CYCLES,
  518. .disabled = 1, // start counting only on demand
  519. .exclude_kernel = 1,
  520. .exclude_hv = 1,
  521. };
  522. printf("benchmarking with Linux Perf Monitoring API\n");
  523. state.sysfd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0);
  524. if (state.sysfd == -1) {
  525. perror("syscall");
  526. return -1;
  527. }
  528. return 0;
  529. }
  530. #endif
  531. static int bench_init_ffmpeg(void)
  532. {
  533. #ifdef AV_READ_TIME
  534. printf("benchmarking with native FFmpeg timers\n");
  535. return 0;
  536. #else
  537. fprintf(stderr, "checkasm: --bench is not supported on your system\n");
  538. return -1;
  539. #endif
  540. }
  541. static int bench_init(void)
  542. {
  543. #if CONFIG_LINUX_PERF
  544. int ret = bench_init_linux();
  545. #else
  546. int ret = bench_init_ffmpeg();
  547. #endif
  548. if (ret < 0)
  549. return ret;
  550. state.nop_time = measure_nop_time();
  551. printf("nop: %d.%d\n", state.nop_time/10, state.nop_time%10);
  552. return 0;
  553. }
  554. static void bench_uninit(void)
  555. {
  556. #if CONFIG_LINUX_PERF
  557. if (state.sysfd > 0)
  558. close(state.sysfd);
  559. #endif
  560. }
  561. int main(int argc, char *argv[])
  562. {
  563. unsigned int seed = av_get_random_seed();
  564. int i, ret = 0;
  565. #if ARCH_ARM && HAVE_ARMV5TE_EXTERNAL
  566. if (have_vfp(av_get_cpu_flags()) || have_neon(av_get_cpu_flags()))
  567. checkasm_checked_call = checkasm_checked_call_vfp;
  568. #endif
  569. if (!tests[0].func || !cpus[0].flag) {
  570. fprintf(stderr, "checkasm: no tests to perform\n");
  571. return 0;
  572. }
  573. while (argc > 1) {
  574. if (!strncmp(argv[1], "--bench", 7)) {
  575. if (bench_init() < 0)
  576. return 1;
  577. if (argv[1][7] == '=') {
  578. state.bench_pattern = argv[1] + 8;
  579. state.bench_pattern_len = strlen(state.bench_pattern);
  580. } else
  581. state.bench_pattern = "";
  582. } else if (!strncmp(argv[1], "--test=", 7)) {
  583. state.test_name = argv[1] + 7;
  584. } else {
  585. seed = strtoul(argv[1], NULL, 10);
  586. }
  587. argc--;
  588. argv++;
  589. }
  590. fprintf(stderr, "checkasm: using random seed %u\n", seed);
  591. av_lfg_init(&checkasm_lfg, seed);
  592. check_cpu_flag(NULL, 0);
  593. for (i = 0; cpus[i].flag; i++)
  594. check_cpu_flag(cpus[i].name, cpus[i].flag);
  595. if (state.num_failed) {
  596. fprintf(stderr, "checkasm: %d of %d tests have failed\n", state.num_failed, state.num_checked);
  597. ret = 1;
  598. } else {
  599. fprintf(stderr, "checkasm: all %d tests passed\n", state.num_checked);
  600. if (state.bench_pattern) {
  601. print_benchs(state.funcs);
  602. }
  603. }
  604. destroy_func_tree(state.funcs);
  605. bench_uninit();
  606. return ret;
  607. }
  608. /* Decide whether or not the specified function needs to be tested and
  609. * allocate/initialize data structures if needed. Returns a pointer to a
  610. * reference function if the function should be tested, otherwise NULL */
  611. void *checkasm_check_func(void *func, const char *name, ...)
  612. {
  613. char name_buf[256];
  614. void *ref = func;
  615. CheckasmFuncVersion *v;
  616. int name_length;
  617. va_list arg;
  618. va_start(arg, name);
  619. name_length = vsnprintf(name_buf, sizeof(name_buf), name, arg);
  620. va_end(arg);
  621. if (!func || name_length <= 0 || name_length >= sizeof(name_buf))
  622. return NULL;
  623. state.current_func = get_func(&state.funcs, name_buf);
  624. state.funcs->color = 1;
  625. v = &state.current_func->versions;
  626. if (v->func) {
  627. CheckasmFuncVersion *prev;
  628. do {
  629. /* Only test functions that haven't already been tested */
  630. if (v->func == func)
  631. return NULL;
  632. if (v->ok)
  633. ref = v->func;
  634. prev = v;
  635. } while ((v = v->next));
  636. v = prev->next = checkasm_malloc(sizeof(CheckasmFuncVersion));
  637. }
  638. v->func = func;
  639. v->ok = 1;
  640. v->cpu = state.cpu_flag;
  641. state.current_func_ver = v;
  642. if (state.cpu_flag)
  643. state.num_checked++;
  644. return ref;
  645. }
  646. /* Decide whether or not the current function needs to be benchmarked */
  647. int checkasm_bench_func(void)
  648. {
  649. return !state.num_failed && state.bench_pattern &&
  650. !strncmp(state.current_func->name, state.bench_pattern, state.bench_pattern_len);
  651. }
  652. /* Indicate that the current test has failed */
  653. void checkasm_fail_func(const char *msg, ...)
  654. {
  655. if (state.current_func_ver->cpu && state.current_func_ver->ok) {
  656. va_list arg;
  657. print_cpu_name();
  658. fprintf(stderr, " %s_%s (", state.current_func->name, cpu_suffix(state.current_func_ver->cpu));
  659. va_start(arg, msg);
  660. vfprintf(stderr, msg, arg);
  661. va_end(arg);
  662. fprintf(stderr, ")\n");
  663. state.current_func_ver->ok = 0;
  664. state.num_failed++;
  665. }
  666. }
  667. /* Get the benchmark context of the current function */
  668. CheckasmPerf *checkasm_get_perf_context(void)
  669. {
  670. CheckasmPerf *perf = &state.current_func_ver->perf;
  671. memset(perf, 0, sizeof(*perf));
  672. perf->sysfd = state.sysfd;
  673. return perf;
  674. }
  675. /* Print the outcome of all tests performed since the last time this function was called */
  676. void checkasm_report(const char *name, ...)
  677. {
  678. static int prev_checked, prev_failed, max_length;
  679. if (state.num_checked > prev_checked) {
  680. int pad_length = max_length + 4;
  681. va_list arg;
  682. print_cpu_name();
  683. pad_length -= fprintf(stderr, " - %s.", state.current_test_name);
  684. va_start(arg, name);
  685. pad_length -= vfprintf(stderr, name, arg);
  686. va_end(arg);
  687. fprintf(stderr, "%*c", FFMAX(pad_length, 0) + 2, '[');
  688. if (state.num_failed == prev_failed)
  689. color_printf(COLOR_GREEN, "OK");
  690. else
  691. color_printf(COLOR_RED, "FAILED");
  692. fprintf(stderr, "]\n");
  693. prev_checked = state.num_checked;
  694. prev_failed = state.num_failed;
  695. } else if (!state.cpu_flag) {
  696. /* Calculate the amount of padding required to make the output vertically aligned */
  697. int length = strlen(state.current_test_name);
  698. va_list arg;
  699. va_start(arg, name);
  700. length += vsnprintf(NULL, 0, name, arg);
  701. va_end(arg);
  702. if (length > max_length)
  703. max_length = length;
  704. }
  705. }