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.

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