-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.c
179 lines (148 loc) · 4.11 KB
/
bench.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <dos.h>
#include "tests/tests.h"
#define TIMER_INTR 0x1c
/* convert ticks into seconds -- this is an approximation */
#define TICKS(seconds) (uint16_t) (18.2 * (seconds))
/* 5 seconds was chosen because it is (very) nearly a whole number of ticks */
#define DEFAULT_SECONDS 5
#define MAX_SECONDS 3600
static uint16_t seconds, total_ticks;
static volatile _Bool started;
static volatile _Bool done;
static volatile uint16_t ticks;
static void (__interrupt __far *prev_handler)(void);
static void __interrupt __far timer_handler(void) {
started = 1;
if (ticks) {
done = --ticks == 0;
} else {
done = 1;
}
_chain_intr(prev_handler);
}
static void bench_start(void) {
prev_handler = _dos_getvect(TIMER_INTR);
started = 0;
done = 0;
ticks = total_ticks + 1;
_dos_setvect(TIMER_INTR, timer_handler);
while (!started) {}
}
static int bench_check(void) {
if (done) {
_dos_setvect(TIMER_INTR, prev_handler);
return 1;
}
return 0;
}
static uint32_t run(const Benchmark *bench) {
uint32_t iterations;
if (bench->init) {
bench->init();
}
bench_start();
for (iterations = 0; !bench_check(); iterations++) {
bench->iter();
}
if (bench->fini) {
bench->fini();
}
return iterations;
}
static uint16_t parse_seconds(const char *arg) {
char *end;
long int parsed = strtol(arg, &end, 10);
/* if we got a non-numeric value, return 0 indicating that the argument
* was actually a benchmark name and not the number of seconds to test */
if (*end) {
return 0;
}
/* if we got a numeric value, but it's out of range, use the default time */
if (parsed < 1 || parsed > MAX_SECONDS) {
return DEFAULT_SECONDS;
}
return (uint16_t) parsed;
}
static const Benchmark separator = {
{ NULL, NULL }, NULL, NULL, NULL
};
static const Benchmark *benchmarks[] = {
&bench_nop,
&separator,
&bench_sqrt_libxt,
&bench_sqrt_turkowski,
&bench_sqrt_arm,
&bench_sqrt_lut,
&bench_sqrt_float,
&separator,
&bench_quat_conj,
&bench_quat_conj2,
&bench_quat_conj3,
&separator,
&bench_hercules_cube,
&separator,
&bench_hercules_clear,
&bench_hercules_clear2,
&bench_hercules_clear3,
&bench_hercules_clear4,
&separator,
&bench_hercules_alines,
&bench_hercules_hlines_short,
&bench_hercules_hlines_long,
&separator,
&bench_hercules_vsync,
NULL
};
typedef struct {
const Benchmark *benchmark;
uint32_t iterations;
} BenchmarkResult;
int main(int argc, char **argv) {
int firstarg = 1;
if (argc > 1) {
seconds = parse_seconds(argv[1]);
if (seconds) {
firstarg ++;
} else {
seconds = DEFAULT_SECONDS;
}
}
total_ticks = TICKS(seconds);
if (argc < firstarg + 1) {
printf("usage: %s [seconds] <benchmark> [...]\n\n", argv[0]);
printf("Available benchmarks:\n");
XT_CMDLINE_LIST(benchmarks);
return 0;
}
BenchmarkResult *results = malloc(sizeof(*results) * (argc - 1));
_Bool invalid = 0;
int count = 0;
for (int i = firstarg; i < argc; i++) {
const Benchmark *bench = XT_CMDLINE_PARSE(Benchmark, benchmarks, argv[i]);
if (bench) {
results[count++].benchmark = bench;
} else {
printf("Unknown benchmark `%s'\n", argv[i]);
invalid = 1;
}
}
if (invalid) {
puts("");
puts("Rerun with no arguments to see valid benchmark names.");
return 0;
}
for (int i = 0; i < count; i++) {
results[i].iterations = run(results[i].benchmark);
}
printf("%-24s \t%-10s \t%-12s\n", "Benchmark", "Iterations", "Iters/Second");
for (int i = 0; i < count; i++) {
printf("%-24s \t%10" PRIu32 " \t%12" PRIu32 "\n",
results[i].benchmark->command.name,
results[i].iterations, results[i].iterations / seconds);
}
free(results);
return 0;
}