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.

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