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.

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