checkasm 1.3.0
Assembly testing and benchmarking framework
Loading...
Searching...
No Matches
utils.h
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
40
41#ifndef CHECKASM_UTILS_H
42#define CHECKASM_UTILS_H
43
44#include <stdint.h>
45
46#include "checkasm/attributes.h"
47
62
68
74
80
86
92
98
104
110
116
122
130
138 /* rng */
140
147typedef struct CheckasmDist {
148 double mean;
149 double stddev;
151
156#define checkasm_dist_standard ((CheckasmDist) { 0.0, 1.0 })
157
164
171
181
187CHECKASM_API void checkasm_randomize(void *buf, size_t bytes);
188
195CHECKASM_API void checkasm_randomize_mask8(uint8_t *buf, int width, uint8_t mask);
196
203CHECKASM_API void checkasm_randomize_mask16(uint16_t *buf, int width, uint16_t mask);
204
211CHECKASM_API void checkasm_randomize_range(double *buf, int width, double range);
212
219CHECKASM_API void checkasm_randomize_rangef(float *buf, int width, float range);
220
228CHECKASM_API void checkasm_randomize_interval(double *buf, int width, double low,
229 double high);
230
238CHECKASM_API void checkasm_randomize_intervalf(float *buf, int width, float low,
239 float high);
240
247CHECKASM_API void checkasm_randomize_dist(double *buf, int width, CheckasmDist dist);
248
255CHECKASM_API void checkasm_randomize_distf(float *buf, int width, CheckasmDist dist);
256
262CHECKASM_API void checkasm_randomize_norm(double *buf, int width);
263
269CHECKASM_API void checkasm_randomize_normf(float *buf, int width);
270
276CHECKASM_API void checkasm_clear(void *buf, size_t bytes);
277
286CHECKASM_API void checkasm_clear8(uint8_t *buf, int width, uint8_t val);
287
294CHECKASM_API void checkasm_clear16(uint16_t *buf, int width, uint16_t val);
295
307CHECKASM_API void checkasm_init(void *buf, size_t bytes);
308
316CHECKASM_API void checkasm_init_mask8(uint8_t *buf, int width, uint8_t mask);
317
325CHECKASM_API void checkasm_init_mask16(uint16_t *buf, int width, uint16_t mask);
326
332#define CLEAR_BUF(buf) checkasm_clear(buf, sizeof(buf))
333
339#define RANDOMIZE_BUF(buf) checkasm_randomize(buf, sizeof(buf))
340
347#define INITIALIZE_BUF(buf) checkasm_init(buf, sizeof(buf))
348 /* memory */
350
359
367CHECKASM_API int checkasm_float_near_ulp(float a, float b, unsigned max_ulp);
368
376CHECKASM_API int checkasm_float_near_abs_eps(float a, float b, float eps);
377
386CHECKASM_API int checkasm_float_near_abs_eps_ulp(float a, float b, float eps,
387 unsigned max_ulp);
388
397CHECKASM_API int checkasm_float_near_ulp_array(const float *a, const float *b,
398 unsigned max_ulp, int len);
399
408CHECKASM_API int checkasm_float_near_abs_eps_array(const float *a, const float *b,
409 float eps, int len);
410
420CHECKASM_API int checkasm_float_near_abs_eps_array_ulp(const float *a, const float *b,
421 float eps, unsigned max_ulp,
422 int len);
423
431CHECKASM_API int checkasm_double_near_abs_eps(double a, double b, double eps);
432
441CHECKASM_API int checkasm_double_near_abs_eps_array(const double *a, const double *b,
442 double eps, unsigned len);
443 /* floatcmp */
445
448#define float_near_ulp checkasm_float_near_ulp
449#define float_near_abs_eps checkasm_float_near_abs_eps
450#define float_near_abs_eps_ulp checkasm_float_near_abs_eps_ulp
451#define float_near_ulp_array checkasm_float_near_ulp_array
452#define float_near_abs_eps_array checkasm_float_near_abs_eps_array
453#define float_near_abs_eps_array_ulp checkasm_float_near_abs_eps_array_ulp
454#define double_near_abs_eps checkasm_double_near_abs_eps
455#define double_near_abs_eps_array checkasm_double_near_abs_eps_array
457
467
483#ifdef _MSC_VER
484 #define CHECKASM_ALIGN(x) __declspec(align(CHECKASM_ALIGNMENT)) x
485#else
486 #define CHECKASM_ALIGN(x) x __attribute__((aligned(CHECKASM_ALIGNMENT)))
487#endif
488
493
494#define DECL_CHECK_FUNC(NAME, TYPE) \
495 int (NAME)(const char *const file, const int line, const TYPE *const buf1, \
496 const ptrdiff_t stride1, const TYPE *const buf2, const ptrdiff_t stride2, \
497 const int w, const int h, const char *const buf_name, const int align_w, \
498 const int align_h, const int padding)
499
500#define DECL_CHECKASM_CHECK_FUNC(type) \
501 CHECKASM_API DECL_CHECK_FUNC(checkasm_check_impl_##type, type)
502
503DECL_CHECKASM_CHECK_FUNC(int);
504DECL_CHECKASM_CHECK_FUNC(int8_t);
505DECL_CHECKASM_CHECK_FUNC(int16_t);
506DECL_CHECKASM_CHECK_FUNC(int32_t);
507
508DECL_CHECKASM_CHECK_FUNC(unsigned);
509DECL_CHECKASM_CHECK_FUNC(uint8_t);
510DECL_CHECKASM_CHECK_FUNC(uint16_t);
511DECL_CHECKASM_CHECK_FUNC(uint32_t);
512
516CHECKASM_API int checkasm_check_impl_float_ulp(const char *file, int line,
517 const float *buf1, ptrdiff_t stride1,
518 const float *buf2, ptrdiff_t stride2,
519 int w, int h, const char *name,
520 unsigned max_ulp, int align_w, int align_h,
521 int padding);
522
523#define checkasm_check_impl2(type) checkasm_check_impl_##type
524#define checkasm_check_impl(type) checkasm_check_impl2(type)
525#define checkasm_check1(type, ...) checkasm_check_impl_##type(__VA_ARGS__)
526#define checkasm_check2(type, ...) checkasm_check1(type, __FILE__, __LINE__, __VA_ARGS__)
527 /* internal */
529
560#define checkasm_check2d(type, ...) checkasm_check2(type, __VA_ARGS__, 0, 0, 0)
561
582#define checkasm_check2d_padded(type, ...) checkasm_check2(type, __VA_ARGS__)
583
604#define checkasm_check1d(type, buf1, buf2, len, ...) \
605 checkasm_check2d(type, buf1, 0, buf2, 0, len, 1, __VA_ARGS__)
606
624#define checkasm_check1d_padded(type, buf1, buf2, len, name, align, padding) \
625 checkasm_check2d_padded(type, buf1, 0, buf2, 0, len, 1, name, align, 0, padding)
626 /* bufcmp */
628
631#define checkasm_check checkasm_check2d
632#define checkasm_check_padded checkasm_check2d_padded
633/* @} */
634
644
652#define CHECKASM_ROUND(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
653
683#define BUF_RECT(type, name, w, h) \
684 DECL_CHECK_FUNC(*checkasm_check_impl_##name##_type, type) \
685 = checkasm_check_impl_##type; \
686 CHECKASM_ALIGN(type name##_buf[((h) + 32) * (CHECKASM_ROUND(w, 64) + 64) + 64]); \
687 const int name##_buf_w = CHECKASM_ROUND(w, 64) + 64; \
688 const int name##_buf_h = (h) + 32; \
689 ptrdiff_t name##_stride = sizeof(type) * name##_buf_w; \
690 (void) checkasm_check_impl(name##_type); \
691 (void) name##_stride; \
692 (void) name##_buf_h; \
693 type *name = name##_buf + name##_buf_w * 16 + 64
694
701#define CLEAR_BUF_RECT(name) CLEAR_BUF(name##_buf)
702
709#define INITIALIZE_BUF_RECT(name) INITIALIZE_BUF(name##_buf)
710
717#define RANDOMIZE_BUF_RECT(name) RANDOMIZE_BUF(name##_buf)
718
726#define checkasm_check_rect(rect1, ...) checkasm_check2d(rect1##_type, rect1, __VA_ARGS__)
727
735#define checkasm_check_rect_padded(rect1, ...) \
736 checkasm_check2d_padded(rect1##_type, rect1, __VA_ARGS__, 1, 1, 8)
737
751#define checkasm_check_rect_padded_align(rect1, ...) \
752 checkasm_check2d_padded(rect1##_type, rect1, __VA_ARGS__, 8)
753
763#define CHECK_BUF_RECT(buf1, buf2, w, h) \
764 checkasm_check_rect_padded(buf1, buf1##_stride, buf2, buf2##_stride, w, h, \
765 #buf1 " vs " #buf2)
766 /* bufrect */
768
769#endif /* CHECKASM_UTILS_H */
Platform and compiler attribute macros.
#define CHECKASM_API
Symbol visibility attribute for public API functions.
Definition attributes.h:90
CHECKASM_API int checkasm_float_near_abs_eps_array(const float *a, const float *b, float eps, int len)
Compare float arrays using absolute epsilon tolerance.
CHECKASM_API int checkasm_float_near_ulp_array(const float *a, const float *b, unsigned max_ulp, int len)
Compare float arrays using ULP tolerance.
CHECKASM_API int checkasm_float_near_ulp(float a, float b, unsigned max_ulp)
Compare floats using ULP (Units in Last Place) tolerance.
CHECKASM_API int checkasm_float_near_abs_eps_ulp(float a, float b, float eps, unsigned max_ulp)
Compare floats using both epsilon and ULP tolerances.
CHECKASM_API int checkasm_double_near_abs_eps_array(const double *a, const double *b, double eps, unsigned len)
Compare double arrays using absolute epsilon tolerance.
CHECKASM_API int checkasm_float_near_abs_eps(float a, float b, float eps)
Compare floats using absolute epsilon tolerance.
CHECKASM_API int checkasm_double_near_abs_eps(double a, double b, double eps)
Compare doubles using absolute epsilon tolerance.
CHECKASM_API int checkasm_float_near_abs_eps_array_ulp(const float *a, const float *b, float eps, unsigned max_ulp, int len)
Compare float arrays using both epsilon and ULP tolerances.
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.
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.
CHECKASM_API void checkasm_randomize_normf(float *buf, int width)
Fill a float buffer with values from a standard normal distribution.
CHECKASM_API void checkasm_randomize_intervalf(float *buf, int width, float low, float high)
Fill a float buffer with random values chosen uniformly from an interval.
CHECKASM_API void checkasm_randomize_interval(double *buf, int width, double low, double high)
Fill a double buffer with random values chosen uniformly from an interval.
CHECKASM_API void checkasm_randomize(void *buf, size_t bytes)
Fill a buffer with uniformly chosen random bytes.
CHECKASM_API void checkasm_clear16(uint16_t *buf, int width, uint16_t val)
Fill a uint16_t buffer with a constant value.
CHECKASM_API void checkasm_clear(void *buf, size_t bytes)
Clear a buffer to a pre-determined pattern (currently 0xAA).
CHECKASM_API 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.
CHECKASM_API void checkasm_randomize_rangef(float *buf, int width, float range)
Fill a float buffer with random values chosen uniformly below a limit.
CHECKASM_API 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.
CHECKASM_API void checkasm_randomize_norm(double *buf, int width)
Fill a double buffer with values from a standard normal distribution.
CHECKASM_API void checkasm_randomize_dist(double *buf, int width, CheckasmDist dist)
Fill a double buffer with normally distributed random values.
CHECKASM_API void checkasm_randomize_range(double *buf, int width, double range)
Fill a double buffer with random values chosen uniformly below a limit.
CHECKASM_API void checkasm_randomize_distf(float *buf, int width, CheckasmDist dist)
Fill a float buffer with normally distributed random values.
CHECKASM_API void checkasm_init(void *buf, size_t bytes)
Initialize a buffer with pathological test patterns.
CHECKASM_API void checkasm_init_mask16(uint16_t *buf, int width, uint16_t mask)
Initialize a uint16_t buffer with pathological values within a mask.
CHECKASM_API void checkasm_clear8(uint8_t *buf, int width, uint8_t val)
Fill a uint8_t buffer with a constant value.
CHECKASM_API int16_t checkasm_rand_int16(void)
Generate a random 16-bit signed integer.
CHECKASM_API int32_t checkasm_rand_int32(void)
Generate a random 32-bit signed integer.
CHECKASM_API int64_t checkasm_rand_int64(void)
Generate a random 64-bit signed integer.
CHECKASM_API int checkasm_rand(void)
Generate a random non-negative integer.
CHECKASM_API uint16_t checkasm_rand_uint16(void)
Generate a random 16-bit unsigned integer.
CHECKASM_API float checkasm_rand_float32(void)
Generate a truly random 32-bit float.
CHECKASM_API uint8_t checkasm_rand_uint8(void)
Generate a random 8-bit unsigned integer.
CHECKASM_API double checkasm_rand_float64(void)
Generate a truly random 64-bit float.
CHECKASM_API double checkasm_randf(void)
Generate a random double-precision floating-point number.
CHECKASM_API uint32_t checkasm_rand_uint32(void)
Generate a random 32-bit unsigned integer.
CHECKASM_API uint64_t checkasm_rand_uint64(void)
Generate a random 64-bit unsigned integer.
CHECKASM_API int8_t checkasm_rand_int8(void)
Generate a random 8-bit signed integer.
Describes a normal (Gaussian) distribution.
Definition utils.h:147
double stddev
Definition utils.h:149
double mean
Definition utils.h:148
CHECKASM_API double checkasm_rand_norm(void)
Generate a random number from the standard normal distribution.
CHECKASM_API double checkasm_rand_dist(CheckasmDist dist)
Generate a normally distributed random number.