checkasm 1.0.1
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
56
62
68
74
80 /* rng */
82
89typedef struct CheckasmDist {
90 double mean;
91 double stddev;
93
98#define checkasm_dist_standard ((CheckasmDist) { 0.0, 1.0 })
99
106
113
123
129CHECKASM_API void checkasm_randomize(void *buf, size_t bytes);
130
137CHECKASM_API void checkasm_randomize_mask8(uint8_t *buf, int width, uint8_t mask);
138
145CHECKASM_API void checkasm_randomize_mask16(uint16_t *buf, int width, uint16_t mask);
146
153CHECKASM_API void checkasm_randomize_range(double *buf, int width, double range);
154
161CHECKASM_API void checkasm_randomize_rangef(float *buf, int width, float range);
162
169CHECKASM_API void checkasm_randomize_dist(double *buf, int width, CheckasmDist dist);
170
177CHECKASM_API void checkasm_randomize_distf(float *buf, int width, CheckasmDist dist);
178
184CHECKASM_API void checkasm_randomize_norm(double *buf, int width);
185
191CHECKASM_API void checkasm_randomize_normf(float *buf, int width);
192
198CHECKASM_API void checkasm_clear(void *buf, size_t bytes);
199
208CHECKASM_API void checkasm_clear8(uint8_t *buf, int width, uint8_t val);
209
216CHECKASM_API void checkasm_clear16(uint16_t *buf, int width, uint16_t val);
217
229CHECKASM_API void checkasm_init(void *buf, size_t bytes);
230
238CHECKASM_API void checkasm_init_mask8(uint8_t *buf, int width, uint8_t mask);
239
247CHECKASM_API void checkasm_init_mask16(uint16_t *buf, int width, uint16_t mask);
248
254#define CLEAR_BUF(buf) checkasm_clear(buf, sizeof(buf))
255
261#define RANDOMIZE_BUF(buf) checkasm_randomize(buf, sizeof(buf))
262
269#define INITIALIZE_BUF(buf) checkasm_init(buf, sizeof(buf))
270 /* memory */
272
281
289CHECKASM_API int checkasm_float_near_ulp(float a, float b, unsigned max_ulp);
290
298CHECKASM_API int checkasm_float_near_abs_eps(float a, float b, float eps);
299
308CHECKASM_API int checkasm_float_near_abs_eps_ulp(float a, float b, float eps,
309 unsigned max_ulp);
310
319CHECKASM_API int checkasm_float_near_ulp_array(const float *a, const float *b,
320 unsigned max_ulp, int len);
321
330CHECKASM_API int checkasm_float_near_abs_eps_array(const float *a, const float *b,
331 float eps, int len);
332
342CHECKASM_API int checkasm_float_near_abs_eps_array_ulp(const float *a, const float *b,
343 float eps, unsigned max_ulp,
344 int len);
345
353CHECKASM_API int checkasm_double_near_abs_eps(double a, double b, double eps);
354
363CHECKASM_API int checkasm_double_near_abs_eps_array(const double *a, const double *b,
364 double eps, unsigned len);
365 /* floatcmp */
367
370#define float_near_ulp checkasm_float_near_ulp
371#define float_near_abs_eps checkasm_float_near_abs_eps
372#define float_near_abs_eps_ulp checkasm_float_near_abs_eps_ulp
373#define float_near_ulp_array checkasm_float_near_ulp_array
374#define float_near_abs_eps_array checkasm_float_near_abs_eps_array
375#define float_near_abs_eps_array_ulp checkasm_float_near_abs_eps_array_ulp
376#define double_near_abs_eps checkasm_double_near_abs_eps
377#define double_near_abs_eps_array checkasm_double_near_abs_eps_array
379
389
405#ifdef _MSC_VER
406 #define CHECKASM_ALIGN(x) __declspec(align(CHECKASM_ALIGNMENT)) x
407#else
408 #define CHECKASM_ALIGN(x) x __attribute__((aligned(CHECKASM_ALIGNMENT)))
409#endif
410
415
416#define DECL_CHECK_FUNC(NAME, TYPE) \
417 int (NAME)(const char *const file, const int line, const TYPE *const buf1, \
418 const ptrdiff_t stride1, const TYPE *const buf2, const ptrdiff_t stride2, \
419 const int w, const int h, const char *const buf_name, const int align_w, \
420 const int align_h, const int padding)
421
422#define DECL_CHECKASM_CHECK_FUNC(type) \
423 CHECKASM_API DECL_CHECK_FUNC(checkasm_check_impl_##type, type)
424
425DECL_CHECKASM_CHECK_FUNC(int);
426DECL_CHECKASM_CHECK_FUNC(int8_t);
427DECL_CHECKASM_CHECK_FUNC(int16_t);
428DECL_CHECKASM_CHECK_FUNC(int32_t);
429
430DECL_CHECKASM_CHECK_FUNC(unsigned);
431DECL_CHECKASM_CHECK_FUNC(uint8_t);
432DECL_CHECKASM_CHECK_FUNC(uint16_t);
433DECL_CHECKASM_CHECK_FUNC(uint32_t);
434
438CHECKASM_API int checkasm_check_impl_float_ulp(const char *file, int line,
439 const float *buf1, ptrdiff_t stride1,
440 const float *buf2, ptrdiff_t stride2,
441 int w, int h, const char *name,
442 unsigned max_ulp, int align_w, int align_h,
443 int padding);
444
445#define checkasm_check_impl2(type) checkasm_check_impl_##type
446#define checkasm_check_impl(type) checkasm_check_impl2(type)
447#define checkasm_check1(type, ...) checkasm_check_impl_##type(__VA_ARGS__)
448#define checkasm_check2(type, ...) checkasm_check1(type, __FILE__, __LINE__, __VA_ARGS__)
449 /* internal */
451
480#define checkasm_check(type, ...) checkasm_check2(type, __VA_ARGS__, 0, 0, 0)
481
501#define checkasm_check_padded(type, ...) checkasm_check2(type, __VA_ARGS__)
502 /* bufcmp */
504
514
522#define CHECKASM_ROUND(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
523
553#define BUF_RECT(type, name, w, h) \
554 DECL_CHECK_FUNC(*checkasm_check_impl_##name##_type, type) \
555 = checkasm_check_impl_##type; \
556 CHECKASM_ALIGN(type name##_buf[((h) + 32) * (CHECKASM_ROUND(w, 64) + 64) + 64]); \
557 ptrdiff_t name##_stride = sizeof(type) * (CHECKASM_ROUND(w, 64) + 64); \
558 int name##_buf_h = (h) + 32; \
559 (void) checkasm_check_impl(name##_type); \
560 (void) name##_stride; \
561 (void) name##_buf_h; \
562 type *name = name##_buf + (CHECKASM_ROUND(w, 64) + 64) * 16 + 64
563
570#define CLEAR_BUF_RECT(name) CLEAR_BUF(name##_buf)
571
578#define INITIALIZE_BUF_RECT(name) INITIALIZE_BUF(name##_buf)
579
586#define RANDOMIZE_BUF_RECT(name) RANDOMIZE_BUF(name##_buf)
587
595#define checkasm_check_rect(rect1, ...) checkasm_check(rect1##_type, rect1, __VA_ARGS__)
596
604#define checkasm_check_rect_padded(rect1, ...) \
605 checkasm_check_padded(rect1##_type, rect1, __VA_ARGS__, 1, 1, 8)
606
620#define checkasm_check_rect_padded_align(rect1, ...) \
621 checkasm_check_padded(rect1##_type, rect1, __VA_ARGS__, 8)
622 /* bufrect */
624
625#endif /* CHECKASM_UTILS_H */
Platform and compiler attribute macros.
#define CHECKASM_API
Symbol visibility attribute for public API functions.
Definition attributes.h:88
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(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 int32_t checkasm_rand_int32(void)
Generate a random 32-bit signed integer.
CHECKASM_API int checkasm_rand(void)
Generate a random non-negative integer.
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.
Describes a normal (Gaussian) distribution.
Definition utils.h:89
double stddev
Definition utils.h:91
double mean
Definition utils.h:90
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.