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.

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