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.

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