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.

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