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.

661 lines
18KB

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