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.

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