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.

488 lines
13KB

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