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.

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