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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
/**************************************************************************
*
* Copyright 2009 VMware, Inc.
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sub license, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
**************************************************************************/
/**
* @file
* Shared testing code.
*
* @author Jose Fonseca <jfonseca@vmware.com>
*/
#include "util/u_cpu_detect.h"
#include "util/u_math.h"
#include "gallivm/lp_bld_const.h"
#include "gallivm/lp_bld_init.h"
#include "gallivm/lp_bld_debug.h"
#include "lp_test.h"
void
dump_type(FILE *fp,
struct lp_type type)
{
fprintf(fp, "%s%s%u%sx%u",
type.sign ? (type.floating || type.fixed ? "" : "s") : "u",
type.floating ? "f" : (type.fixed ? "h" : "i"),
type.width,
type.norm ? "n" : "",
type.length);
}
double
read_elem(struct lp_type type, const void *src, unsigned index)
{
double scale = lp_const_scale(type);
double value;
assert(index < type.length);
if (type.floating) {
switch(type.width) {
case 32:
value = *((const float *)src + index);
break;
case 64:
value = *((const double *)src + index);
break;
default:
assert(0);
return 0.0;
}
}
else {
if(type.sign) {
switch(type.width) {
case 8:
value = *((const int8_t *)src + index);
break;
case 16:
value = *((const int16_t *)src + index);
break;
case 32:
value = *((const int32_t *)src + index);
break;
case 64:
value = *((const int64_t *)src + index);
break;
default:
assert(0);
return 0.0;
}
}
else {
switch(type.width) {
case 8:
value = *((const uint8_t *)src + index);
break;
case 16:
value = *((const uint16_t *)src + index);
break;
case 32:
value = *((const uint32_t *)src + index);
break;
case 64:
value = *((const uint64_t *)src + index);
break;
default:
assert(0);
return 0.0;
}
}
}
return value/scale;
}
void
write_elem(struct lp_type type, void *dst, unsigned index, double value)
{
assert(index < type.length);
if(!type.sign && value < 0.0)
value = 0.0;
if(type.norm && value < -1.0)
value = -1.0;
if(type.norm && value > 1.0)
value = 1.0;
if (type.floating) {
switch(type.width) {
case 32:
*((float *)dst + index) = (float)(value);
break;
case 64:
*((double *)dst + index) = value;
break;
default:
assert(0);
}
}
else {
double scale = lp_const_scale(type);
value = round(value*scale);
if(type.sign) {
long long lvalue = (long long)value;
lvalue = MIN2(lvalue, ((long long)1 << (type.width - 1)) - 1);
switch(type.width) {
case 8:
*((int8_t *)dst + index) = (int8_t)lvalue;
break;
case 16:
*((int16_t *)dst + index) = (int16_t)lvalue;
break;
case 32:
*((int32_t *)dst + index) = (int32_t)lvalue;
break;
case 64:
*((int64_t *)dst + index) = (int64_t)lvalue;
break;
default:
assert(0);
}
}
else {
unsigned long long lvalue = (long long)value;
lvalue = MIN2(lvalue, ((unsigned long long)1 << type.width) - 1);
switch(type.width) {
case 8:
*((uint8_t *)dst + index) = (uint8_t)lvalue;
break;
case 16:
*((uint16_t *)dst + index) = (uint16_t)lvalue;
break;
case 32:
*((uint32_t *)dst + index) = (uint32_t)lvalue;
break;
case 64:
*((uint64_t *)dst + index) = (uint64_t)lvalue;
break;
default:
assert(0);
}
}
}
}
void
random_elem(struct lp_type type, void *dst, unsigned index)
{
double value;
assert(index < type.length);
value = (double)rand()/(double)RAND_MAX;
if(!type.norm) {
if (type.floating) {
value *= 2.0;
}
else {
unsigned long long mask;
if (type.fixed)
mask = ((unsigned long long)1 << (type.width / 2)) - 1;
else if (type.sign)
mask = ((unsigned long long)1 << (type.width - 1)) - 1;
else
mask = ((unsigned long long)1 << type.width) - 1;
value += (double)(mask & rand());
}
}
if(!type.sign)
if(rand() & 1)
value = -value;
write_elem(type, dst, index, value);
}
void
read_vec(struct lp_type type, const void *src, double *dst)
{
unsigned i;
for (i = 0; i < type.length; ++i)
dst[i] = read_elem(type, src, i);
}
void
write_vec(struct lp_type type, void *dst, const double *src)
{
unsigned i;
for (i = 0; i < type.length; ++i)
write_elem(type, dst, i, src[i]);
}
float
random_float(void)
{
return (float)((double)rand()/(double)RAND_MAX);
}
void
random_vec(struct lp_type type, void *dst)
{
unsigned i;
for (i = 0; i < type.length; ++i)
random_elem(type, dst, i);
}
boolean
compare_vec_with_eps(struct lp_type type, const void *res, const void *ref, double eps)
{
unsigned i;
eps *= type.floating ? 8.0 : 2.0;
for (i = 0; i < type.length; ++i) {
double res_elem = read_elem(type, res, i);
double ref_elem = read_elem(type, ref, i);
double delta = res_elem - ref_elem;
if (ref_elem < -1.0 || ref_elem > 1.0) {
delta /= ref_elem;
}
delta = fabs(delta);
if (delta >= eps) {
return FALSE;
}
}
return TRUE;
}
boolean
compare_vec(struct lp_type type, const void *res, const void *ref)
{
double eps = lp_const_eps(type);
return compare_vec_with_eps(type, res, ref, eps);
}
void
dump_vec(FILE *fp, struct lp_type type, const void *src)
{
unsigned i;
for (i = 0; i < type.length; ++i) {
if(i)
fprintf(fp, " ");
if (type.floating) {
double value;
switch(type.width) {
case 32:
value = *((const float *)src + i);
break;
case 64:
value = *((const double *)src + i);
break;
default:
assert(0);
value = 0.0;
}
fprintf(fp, "%f", value);
}
else {
if(type.sign && !type.norm) {
long long value;
const char *format;
switch(type.width) {
case 8:
value = *((const int8_t *)src + i);
format = "%3lli";
break;
case 16:
value = *((const int16_t *)src + i);
format = "%5lli";
break;
case 32:
value = *((const int32_t *)src + i);
format = "%10lli";
break;
case 64:
value = *((const int64_t *)src + i);
format = "%20lli";
break;
default:
assert(0);
value = 0.0;
format = "?";
}
fprintf(fp, format, value);
}
else {
unsigned long long value;
const char *format;
switch(type.width) {
case 8:
value = *((const uint8_t *)src + i);
format = type.norm ? "%2x" : "%4llu";
break;
case 16:
value = *((const uint16_t *)src + i);
format = type.norm ? "%4x" : "%6llx";
break;
case 32:
value = *((const uint32_t *)src + i);
format = type.norm ? "%8x" : "%11llx";
break;
case 64:
value = *((const uint64_t *)src + i);
format = type.norm ? "%16x" : "%21llx";
break;
default:
assert(0);
value = 0.0;
format = "?";
}
fprintf(fp, format, value);
}
}
}
}
int main(int argc, char **argv)
{
unsigned verbose = 0;
FILE *fp = NULL;
unsigned long n = 1000;
unsigned i;
boolean success;
boolean single = FALSE;
unsigned fpstate;
util_cpu_detect();
fpstate = util_fpstate_get();
util_fpstate_set_denorms_to_zero(fpstate);
if (!lp_build_init())
return 1;
for(i = 1; i < argc; ++i) {
if(strcmp(argv[i], "-v") == 0)
++verbose;
else if(strcmp(argv[i], "-s") == 0)
single = TRUE;
else if(strcmp(argv[i], "-o") == 0)
fp = fopen(argv[++i], "wt");
else
n = atoi(argv[i]);
}
#ifdef DEBUG
if (verbose >= 2) {
gallivm_debug |= GALLIVM_DEBUG_IR;
gallivm_debug |= GALLIVM_DEBUG_ASM;
}
#endif
util_cpu_detect();
if(fp) {
/* Warm up the caches */
test_some(0, NULL, 100);
write_tsv_header(fp);
}
if (single)
success = test_single(verbose, fp);
else if (n)
success = test_some(verbose, fp, n);
else
success = test_all(verbose, fp);
if(fp)
fclose(fp);
return success ? 0 : 1;
}
|