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.

550 lines
15KB

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