checkasm 1.3.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_clear_cpu_state(); \
255 checkasm_set_signal_handler_state(0)
256
272#ifndef checkasm_call_checked
273 #define checkasm_call_checked(func, ...) \
274 (checkasm_set_signal_handler_state(1), \
275 checkasm_push_stack_guard((uintptr_t[16]) { 0, 0 }), \
276 ((func_type *) (func))(__VA_ARGS__)); \
277 checkasm_pop_stack_guard(); \
278 checkasm_set_signal_handler_state(0)
279#endif
280
308
319
329
339#define checkasm_func_ref ((func_type *) checkasm_key_ref)
340
353#define checkasm_func_new ((func_type *) checkasm_key_new)
354
367#define checkasm_call_ref(...) checkasm_call(checkasm_func_ref, __VA_ARGS__)
368
382#define checkasm_call_new(...) checkasm_call_checked(checkasm_func_new, __VA_ARGS__)
383
403#define checkasm_bench(func, ...) \
404 do { \
405 if (checkasm_bench_func()) { \
406 func_type *const bench_func = (func); \
407 checkasm_set_signal_handler_state(1); \
408 for (int truns; (truns = checkasm_bench_runs());) { \
409 uint64_t time; \
410 CHECKASM_PERF_BENCH(truns, time, __VA_ARGS__); \
411 checkasm_clear_cpu_state(); \
412 checkasm_bench_update(truns, time); \
413 } \
414 checkasm_set_signal_handler_state(0); \
415 checkasm_bench_finish(); \
416 } else { \
417 const int tidx = 0; \
418 (void) tidx; \
419 checkasm_call_checked(func, __VA_ARGS__); \
420 } \
421 } while (0)
422
447#define checkasm_bench_new(...) checkasm_bench(checkasm_func_new, __VA_ARGS__)
448
469#define checkasm_alternate(a, b) ((tidx & 1) ? (b) : (a))
470
479#define fail checkasm_fail
480#define report checkasm_report
481#define check_func checkasm_check_func
482#define check_key checkasm_check_key
483#define func_ref checkasm_func_ref
484#define func_new checkasm_func_new
485#define call_ref checkasm_call_ref
486#define call_new checkasm_call_new
487#define bench_new checkasm_bench_new
488#define alternate checkasm_alternate
489#define declare_func checkasm_declare
490#define declare_func_emms checkasm_declare_emms
492
501
507
513CHECKASM_API void checkasm_pop_stack_guard(void);
514
522#ifndef checkasm_clear_cpu_state
523 #define checkasm_clear_cpu_state() \
524 do { \
525 } while (0)
526#endif
527
528typedef struct CheckasmPerf {
534 uint64_t (*start)(void);
535
541 uint64_t (*stop)(uint64_t start_time);
542
544 const char *name;
545
547 const char *unit;
548
549#ifdef CHECKASM_PERF_ASM
551 int asm_usable;
552#endif
554
555#define CHECKASM_PERF_CALL4(...) \
556 do { \
557 int tidx = 0; \
558 bench_func(__VA_ARGS__); \
559 tidx = 1; \
560 bench_func(__VA_ARGS__); \
561 tidx = 2; \
562 bench_func(__VA_ARGS__); \
563 tidx = 3; \
564 bench_func(__VA_ARGS__); \
565 (void) tidx; \
566 } while (0)
567
568#define CHECKASM_PERF_CALL16(...) \
569 do { \
570 CHECKASM_PERF_CALL4(__VA_ARGS__); \
571 CHECKASM_PERF_CALL4(__VA_ARGS__); \
572 CHECKASM_PERF_CALL4(__VA_ARGS__); \
573 CHECKASM_PERF_CALL4(__VA_ARGS__); \
574 } while (0)
575
576/* Naive loop; used when perf.start/stop() is expected to be slow or imprecise, or when
577 * we have no ASM cycle counters, or when the number of iterations is low */
578#define CHECKASM_PERF_BENCH_SIMPLE(count, time, ...) \
579 do { \
580 time = perf.start(); \
581 for (int tidx = 0; tidx < count; tidx++) \
582 bench_func(__VA_ARGS__); \
583 time = perf.stop(time); \
584 } while (0)
585
586/* Unrolled loop with inline outlier rejection; used when we have asm cycle counters */
587#define CHECKASM_PERF_BENCH_ASM(total_count, time, ...) \
588 do { \
589 int tcount_trim = 0; \
590 uint64_t tsum_trim = 0; \
591 for (int titer = 0; titer < total_count; titer += 32) { \
592 uint64_t t = CHECKASM_PERF_ASM(); \
593 CHECKASM_PERF_CALL16(__VA_ARGS__); \
594 CHECKASM_PERF_CALL16(__VA_ARGS__); \
595 t = CHECKASM_PERF_ASM() - t; \
596 if (t * tcount_trim <= tsum_trim * 4 && (titer > 0 || total_count < 1000)) { \
597 tsum_trim += t; \
598 tcount_trim++; \
599 } \
600 } \
601 time = tsum_trim; \
602 total_count = tcount_trim << 5; \
603 } while (0)
604
605/* Select the best benchmarking method at runtime */
606CHECKASM_API const CheckasmPerf *checkasm_get_perf(void);
607
608#ifdef CHECKASM_PERF_ASM
609 #ifndef CHECKASM_PERF_ASM_USABLE
610 #define CHECKASM_PERF_ASM_USABLE perf.asm_usable
611 #endif
612 #define CHECKASM_PERF_BENCH(count, time, ...) \
613 do { \
614 const CheckasmPerf perf = *checkasm_get_perf(); \
615 if (CHECKASM_PERF_ASM_USABLE && count >= 128) { \
616 CHECKASM_PERF_BENCH_ASM(count, time, __VA_ARGS__); \
617 } else { \
618 CHECKASM_PERF_BENCH_SIMPLE(count, time, __VA_ARGS__); \
619 } \
620 } while (0)
621#else /* !CHECKASM_PERF_ASM */
622 #define CHECKASM_PERF_BENCH(count, time, ...) \
623 do { \
624 const CheckasmPerf perf = *checkasm_get_perf(); \
625 CHECKASM_PERF_BENCH_SIMPLE(count, time, __VA_ARGS__); \
626 } while (0)
627#endif
628
634
640
646CHECKASM_API void checkasm_bench_update(int iterations, uint64_t cycles);
647
652
656static inline void checkasm_unused(void)
657{
658 (void) checkasm_key_ref;
659 (void) checkasm_key_new;
660}
661 /* internal */
663
664#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:656
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:528
uint64_t(* start)(void)
Start timing measurement.
Definition test.h:534
const char * unit
Unit of measurement (e.g., "ns", "cycles").
Definition test.h:547
const char * name
Name of the timing mechanism (e.g., "clock_gettime").
Definition test.h:544
uint64_t(* stop)(uint64_t start_time)
Stop timing measurement.
Definition test.h:541
static CheckasmKey checkasm_key_ref
Key identifying the reference implementation.
Definition test.h:318
static CheckasmKey checkasm_key_new
Key identifying the implementation being tested.
Definition test.h:328
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.