-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsemifloat_test.c
319 lines (273 loc) · 10.8 KB
/
semifloat_test.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include "wasm_simd128_polyfill.h"
#include "rdtsc.h"
typedef struct running_avg_t running_avg_t;
typedef struct finst_t finst_t;
typedef enum fsrc_t fsrc_t;
typedef enum fdest_t fdest_t;
// flags
enum fsrc_t {
FSRC_A = 1, // read only A register
FSRC_R = 2, // read from memory from register R
};
// only one
enum fdest_t {
FDEST_F, // additive instruction F
FDEST_E, // multiplicative instruction E
};
struct finst_t {
const char *desc;
fdest_t dest;
fsrc_t src;
v128_t (*fprc[4])(v128_t, v128_t); // probably safe to call one arg function with two args
};
v128_t fadd_0(v128_t dest, v128_t src);
v128_t fadd_1(v128_t dest, v128_t src);
v128_t fadd_2(v128_t dest, v128_t src);
v128_t fadd_3(v128_t dest, v128_t src);
v128_t fsub_0(v128_t dest, v128_t src) ;
v128_t fsub_1(v128_t dest, v128_t src);
v128_t fsub_2(v128_t dest, v128_t src);
v128_t fsub_3(v128_t dest, v128_t src);
v128_t fmul_0(v128_t dest, v128_t src);
v128_t fmul_1(v128_t dest, v128_t src);
v128_t fmul_2(v128_t dest, v128_t src);
v128_t fmul_3(v128_t dest, v128_t src);
v128_t fmul_fma_1(v128_t dest, v128_t src);
v128_t fmul_fma_2(v128_t dest, v128_t src);
v128_t fmul_fma_3(v128_t dest, v128_t src);
v128_t fdiv_0(v128_t dest, v128_t src);
v128_t fdiv_1(v128_t dest, v128_t src);
v128_t fdiv_2(v128_t dest, v128_t src);
v128_t fdiv_3(v128_t dest, v128_t src);
v128_t fdiv_fma_1(v128_t dest, v128_t src);
v128_t fdiv_fma_2(v128_t dest, v128_t src);
v128_t fdiv_fma_3(v128_t dest, v128_t src);
v128_t fsqrt_0(v128_t dest);
v128_t fsqrt_1(v128_t dest);
v128_t fsqrt_2(v128_t dest);
v128_t fsqrt_3(v128_t dest);
v128_t fsqrt_fma_1(v128_t dest);
v128_t fsqrt_fma_2(v128_t dest);
v128_t fsqrt_fma_3(v128_t dest);
struct running_avg_t {
double avg;
unsigned count;
};
// TODO: i should probably be removing statistical outliers
static void running_avg_update(running_avg_t *avg, double value) {
if (value <= 0.0) {
return;
}
if (value > 75) {
// probably some context switch
return;
}
avg->count++;
double a = 1.0 / avg->count;
double b = 1.0 - a;
avg->avg = a * value + b * avg->avg;
}
#include "the_randomx_fp_heap.h"
static bool fp_heap_failed = false;
static uint64_t fp_heap_v = 0;
static uint64_t fp_heap_vu = 0;
static const char *fp_heap_opstr[FCOUNT] = {
[FADD_0] = "fadd_0",
[FADD_1] = "fadd_1",
[FADD_2] = "fadd_2",
[FADD_3] = "fadd_3",
[FSUB_0] = "fsub_0",
[FSUB_1] = "fsub_1",
[FSUB_2] = "fsub_2",
[FSUB_3] = "fsub_3",
[FMUL_0] = "fmul_0",
[FMUL_1] = "fmul_1",
[FMUL_2] = "fmul_2",
[FMUL_3] = "fmul_3",
[FMUL_FMA_1] = "fmul_fma_1",
[FMUL_FMA_2] = "fmul_fma_2",
[FMUL_FMA_3] = "fmul_fma_3",
[FDIV_0] = "fdiv_0",
[FDIV_1] = "fdiv_1",
[FDIV_2] = "fdiv_2",
[FDIV_3] = "fdiv_3",
[FDIV_FMA_1] = "fdiv_fma_1",
[FDIV_FMA_2] = "fdiv_fma_2",
[FDIV_FMA_3] = "fdiv_fma_3",
[FSQRT_0] = "fsqrt_0",
[FSQRT_1] = "fsqrt_1",
[FSQRT_2] = "fsqrt_2",
[FSQRT_3] = "fsqrt_3",
[FSQRT_FMA_1] = "fsqrt_fma_1",
[FSQRT_FMA_2] = "fsqrt_fma_2",
[FSQRT_FMA_3] = "fsqrt_fma_3",
};
static running_avg_t fp_heap_running_avg[FCOUNT] = {};
static unsigned fp_heap_passed[FCOUNT] = {};
static unsigned fp_heap_count[FCOUNT] = {};
#define TIMEIT(v, f) \
do { \
uint32_t rdtsc_base = rdtsc_overhead(); \
uint32_t start = rdtsc(); \
f; \
uint32_t end = rdtsc(); \
int32_t ncycles = (int32_t)(end - start) - rdtsc_base; \
running_avg_update(&fp_heap_running_avg[v], ncycles < 0 ? 0 : ncycles); \
} while (0)
#define LANE_V(n, fn, heap) \
do { \
if (result##n != p->z##n) { \
printf("FAIL: %s(%a, %a) == %a != %a\n", #fn, p->x##n, p->y##n, result##n, p->z##n); \
fp_heap_failed = true; \
} else { \
fp_heap_passed[heap]++; \
} \
fp_heap_count[heap]++; \
} while (0)
#define LANE_VU(n, fn, heap) \
do { \
if (result##n != p->z##n) { \
printf("FAIL: %s(%a) == %a != %a\n", #fn, p->x##n, result##n, p->z##n); \
fp_heap_failed = true; \
} else { \
fp_heap_passed[heap]++; \
} \
fp_heap_count[heap]++; \
} while (0)
#define ITERATION_COUNT 50000
volatile v128_t _out;
#define RUNFOR_V(fn, heap) \
p = HEAP_##heap, endp = p + sizeof(HEAP_##heap) / sizeof(HEAP_##heap[0]); \
while (p != endp) { \
v128_t v = wasm_f64x2_make(p->x0, p->x1); \
v128_t w = wasm_f64x2_make(p->y0, p->y1); \
fp_heap_v++; \
v128_t result = fn(v, w); \
double result0 = wasm_f64x2_extract_lane(result, 0); \
double result1 = wasm_f64x2_extract_lane(result, 1); \
LANE_V(0, fn, heap); \
LANE_V(1, fn, heap); \
p++; \
} \
p = HEAP_##heap, endp = p + sizeof(HEAP_##heap) / sizeof(HEAP_##heap[0]); \
for (unsigned c = 0; c < ITERATION_COUNT; c++) { \
v128_t v = wasm_f64x2_make(p->x0, p->x1); \
v128_t w = wasm_f64x2_make(p->y0, p->y1); \
TIMEIT(heap, { _out = fn(v, w); }); \
if (++p == endp) { \
p = HEAP_##heap; \
} \
}
#define RUNFOR_VU(fn, heap) \
p = HEAP_##heap, endp = p + sizeof(HEAP_##heap) / sizeof(HEAP_##heap[0]); \
while (p != endp) { \
v128_t v = wasm_f64x2_make(p->x0, p->x1); \
fp_heap_vu++; \
v128_t result = fn(v); \
double result0 = wasm_f64x2_extract_lane(result, 0); \
double result1 = wasm_f64x2_extract_lane(result, 1); \
LANE_VU(0, fn, heap); \
LANE_VU(1, fn, heap); \
p++; \
} \
p = HEAP_##heap, endp = p + sizeof(HEAP_##heap) / sizeof(HEAP_##heap[0]); \
for (unsigned c = 0; c < ITERATION_COUNT; c++) { \
v128_t v = wasm_f64x2_make(p->x0, p->x1); \
TIMEIT(heap, { _out = fn(v); }); \
if (++p == endp) { \
p = HEAP_##heap; \
} \
}
bool fp_heap() {
{
TESTV *p, *endp;
RUNFOR_V(fadd_0, FADD_0);
RUNFOR_V(fadd_1, FADD_1);
RUNFOR_V(fadd_2, FADD_2);
RUNFOR_V(fadd_3, FADD_3);
RUNFOR_V(fsub_0, FSUB_0);
RUNFOR_V(fsub_1, FSUB_1);
RUNFOR_V(fsub_2, FSUB_2);
RUNFOR_V(fsub_3, FSUB_3);
RUNFOR_V(fmul_0, FMUL_0);
RUNFOR_V(fmul_1, FMUL_1);
RUNFOR_V(fmul_2, FMUL_2);
RUNFOR_V(fmul_3, FMUL_3);
RUNFOR_V(fmul_fma_1, FMUL_FMA_1);
RUNFOR_V(fmul_fma_2, FMUL_FMA_2);
RUNFOR_V(fmul_fma_3, FMUL_FMA_3);
RUNFOR_V(fdiv_0, FDIV_0);
RUNFOR_V(fdiv_1, FDIV_1);
RUNFOR_V(fdiv_2, FDIV_2);
RUNFOR_V(fdiv_3, FDIV_3);
RUNFOR_V(fdiv_fma_1, FDIV_FMA_1);
RUNFOR_V(fdiv_fma_2, FDIV_FMA_2);
RUNFOR_V(fdiv_fma_3, FDIV_FMA_3);
}
{
TESTVU *p, *endp;
RUNFOR_VU(fsqrt_0, FSQRT_0);
RUNFOR_VU(fsqrt_1, FSQRT_1);
RUNFOR_VU(fsqrt_2, FSQRT_2);
RUNFOR_VU(fsqrt_3, FSQRT_3);
RUNFOR_VU(fsqrt_fma_1, FSQRT_FMA_1);
RUNFOR_VU(fsqrt_fma_2, FSQRT_FMA_2);
RUNFOR_VU(fsqrt_fma_3, FSQRT_FMA_3);
}
printf("FP heap: %lu infix, %lu unary, %lu total\n", fp_heap_v, fp_heap_vu, fp_heap_v + fp_heap_vu);
int lowbounds[] = {FADD_0, FSUB_0, FMUL_0, FDIV_0, FSQRT_0};
for (int v = 0; v < FCOUNT; v++) {
// iterate backwards
bool ref_exact = false;
int ref = 0;
for (int i = 5; i-- > 0;) {
if (v >= lowbounds[i]) {
if (v == lowbounds[i]) {
ref_exact = true;
}
ref = lowbounds[i];
break;
}
}
double reference_avg = fp_heap_running_avg[ref].avg;
printf(" %11s | %3uclks", fp_heap_opstr[v], (unsigned)round(fp_heap_running_avg[v].avg));
if (!ref_exact) {
printf(" x%0.1f overhead", fp_heap_running_avg[v].avg / reference_avg);
unsigned passes = fp_heap_passed[v];
unsigned out_of = fp_heap_count[v];
if (passes < out_of) {
printf(" [%u/%u]", passes, out_of);
}
}
printf("\n");
}
return fp_heap_failed;
}
#include <immintrin.h>
#ifndef __x86_64__
#error "x86_64 only FP tests, sorry"
#endif
bool __attribute__((optnone)) simd_fmatest(void) {
__m128d a = _mm_set_pd(0x1.0000000000001p+0, 0.0);
__m128d b = _mm_set_pd(0x1.ffffffffffffep-1, 0.0);
__m128d c = _mm_set_pd(-1.0, 0.0);
__m128d d = _mm_fmadd_pd(a, b, c);
return d[1] == -0x1.0p-104;
}
bool __attribute__((optnone)) scalar_fmatest(void) {
double a = 0x1.0000000000001p+0;
double b = 0x1.ffffffffffffep-1;
double c = -1.0;
double d = fma(a, b, c);
return d == -0x1.0p-104;
}
int main() {
assert(scalar_fmatest());
assert(simd_fmatest());
// used to have boundary tests and overhead tests, now the FP heap covers everything
return fp_heap();
}