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.

657 lines
18KB

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