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.

565 lines
16KB

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