@@@ all the mess ever
[mLib] / test / bench.h
1 /* -*-c-*-
2 *
3 * Benchmarking support
4 *
5 * (c) 2023 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the mLib utilities library.
11 *
12 * mLib is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU Library General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
16 *
17 * mLib is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
20 * License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib. If not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
25 * USA.
26 */
27
28 #ifndef MLIB_BENCH_H
29 #define MLIB_BENCH_H
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 /*----- Header files ------------------------------------------------------*/
36
37 #ifndef MLIB_BITS_H
38 # include "bits.h"
39 #endif
40
41 /*----- Data structures ---------------------------------------------------*/
42
43 struct bench_time {
44 unsigned f;
45 #define BTF_TIMEOK 1u
46 #define BTF_CYOK 2u
47 #define BTF_ANY (BTF_TIMEOK | BTF_CYOK)
48 kludge64 s; uint32 ns;
49 kludge64 cy;
50 };
51
52 struct bench_timing {
53 unsigned f;
54 unsigned long n;
55 double t, cy;
56 };
57
58 struct bench_timer { const struct bench_timerops *ops; };
59
60 struct bench_timerops {
61 void (*now)(struct bench_timer */*bt*/, struct bench_time */*t_out*/);
62 void (*destroy)(struct bench_timer */*bt*/);
63 };
64
65 struct bench_state {
66 struct bench_timer *tm;
67 double target_s;
68 unsigned f;
69 struct { double m, c; } clk, cy;
70 };
71
72 typedef void bench_fn(unsigned long /*n*/, void */*p*/);
73
74 /*----- Functions provided ------------------------------------------------*/
75
76 extern struct bench_timer *bench_createtimer(void);
77
78 extern void bench_init(struct bench_state *b, struct bench_timer *tm);
79
80 extern void bench_destroy(struct bench_state *b);
81
82 extern int bench_calibrate(struct bench_state */*b*/);
83
84 extern int bench_measure(struct bench_timing */*t_out*/,
85 struct bench_state */*b*/,
86 bench_fn */*fn*/, void */*p*/);
87
88 /*----- That's all, folks -------------------------------------------------*/
89
90 #ifdef __cplusplus
91 }
92 #endif
93
94 #endif