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.

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