checkasm 1.2.0
Assembly testing and benchmarking framework
Loading...
Searching...
No Matches
test.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
36
37#ifndef CHECKASM_TEST_H
38#define CHECKASM_TEST_H
39
40#include <stdint.h>
41
42#include "checkasm/attributes.h"
43#include "checkasm/checkasm.h"
44#include "checkasm/perf.h"
45#include "checkasm/platform.h"
46
77#define checkasm_check_func(func, ...) \
78 (checkasm_key_ref \
79 = checkasm_check_key((checkasm_key_new = (CheckasmKey) (func)), __VA_ARGS__))
80
100 CHECKASM_PRINTF(2, 3);
101
118
136CHECKASM_API int checkasm_fail_func(const char *msg, ...) CHECKASM_PRINTF(1, 2);
137
155#define checkasm_fail() checkasm_fail_func("%s:%d", __FILE__, __LINE__)
156
181CHECKASM_API void checkasm_report(const char *name, ...) CHECKASM_PRINTF(1, 2);
182
205#define checkasm_declare(ret, ...) \
206 checkasm_declare_impl(ret, __VA_ARGS__); \
207 typedef ret func_type(__VA_ARGS__); \
208 (void) ((func_type *) NULL)
209
233#ifndef checkasm_declare_emms
234 #define checkasm_declare_emms(cpu_flags, ret, ...) checkasm_declare(ret, __VA_ARGS__)
235#endif
236
252#define checkasm_call(func, ...) \
253 (checkasm_set_signal_handler_state(1), (func) (__VA_ARGS__)); \
254 checkasm_set_signal_handler_state(0)
255
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)
278#endif
279
307
318
328
338#define checkasm_func_ref ((func_type *) checkasm_key_ref)
339
352#define checkasm_func_new ((func_type *) checkasm_key_new)
353
366#define checkasm_call_ref(...) checkasm_call(checkasm_func_ref, __VA_ARGS__)
367
381#define checkasm_call_new(...) checkasm_call_checked(checkasm_func_new, __VA_ARGS__)
382
402#define checkasm_bench(func, ...) \
403 do { \
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());) { \
408 uint64_t time; \
409 CHECKASM_PERF_BENCH(truns, time, __VA_ARGS__); \
410 checkasm_clear_cpu_state(); \
411 checkasm_bench_update(truns, time); \
412 } \
413 checkasm_set_signal_handler_state(0); \
414 checkasm_bench_finish(); \
415 } else { \
416 const int tidx = 0; \
417 (void) tidx; \
418 checkasm_call_checked(func, __VA_ARGS__); \
419 } \
420 } while (0)
421
446#define checkasm_bench_new(...) checkasm_bench(checkasm_func_new, __VA_ARGS__)
447
468#define checkasm_alternate(a, b) ((tidx & 1) ? (b) : (a))
469
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
491
500
506
512CHECKASM_API void checkasm_pop_stack_guard(void);
513
521#ifndef checkasm_clear_cpu_state
522 #define checkasm_clear_cpu_state() \
523 do { \
524 } while (0)
525#endif
526
527typedef struct CheckasmPerf {
533 uint64_t (*start)(void);
534
540 uint64_t (*stop)(uint64_t start_time);
541
543 const char *name;
544
546 const char *unit;
547
548#ifdef CHECKASM_PERF_ASM
550 int asm_usable;
551#endif
553
554#define CHECKASM_PERF_CALL4(...) \
555 do { \
556 int tidx = 0; \
557 bench_func(__VA_ARGS__); \
558 tidx = 1; \
559 bench_func(__VA_ARGS__); \
560 tidx = 2; \
561 bench_func(__VA_ARGS__); \
562 tidx = 3; \
563 bench_func(__VA_ARGS__); \
564 (void) tidx; \
565 } while (0)
566
567#define CHECKASM_PERF_CALL16(...) \
568 do { \
569 CHECKASM_PERF_CALL4(__VA_ARGS__); \
570 CHECKASM_PERF_CALL4(__VA_ARGS__); \
571 CHECKASM_PERF_CALL4(__VA_ARGS__); \
572 CHECKASM_PERF_CALL4(__VA_ARGS__); \
573 } while (0)
574
575/* Naive loop; used when perf.start/stop() is expected to be slow or imprecise, or when
576 * we have no ASM cycle counters, or when the number of iterations is low */
577#define CHECKASM_PERF_BENCH_SIMPLE(count, time, ...) \
578 do { \
579 time = perf.start(); \
580 for (int tidx = 0; tidx < count; tidx++) \
581 bench_func(__VA_ARGS__); \
582 time = perf.stop(time); \
583 } while (0)
584
585/* Unrolled loop with inline outlier rejection; used when we have asm cycle counters */
586#define CHECKASM_PERF_BENCH_ASM(total_count, time, ...) \
587 do { \
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)) { \
596 tsum_trim += t; \
597 tcount_trim++; \
598 } \
599 } \
600 time = tsum_trim; \
601 total_count = tcount_trim << 5; \
602 } while (0)
603
604/* Select the best benchmarking method at runtime */
605CHECKASM_API const CheckasmPerf *checkasm_get_perf(void);
606
607#ifdef CHECKASM_PERF_ASM
608 #ifndef CHECKASM_PERF_ASM_USABLE
609 #define CHECKASM_PERF_ASM_USABLE perf.asm_usable
610 #endif
611 #define CHECKASM_PERF_BENCH(count, time, ...) \
612 do { \
613 const CheckasmPerf perf = *checkasm_get_perf(); \
614 if (CHECKASM_PERF_ASM_USABLE && count >= 128) { \
615 CHECKASM_PERF_BENCH_ASM(count, time, __VA_ARGS__); \
616 } else { \
617 CHECKASM_PERF_BENCH_SIMPLE(count, time, __VA_ARGS__); \
618 } \
619 } while (0)
620#else /* !CHECKASM_PERF_ASM */
621 #define CHECKASM_PERF_BENCH(count, time, ...) \
622 do { \
623 const CheckasmPerf perf = *checkasm_get_perf(); \
624 CHECKASM_PERF_BENCH_SIMPLE(count, time, __VA_ARGS__); \
625 } while (0)
626#endif
627
633
639
645CHECKASM_API void checkasm_bench_update(int iterations, uint64_t cycles);
646
651
655static inline void checkasm_unused(void)
656{
657 (void) checkasm_key_ref;
658 (void) checkasm_key_new;
659}
660 /* internal */
662
663#endif /* CHECKASM_TEST_H */
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.
Definition test.h:527
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.