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.

651 lines
17KB

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