FFmpeg
Loading...
Searching...
No Matches
utils.c
Go to the documentation of this file.
1/*
2 * Copyright © 2025, Niklas Haas
3 * Copyright © 2018, VideoLAN and dav1d authors
4 * Copyright © 2018, Two Orioles, LLC
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <assert.h>
30#include <inttypes.h>
31#include <limits.h>
32#include <math.h>
33#include <stdarg.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37#include <time.h>
38
39#include "checkasm_config.h"
40
41#ifdef _WIN32
42 #include <windows.h>
43 #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
44 #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x04
45 #endif
46#else
47 #if HAVE_ISATTY
48 #include <unistd.h>
49 #endif
50 #if HAVE_IOCTL
51 #include <sys/ioctl.h>
52 #endif
53#endif
54
55#if defined(__APPLE__) && defined(__MACH__)
56 #include <mach/mach_time.h>
57#endif
58
59#include "checkasm/test.h"
60#include "checkasm/utils.h"
61#include "internal.h"
62
63NOINLINE void checkasm_noop(void *ptr)
64{
65 (void) ptr;
66}
67
68static ALWAYS_INLINE uint64_t gettime_nsec(int is_seed)
69{
70#ifdef _WIN32
71 static LARGE_INTEGER freq;
72 LARGE_INTEGER ts;
73 if (!freq.QuadPart) {
74 if (!QueryPerformanceFrequency(&freq))
75 return -1;
76 }
77 if (!QueryPerformanceCounter(&ts))
78 return -1;
79 return UINT64_C(1000000000) * ts.QuadPart / freq.QuadPart;
80#elif defined(__APPLE__) && defined(__MACH__)
81 static mach_timebase_info_data_t tb_info;
82 if (!tb_info.denom) {
83 if (mach_timebase_info(&tb_info) != KERN_SUCCESS)
84 return -1;
85 }
86 return mach_absolute_time() * tb_info.numer / tb_info.denom;
87#elif HAVE_CLOCK_GETTIME
88 struct timespec ts;
89 clockid_t id;
90 if (!is_seed) {
91 #ifdef CLOCK_MONOTONIC_RAW
92 id = CLOCK_MONOTONIC_RAW;
93 #else
94 id = CLOCK_MONOTONIC;
95 #endif
96 } else {
97 id = CLOCK_REALTIME;
98 }
99 if (clock_gettime(id, &ts) < 0)
100 return -1;
101 return UINT64_C(1000000000) * ts.tv_sec + ts.tv_nsec;
102#else
103 return -1;
104#endif
105}
106
108{
109 return gettime_nsec(0);
110}
111
112uint64_t checkasm_gettime_nsec_diff(uint64_t t)
113{
114 return gettime_nsec(0) - t;
115}
116
117unsigned checkasm_seed(void)
118{
119 return (unsigned) gettime_nsec(1);
120}
121
122// (parallel) xoshiro128++ from https://prng.di.unimi.it/
123typedef struct CheckasmRand {
124#define CHECKASM_PRNG_NUM 4
130
132
133static ALWAYS_INLINE uint32_t rotl(const uint32_t x, int k)
134{
135 return (x << k) | (x >> (32 - k));
136}
137
138/* Single round of a parallel xoshiro128++, generates a full block */
139static ALWAYS_INLINE void xoshiro128pp(CheckasmRand *restrict xs, uint32_t *restrict buf)
140{
141 for (int i = 0; i < CHECKASM_PRNG_NUM; i++) {
142 buf[i] = rotl(xs->s0[i] + xs->s3[i], 7) + xs->s0[i];
143
144 const uint32_t t = xs->s1[i] << 9;
145 xs->s2[i] ^= xs->s0[i];
146 xs->s3[i] ^= xs->s1[i];
147 xs->s1[i] ^= xs->s2[i];
148 xs->s0[i] ^= xs->s3[i];
149 xs->s2[i] ^= t;
150 xs->s3[i] = rotl(xs->s3[i], 11);
151 }
152}
153
154static void prng(CheckasmRand *restrict xs, uint8_t *restrict buf, size_t size)
155{
156 uint32_t tmp[CHECKASM_PRNG_NUM];
157 const size_t block_size = sizeof(tmp);
158 CheckasmRand xs_copy = *xs;
159
160 while (size >= block_size) {
161 xoshiro128pp(&xs_copy, tmp);
162 memcpy(buf, tmp, block_size);
163 buf += block_size;
164 size -= block_size;
165 }
166
167 if (size) {
168 xoshiro128pp(&xs_copy, tmp);
169 memcpy(buf, tmp, size);
170 }
171
172 *xs = xs_copy;
173}
174
175/* Efficient wrapper for generating individual random integers, by caching
176 * the result of a single call to the underlying generator() */
177static struct {
178 #define PRNG_CACHE_SIZE 64
180 uint16_t buf16[PRNG_CACHE_SIZE >> 1];
181 uint32_t buf32[PRNG_CACHE_SIZE >> 2];
182 uint64_t buf64[PRNG_CACHE_SIZE >> 3];
183 int num8;
184 int num16;
185 int num32;
186 int num64;
188
189static_assert(PRNG_CACHE_SIZE % sizeof(uint32_t[CHECKASM_PRNG_NUM]) == 0,
190 "PRNG_CACHE_SIZE should be a multiple of uint32_t[CHECKASM_PRNG_NUM]");
191
192#define DEF_CHECKASM_RAND(BITS, TYPE, NAME) \
193 TYPE checkasm_rand_##NAME(void) \
194 { \
195 if (!prng_cache.num##BITS) { \
196 prng(&checkasm_prng, (uint8_t *) prng_cache.buf##BITS, \
197 sizeof(prng_cache.buf##BITS)); \
198 prng_cache.num##BITS = ARRAY_SIZE(prng_cache.buf##BITS); \
199 } \
200 \
201 union { \
202 TYPE type; \
203 uint##BITS##_t raw; \
204 } val; \
205 val.raw = prng_cache.buf##BITS[--prng_cache.num##BITS]; \
206 return val.type; \
207 }
208
209DEF_CHECKASM_RAND(8, int8_t, int8)
210DEF_CHECKASM_RAND(8, uint8_t, uint8)
211DEF_CHECKASM_RAND(16, int16_t, int16)
212DEF_CHECKASM_RAND(16, uint16_t, uint16)
213DEF_CHECKASM_RAND(32, int32_t, int32)
214DEF_CHECKASM_RAND(32, uint32_t, uint32)
215DEF_CHECKASM_RAND(32, float, float32)
216DEF_CHECKASM_RAND(64, int64_t, int64)
217DEF_CHECKASM_RAND(64, uint64_t, uint64)
218DEF_CHECKASM_RAND(64, double, float64)
219
220static inline uint64_t splitmix64(uint64_t *state)
221{
222 uint64_t z = (*state += 0x9e3779b97f4a7c15);
223
224 z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
225 z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
226 return z ^ (z >> 31);
227}
228
229void checkasm_srand(unsigned seed)
230{
231 /* Seed using splitmix64() as recommended by xoroshiro128 authors */
232 uint64_t s = seed;
233
234 for (int i = 0; i < CHECKASM_PRNG_NUM; i++) {
235 const uint64_t a = splitmix64(&s);
236 const uint64_t b = splitmix64(&s);
237
238 checkasm_prng.s0[i] = (uint32_t) a;
239 checkasm_prng.s1[i] = (uint32_t) b;
240 checkasm_prng.s2[i] = a >> 32;
241 checkasm_prng.s3[i] = b >> 32;
242 }
243
244 /* discard cached random bytes */
245 prng_cache.num8 = prng_cache.num16 = prng_cache.num32 = prng_cache.num64 = 0;
246}
247
249{
250 static_assert(sizeof(int) <= sizeof(uint32_t), "int larger than 32 bits");
251 return checkasm_rand_uint32() & INT_MAX;
252}
253
254double checkasm_randf(void)
255{
256 return checkasm_rand_uint32() / (UINT32_MAX + 1.0);
257}
258
259/* Marsaglia polar method */
260static inline double marsaglia(double *z2)
261{
262 double u1, u2, w;
263 do {
264 u1 = 2.0 / UINT32_MAX * checkasm_rand_uint32() - 1.0;
265 u2 = 2.0 / UINT32_MAX * checkasm_rand_uint32() - 1.0;
266 w = u1 * u1 + u2 * u2;
267 } while (w >= 1.0);
268
269 w = sqrt((-2.0 * log(w)) / w);
270 *z2 = u2 * w;
271 return u1 * w;
272}
273
275{
276 static int cached;
277 static double cache;
278 if ((cached = !cached)) {
279 return marsaglia(&cache);
280 } else {
281 return cache;
282 }
283}
284
286{
287 return dist.mean + dist.stddev * checkasm_rand_norm();
288}
289
290void checkasm_randomize(void *buf, size_t bytes)
291{
292 prng(&checkasm_prng, buf, bytes);
293}
294
295void checkasm_randomize_mask8(uint8_t *buf, int width, uint8_t mask)
296{
297 prng(&checkasm_prng, (uint8_t *) buf, width * sizeof(*buf));
298 for (int i = 0; i < width; i++)
299 buf[i] &= mask;
300}
301
302void checkasm_randomize_mask16(uint16_t *buf, int width, uint16_t mask)
303{
304 prng(&checkasm_prng, (uint8_t *) buf, width * sizeof(*buf));
305 for (int i = 0; i < width; i++)
306 buf[i] &= mask;
307}
308
309void checkasm_randomize_range(double *buf, int width, double range)
310{
311 const double scale = range / (UINT32_MAX + 1.0);
312 while (width--)
313 *buf++ = scale * checkasm_rand_uint32();
314}
315
316void checkasm_randomize_rangef(float *buf, int width, float range)
317{
318 const float scale = (float) (range / (UINT32_MAX + 1.0));
319 while (width--)
320 *buf++ = scale * checkasm_rand_uint32();
321}
322
323void checkasm_randomize_interval(double *buf, int width, double low, double high)
324{
325 const double scale = (high - low) / (double) UINT32_MAX;
326 while (width--)
327 *buf++ = scale * checkasm_rand_uint32() + low;
328}
329
330void checkasm_randomize_intervalf(float *buf, int width, float low, float high)
331{
332 const float scale = (high - low) / (float) UINT32_MAX;
333 while (width--)
334 *buf++ = scale * checkasm_rand_uint32() + low;
335}
336
337#define RANDOMIZE_DIST(buf, ftype, width, mean, stddev) \
338 do { \
339 if ((width) & 1) { \
340 *(buf)++ = (ftype) ((mean) + (stddev) * checkasm_rand_norm()); \
341 (width) ^= 1; \
342 } \
343 \
344 for (; width; width -= 2) { \
345 double z1, z2; \
346 z1 = marsaglia(&z2); \
347 *(buf)++ = (ftype) ((mean) + (stddev) * z1); \
348 *(buf)++ = (ftype) ((mean) + (stddev) * z2); \
349 } \
350 } while (0)
351
352void checkasm_randomize_dist(double *buf, int width, CheckasmDist dist)
353{
354 RANDOMIZE_DIST(buf, double, width, dist.mean, dist.stddev);
355}
356
357void checkasm_randomize_distf(float *buf, int width, CheckasmDist dist)
358{
359 RANDOMIZE_DIST(buf, float, width, dist.mean, dist.stddev);
360}
361
362void checkasm_randomize_norm(double *buf, int width)
363{
364 RANDOMIZE_DIST(buf, double, width, 0.0, 1.0);
365}
366
367void checkasm_randomize_normf(float *buf, int width)
368{
369 RANDOMIZE_DIST(buf, float, width, 0.0, 1.0);
370}
371
372void checkasm_clear(void *buf, size_t bytes)
373{
374 memset(buf, 0xAA, bytes);
375}
376
377void checkasm_clear8(uint8_t *buf, int width, uint8_t val)
378{
379 memset(buf, val, width);
380}
381
382void checkasm_clear16(uint16_t *buf, int width, uint16_t val)
383{
384 while (width--)
385 *buf++ = val;
386}
387
388#if HAVE_STDBIT_H
389 #include <stdbit.h>
390
391static inline int clz(const unsigned int mask)
392{
394}
395
396#elif defined(_MSC_VER) && !defined(__clang__)
397 #include <intrin.h>
398
399static inline int clz(const unsigned int mask)
400{
401 unsigned long leading_zero = 0;
402 _BitScanReverse(&leading_zero, mask);
403 return (31 - leading_zero);
404}
405
406#else /* !_MSC_VER */
407static inline int clz(const unsigned int mask)
408{
409 return __builtin_clz(mask);
410}
411#endif /* !_MSC_VER */
412
413/* Randomly downshift an integer */
414static int shift_rand(int x)
415{
416 const int bits = 8 * sizeof(x) - clz(x);
417 return x ? (x >> (checkasm_rand() % bits)) : 0;
418}
419
420enum {
421 PAT_ZERO, // all zero
422 PAT_ONE, // all one
423 PAT_RAND, // random data
424 PAT_LOW, // all low
425 PAT_HIGH, // all high
426 PAT_ALTLO, // alternating low and high
427 PAT_ALTHI, // alternating high and low
428 PAT_MIX, // random mix of low and high
429};
430
431void checkasm_init(void *buf, size_t bytes)
432{
433 checkasm_init_mask8(buf, (int) bytes, 0xFF);
434}
435
436#define DEF_CHECKASM_INIT_MASK(BITS, PIXEL) \
437 void checkasm_init_mask##BITS(PIXEL *buf, const int width, const PIXEL mask_pixel) \
438 { \
439 if (!width) \
440 return; \
441 \
442 int step = 0, mode = 0, mask = mask_pixel; \
443 for (int i = 0; i < width; i++, step--) { \
444 if (!step) { \
445 step = imax(shift_rand(width), 1); \
446 mode = checkasm_rand_uint8() & 7; \
447 mask = shift_rand(mask_pixel); \
448 } \
449 \
450 const PIXEL low = checkasm_rand_uint##BITS() & mask; \
451 const PIXEL high = mask_pixel - low; \
452 switch (mode) { \
453 case PAT_ZERO: buf[i] = 0; break; \
454 case PAT_ONE: buf[i] = mask_pixel; break; \
455 case PAT_RAND: buf[i] = checkasm_rand_uint##BITS() & mask_pixel; break; \
456 case PAT_LOW: buf[i] = low; break; \
457 case PAT_HIGH: buf[i] = high; break; \
458 case PAT_ALTLO: buf[i] = (i & 1) ? high : low; break; \
459 case PAT_ALTHI: buf[i] = (i & 1) ? low : high; break; \
460 case PAT_MIX: buf[i] = (checkasm_rand_uint8() & 1) ? low : high; break; \
461 } \
462 } \
463 }
464
465DEF_CHECKASM_INIT_MASK(8, uint8_t)
466DEF_CHECKASM_INIT_MASK(16, uint16_t)
467
468static int use_printf_color[2];
469static char statusline[256];
471static int get_terminal_width(void);
472
473static void statusline_clear(void)
474{
476 return;
477 fprintf(stderr, "\r\033[K"); /* clear line */
479}
480
481static void statusline_show(void)
482{
483 if (!statusline[0])
484 return;
485 const int term_w = get_terminal_width();
486 int len = (int) strlen(statusline);
487 if (term_w && len > term_w)
488 len = term_w;
489 fprintf(stderr, "%.*s", len, statusline);
491}
492
493/* Print colored text to stderr if the terminal supports it */
494int checkasm_vfprintf(FILE *const f, const int color, const char *const fmt, va_list arg)
495{
496 size_t fmt_len = strlen(fmt);
497 int use_color = use_printf_color[f == stderr];
498 if (!use_color || !fmt_len)
499 return vfprintf(f, fmt, arg);
500
501 if (f == stderr)
503
504 if (color >= 0)
505 fprintf(f, "\x1b[0;%dm", color);
506
507 int ret = vfprintf(f, fmt, arg);
508
509 if (color >= 0)
510 fprintf(f, "\x1b[0m");
511
512 if (f == stderr && fmt[fmt_len - 1] == '\n')
514
515 return ret;
516}
517
518void checkasm_statusline(const char *status)
519{
520 if (!status)
521 status = "";
522
523 if (!use_printf_color[1] || !strcmp(statusline, status))
524 return; /* don't re-paint unchanged status */
525
526 snprintf(statusline, sizeof(statusline), "%s", status);
527
528 /* refresh statusline */
531}
532
533static COLD int should_use_color(FILE *const f)
534{
535#ifdef _WIN32
536 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
537 HANDLE con = GetStdHandle(f == stderr ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE);
538 DWORD con_mode = 0;
539 return con && con != INVALID_HANDLE_VALUE && GetConsoleMode(con, &con_mode)
540 && SetConsoleMode(con, con_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
541 #else
542 return 0;
543 #endif
544#elif HAVE_ISATTY
545 if (isatty(f == stderr ? 2 : 1)) {
546 const char *const term = getenv("TERM");
547 return term && strcmp(term, "dumb");
548 }
549 return 0;
550#else
551 return 0;
552#endif
553}
554
556{
559}
560
561static int get_terminal_width(void)
562{
563#ifdef _WIN32
564 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
565 CONSOLE_SCREEN_BUFFER_INFO csbi;
566 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
567 return csbi.srWindow.Right - csbi.srWindow.Left + 1;
568 #endif
569#elif defined(__OS2__)
570 int dst[2];
571 _scrsize(dst);
572 return dst[0];
573#elif HAVE_IOCTL && defined(TIOCGWINSZ)
574 struct winsize w;
575 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1)
576 return w.ws_col;
577#endif
578 return 0;
579}
580
581void checkasm_json(CheckasmJson *json, const char *key, const char *const fmt, ...)
582{
583 assert(json->level > 0);
584 fputs(json->nonempty ? ",\n" : "\n", json->file);
585 for (int i = 0; i < json->level; i++)
586 fputc(' ', json->file);
587
588 va_list ap;
589 va_start(ap, fmt);
590 if (key)
591 fprintf(json->file, "\"%s\": ", key);
592 vfprintf(json->file, fmt, ap);
593 va_end(ap);
594 json->nonempty = 1;
595}
596
597void checkasm_json_str(CheckasmJson *json, const char *key, const char *str)
598{
599 assert(json->level > 0);
600 fputs(json->nonempty ? ",\n" : "\n", json->file);
601 for (int i = 0; i < json->level; i++)
602 fputc(' ', json->file);
603
604 if (key)
605 fprintf(json->file, "\"%s\": \"", key);
606 else
607 fputc('"', json->file);
608
609 while (*str) {
610 switch (*str) {
611 case '\\': fputs("\\\\", json->file); break;
612 case '"': fputs("\\\"", json->file); break;
613 case '\n': fputs("\\n", json->file); break;
614 default: fputc(*str, json->file); break;
615 }
616 str++;
617 }
618 fputc('"', json->file);
619 json->nonempty = 1;
620}
621
622void checkasm_json_push(CheckasmJson *json, const char *const key, const char type)
623{
624 fputs(json->nonempty ? ",\n" : "\n", json->file);
625 for (int i = 0; i < json->level; i++)
626 fputc(' ', json->file);
627
628 if (key) {
629 fprintf(json->file, "\"%s\": %c", key, type);
630 } else {
631 fputc(type, json->file);
632 }
633
634 json->level += 2;
635 json->nonempty = 0;
636}
637
639{
640 assert(json->level >= 2);
641 json->level -= 2;
642 if (json->nonempty) {
643 fputc('\n', json->file);
644 for (int i = 0; i < json->level; i++)
645 fputc(' ', json->file);
646 }
647 fputc(type, json->file);
648 json->nonempty = 1;
649}
650
651/* float compare support code */
652typedef union {
653 float f;
654 uint32_t i;
655} intfloat;
656
657static int is_negative(const intfloat u)
658{
659 return u.i >> 31;
660}
661
662int checkasm_float_near_ulp(const float a, const float b, const unsigned max_ulp)
663{
664 intfloat x, y;
665
666 x.f = a;
667 y.f = b;
668
669 if (is_negative(x) != is_negative(y)) {
670 // handle -0.0 == +0.0
671 return a == b;
672 }
673
674 if (llabs((int64_t) x.i - y.i) <= max_ulp)
675 return 1;
676
677 return 0;
678}
679
680int checkasm_float_near_ulp_array(const float *const a, const float *const b,
681 const unsigned max_ulp, const int len)
682{
683 for (int i = 0; i < len; i++)
684 if (!float_near_ulp(a[i], b[i], max_ulp))
685 return 0;
686
687 return 1;
688}
689
690int checkasm_float_near_abs_eps(const float a, const float b, const float eps)
691{
692 return fabsf(a - b) < eps;
693}
694
695int checkasm_float_near_abs_eps_array(const float *const a, const float *const b,
696 const float eps, const int len)
697{
698 for (int i = 0; i < len; i++)
699 if (!float_near_abs_eps(a[i], b[i], eps))
700 return 0;
701
702 return 1;
703}
704
705int checkasm_float_near_abs_eps_ulp(const float a, const float b, const float eps,
706 const unsigned max_ulp)
707{
708 return float_near_ulp(a, b, max_ulp) || float_near_abs_eps(a, b, eps);
709}
710
711int checkasm_float_near_abs_eps_array_ulp(const float *const a, const float *const b,
712 const float eps, const unsigned max_ulp,
713 const int len)
714{
715 for (int i = 0; i < len; i++)
716 if (!float_near_abs_eps_ulp(a[i], b[i], eps, max_ulp))
717 return 0;
718
719 return 1;
720}
721
722int checkasm_double_near_abs_eps(const double a, const double b, const double eps)
723{
724 return fabs(a - b) < eps;
725}
726
727int checkasm_double_near_abs_eps_array(const double *const a, const double *const b,
728 const double eps, const unsigned len)
729{
730 for (unsigned i = 0; i < len; i++)
731 if (!double_near_abs_eps(a[i], b[i], eps))
732 return 0;
733
734 return 1;
735}
736
737static int check_err(const char *const file, const int line, const char *const name,
738 const int w, const int h, int *const err)
739{
740 if (*err)
741 return 0;
742 if (!checkasm_fail_func("%s:%d", file, line))
743 return 1;
744 *err = 1;
745 fprintf(stderr, "%s (%dx%d):\n", name, w, h);
746 return 0;
747}
748
749#define PRINT_LINE(buf1, buf2, xstart, xend, xpad, fmt, fmtw) \
750 do { \
751 for (int x = xstart; x < xend; x++) { \
752 if (buf1[x] != buf2[x]) \
753 checkasm_fprintf(stderr, COLOR_RED, " " fmt, buf1[x]); \
754 else \
755 fprintf(stderr, " " fmt, buf1[x]); \
756 } \
757 for (int pad = xend; pad < xstart + xpad; pad++) \
758 fprintf(stderr, &" "[9 - fmtw]); \
759 } while (0)
760
761#define PRINT_RECT(type, buf1, buf2, ystart, yend, xstart, xend, fmt, fmtw) \
762 do { \
763 const type *ptr1 = (buf1) + ystart * stride1; \
764 const type *ptr2 = (buf2) + ystart * stride1; \
765 const int elem_size = 2 * (fmtw + 1) + 1; \
766 const int display_elems = imin(term_width / elem_size, xend - xstart); \
767 for (int y = ystart; y < yend; y++) { \
768 for (int xpos = xstart; xpos < xend; xpos += display_elems) { \
769 const int xstep = imin(xpos + display_elems, xend); \
770 if (xpos == xstart) /* line change */ \
771 checkasm_fprintf(stderr, COLOR_BLUE, "%3d: ", y); \
772 else \
773 fprintf(stderr, " "); \
774 PRINT_LINE(ptr1, ptr2, xpos, xstep, display_elems, fmt, fmtw); \
775 fprintf(stderr, " "); \
776 PRINT_LINE(ptr2, ptr1, xpos, xstep, display_elems, fmt, fmtw); \
777 fprintf(stderr, " "); \
778 for (int x = xpos; x < xstep; x++) { \
779 if (ptr1[x] != ptr2[x]) \
780 checkasm_fprintf(stderr, COLOR_RED, "x"); \
781 else \
782 fprintf(stderr, "."); \
783 } \
784 fprintf(stderr, "\n"); \
785 } \
786 ptr1 += stride1; \
787 ptr2 += stride2; \
788 } \
789 } while (0)
790
791#define CHECK_RECT(buf1, buf2, ystart, yend, xstart, xend, msg, compare, type, fmt, \
792 fmtw) \
793 do { \
794 const int xw = xend - xstart; \
795 for (int y = ystart; y < yend; y++) { \
796 if (compare(&buf1[y * stride1 + xstart], &buf2[y * stride2 + xstart], xw)) \
797 continue; \
798 if (check_err(file, line, name, w, h, &err)) \
799 return 1; \
800 /* Exclude unneeded lines on overwrite above */ \
801 int yprint = y < 0 ? y : ystart; \
802 if (msg[0]) \
803 fprintf(stderr, " %s (%dx%d, from idx [%d]):\n", msg, xend - xstart, \
804 yend - yprint, xstart); \
805 PRINT_RECT(type, buf1, buf2, yprint, yend, xstart, xend, fmt, fmtw); \
806 break; \
807 } \
808 } while (0)
809
810#define DEF_CHECKASM_CHECK_BODY(compare, type, fmt, fmtw) \
811 do { \
812 const int overhead = 5 + 3 + 3; \
813 const int term_w_raw = get_terminal_width(); \
814 const int term_width = (term_w_raw ? term_w_raw : 80) - overhead; \
815 const int aligned_w = (w + align_w - 1) & ~(align_w - 1); \
816 stride1 /= sizeof(type); \
817 stride2 /= sizeof(type); \
818 \
819 int err = 0; \
820 CHECK_RECT(buf1, buf2, 0, h, 0, w, "", compare, type, fmt, fmtw); \
821 if (align_h >= 1) { \
822 const int aligned_h = (h + align_h - 1) & ~(align_h - 1); \
823 CHECK_RECT(buf1, buf2, -padding, 0, -padding, w + padding, "overwrite top", \
824 compare, type, fmt, fmtw); \
825 CHECK_RECT(buf1, buf2, aligned_h, aligned_h + padding, -padding, \
826 w + padding, "overwrite bottom", compare, type, fmt, fmtw); \
827 } \
828 CHECK_RECT(buf1, buf2, 0, h, -padding, 0, "overwrite left", compare, type, fmt, \
829 fmtw); \
830 CHECK_RECT(buf1, buf2, 0, h, aligned_w, aligned_w + padding, "overwrite right", \
831 compare, type, fmt, fmtw); \
832 return err; \
833 } while (0)
834
835#define cmp_int(a, b, len) (!memcmp(a, b, (len) * sizeof(*(a))))
836#define DEF_CHECKASM_CHECK_FUNC(type, fmt, fmtw) \
837 int checkasm_check_impl_##type(const char *file, int line, const type *buf1, \
838 ptrdiff_t stride1, const type *buf2, \
839 ptrdiff_t stride2, int w, int h, const char *name, \
840 int align_w, int align_h, int padding) \
841 { \
842 DEF_CHECKASM_CHECK_BODY(cmp_int, type, fmt, fmtw); \
843 }
844
845DEF_CHECKASM_CHECK_FUNC(int, "%9d", 9)
846DEF_CHECKASM_CHECK_FUNC(int8_t, "%4" PRId8, 4)
847DEF_CHECKASM_CHECK_FUNC(int16_t, "%6" PRId16, 6)
848DEF_CHECKASM_CHECK_FUNC(int32_t, "%9" PRId32, 9)
849
850DEF_CHECKASM_CHECK_FUNC(unsigned, "%08x", 8)
851DEF_CHECKASM_CHECK_FUNC(uint8_t, "%02" PRIx8, 2)
852DEF_CHECKASM_CHECK_FUNC(uint16_t, "%04" PRIx16, 4)
853DEF_CHECKASM_CHECK_FUNC(uint32_t, "%08" PRIx32, 8)
854
855int checkasm_check_impl_float_ulp(const char *file, int line, const float *buf1,
856 ptrdiff_t stride1, const float *buf2, ptrdiff_t stride2,
857 int w, int h, const char *name, unsigned max_ulp,
858 int align_w, int align_h, int padding)
859{
860#define cmp_float(a, b, len) float_near_ulp_array(a, b, max_ulp, len)
861 DEF_CHECKASM_CHECK_BODY(cmp_float, float, "%7g", 7);
862#undef cmp_float
863}
864
865char *checkasm_vasprintf(const char *fmt, va_list arg)
866{
867 va_list arg2;
868 va_copy(arg2, arg);
869 int len = vsnprintf(NULL, 0, fmt, arg2);
870 va_end(arg2);
871 if (len < 0)
872 return NULL;
873
874 char *buf = checkasm_mallocz(len + 1);
875 vsnprintf(buf, len + 1, fmt, arg);
876 return buf;
877}
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
static double val(void *priv, double ch)
Definition aeval.c:77
int32_t
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
#define xs(width, name, var, subs,...)
Definition cbs_vp9.c:222
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static __device__ float fabsf(float a)
static __device__ float fabs(float a)
int high
Definition dovi_rpuenc.c:39
enum AVCodecID id
Definition dts2pts.c:612
static const uint8_t bits[8]
Definition fastaudio.c:100
const char * key
int ret
Definition ffplay.c:391
#define float_near_ulp
Definition utils.h:448
#define float_near_abs_eps
Definition utils.h:449
#define float_near_abs_eps_ulp
Definition utils.h:450
#define double_near_abs_eps
Definition utils.h:454
int checkasm_float_near_abs_eps_array(const float *const a, const float *const b, const float eps, const int len)
Compare float arrays using absolute epsilon tolerance.
Definition utils.c:695
int checkasm_float_near_ulp_array(const float *const a, const float *const b, const unsigned max_ulp, const int len)
Compare float arrays using ULP tolerance.
Definition utils.c:680
int checkasm_float_near_ulp(const float a, const float b, const unsigned max_ulp)
Compare floats using ULP (Units in Last Place) tolerance.
Definition utils.c:662
int checkasm_float_near_abs_eps_ulp(const float a, const float b, const float eps, const unsigned max_ulp)
Compare floats using both epsilon and ULP tolerances.
Definition utils.c:705
int checkasm_double_near_abs_eps_array(const double *const a, const double *const b, const double eps, const unsigned len)
Compare double arrays using absolute epsilon tolerance.
Definition utils.c:727
int checkasm_float_near_abs_eps(const float a, const float b, const float eps)
Compare floats using absolute epsilon tolerance.
Definition utils.c:690
int checkasm_double_near_abs_eps(const double a, const double b, const double eps)
Compare doubles using absolute epsilon tolerance.
Definition utils.c:722
int checkasm_float_near_abs_eps_array_ulp(const float *const a, const float *const b, const float eps, const unsigned max_ulp, const int len)
Compare float arrays using both epsilon and ULP tolerances.
Definition utils.c:711
CHECKASM_API int checkasm_check_impl_float_ulp(const char *file, int line, const float *buf1, ptrdiff_t stride1, const float *buf2, ptrdiff_t stride2, int w, int h, const char *name, unsigned max_ulp, int align_w, int align_h, int padding)
Compare float buffers with ULP tolerance.
Definition utils.c:855
CHECKASM_API void checkasm_init_mask8(uint8_t *buf, int width, uint8_t mask)
Initialize a uint8_t buffer with pathological values within a mask.
void checkasm_randomize_normf(float *buf, int width)
Fill a float buffer with values from a standard normal distribution.
Definition utils.c:367
void checkasm_randomize_intervalf(float *buf, int width, float low, float high)
Fill a float buffer with random values chosen uniformly from an interval.
Definition utils.c:330
void checkasm_randomize_interval(double *buf, int width, double low, double high)
Fill a double buffer with random values chosen uniformly from an interval.
Definition utils.c:323
void checkasm_randomize(void *buf, size_t bytes)
Fill a buffer with uniformly chosen random bytes.
Definition utils.c:290
void checkasm_clear16(uint16_t *buf, int width, uint16_t val)
Fill a uint16_t buffer with a constant value.
Definition utils.c:382
void checkasm_clear(void *buf, size_t bytes)
Clear a buffer to a pre-determined pattern (currently 0xAA)
Definition utils.c:372
void checkasm_randomize_mask16(uint16_t *buf, int width, uint16_t mask)
Fill a uint16_t buffer with random values chosen uniformly within a mask.
Definition utils.c:302
void checkasm_randomize_rangef(float *buf, int width, float range)
Fill a float buffer with random values chosen uniformly below a limit.
Definition utils.c:316
void checkasm_randomize_mask8(uint8_t *buf, int width, uint8_t mask)
Fill a uint8_t buffer with random values chosen uniformly within a mask.
Definition utils.c:295
void checkasm_randomize_norm(double *buf, int width)
Fill a double buffer with values from a standard normal distribution.
Definition utils.c:362
void checkasm_randomize_dist(double *buf, int width, CheckasmDist dist)
Fill a double buffer with normally distributed random values.
Definition utils.c:352
void checkasm_randomize_range(double *buf, int width, double range)
Fill a double buffer with random values chosen uniformly below a limit.
Definition utils.c:309
void checkasm_randomize_distf(float *buf, int width, CheckasmDist dist)
Fill a float buffer with normally distributed random values.
Definition utils.c:357
void checkasm_init(void *buf, size_t bytes)
Initialize a buffer with pathological test patterns.
Definition utils.c:431
void checkasm_clear8(uint8_t *buf, int width, uint8_t val)
Fill a uint8_t buffer with a constant value.
Definition utils.c:377
int checkasm_rand(void)
Generate a random non-negative integer.
Definition utils.c:248
double checkasm_randf(void)
Generate a random double-precision floating-point number.
Definition utils.c:254
CHECKASM_API uint32_t checkasm_rand_uint32(void)
Generate a random 32-bit unsigned integer.
int a
static av_always_inline void scale(vec_s32 res[4], vec_s16 res_packed[2], const int shift)
#define b
Definition input.c:43
enum MPFType type
Definition jpegmpfenc.c:55
#define u(width, name, range_min, range_max)
Definition cbs_apv.c:68
const char * arg
Definition jacosubdec.c:66
uint8_t w
Definition llvidencdsp.c:39
static int use_color
Definition log.c:127
static const uint16_t mask[17]
Definition lzw.c:38
enum AVColorRange range
const char * name
Definition qsvenc.c:142
static unsigned int stdc_leading_zeros_ui(unsigned int value)
Definition stdbit.h:61
Describes a normal (Gaussian) distribution.
Definition utils.h:147
double stddev
Standard deviation (spread) of the distribution.
Definition utils.h:149
double mean
Mean (center) of the distribution.
Definition utils.h:148
FILE * file
Definition internal.h:134
uint32_t s2[CHECKASM_PRNG_NUM]
Definition utils.c:127
uint32_t s1[CHECKASM_PRNG_NUM]
Definition utils.c:126
uint32_t s0[CHECKASM_PRNG_NUM]
Definition utils.c:125
uint32_t s3[CHECKASM_PRNG_NUM]
Definition utils.c:128
Test writing API for checkasm.
CHECKASM_API CheckasmKey CHECKASM_API void CHECKASM_API int checkasm_fail_func(const char *msg,...) CHECKASM_PRINTF(1
Mark the current function as failed with a custom message.
static uint8_t tmp[40]
Definition aes_ctr.c:52
Utility functions for checkasm tests.
static void * checkasm_mallocz(const size_t size)
Definition internal.h:198
#define NOINLINE
Definition internal.h:59
#define ALWAYS_INLINE
Definition internal.h:72
#define COLD
Definition internal.h:45
uint64_t checkasm_gettime_nsec_diff(uint64_t t)
Definition utils.c:112
static int get_terminal_width(void)
Definition utils.c:561
@ PAT_HIGH
Definition utils.c:425
@ PAT_RAND
Definition utils.c:423
@ PAT_ALTLO
Definition utils.c:426
@ PAT_MIX
Definition utils.c:428
@ PAT_ONE
Definition utils.c:422
@ PAT_ALTHI
Definition utils.c:427
@ PAT_LOW
Definition utils.c:424
@ PAT_ZERO
Definition utils.c:421
#define DEF_CHECKASM_CHECK_BODY(compare, type, fmt, fmtw)
Definition utils.c:810
static double marsaglia(double *z2)
Definition utils.c:260
void checkasm_json(CheckasmJson *json, const char *key, const char *const fmt,...)
Definition utils.c:581
int num32
Definition utils.c:185
COLD void checkasm_setup_fprintf(void)
Definition utils.c:555
void checkasm_srand(unsigned seed)
Definition utils.c:229
uint64_t checkasm_gettime_nsec(void)
Definition utils.c:107
#define cmp_float(a, b, len)
static char statusline[256]
Definition utils.c:469
void checkasm_json_str(CheckasmJson *json, const char *key, const char *str)
Definition utils.c:597
uint64_t buf64[PRNG_CACHE_SIZE > > 3]
Definition utils.c:182
#define PRNG_CACHE_SIZE
Definition utils.c:178
static struct @043020231131004364200274111274237375066036336150 prng_cache
static ALWAYS_INLINE uint32_t rotl(const uint32_t x, int k)
Definition utils.c:133
uint32_t buf32[PRNG_CACHE_SIZE > > 2]
Definition utils.c:181
static void prng(CheckasmRand *restrict xs, uint8_t *restrict buf, size_t size)
Definition utils.c:154
double checkasm_rand_norm(void)
Generate a random number from the standard normal distribution.
Definition utils.c:274
static void statusline_show(void)
Definition utils.c:481
uint8_t buf8[PRNG_CACHE_SIZE]
Definition utils.c:179
static void statusline_clear(void)
Definition utils.c:473
#define DEF_CHECKASM_INIT_MASK(BITS, PIXEL)
Definition utils.c:436
int num8
Definition utils.c:183
#define CHECKASM_PRNG_NUM
Definition utils.c:124
static int clz(const unsigned int mask)
Definition utils.c:407
NOINLINE void checkasm_noop(void *ptr)
Definition utils.c:63
static uint64_t splitmix64(uint64_t *state)
Definition utils.c:220
void checkasm_json_pop(CheckasmJson *json, char type)
Definition utils.c:638
void checkasm_statusline(const char *status)
Definition utils.c:518
static COLD int should_use_color(FILE *const f)
Definition utils.c:533
int num16
Definition utils.c:184
static int use_printf_color[2]
Definition utils.c:468
static int is_negative(const intfloat u)
Definition utils.c:657
static CheckasmRand checkasm_prng
Definition utils.c:131
unsigned checkasm_seed(void)
Definition utils.c:117
void checkasm_json_push(CheckasmJson *json, const char *const key, const char type)
Definition utils.c:622
static ALWAYS_INLINE void xoshiro128pp(CheckasmRand *restrict xs, uint32_t *restrict buf)
Definition utils.c:139
#define RANDOMIZE_DIST(buf, ftype, width, mean, stddev)
Definition utils.c:337
static int shift_rand(int x)
Definition utils.c:414
static ALWAYS_INLINE uint64_t gettime_nsec(int is_seed)
Definition utils.c:68
int num64
Definition utils.c:186
char * checkasm_vasprintf(const char *fmt, va_list arg)
Definition utils.c:865
static int check_err(const char *const file, const int line, const char *const name, const int w, const int h, int *const err)
Definition utils.c:737
double checkasm_rand_dist(CheckasmDist dist)
Generate a normally distributed random number.
Definition utils.c:285
#define DEF_CHECKASM_RAND(BITS, TYPE, NAME)
Definition utils.c:192
uint16_t buf16[PRNG_CACHE_SIZE > > 1]
Definition utils.c:180
static int statusline_visible
Definition utils.c:470
int checkasm_vfprintf(FILE *const f, const int color, const char *const fmt, va_list arg)
Definition utils.c:494
#define DEF_CHECKASM_CHECK_FUNC(type, fmt, fmtw)
Definition utils.c:836
#define width
Definition dsp.h:99
static struct @232240247175332074307271266321204261331170211232 state
int size
float f
Definition utils.c:653
uint32_t i
Definition utils.c:654
static unsigned int seed
Definition videogen.c:78
int len