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.

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