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.

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