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.

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