checkasm 1.1.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
76#define checkasm_check_func(func, ...) \
77 (checkasm_key_ref \
78 = checkasm_check_key((checkasm_key_new = (CheckasmKey) (func)), __VA_ARGS__))
79
97CHECKASM_API int checkasm_fail_func(const char *msg, ...) CHECKASM_PRINTF(1, 2);
98
116#define checkasm_fail() checkasm_fail_func("%s:%d", __FILE__, __LINE__)
117
142CHECKASM_API void checkasm_report(const char *name, ...) CHECKASM_PRINTF(1, 2);
143
166#define checkasm_declare(ret, ...) \
167 checkasm_declare_impl(ret, __VA_ARGS__); \
168 typedef ret func_type(__VA_ARGS__); \
169 (void) ((func_type *) NULL)
170
194#ifndef checkasm_declare_emms
195 #define checkasm_declare_emms(cpu_flags, ret, ...) checkasm_declare(ret, __VA_ARGS__)
196#endif
197
213#define checkasm_call(func, ...) \
214 (checkasm_set_signal_handler_state(1), (func) (__VA_ARGS__)); \
215 checkasm_set_signal_handler_state(0)
216
232#ifndef checkasm_call_checked
233 #define checkasm_call_checked(func, ...) \
234 (checkasm_set_signal_handler_state(1), \
235 checkasm_push_stack_guard((uintptr_t[16]) { 0, 0 }), \
236 ((func_type *) (func))(__VA_ARGS__)); \
237 checkasm_pop_stack_guard(); \
238 checkasm_set_signal_handler_state(0)
239#endif
240
268
279
289
299#define checkasm_func_ref ((func_type *) checkasm_key_ref)
300
313#define checkasm_func_new ((func_type *) checkasm_key_new)
314
327#define checkasm_call_ref(...) checkasm_call(checkasm_func_ref, __VA_ARGS__)
328
342#define checkasm_call_new(...) checkasm_call_checked(checkasm_func_new, __VA_ARGS__)
343
363#define checkasm_bench(func, ...) \
364 do { \
365 if (checkasm_bench_func()) { \
366 func_type *const bench_func = (func); \
367 checkasm_set_signal_handler_state(1); \
368 for (int truns; (truns = checkasm_bench_runs());) { \
369 uint64_t time; \
370 CHECKASM_PERF_BENCH(truns, time, __VA_ARGS__); \
371 checkasm_clear_cpu_state(); \
372 checkasm_bench_update(truns, time); \
373 } \
374 checkasm_set_signal_handler_state(0); \
375 checkasm_bench_finish(); \
376 } else { \
377 const int tidx = 0; \
378 (void) tidx; \
379 checkasm_call_checked(func, __VA_ARGS__); \
380 } \
381 } while (0)
382
407#define checkasm_bench_new(...) checkasm_bench(checkasm_func_new, __VA_ARGS__)
408
429#define checkasm_alternate(a, b) ((tidx & 1) ? (b) : (a))
430
439#define fail checkasm_fail
440#define report checkasm_report
441#define check_func checkasm_check_func
442#define check_key checkasm_check_key
443#define func_ref checkasm_func_ref
444#define func_new checkasm_func_new
445#define call_ref checkasm_call_ref
446#define call_new checkasm_call_new
447#define bench_new checkasm_bench_new
448#define alternate checkasm_alternate
449#define declare_func checkasm_declare
450#define declare_func_emms checkasm_declare_emms
452
461
467 CHECKASM_PRINTF(2, 3);
468
474
480CHECKASM_API void checkasm_pop_stack_guard(void);
481
489#ifndef checkasm_clear_cpu_state
490 #define checkasm_clear_cpu_state() \
491 do { \
492 } while (0)
493#endif
494
495typedef struct CheckasmPerf {
501 uint64_t (*start)(void);
502
508 uint64_t (*stop)(uint64_t start_time);
509
511 const char *name;
512
514 const char *unit;
515
516#ifdef CHECKASM_PERF_ASM
518 int asm_usable;
519#endif
521
522#define CHECKASM_PERF_CALL4(...) \
523 do { \
524 int tidx = 0; \
525 bench_func(__VA_ARGS__); \
526 tidx = 1; \
527 bench_func(__VA_ARGS__); \
528 tidx = 2; \
529 bench_func(__VA_ARGS__); \
530 tidx = 3; \
531 bench_func(__VA_ARGS__); \
532 (void) tidx; \
533 } while (0)
534
535#define CHECKASM_PERF_CALL16(...) \
536 do { \
537 CHECKASM_PERF_CALL4(__VA_ARGS__); \
538 CHECKASM_PERF_CALL4(__VA_ARGS__); \
539 CHECKASM_PERF_CALL4(__VA_ARGS__); \
540 CHECKASM_PERF_CALL4(__VA_ARGS__); \
541 } while (0)
542
543/* Naive loop; used when perf.start/stop() is expected to be slow or imprecise, or when
544 * we have no ASM cycle counters, or when the number of iterations is low */
545#define CHECKASM_PERF_BENCH_SIMPLE(count, time, ...) \
546 do { \
547 time = perf.start(); \
548 for (int tidx = 0; tidx < count; tidx++) \
549 bench_func(__VA_ARGS__); \
550 time = perf.stop(time); \
551 } while (0)
552
553/* Unrolled loop with inline outlier rejection; used when we have asm cycle counters */
554#define CHECKASM_PERF_BENCH_ASM(total_count, time, ...) \
555 do { \
556 int tcount_trim = 0; \
557 uint64_t tsum_trim = 0; \
558 for (int titer = 0; titer < total_count; titer += 32) { \
559 uint64_t t = CHECKASM_PERF_ASM(); \
560 CHECKASM_PERF_CALL16(__VA_ARGS__); \
561 CHECKASM_PERF_CALL16(__VA_ARGS__); \
562 t = CHECKASM_PERF_ASM() - t; \
563 if (t * tcount_trim <= tsum_trim * 4 && (titer > 0 || total_count < 1000)) { \
564 tsum_trim += t; \
565 tcount_trim++; \
566 } \
567 } \
568 time = tsum_trim; \
569 total_count = tcount_trim << 5; \
570 } while (0)
571
572/* Select the best benchmarking method at runtime */
573CHECKASM_API const CheckasmPerf *checkasm_get_perf(void);
574
575#ifdef CHECKASM_PERF_ASM
576 #ifndef CHECKASM_PERF_ASM_USABLE
577 #define CHECKASM_PERF_ASM_USABLE perf.asm_usable
578 #endif
579 #define CHECKASM_PERF_BENCH(count, time, ...) \
580 do { \
581 const CheckasmPerf perf = *checkasm_get_perf(); \
582 if (CHECKASM_PERF_ASM_USABLE && count >= 128) { \
583 CHECKASM_PERF_BENCH_ASM(count, time, __VA_ARGS__); \
584 } else { \
585 CHECKASM_PERF_BENCH_SIMPLE(count, time, __VA_ARGS__); \
586 } \
587 } while (0)
588#else /* !CHECKASM_PERF_ASM */
589 #define CHECKASM_PERF_BENCH(count, time, ...) \
590 do { \
591 const CheckasmPerf perf = *checkasm_get_perf(); \
592 CHECKASM_PERF_BENCH_SIMPLE(count, time, __VA_ARGS__); \
593 } while (0)
594#endif
595
601
607
613CHECKASM_API void checkasm_bench_update(int iterations, uint64_t cycles);
614
619
623static inline void checkasm_unused(void)
624{
625 (void) checkasm_key_ref;
626 (void) checkasm_key_new;
627}
628 /* internal */
630
631#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:623
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 CheckasmKey checkasm_check_key(CheckasmKey version, const char *name,...) CHECKASM_PRINTF(2
Internal implementation of checkasm_check_func().
CHECKASM_API CheckasmKey CHECKASM_API void checkasm_set_signal_handler_state(int enabled)
Enable or disable signal handling.
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.
Definition test.h:495
uint64_t(* start)(void)
Start timing measurement.
Definition test.h:501
const char * unit
Unit of measurement (e.g., "ns", "cycles").
Definition test.h:514
const char * name
Name of the timing mechanism (e.g., "clock_gettime").
Definition test.h:511
uint64_t(* stop)(uint64_t start_time)
Stop timing measurement.
Definition test.h:508
static CheckasmKey checkasm_key_ref
Key identifying the reference implementation.
Definition test.h:278
static CheckasmKey checkasm_key_new
Key identifying the implementation being tested.
Definition test.h:288
CHECKASM_API void checkasm_report(const char *name,...) CHECKASM_PRINTF(1
Report test outcome for a named group of functions.
CHECKASM_API int checkasm_fail_func(const char *msg,...) CHECKASM_PRINTF(1
Mark the current function as failed with a custom message.
CHECKASM_API int checkasm_should_fail(CheckasmCpu cpu_flags)
Mark a block of tests as expected to fail.