checkasm.c 19 KB

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