checkasm.c 22 KB

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