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.

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