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.

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