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.

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