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.

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