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.

623 lines
17KB

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