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.

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