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.

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