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.

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