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.

513 lines
14KB

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