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.

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