37#ifndef CHECKASM_TEST_H
38#define CHECKASM_TEST_H
44#include "checkasm/perf.h"
45#include "checkasm/platform.h"
77#define checkasm_check_func(func, ...) \
79 = checkasm_check_key((checkasm_key_new = (CheckasmKey) (func)), __VA_ARGS__))
155#define checkasm_fail() checkasm_fail_func("%s:%d", __FILE__, __LINE__)
205#define checkasm_declare(ret, ...) \
206 checkasm_declare_impl(ret, __VA_ARGS__); \
207 typedef ret func_type(__VA_ARGS__); \
208 (void) ((func_type *) NULL)
233#ifndef checkasm_declare_emms
234 #define checkasm_declare_emms(cpu_flags, ret, ...) checkasm_declare(ret, __VA_ARGS__)
252#define checkasm_call(func, ...) \
253 (checkasm_set_signal_handler_state(1), (func) (__VA_ARGS__)); \
254 checkasm_set_signal_handler_state(0)
271#ifndef checkasm_call_checked
272 #define checkasm_call_checked(func, ...) \
273 (checkasm_set_signal_handler_state(1), \
274 checkasm_push_stack_guard((uintptr_t[16]) { 0, 0 }), \
275 ((func_type *) (func))(__VA_ARGS__)); \
276 checkasm_pop_stack_guard(); \
277 checkasm_set_signal_handler_state(0)
338#define checkasm_func_ref ((func_type *) checkasm_key_ref)
352#define checkasm_func_new ((func_type *) checkasm_key_new)
366#define checkasm_call_ref(...) checkasm_call(checkasm_func_ref, __VA_ARGS__)
381#define checkasm_call_new(...) checkasm_call_checked(checkasm_func_new, __VA_ARGS__)
402#define checkasm_bench(func, ...) \
404 if (checkasm_bench_func()) { \
405 func_type *const bench_func = (func); \
406 checkasm_set_signal_handler_state(1); \
407 for (int truns; (truns = checkasm_bench_runs());) { \
409 CHECKASM_PERF_BENCH(truns, time, __VA_ARGS__); \
410 checkasm_clear_cpu_state(); \
411 checkasm_bench_update(truns, time); \
413 checkasm_set_signal_handler_state(0); \
414 checkasm_bench_finish(); \
416 const int tidx = 0; \
418 checkasm_call_checked(func, __VA_ARGS__); \
446#define checkasm_bench_new(...) checkasm_bench(checkasm_func_new, __VA_ARGS__)
468#define checkasm_alternate(a, b) ((tidx & 1) ? (b) : (a))
478#define fail checkasm_fail
479#define report checkasm_report
480#define check_func checkasm_check_func
481#define check_key checkasm_check_key
482#define func_ref checkasm_func_ref
483#define func_new checkasm_func_new
484#define call_ref checkasm_call_ref
485#define call_new checkasm_call_new
486#define bench_new checkasm_bench_new
487#define alternate checkasm_alternate
488#define declare_func checkasm_declare
489#define declare_func_emms checkasm_declare_emms
521#ifndef checkasm_clear_cpu_state
522 #define checkasm_clear_cpu_state() \
540 uint64_t (*
stop)(uint64_t start_time);
548#ifdef CHECKASM_PERF_ASM
554#define CHECKASM_PERF_CALL4(...) \
557 bench_func(__VA_ARGS__); \
559 bench_func(__VA_ARGS__); \
561 bench_func(__VA_ARGS__); \
563 bench_func(__VA_ARGS__); \
567#define CHECKASM_PERF_CALL16(...) \
569 CHECKASM_PERF_CALL4(__VA_ARGS__); \
570 CHECKASM_PERF_CALL4(__VA_ARGS__); \
571 CHECKASM_PERF_CALL4(__VA_ARGS__); \
572 CHECKASM_PERF_CALL4(__VA_ARGS__); \
577#define CHECKASM_PERF_BENCH_SIMPLE(count, time, ...) \
579 time = perf.start(); \
580 for (int tidx = 0; tidx < count; tidx++) \
581 bench_func(__VA_ARGS__); \
582 time = perf.stop(time); \
586#define CHECKASM_PERF_BENCH_ASM(total_count, time, ...) \
588 int tcount_trim = 0; \
589 uint64_t tsum_trim = 0; \
590 for (int titer = 0; titer < total_count; titer += 32) { \
591 uint64_t t = CHECKASM_PERF_ASM(); \
592 CHECKASM_PERF_CALL16(__VA_ARGS__); \
593 CHECKASM_PERF_CALL16(__VA_ARGS__); \
594 t = CHECKASM_PERF_ASM() - t; \
595 if (t * tcount_trim <= tsum_trim * 4 && (titer > 0 || total_count < 1000)) { \
601 total_count = tcount_trim << 5; \
607#ifdef CHECKASM_PERF_ASM
608 #ifndef CHECKASM_PERF_ASM_USABLE
609 #define CHECKASM_PERF_ASM_USABLE perf.asm_usable
611 #define CHECKASM_PERF_BENCH(count, time, ...) \
613 const CheckasmPerf perf = *checkasm_get_perf(); \
614 if (CHECKASM_PERF_ASM_USABLE && count >= 128) { \
615 CHECKASM_PERF_BENCH_ASM(count, time, __VA_ARGS__); \
617 CHECKASM_PERF_BENCH_SIMPLE(count, time, __VA_ARGS__); \
621 #define CHECKASM_PERF_BENCH(count, time, ...) \
623 const CheckasmPerf perf = *checkasm_get_perf(); \
624 CHECKASM_PERF_BENCH_SIMPLE(count, time, __VA_ARGS__); \
Platform and compiler attribute macros.
#define CHECKASM_PRINTF(fmt, attr)
Printf-style format string checking attribute.
Definition attributes.h:63
#define CHECKASM_API
Symbol visibility attribute for public API functions.
Definition attributes.h:90
Main checkasm API for test suite configuration and execution.
uint64_t CheckasmCpu
Opaque type representing a set of CPU feature flags.
Definition checkasm.h:88
uintptr_t CheckasmKey
Opaque type used to identify function implementations.
Definition checkasm.h:96
CHECKASM_API int checkasm_bench_func(void)
Check if current function should be benchmarked.
static void checkasm_unused(void)
Suppress unused variable warnings.
Definition test.h:655
CHECKASM_API void checkasm_bench_finish(void)
Finalize and store benchmark results.
CHECKASM_API void checkasm_bench_update(int iterations, uint64_t cycles)
Update benchmark statistics with timing results.
CHECKASM_API int checkasm_bench_runs(void)
Get number of iterations for current benchmark run.
CHECKASM_API void checkasm_push_stack_guard(uintptr_t guard[2])
Push stack guard values for corruption detection.
CHECKASM_API void checkasm_set_signal_handler_state(int enabled)
Enable or disable signal handling.
uint64_t(* start)(void)
Start timing measurement.
Definition test.h:533
const char * unit
Unit of measurement (e.g., "ns", "cycles").
Definition test.h:546
const char * name
Name of the timing mechanism (e.g., "clock_gettime").
Definition test.h:543
uint64_t(* stop)(uint64_t start_time)
Stop timing measurement.
Definition test.h:540
static CheckasmKey checkasm_key_ref
Key identifying the reference implementation.
Definition test.h:317
static CheckasmKey checkasm_key_new
Key identifying the implementation being tested.
Definition test.h:327
CHECKASM_API void checkasm_report(const char *name,...) CHECKASM_PRINTF(1
Report test outcome for a named group of functions.
CHECKASM_API CheckasmKey checkasm_check_key(CheckasmKey key, const char *name,...) CHECKASM_PRINTF(2
Check if a key should be tested.
CHECKASM_API int checkasm_should_fail(CheckasmCpu cpu_flags)
Mark a block of tests as expected to fail.
CHECKASM_API CheckasmKey CHECKASM_API void checkasm_set_func_variant(const char *id,...) CHECKASM_PRINTF(1
Set a custom variant identifier for the next checkasm_check_func() call.
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.