1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
31 * Shared testing code.
33 * @author Jose Fonseca <jfonseca@vmware.com>
37 #include "util/u_cpu_detect.h"
38 #include "util/u_math.h"
40 #include "gallivm/lp_bld_const.h"
41 #include "gallivm/lp_bld_init.h"
42 #include "gallivm/lp_bld_debug.h"
50 fprintf(fp
, "%s%s%u%sx%u",
51 type
.sign
? (type
.floating
|| type
.fixed
? "" : "s") : "u",
52 type
.floating
? "f" : (type
.fixed
? "h" : "i"),
60 read_elem(struct lp_type type
, const void *src
, unsigned index
)
62 double scale
= lp_const_scale(type
);
64 assert(index
< type
.length
);
68 value
= *((const float *)src
+ index
);
71 value
= *((const double *)src
+ index
);
82 value
= *((const int8_t *)src
+ index
);
85 value
= *((const int16_t *)src
+ index
);
88 value
= *((const int32_t *)src
+ index
);
91 value
= *((const int64_t *)src
+ index
);
101 value
= *((const uint8_t *)src
+ index
);
104 value
= *((const uint16_t *)src
+ index
);
107 value
= *((const uint32_t *)src
+ index
);
110 value
= *((const uint64_t *)src
+ index
);
123 write_elem(struct lp_type type
, void *dst
, unsigned index
, double value
)
125 assert(index
< type
.length
);
126 if(!type
.sign
&& value
< 0.0)
128 if(type
.norm
&& value
< -1.0)
130 if(type
.norm
&& value
> 1.0)
135 *((float *)dst
+ index
) = (float)(value
);
138 *((double *)dst
+ index
) = value
;
145 double scale
= lp_const_scale(type
);
146 value
= round(value
*scale
);
148 long long lvalue
= (long long)value
;
149 lvalue
= MIN2(lvalue
, ((long long)1 << (type
.width
- 1)) - 1);
152 *((int8_t *)dst
+ index
) = (int8_t)lvalue
;
155 *((int16_t *)dst
+ index
) = (int16_t)lvalue
;
158 *((int32_t *)dst
+ index
) = (int32_t)lvalue
;
161 *((int64_t *)dst
+ index
) = (int64_t)lvalue
;
168 unsigned long long lvalue
= (long long)value
;
169 lvalue
= MIN2(lvalue
, ((unsigned long long)1 << type
.width
) - 1);
172 *((uint8_t *)dst
+ index
) = (uint8_t)lvalue
;
175 *((uint16_t *)dst
+ index
) = (uint16_t)lvalue
;
178 *((uint32_t *)dst
+ index
) = (uint32_t)lvalue
;
181 *((uint64_t *)dst
+ index
) = (uint64_t)lvalue
;
192 random_elem(struct lp_type type
, void *dst
, unsigned index
)
195 assert(index
< type
.length
);
196 value
= (double)rand()/(double)RAND_MAX
;
202 unsigned long long mask
;
204 mask
= ((unsigned long long)1 << (type
.width
/ 2)) - 1;
206 mask
= ((unsigned long long)1 << (type
.width
- 1)) - 1;
208 mask
= ((unsigned long long)1 << type
.width
) - 1;
209 value
+= (double)(mask
& rand());
215 write_elem(type
, dst
, index
, value
);
220 read_vec(struct lp_type type
, const void *src
, double *dst
)
223 for (i
= 0; i
< type
.length
; ++i
)
224 dst
[i
] = read_elem(type
, src
, i
);
229 write_vec(struct lp_type type
, void *dst
, const double *src
)
232 for (i
= 0; i
< type
.length
; ++i
)
233 write_elem(type
, dst
, i
, src
[i
]);
240 return (float)((double)rand()/(double)RAND_MAX
);
245 random_vec(struct lp_type type
, void *dst
)
248 for (i
= 0; i
< type
.length
; ++i
)
249 random_elem(type
, dst
, i
);
254 compare_vec_with_eps(struct lp_type type
, const void *res
, const void *ref
, double eps
)
257 eps
*= type
.floating
? 8.0 : 2.0;
258 for (i
= 0; i
< type
.length
; ++i
) {
259 double res_elem
= read_elem(type
, res
, i
);
260 double ref_elem
= read_elem(type
, ref
, i
);
261 double delta
= res_elem
- ref_elem
;
262 if (ref_elem
< -1.0 || ref_elem
> 1.0) {
276 compare_vec(struct lp_type type
, const void *res
, const void *ref
)
278 double eps
= lp_const_eps(type
);
279 return compare_vec_with_eps(type
, res
, ref
, eps
);
284 dump_vec(FILE *fp
, struct lp_type type
, const void *src
)
287 for (i
= 0; i
< type
.length
; ++i
) {
294 value
= *((const float *)src
+ i
);
297 value
= *((const double *)src
+ i
);
303 fprintf(fp
, "%f", value
);
306 if(type
.sign
&& !type
.norm
) {
311 value
= *((const int8_t *)src
+ i
);
315 value
= *((const int16_t *)src
+ i
);
319 value
= *((const int32_t *)src
+ i
);
323 value
= *((const int64_t *)src
+ i
);
331 fprintf(fp
, format
, value
);
334 unsigned long long value
;
338 value
= *((const uint8_t *)src
+ i
);
339 format
= type
.norm
? "%2x" : "%4llu";
342 value
= *((const uint16_t *)src
+ i
);
343 format
= type
.norm
? "%4x" : "%6llx";
346 value
= *((const uint32_t *)src
+ i
);
347 format
= type
.norm
? "%8x" : "%11llx";
350 value
= *((const uint64_t *)src
+ i
);
351 format
= type
.norm
? "%16x" : "%21llx";
358 fprintf(fp
, format
, value
);
365 int main(int argc
, char **argv
)
367 unsigned verbose
= 0;
369 unsigned long n
= 1000;
372 boolean single
= FALSE
;
376 fpstate
= util_fpstate_get();
377 util_fpstate_set_denorms_to_zero(fpstate
);
379 if (!lp_build_init())
382 for(i
= 1; i
< argc
; ++i
) {
383 if(strcmp(argv
[i
], "-v") == 0)
385 else if(strcmp(argv
[i
], "-s") == 0)
387 else if(strcmp(argv
[i
], "-o") == 0)
388 fp
= fopen(argv
[++i
], "wt");
395 gallivm_debug
|= GALLIVM_DEBUG_IR
;
396 gallivm_debug
|= GALLIVM_DEBUG_ASM
;
401 /* Warm up the caches */
402 test_some(0, NULL
, 100);
404 write_tsv_header(fp
);
408 success
= test_single(verbose
, fp
);
410 success
= test_some(verbose
, fp
, n
);
412 success
= test_all(verbose
, fp
);
417 return success
? 0 : 1;