FFmpeg
Loading...
Searching...
No Matches
utils.c File Reference
#include <assert.h>
#include <inttypes.h>
#include <limits.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "checkasm_config.h"
#include "checkasm/test.h"
#include "checkasm/utils.h"
#include "internal.h"

Go to the source code of this file.

Data Structures

struct  CheckasmRand
 
union  intfloat
 

Macros

#define CHECKASM_PRNG_NUM   4
 
#define PRNG_CACHE_SIZE   64
 
#define DEF_CHECKASM_RAND(BITS, TYPE, NAME)
 
#define RANDOMIZE_DIST(buf, ftype, width, mean, stddev)
 
#define DEF_CHECKASM_INIT_MASK(BITS, PIXEL)
 
#define PRINT_LINE(buf1, buf2, xstart, xend, xpad, fmt, fmtw)
 
#define PRINT_RECT(type, buf1, buf2, ystart, yend, xstart, xend, fmt, fmtw)
 
#define CHECK_RECT(buf1, buf2, ystart, yend, xstart, xend, msg, compare, type, fmt, fmtw)
 
#define DEF_CHECKASM_CHECK_BODY(compare, type, fmt, fmtw)
 
#define cmp_int(a, b, len)
 
#define DEF_CHECKASM_CHECK_FUNC(type, fmt, fmtw)
 
#define cmp_float(a, b, len)
 

Enumerations

enum  {
  PAT_ZERO , PAT_ONE , PAT_RAND , PAT_LOW ,
  PAT_HIGH , PAT_ALTLO , PAT_ALTHI , PAT_MIX
}
 

Functions

NOINLINE void checkasm_noop (void *ptr)
 
static ALWAYS_INLINE uint64_t gettime_nsec (int is_seed)
 
uint64_t checkasm_gettime_nsec (void)
 
uint64_t checkasm_gettime_nsec_diff (uint64_t t)
 
unsigned checkasm_seed (void)
 
static ALWAYS_INLINE uint32_t rotl (const uint32_t x, int k)
 
static ALWAYS_INLINE void xoshiro128pp (CheckasmRand *restrict xs, uint32_t *restrict buf)
 
static void prng (CheckasmRand *restrict xs, uint8_t *restrict buf, size_t size)
 
static uint64_t splitmix64 (uint64_t *state)
 
void checkasm_srand (unsigned seed)
 
int checkasm_rand (void)
 Generate a random non-negative integer.
 
double checkasm_randf (void)
 Generate a random double-precision floating-point number.
 
static double marsaglia (double *z2)
 
double checkasm_rand_norm (void)
 Generate a random number from the standard normal distribution.
 
double checkasm_rand_dist (CheckasmDist dist)
 Generate a normally distributed random number.
 
void checkasm_randomize (void *buf, size_t bytes)
 Fill a buffer with uniformly chosen random bytes.
 
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.
 
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.
 
void checkasm_randomize_range (double *buf, int width, double range)
 Fill a double buffer with random values chosen uniformly below a limit.
 
void checkasm_randomize_rangef (float *buf, int width, float range)
 Fill a float buffer with random values chosen uniformly below a limit.
 
void checkasm_randomize_interval (double *buf, int width, double low, double high)
 Fill a double buffer with random values chosen uniformly from an interval.
 
void checkasm_randomize_intervalf (float *buf, int width, float low, float high)
 Fill a float buffer with random values chosen uniformly from an interval.
 
void checkasm_randomize_dist (double *buf, int width, CheckasmDist dist)
 Fill a double buffer with normally distributed random values.
 
void checkasm_randomize_distf (float *buf, int width, CheckasmDist dist)
 Fill a float buffer with normally distributed random values.
 
void checkasm_randomize_norm (double *buf, int width)
 Fill a double buffer with values from a standard normal distribution.
 
void checkasm_randomize_normf (float *buf, int width)
 Fill a float buffer with values from a standard normal distribution.
 
void checkasm_clear (void *buf, size_t bytes)
 Clear a buffer to a pre-determined pattern (currently 0xAA)
 
void checkasm_clear8 (uint8_t *buf, int width, uint8_t val)
 Fill a uint8_t buffer with a constant value.
 
void checkasm_clear16 (uint16_t *buf, int width, uint16_t val)
 Fill a uint16_t buffer with a constant value.
 
static int clz (const unsigned int mask)
 
static int shift_rand (int x)
 
void checkasm_init (void *buf, size_t bytes)
 Initialize a buffer with pathological test patterns.
 
int checkasm_vfprintf (FILE *const f, const int color, const char *const fmt, va_list arg)
 
void checkasm_statusline (const char *status)
 
static COLD int should_use_color (FILE *const f)
 
COLD void checkasm_setup_fprintf (void)
 
static int get_terminal_width (void)
 
void checkasm_json (CheckasmJson *json, const char *key, const char *const fmt,...)
 
void checkasm_json_str (CheckasmJson *json, const char *key, const char *str)
 
void checkasm_json_push (CheckasmJson *json, const char *const key, const char type)
 
void checkasm_json_pop (CheckasmJson *json, char type)
 
static int is_negative (const intfloat u)
 
int checkasm_float_near_ulp (const float a, const float b, const unsigned max_ulp)
 Compare floats using ULP (Units in Last Place) tolerance.
 
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.
 
int checkasm_float_near_abs_eps (const float a, const float b, const float eps)
 Compare floats using absolute epsilon tolerance.
 
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.
 
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.
 
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.
 
int checkasm_double_near_abs_eps (const double a, const double b, const double eps)
 Compare doubles using absolute epsilon tolerance.
 
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.
 
static int check_err (const char *const file, const int line, const char *const name, const int w, const int h, int *const err)
 
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.
 
char * checkasm_vasprintf (const char *fmt, va_list arg)
 

Variables

static CheckasmRand checkasm_prng
 
struct { 
 
   uint8_t   buf8 [PRNG_CACHE_SIZE
 
   uint16_t   buf16 [PRNG_CACHE_SIZE > > 1] 
 
   uint32_t   buf32 [PRNG_CACHE_SIZE > > 2] 
 
   uint64_t   buf64 [PRNG_CACHE_SIZE > > 3] 
 
   int   num8 
 
   int   num16 
 
   int   num32 
 
   int   num64 
 
prng_cache 
 
static int use_printf_color [2]
 
static char statusline [256]
 
static int statusline_visible
 

Macro Definition Documentation

◆ CHECKASM_PRNG_NUM

#define CHECKASM_PRNG_NUM   4

Definition at line 124 of file utils.c.

Referenced by checkasm_srand(), prng(), and xoshiro128pp().

◆ PRNG_CACHE_SIZE

#define PRNG_CACHE_SIZE   64

Definition at line 178 of file utils.c.

◆ DEF_CHECKASM_RAND

#define DEF_CHECKASM_RAND ( BITS,
TYPE,
NAME )
Value:
TYPE checkasm_rand_##NAME(void) \
{ \
if (!prng_cache.num##BITS) { \
prng(&checkasm_prng, (uint8_t *) prng_cache.buf##BITS, \
sizeof(prng_cache.buf##BITS)); \
} \
\
union { \
uint##BITS##_t raw; \
} val; \
val.raw = prng_cache.buf##BITS[--prng_cache.num##BITS]; \
return val.type; \
}
static double val(void *priv, double ch)
Definition aeval.c:77
#define TYPE
Definition ffv1dec.c:90
#define BITS
Definition h264_mb.c:788
cl_device_type type
#define ARRAY_SIZE(a)
Definition internal.h:81
static struct @043020231131004364200274111274237375066036336150 prng_cache
static CheckasmRand checkasm_prng
Definition utils.c:131

Definition at line 192 of file utils.c.

◆ RANDOMIZE_DIST

#define RANDOMIZE_DIST ( buf,
ftype,
width,
mean,
stddev )
Value:
do { \
if ((width) & 1) { \
*(buf)++ = (ftype) ((mean) + (stddev) * checkasm_rand_norm()); \
(width) ^= 1; \
} \
\
for (; width; width -= 2) { \
double z1, z2; \
z1 = marsaglia(&z2); \
*(buf)++ = (ftype) ((mean) + (stddev) * z1); \
*(buf)++ = (ftype) ((mean) + (stddev) * z2); \
} \
} while (0)
#define ftype
CHECKASM_API double checkasm_rand_norm(void)
Generate a random number from the standard normal distribution.
Definition utils.c:274
static double marsaglia(double *z2)
Definition utils.c:260
#define width
Definition dsp.h:89
static float mean(const float *input, int size)
Definition vf_nnedi.c:861

Definition at line 337 of file utils.c.

Referenced by checkasm_randomize_dist(), checkasm_randomize_distf(), checkasm_randomize_norm(), and checkasm_randomize_normf().

◆ DEF_CHECKASM_INIT_MASK

#define DEF_CHECKASM_INIT_MASK ( BITS,
PIXEL )
Value:
void checkasm_init_mask##BITS(PIXEL *buf, const int width, const PIXEL mask_pixel) \
{ \
if (!width) \
return; \
\
int step = 0, mode = 0, mask = mask_pixel; \
for (int i = 0; i < width; i++, step--) { \
if (!step) { \
step = imax(shift_rand(width), 1); \
mask = shift_rand(mask_pixel); \
} \
\
const PIXEL low = checkasm_rand_uint##BITS() & mask; \
const PIXEL high = mask_pixel - low; \
switch (mode) { \
case PAT_ZERO: buf[i] = 0; break; \
case PAT_ONE: buf[i] = mask_pixel; break; \
case PAT_RAND: buf[i] = checkasm_rand_uint##BITS() & mask_pixel; break; \
case PAT_LOW: buf[i] = low; break; \
case PAT_HIGH: buf[i] = high; break; \
case PAT_ALTLO: buf[i] = (i & 1) ? high : low; break; \
case PAT_ALTHI: buf[i] = (i & 1) ? low : high; break; \
case PAT_MIX: buf[i] = (checkasm_rand_uint8() & 1) ? low : high; break; \
} \
} \
}
#define PIXEL
Definition blend_modes.c:45
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
int high
Definition dovi_rpuenc.c:39
CHECKASM_API uint8_t checkasm_rand_uint8(void)
Generate a random 8-bit unsigned integer.
static const uint16_t mask[17]
Definition lzw.c:38
Definition swscale.c:71
static int imax(const int a, const int b)
Definition internal.h:177
@ 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
static int shift_rand(int x)
Definition utils.c:414

Definition at line 436 of file utils.c.

◆ PRINT_LINE

#define PRINT_LINE ( buf1,
buf2,
xstart,
xend,
xpad,
fmt,
fmtw )
Value:
do { \
for (int x = xstart; x < xend; x++) { \
if (buf1[x] != buf2[x]) \
checkasm_fprintf(stderr, COLOR_RED, " " fmt, buf1[x]); \
else \
fprintf(stderr, " " fmt, buf1[x]); \
} \
for (int pad = xend; pad < xstart + xpad; pad++) \
fprintf(stderr, &" "[9 - fmtw]); \
} while (0)
#define COLOR_RED
Definition internal.h:102

Definition at line 738 of file utils.c.

◆ PRINT_RECT

#define PRINT_RECT ( type,
buf1,
buf2,
ystart,
yend,
xstart,
xend,
fmt,
fmtw )
Value:
do { \
const type *ptr1 = (buf1) + ystart * stride1; \
const type *ptr2 = (buf2) + ystart * stride1; \
const int elem_size = 2 * (fmtw + 1) + 1; \
const int display_elems = imin(term_width / elem_size, xend - xstart); \
for (int y = ystart; y < yend; y++) { \
for (int xpos = xstart; xpos < xend; xpos += display_elems) { \
const int xstep = imin(xpos + display_elems, xend); \
if (xpos == xstart) /* line change */ \
checkasm_fprintf(stderr, COLOR_BLUE, "%3d: ", y); \
else \
fprintf(stderr, " "); \
PRINT_LINE(ptr1, ptr2, xpos, xstep, display_elems, fmt, fmtw); \
fprintf(stderr, " "); \
PRINT_LINE(ptr2, ptr1, xpos, xstep, display_elems, fmt, fmtw); \
fprintf(stderr, " "); \
for (int x = xpos; x < xstep; x++) { \
if (ptr1[x] != ptr2[x]) \
checkasm_fprintf(stderr, COLOR_RED, "x"); \
else \
fprintf(stderr, "."); \
} \
fprintf(stderr, "\n"); \
} \
ptr1 += stride1; \
ptr2 += stride2; \
} \
} while (0)
static int imin(const int a, const int b)
Definition internal.h:182
#define COLOR_BLUE
Definition internal.h:105

Definition at line 750 of file utils.c.

◆ CHECK_RECT

#define CHECK_RECT ( buf1,
buf2,
ystart,
yend,
xstart,
xend,
msg,
compare,
type,
fmt,
fmtw )
Value:
do { \
const int xw = xend - xstart; \
for (int y = ystart; y < yend; y++) { \
if (compare(&buf1[y * stride1 + xstart], &buf2[y * stride2 + xstart], xw)) \
continue; \
if (check_err(file, line, name, w, h, &err)) \
return 1; \
/* Exclude unneeded lines on overwrite above */ \
int yprint = y < 0 ? y : ystart; \
if (msg[0]) \
fprintf(stderr, " %s (%dx%d, from idx [%d]):\n", msg, xend - xstart, \
yend - yprint, xstart); \
PRINT_RECT(type, buf1, buf2, yprint, yend, xstart, xend, fmt, fmtw); \
break; \
} \
} while (0)
uint8_t w
Definition llvidencdsp.c:39
const char * name
Definition qsvenc.c:142
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:726
static float compare(const AVFrame *haystack, const AVFrame *obj, int offx, int offy)

Definition at line 780 of file utils.c.

◆ DEF_CHECKASM_CHECK_BODY

#define DEF_CHECKASM_CHECK_BODY ( compare,
type,
fmt,
fmtw )
Value:
do { \
const int overhead = 5 + 3 + 3; \
const int term_width = get_terminal_width() - overhead; \
const int aligned_w = (w + align_w - 1) & ~(align_w - 1); \
stride1 /= sizeof(type); \
stride2 /= sizeof(type); \
\
int err = 0; \
CHECK_RECT(buf1, buf2, 0, h, 0, w, "", compare, type, fmt, fmtw); \
if (align_h >= 1) { \
const int aligned_h = (h + align_h - 1) & ~(align_h - 1); \
CHECK_RECT(buf1, buf2, -padding, 0, -padding, w + padding, "overwrite top", \
compare, type, fmt, fmtw); \
CHECK_RECT(buf1, buf2, aligned_h, aligned_h + padding, -padding, \
w + padding, "overwrite bottom", compare, type, fmt, fmtw); \
} \
CHECK_RECT(buf1, buf2, 0, h, -padding, 0, "overwrite left", compare, type, fmt, \
fmtw); \
CHECK_RECT(buf1, buf2, 0, h, aligned_w, aligned_w + padding, "overwrite right", \
compare, type, fmt, fmtw); \
return err; \
} while (0)
static int get_terminal_width(void)
Definition utils.c:550

Definition at line 799 of file utils.c.

Referenced by checkasm_check_impl_float_ulp().

◆ cmp_int

#define cmp_int ( a,
b,
len )
Value:
(!memcmp(a, b, (len) * sizeof(*(a))))
int a
#define b
Definition input.c:43
int len

Definition at line 823 of file utils.c.

Referenced by mode02(), mode03(), and mode04().

◆ DEF_CHECKASM_CHECK_FUNC

#define DEF_CHECKASM_CHECK_FUNC ( type,
fmt,
fmtw )
Value:
int checkasm_check_impl_##type(const char *file, int line, const type *buf1, \
ptrdiff_t stride1, const type *buf2, \
ptrdiff_t stride2, int w, int h, const char *name, \
int align_w, int align_h, int padding) \
{ \
DEF_CHECKASM_CHECK_BODY(cmp_int, type, fmt, fmtw); \
}
#define cmp_int(a, b, len)
Definition utils.c:823

Definition at line 824 of file utils.c.

◆ cmp_float

#define cmp_float ( a,
b,
len )
Value:
#define float_near_ulp_array
Definition utils.h:451

Referenced by checkasm_check_impl_float_ulp().

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
PAT_ZERO 
PAT_ONE 
PAT_RAND 
PAT_LOW 
PAT_HIGH 
PAT_ALTLO 
PAT_ALTHI 
PAT_MIX 

Definition at line 420 of file utils.c.

Function Documentation

◆ checkasm_noop()

NOINLINE void checkasm_noop ( void * ptr)

◆ gettime_nsec()

static ALWAYS_INLINE uint64_t gettime_nsec ( int is_seed)
static

Definition at line 68 of file utils.c.

Referenced by checkasm_gettime_nsec(), checkasm_gettime_nsec_diff(), and checkasm_seed().

◆ checkasm_gettime_nsec()

◆ checkasm_gettime_nsec_diff()

uint64_t checkasm_gettime_nsec_diff ( uint64_t t)

◆ checkasm_seed()

unsigned checkasm_seed ( void )

Definition at line 117 of file utils.c.

Referenced by checkasm_perf_init_macos(), and checkasm_run().

◆ rotl()

static ALWAYS_INLINE uint32_t rotl ( const uint32_t x,
int k )
static

Definition at line 133 of file utils.c.

Referenced by xoshiro128pp().

◆ xoshiro128pp()

static ALWAYS_INLINE void xoshiro128pp ( CheckasmRand *restrict xs,
uint32_t *restrict buf )
static

Definition at line 139 of file utils.c.

Referenced by prng().

◆ prng()

static void prng ( CheckasmRand *restrict xs,
uint8_t *restrict buf,
size_t size )
static

◆ splitmix64()

static uint64_t splitmix64 ( uint64_t * state)
inlinestatic

Definition at line 220 of file utils.c.

Referenced by checkasm_srand(), and splitmix64().

◆ checkasm_srand()

void checkasm_srand ( unsigned seed)

Definition at line 229 of file utils.c.

Referenced by check_cpu_flag(), checkasm_check_key(), and run_all_tests().

◆ marsaglia()

static double marsaglia ( double * z2)
inlinestatic

Definition at line 260 of file utils.c.

Referenced by checkasm_rand_norm().

◆ checkasm_rand_norm()

double checkasm_rand_norm ( void )

Generate a random number from the standard normal distribution.

Returns
Random value from N(0,1) distribution
See also
checkasm_dist_standard

Definition at line 274 of file utils.c.

Referenced by checkasm_rand_dist().

◆ checkasm_rand_dist()

double checkasm_rand_dist ( CheckasmDist dist)

Generate a normally distributed random number.

Parameters
[in]distDistribution parameters (mean and standard deviation)
Returns
Random value from the specified normal distribution

Definition at line 285 of file utils.c.

◆ clz()

static int clz ( const unsigned int mask)
inlinestatic

Definition at line 407 of file utils.c.

Referenced by shift_rand().

◆ shift_rand()

static int shift_rand ( int x)
static

Definition at line 414 of file utils.c.

◆ checkasm_vfprintf()

int checkasm_vfprintf ( FILE *const f,
const int color,
const char *const fmt,
va_list arg )

Definition at line 473 of file utils.c.

◆ checkasm_statusline()

void checkasm_statusline ( const char * status)

Definition at line 501 of file utils.c.

Referenced by print_summary(), and update_statusline().

◆ should_use_color()

static COLD int should_use_color ( FILE *const f)
static

Definition at line 522 of file utils.c.

Referenced by checkasm_setup_fprintf().

◆ checkasm_setup_fprintf()

COLD void checkasm_setup_fprintf ( void )

Definition at line 544 of file utils.c.

Referenced by checkasm_list_cpu_flags(), and checkasm_run().

◆ get_terminal_width()

static int get_terminal_width ( void )
static

Definition at line 550 of file utils.c.

◆ checkasm_json()

void checkasm_json ( CheckasmJson * json,
const char * key,
const char *const fmt,
... )

Definition at line 570 of file utils.c.

◆ checkasm_json_str()

void checkasm_json_str ( CheckasmJson * json,
const char * key,
const char * str )

◆ checkasm_json_push()

void checkasm_json_push ( CheckasmJson * json,
const char *const key,
const char type )

◆ checkasm_json_pop()

void checkasm_json_pop ( CheckasmJson * json,
char type )

◆ is_negative()

static int is_negative ( const intfloat u)
static

Definition at line 646 of file utils.c.

Referenced by checkasm_float_near_ulp().

◆ check_err()

static int check_err ( const char *const file,
const int line,
const char *const name,
const int w,
const int h,
int *const err )
static

Definition at line 726 of file utils.c.

◆ checkasm_vasprintf()

char * checkasm_vasprintf ( const char * fmt,
va_list arg )

Definition at line 853 of file utils.c.

Referenced by checkasm_set_func_variant().

Variable Documentation

◆ checkasm_prng

CheckasmRand checkasm_prng
static

◆ buf8

uint8_t buf8[PRNG_CACHE_SIZE]

Definition at line 179 of file utils.c.

◆ buf16

uint16_t buf16[PRNG_CACHE_SIZE > > 1]

Definition at line 180 of file utils.c.

Referenced by imc_decode_frame().

◆ buf32

uint32_t buf32[PRNG_CACHE_SIZE > > 2]

Definition at line 181 of file utils.c.

Referenced by decode_frame().

◆ buf64

uint64_t buf64[PRNG_CACHE_SIZE > > 3]

Definition at line 182 of file utils.c.

◆ num8

int num8

Definition at line 183 of file utils.c.

◆ num16

int num16

Definition at line 184 of file utils.c.

◆ num32

int num32

Definition at line 185 of file utils.c.

◆ num64

int num64

Definition at line 186 of file utils.c.

Referenced by get_avg_xpsnr().

◆ [struct]

struct { ... } prng_cache

Referenced by checkasm_srand().

◆ use_printf_color

int use_printf_color[2]
static

Definition at line 468 of file utils.c.

Referenced by checkasm_setup_fprintf(), checkasm_statusline(), and checkasm_vfprintf().

◆ statusline

char statusline[256]
static

Definition at line 469 of file utils.c.

Referenced by checkasm_statusline(), and checkasm_vfprintf().

◆ statusline_visible

int statusline_visible
static

Definition at line 470 of file utils.c.

Referenced by checkasm_statusline(), and checkasm_vfprintf().