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.

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