checkasm.c 13 KB

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