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.

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