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"
39 #include "lp_bld_const.h"
47 fprintf(fp
, "%s%s%u%sx%u",
48 type
.sign
? (type
.floating
|| type
.fixed
? "" : "s") : "u",
49 type
.floating
? "f" : (type
.fixed
? "h" : "i"),
57 read_elem(struct lp_type type
, const void *src
, unsigned index
)
59 double scale
= lp_const_scale(type
);
61 assert(index
< type
.length
);
65 value
= *((const float *)src
+ index
);
68 value
= *((const double *)src
+ index
);
79 value
= *((const int8_t *)src
+ index
);
82 value
= *((const int16_t *)src
+ index
);
85 value
= *((const int32_t *)src
+ index
);
88 value
= *((const int64_t *)src
+ index
);
98 value
= *((const uint8_t *)src
+ index
);
101 value
= *((const uint16_t *)src
+ index
);
104 value
= *((const uint32_t *)src
+ index
);
107 value
= *((const uint64_t *)src
+ index
);
120 write_elem(struct lp_type type
, void *dst
, unsigned index
, double value
)
122 assert(index
< type
.length
);
123 if(!type
.sign
&& value
< 0.0)
125 if(type
.norm
&& value
< -1.0)
127 if(type
.norm
&& value
> 1.0)
132 *((float *)dst
+ index
) = (float)(value
);
135 *((double *)dst
+ index
) = value
;
142 double scale
= lp_const_scale(type
);
143 value
= round(value
*scale
);
145 long long lvalue
= (long long)value
;
146 lvalue
= MIN2(lvalue
, ((long long)1 << (type
.width
- 1)) - 1);
149 *((int8_t *)dst
+ index
) = (int8_t)lvalue
;
152 *((int16_t *)dst
+ index
) = (int16_t)lvalue
;
155 *((int32_t *)dst
+ index
) = (int32_t)lvalue
;
158 *((int64_t *)dst
+ index
) = (int64_t)lvalue
;
165 unsigned long long lvalue
= (long long)value
;
166 lvalue
= MIN2(lvalue
, ((unsigned long long)1 << type
.width
) - 1);
169 *((uint8_t *)dst
+ index
) = (uint8_t)lvalue
;
172 *((uint16_t *)dst
+ index
) = (uint16_t)lvalue
;
175 *((uint32_t *)dst
+ index
) = (uint32_t)lvalue
;
178 *((uint64_t *)dst
+ index
) = (uint64_t)lvalue
;
189 random_elem(struct lp_type type
, void *dst
, unsigned index
)
192 assert(index
< type
.length
);
193 value
= (double)rand()/(double)RAND_MAX
;
195 unsigned long long mask
;
197 mask
= ~(unsigned long long)0;
199 mask
= ((unsigned long long)1 << (type
.width
/ 2)) - 1;
201 mask
= ((unsigned long long)1 << (type
.width
- 1)) - 1;
203 mask
= ((unsigned long long)1 << type
.width
) - 1;
204 value
+= (double)(mask
& rand());
209 write_elem(type
, dst
, index
, value
);
214 read_vec(struct lp_type type
, const void *src
, double *dst
)
217 for (i
= 0; i
< type
.length
; ++i
)
218 dst
[i
] = read_elem(type
, src
, i
);
223 write_vec(struct lp_type type
, void *dst
, const double *src
)
226 for (i
= 0; i
< type
.length
; ++i
)
227 write_elem(type
, dst
, i
, src
[i
]);
234 return (float)((double)rand()/(double)RAND_MAX
);
239 random_vec(struct lp_type type
, void *dst
)
242 for (i
= 0; i
< type
.length
; ++i
)
243 random_elem(type
, dst
, i
);
248 compare_vec_with_eps(struct lp_type type
, const void *res
, const void *ref
, double eps
)
251 for (i
= 0; i
< type
.length
; ++i
) {
252 double res_elem
= read_elem(type
, res
, i
);
253 double ref_elem
= read_elem(type
, ref
, i
);
254 double delta
= fabs(res_elem
- ref_elem
);
264 compare_vec(struct lp_type type
, const void *res
, const void *ref
)
266 double eps
= lp_const_eps(type
);
267 return compare_vec_with_eps(type
, res
, ref
, eps
);
272 dump_vec(FILE *fp
, struct lp_type type
, const void *src
)
275 for (i
= 0; i
< type
.length
; ++i
) {
282 value
= *((const float *)src
+ i
);
285 value
= *((const double *)src
+ i
);
291 fprintf(fp
, "%f", value
);
294 if(type
.sign
&& !type
.norm
) {
299 value
= *((const int8_t *)src
+ i
);
303 value
= *((const int16_t *)src
+ i
);
307 value
= *((const int32_t *)src
+ i
);
311 value
= *((const int64_t *)src
+ i
);
319 fprintf(fp
, format
, value
);
322 unsigned long long value
;
326 value
= *((const uint8_t *)src
+ i
);
327 format
= type
.norm
? "%2x" : "%4llu";
330 value
= *((const uint16_t *)src
+ i
);
331 format
= type
.norm
? "%4x" : "%6llx";
334 value
= *((const uint32_t *)src
+ i
);
335 format
= type
.norm
? "%8x" : "%11llx";
338 value
= *((const uint64_t *)src
+ i
);
339 format
= type
.norm
? "%16x" : "%21llx";
346 fprintf(fp
, format
, value
);
353 int main(int argc
, char **argv
)
355 unsigned verbose
= 0;
357 unsigned long n
= 1000;
361 for(i
= 1; i
< argc
; ++i
) {
362 if(strcmp(argv
[i
], "-v") == 0)
364 else if(strcmp(argv
[i
], "-o") == 0)
365 fp
= fopen(argv
[++i
], "wt");
370 #ifdef LLVM_NATIVE_ARCH
372 LLVMInitializeNativeTarget();
378 /* Warm up the caches */
379 test_some(0, NULL
, 100);
381 write_tsv_header(fp
);
385 success
= test_some(verbose
, fp
, n
);
387 success
= test_all(verbose
, fp
);
392 return success
? 0 : 1;