checkasm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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 <stdarg.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include "checkasm.h"
  27. #include "libavutil/common.h"
  28. #include "libavutil/cpu.h"
  29. #include "libavutil/intfloat.h"
  30. #include "libavutil/random_seed.h"
  31. #if HAVE_IO_H
  32. #include <io.h>
  33. #endif
  34. #if HAVE_SETCONSOLETEXTATTRIBUTE
  35. #include <windows.h>
  36. #define COLOR_RED FOREGROUND_RED
  37. #define COLOR_GREEN FOREGROUND_GREEN
  38. #define COLOR_YELLOW (FOREGROUND_RED|FOREGROUND_GREEN)
  39. #else
  40. #define COLOR_RED 1
  41. #define COLOR_GREEN 2
  42. #define COLOR_YELLOW 3
  43. #endif
  44. #if HAVE_UNISTD_H
  45. #include <unistd.h>
  46. #endif
  47. #if !HAVE_ISATTY
  48. #define isatty(fd) 1
  49. #endif
  50. #if ARCH_ARM && HAVE_ARMV5TE_EXTERNAL
  51. #include "libavutil/arm/cpu.h"
  52. void (*checkasm_checked_call)(void *func, int dummy, ...) = checkasm_checked_call_novfp;
  53. #endif
  54. /* List of tests to invoke */
  55. static const struct {
  56. const char *name;
  57. void (*func)(void);
  58. } tests[] = {
  59. #if CONFIG_AVCODEC
  60. #if CONFIG_ALAC_DECODER
  61. { "alacdsp", checkasm_check_alacdsp },
  62. #endif
  63. #if CONFIG_AUDIODSP
  64. { "audiodsp", checkasm_check_audiodsp },
  65. #endif
  66. #if CONFIG_BLOCKDSP
  67. { "blockdsp", checkasm_check_blockdsp },
  68. #endif
  69. #if CONFIG_BSWAPDSP
  70. { "bswapdsp", checkasm_check_bswapdsp },
  71. #endif
  72. #if CONFIG_DCA_DECODER
  73. { "synth_filter", checkasm_check_synth_filter },
  74. #endif
  75. #if CONFIG_FLACDSP
  76. { "flacdsp", checkasm_check_flacdsp },
  77. #endif
  78. #if CONFIG_FMTCONVERT
  79. { "fmtconvert", checkasm_check_fmtconvert },
  80. #endif
  81. #if CONFIG_H264DSP
  82. { "h264dsp", checkasm_check_h264dsp },
  83. #endif
  84. #if CONFIG_H264PRED
  85. { "h264pred", checkasm_check_h264pred },
  86. #endif
  87. #if CONFIG_H264QPEL
  88. { "h264qpel", checkasm_check_h264qpel },
  89. #endif
  90. #if CONFIG_HEVC_DECODER
  91. { "hevc_add_res", checkasm_check_hevc_add_res },
  92. { "hevc_idct", checkasm_check_hevc_idct },
  93. #endif
  94. #if CONFIG_JPEG2000_DECODER
  95. { "jpeg2000dsp", checkasm_check_jpeg2000dsp },
  96. #endif
  97. #if CONFIG_HUFFYUVDSP
  98. { "llviddsp", checkasm_check_llviddsp },
  99. #endif
  100. #if CONFIG_PIXBLOCKDSP
  101. { "pixblockdsp", checkasm_check_pixblockdsp },
  102. #endif
  103. #if CONFIG_V210_ENCODER
  104. { "v210enc", checkasm_check_v210enc },
  105. #endif
  106. #if CONFIG_VP8DSP
  107. { "vp8dsp", checkasm_check_vp8dsp },
  108. #endif
  109. #if CONFIG_VP9_DECODER
  110. { "vp9dsp", checkasm_check_vp9dsp },
  111. #endif
  112. #if CONFIG_VIDEODSP
  113. { "videodsp", checkasm_check_videodsp },
  114. #endif
  115. #endif
  116. #if CONFIG_AVFILTER
  117. #if CONFIG_BLEND_FILTER
  118. { "vf_blend", checkasm_check_blend },
  119. #endif
  120. #if CONFIG_COLORSPACE_FILTER
  121. { "vf_colorspace", checkasm_check_colorspace },
  122. #endif
  123. #endif
  124. { NULL }
  125. };
  126. /* List of cpu flags to check */
  127. static const struct {
  128. const char *name;
  129. const char *suffix;
  130. int flag;
  131. } cpus[] = {
  132. #if ARCH_AARCH64
  133. { "ARMV8", "armv8", AV_CPU_FLAG_ARMV8 },
  134. { "NEON", "neon", AV_CPU_FLAG_NEON },
  135. #elif ARCH_ARM
  136. { "ARMV5TE", "armv5te", AV_CPU_FLAG_ARMV5TE },
  137. { "ARMV6", "armv6", AV_CPU_FLAG_ARMV6 },
  138. { "ARMV6T2", "armv6t2", AV_CPU_FLAG_ARMV6T2 },
  139. { "VFP", "vfp", AV_CPU_FLAG_VFP },
  140. { "VFP_VM", "vfp_vm", AV_CPU_FLAG_VFP_VM },
  141. { "VFPV3", "vfp3", AV_CPU_FLAG_VFPV3 },
  142. { "NEON", "neon", AV_CPU_FLAG_NEON },
  143. #elif ARCH_PPC
  144. { "ALTIVEC", "altivec", AV_CPU_FLAG_ALTIVEC },
  145. { "VSX", "vsx", AV_CPU_FLAG_VSX },
  146. { "POWER8", "power8", AV_CPU_FLAG_POWER8 },
  147. #elif ARCH_X86
  148. { "MMX", "mmx", AV_CPU_FLAG_MMX|AV_CPU_FLAG_CMOV },
  149. { "MMXEXT", "mmxext", AV_CPU_FLAG_MMXEXT },
  150. { "3DNOW", "3dnow", AV_CPU_FLAG_3DNOW },
  151. { "3DNOWEXT", "3dnowext", AV_CPU_FLAG_3DNOWEXT },
  152. { "SSE", "sse", AV_CPU_FLAG_SSE },
  153. { "SSE2", "sse2", AV_CPU_FLAG_SSE2|AV_CPU_FLAG_SSE2SLOW },
  154. { "SSE3", "sse3", AV_CPU_FLAG_SSE3|AV_CPU_FLAG_SSE3SLOW },
  155. { "SSSE3", "ssse3", AV_CPU_FLAG_SSSE3|AV_CPU_FLAG_ATOM },
  156. { "SSE4.1", "sse4", AV_CPU_FLAG_SSE4 },
  157. { "SSE4.2", "sse42", AV_CPU_FLAG_SSE42 },
  158. { "AES-NI", "aesni", AV_CPU_FLAG_AESNI },
  159. { "AVX", "avx", AV_CPU_FLAG_AVX },
  160. { "XOP", "xop", AV_CPU_FLAG_XOP },
  161. { "FMA3", "fma3", AV_CPU_FLAG_FMA3 },
  162. { "FMA4", "fma4", AV_CPU_FLAG_FMA4 },
  163. { "AVX2", "avx2", AV_CPU_FLAG_AVX2 },
  164. #endif
  165. { NULL }
  166. };
  167. typedef struct CheckasmFuncVersion {
  168. struct CheckasmFuncVersion *next;
  169. void *func;
  170. int ok;
  171. int cpu;
  172. int iterations;
  173. uint64_t cycles;
  174. } CheckasmFuncVersion;
  175. /* Binary search tree node */
  176. typedef struct CheckasmFunc {
  177. struct CheckasmFunc *child[2];
  178. CheckasmFuncVersion versions;
  179. uint8_t color; /* 0 = red, 1 = black */
  180. char name[1];
  181. } CheckasmFunc;
  182. /* Internal state */
  183. static struct {
  184. CheckasmFunc *funcs;
  185. CheckasmFunc *current_func;
  186. CheckasmFuncVersion *current_func_ver;
  187. const char *current_test_name;
  188. const char *bench_pattern;
  189. int bench_pattern_len;
  190. int num_checked;
  191. int num_failed;
  192. int nop_time;
  193. int cpu_flag;
  194. const char *cpu_flag_name;
  195. const char *test_name;
  196. } state;
  197. /* PRNG state */
  198. AVLFG checkasm_lfg;
  199. /* float compare support code */
  200. static int is_negative(union av_intfloat32 u)
  201. {
  202. return u.i >> 31;
  203. }
  204. int float_near_ulp(float a, float b, unsigned max_ulp)
  205. {
  206. union av_intfloat32 x, y;
  207. x.f = a;
  208. y.f = b;
  209. if (is_negative(x) != is_negative(y)) {
  210. // handle -0.0 == +0.0
  211. return a == b;
  212. }
  213. if (llabs((int64_t)x.i - y.i) <= max_ulp)
  214. return 1;
  215. return 0;
  216. }
  217. int float_near_ulp_array(const float *a, const float *b, unsigned max_ulp,
  218. unsigned len)
  219. {
  220. unsigned i;
  221. for (i = 0; i < len; i++) {
  222. if (!float_near_ulp(a[i], b[i], max_ulp))
  223. return 0;
  224. }
  225. return 1;
  226. }
  227. int float_near_abs_eps(float a, float b, float eps)
  228. {
  229. float abs_diff = fabsf(a - b);
  230. return abs_diff < eps;
  231. }
  232. int float_near_abs_eps_array(const float *a, const float *b, float eps,
  233. unsigned len)
  234. {
  235. unsigned i;
  236. for (i = 0; i < len; i++) {
  237. if (!float_near_abs_eps(a[i], b[i], eps))
  238. return 0;
  239. }
  240. return 1;
  241. }
  242. int float_near_abs_eps_ulp(float a, float b, float eps, unsigned max_ulp)
  243. {
  244. return float_near_ulp(a, b, max_ulp) || float_near_abs_eps(a, b, eps);
  245. }
  246. int float_near_abs_eps_array_ulp(const float *a, const float *b, float eps,
  247. unsigned max_ulp, unsigned len)
  248. {
  249. unsigned i;
  250. for (i = 0; i < len; i++) {
  251. if (!float_near_abs_eps_ulp(a[i], b[i], eps, max_ulp))
  252. return 0;
  253. }
  254. return 1;
  255. }
  256. /* Print colored text to stderr if the terminal supports it */
  257. static void color_printf(int color, const char *fmt, ...)
  258. {
  259. static int use_color = -1;
  260. va_list arg;
  261. #if HAVE_SETCONSOLETEXTATTRIBUTE
  262. static HANDLE con;
  263. static WORD org_attributes;
  264. if (use_color < 0) {
  265. CONSOLE_SCREEN_BUFFER_INFO con_info;
  266. con = GetStdHandle(STD_ERROR_HANDLE);
  267. if (con && con != INVALID_HANDLE_VALUE && GetConsoleScreenBufferInfo(con, &con_info)) {
  268. org_attributes = con_info.wAttributes;
  269. use_color = 1;
  270. } else
  271. use_color = 0;
  272. }
  273. if (use_color)
  274. SetConsoleTextAttribute(con, (org_attributes & 0xfff0) | (color & 0x0f));
  275. #else
  276. if (use_color < 0) {
  277. const char *term = getenv("TERM");
  278. use_color = term && strcmp(term, "dumb") && isatty(2);
  279. }
  280. if (use_color)
  281. fprintf(stderr, "\x1b[%d;3%dm", (color & 0x08) >> 3, color & 0x07);
  282. #endif
  283. va_start(arg, fmt);
  284. vfprintf(stderr, fmt, arg);
  285. va_end(arg);
  286. if (use_color) {
  287. #if HAVE_SETCONSOLETEXTATTRIBUTE
  288. SetConsoleTextAttribute(con, org_attributes);
  289. #else
  290. fprintf(stderr, "\x1b[0m");
  291. #endif
  292. }
  293. }
  294. /* Deallocate a tree */
  295. static void destroy_func_tree(CheckasmFunc *f)
  296. {
  297. if (f) {
  298. CheckasmFuncVersion *v = f->versions.next;
  299. while (v) {
  300. CheckasmFuncVersion *next = v->next;
  301. free(v);
  302. v = next;
  303. }
  304. destroy_func_tree(f->child[0]);
  305. destroy_func_tree(f->child[1]);
  306. free(f);
  307. }
  308. }
  309. /* Allocate a zero-initialized block, clean up and exit on failure */
  310. static void *checkasm_malloc(size_t size)
  311. {
  312. void *ptr = calloc(1, size);
  313. if (!ptr) {
  314. fprintf(stderr, "checkasm: malloc failed\n");
  315. destroy_func_tree(state.funcs);
  316. exit(1);
  317. }
  318. return ptr;
  319. }
  320. /* Get the suffix of the specified cpu flag */
  321. static const char *cpu_suffix(int cpu)
  322. {
  323. int i = FF_ARRAY_ELEMS(cpus);
  324. while (--i >= 0)
  325. if (cpu & cpus[i].flag)
  326. return cpus[i].suffix;
  327. return "c";
  328. }
  329. #ifdef AV_READ_TIME
  330. static int cmp_nop(const void *a, const void *b)
  331. {
  332. return *(const uint16_t*)a - *(const uint16_t*)b;
  333. }
  334. /* Measure the overhead of the timing code (in decicycles) */
  335. static int measure_nop_time(void)
  336. {
  337. uint16_t nops[10000];
  338. int i, nop_sum = 0;
  339. for (i = 0; i < 10000; i++) {
  340. uint64_t t = AV_READ_TIME();
  341. nops[i] = AV_READ_TIME() - t;
  342. }
  343. qsort(nops, 10000, sizeof(uint16_t), cmp_nop);
  344. for (i = 2500; i < 7500; i++)
  345. nop_sum += nops[i];
  346. return nop_sum / 500;
  347. }
  348. /* Print benchmark results */
  349. static void print_benchs(CheckasmFunc *f)
  350. {
  351. if (f) {
  352. print_benchs(f->child[0]);
  353. /* Only print functions with at least one assembly version */
  354. if (f->versions.cpu || f->versions.next) {
  355. CheckasmFuncVersion *v = &f->versions;
  356. do {
  357. if (v->iterations) {
  358. int decicycles = (10*v->cycles/v->iterations - state.nop_time) / 4;
  359. printf("%s_%s: %d.%d\n", f->name, cpu_suffix(v->cpu), decicycles/10, decicycles%10);
  360. }
  361. } while ((v = v->next));
  362. }
  363. print_benchs(f->child[1]);
  364. }
  365. }
  366. #endif
  367. /* ASCIIbetical sort except preserving natural order for numbers */
  368. static int cmp_func_names(const char *a, const char *b)
  369. {
  370. const char *start = a;
  371. int ascii_diff, digit_diff;
  372. for (; !(ascii_diff = *(const unsigned char*)a - *(const unsigned char*)b) && *a; a++, b++);
  373. for (; av_isdigit(*a) && av_isdigit(*b); a++, b++);
  374. if (a > start && av_isdigit(a[-1]) && (digit_diff = av_isdigit(*a) - av_isdigit(*b)))
  375. return digit_diff;
  376. return ascii_diff;
  377. }
  378. /* Perform a tree rotation in the specified direction and return the new root */
  379. static CheckasmFunc *rotate_tree(CheckasmFunc *f, int dir)
  380. {
  381. CheckasmFunc *r = f->child[dir^1];
  382. f->child[dir^1] = r->child[dir];
  383. r->child[dir] = f;
  384. r->color = f->color;
  385. f->color = 0;
  386. return r;
  387. }
  388. #define is_red(f) ((f) && !(f)->color)
  389. /* Balance a left-leaning red-black tree at the specified node */
  390. static void balance_tree(CheckasmFunc **root)
  391. {
  392. CheckasmFunc *f = *root;
  393. if (is_red(f->child[0]) && is_red(f->child[1])) {
  394. f->color ^= 1;
  395. f->child[0]->color = f->child[1]->color = 1;
  396. }
  397. if (!is_red(f->child[0]) && is_red(f->child[1]))
  398. *root = rotate_tree(f, 0); /* Rotate left */
  399. else if (is_red(f->child[0]) && is_red(f->child[0]->child[0]))
  400. *root = rotate_tree(f, 1); /* Rotate right */
  401. }
  402. /* Get a node with the specified name, creating it if it doesn't exist */
  403. static CheckasmFunc *get_func(CheckasmFunc **root, const char *name)
  404. {
  405. CheckasmFunc *f = *root;
  406. if (f) {
  407. /* Search the tree for a matching node */
  408. int cmp = cmp_func_names(name, f->name);
  409. if (cmp) {
  410. f = get_func(&f->child[cmp > 0], name);
  411. /* Rebalance the tree on the way up if a new node was inserted */
  412. if (!f->versions.func)
  413. balance_tree(root);
  414. }
  415. } else {
  416. /* Allocate and insert a new node into the tree */
  417. int name_length = strlen(name);
  418. f = *root = checkasm_malloc(sizeof(CheckasmFunc) + name_length);
  419. memcpy(f->name, name, name_length + 1);
  420. }
  421. return f;
  422. }
  423. /* Perform tests and benchmarks for the specified cpu flag if supported by the host */
  424. static void check_cpu_flag(const char *name, int flag)
  425. {
  426. int old_cpu_flag = state.cpu_flag;
  427. flag |= old_cpu_flag;
  428. av_force_cpu_flags(-1);
  429. state.cpu_flag = flag & av_get_cpu_flags();
  430. av_force_cpu_flags(state.cpu_flag);
  431. if (!flag || state.cpu_flag != old_cpu_flag) {
  432. int i;
  433. state.cpu_flag_name = name;
  434. for (i = 0; tests[i].func; i++) {
  435. if (state.test_name && strcmp(tests[i].name, state.test_name))
  436. continue;
  437. state.current_test_name = tests[i].name;
  438. tests[i].func();
  439. }
  440. }
  441. }
  442. /* Print the name of the current CPU flag, but only do it once */
  443. static void print_cpu_name(void)
  444. {
  445. if (state.cpu_flag_name) {
  446. color_printf(COLOR_YELLOW, "%s:\n", state.cpu_flag_name);
  447. state.cpu_flag_name = NULL;
  448. }
  449. }
  450. int main(int argc, char *argv[])
  451. {
  452. unsigned int seed = av_get_random_seed();
  453. int i, ret = 0;
  454. #if ARCH_ARM && HAVE_ARMV5TE_EXTERNAL
  455. if (have_vfp(av_get_cpu_flags()) || have_neon(av_get_cpu_flags()))
  456. checkasm_checked_call = checkasm_checked_call_vfp;
  457. #endif
  458. if (!tests[0].func || !cpus[0].flag) {
  459. fprintf(stderr, "checkasm: no tests to perform\n");
  460. return 0;
  461. }
  462. while (argc > 1) {
  463. if (!strncmp(argv[1], "--bench", 7)) {
  464. #ifndef AV_READ_TIME
  465. fprintf(stderr, "checkasm: --bench is not supported on your system\n");
  466. return 1;
  467. #endif
  468. if (argv[1][7] == '=') {
  469. state.bench_pattern = argv[1] + 8;
  470. state.bench_pattern_len = strlen(state.bench_pattern);
  471. } else
  472. state.bench_pattern = "";
  473. } else if (!strncmp(argv[1], "--test=", 7)) {
  474. state.test_name = argv[1] + 7;
  475. } else {
  476. seed = strtoul(argv[1], NULL, 10);
  477. }
  478. argc--;
  479. argv++;
  480. }
  481. fprintf(stderr, "checkasm: using random seed %u\n", seed);
  482. av_lfg_init(&checkasm_lfg, seed);
  483. check_cpu_flag(NULL, 0);
  484. for (i = 0; cpus[i].flag; i++)
  485. check_cpu_flag(cpus[i].name, cpus[i].flag);
  486. if (state.num_failed) {
  487. fprintf(stderr, "checkasm: %d of %d tests have failed\n", state.num_failed, state.num_checked);
  488. ret = 1;
  489. } else {
  490. fprintf(stderr, "checkasm: all %d tests passed\n", state.num_checked);
  491. #ifdef AV_READ_TIME
  492. if (state.bench_pattern) {
  493. state.nop_time = measure_nop_time();
  494. printf("nop: %d.%d\n", state.nop_time/10, state.nop_time%10);
  495. print_benchs(state.funcs);
  496. }
  497. #endif
  498. }
  499. destroy_func_tree(state.funcs);
  500. return ret;
  501. }
  502. /* Decide whether or not the specified function needs to be tested and
  503. * allocate/initialize data structures if needed. Returns a pointer to a
  504. * reference function if the function should be tested, otherwise NULL */
  505. void *checkasm_check_func(void *func, const char *name, ...)
  506. {
  507. char name_buf[256];
  508. void *ref = func;
  509. CheckasmFuncVersion *v;
  510. int name_length;
  511. va_list arg;
  512. va_start(arg, name);
  513. name_length = vsnprintf(name_buf, sizeof(name_buf), name, arg);
  514. va_end(arg);
  515. if (!func || name_length <= 0 || name_length >= sizeof(name_buf))
  516. return NULL;
  517. state.current_func = get_func(&state.funcs, name_buf);
  518. state.funcs->color = 1;
  519. v = &state.current_func->versions;
  520. if (v->func) {
  521. CheckasmFuncVersion *prev;
  522. do {
  523. /* Only test functions that haven't already been tested */
  524. if (v->func == func)
  525. return NULL;
  526. if (v->ok)
  527. ref = v->func;
  528. prev = v;
  529. } while ((v = v->next));
  530. v = prev->next = checkasm_malloc(sizeof(CheckasmFuncVersion));
  531. }
  532. v->func = func;
  533. v->ok = 1;
  534. v->cpu = state.cpu_flag;
  535. state.current_func_ver = v;
  536. if (state.cpu_flag)
  537. state.num_checked++;
  538. return ref;
  539. }
  540. /* Decide whether or not the current function needs to be benchmarked */
  541. int checkasm_bench_func(void)
  542. {
  543. return !state.num_failed && state.bench_pattern &&
  544. !strncmp(state.current_func->name, state.bench_pattern, state.bench_pattern_len);
  545. }
  546. /* Indicate that the current test has failed */
  547. void checkasm_fail_func(const char *msg, ...)
  548. {
  549. if (state.current_func_ver->cpu && state.current_func_ver->ok) {
  550. va_list arg;
  551. print_cpu_name();
  552. fprintf(stderr, " %s_%s (", state.current_func->name, cpu_suffix(state.current_func_ver->cpu));
  553. va_start(arg, msg);
  554. vfprintf(stderr, msg, arg);
  555. va_end(arg);
  556. fprintf(stderr, ")\n");
  557. state.current_func_ver->ok = 0;
  558. state.num_failed++;
  559. }
  560. }
  561. /* Update benchmark results of the current function */
  562. void checkasm_update_bench(int iterations, uint64_t cycles)
  563. {
  564. state.current_func_ver->iterations += iterations;
  565. state.current_func_ver->cycles += cycles;
  566. }
  567. /* Print the outcome of all tests performed since the last time this function was called */
  568. void checkasm_report(const char *name, ...)
  569. {
  570. static int prev_checked, prev_failed, max_length;
  571. if (state.num_checked > prev_checked) {
  572. int pad_length = max_length + 4;
  573. va_list arg;
  574. print_cpu_name();
  575. pad_length -= fprintf(stderr, " - %s.", state.current_test_name);
  576. va_start(arg, name);
  577. pad_length -= vfprintf(stderr, name, arg);
  578. va_end(arg);
  579. fprintf(stderr, "%*c", FFMAX(pad_length, 0) + 2, '[');
  580. if (state.num_failed == prev_failed)
  581. color_printf(COLOR_GREEN, "OK");
  582. else
  583. color_printf(COLOR_RED, "FAILED");
  584. fprintf(stderr, "]\n");
  585. prev_checked = state.num_checked;
  586. prev_failed = state.num_failed;
  587. } else if (!state.cpu_flag) {
  588. /* Calculate the amount of padding required to make the output vertically aligned */
  589. int length = strlen(state.current_test_name);
  590. va_list arg;
  591. va_start(arg, name);
  592. length += vsnprintf(NULL, 0, name, arg);
  593. va_end(arg);
  594. if (length > max_length)
  595. max_length = length;
  596. }
  597. }