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.

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