34e6bf31fcf9aae3da0d387af75b7fbd11bb498d
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 "lp_bld_const.h"
45 fprintf(fp
, "%s%u%sx%u",
46 type
.floating
? "f" : (type
.fixed
? "h" : (type
.sign
? "s" : "u")),
54 read_elem(union lp_type type
, const void *src
, unsigned index
)
56 double scale
= lp_const_scale(type
);
58 assert(index
< type
.length
);
62 value
= *((const float *)src
+ index
);
65 value
= *((const double *)src
+ index
);
76 value
= *((const int8_t *)src
+ index
);
79 value
= *((const int16_t *)src
+ index
);
82 value
= *((const int32_t *)src
+ index
);
85 value
= *((const int64_t *)src
+ index
);
95 value
= *((const uint8_t *)src
+ index
);
98 value
= *((const uint16_t *)src
+ index
);
101 value
= *((const uint32_t *)src
+ index
);
104 value
= *((const uint64_t *)src
+ index
);
117 write_elem(union lp_type type
, void *dst
, unsigned index
, double src
)
119 double scale
= lp_const_scale(type
);
120 double value
= scale
*src
;
121 assert(index
< type
.length
);
125 *((float *)dst
+ index
) = (float)(value
);
128 *((double *)dst
+ index
) = value
;
138 *((int8_t *)dst
+ index
) = (int8_t)round(value
);
141 *((int16_t *)dst
+ index
) = (int16_t)round(value
);
144 *((int32_t *)dst
+ index
) = (int32_t)round(value
);
147 *((int64_t *)dst
+ index
) = (int32_t)round(value
);
156 *((uint8_t *)dst
+ index
) = (uint8_t)round(value
);
159 *((uint16_t *)dst
+ index
) = (uint16_t)round(value
);
162 *((uint32_t *)dst
+ index
) = (uint32_t)round(value
);
165 *((uint64_t *)dst
+ index
) = (uint64_t)round(value
);
176 random_elem(union lp_type type
, void *dst
, unsigned index
)
178 assert(index
< type
.length
);
180 double value
= (double)random()/(double)RAND_MAX
;
182 value
+= (double)random();
188 *((float *)dst
+ index
) = (float)value
;
191 *((double *)dst
+ index
) = value
;
200 *((uint8_t *)dst
+ index
) = (uint8_t)random();
203 *((uint16_t *)dst
+ index
) = (uint16_t)random();
206 *((uint32_t *)dst
+ index
) = (uint32_t)random();
209 *((uint64_t *)dst
+ index
) = (uint64_t)random();
219 read_vec(union lp_type type
, const void *src
, double *dst
)
222 for (i
= 0; i
< type
.length
; ++i
)
223 dst
[i
] = read_elem(type
, src
, i
);
228 write_vec(union lp_type type
, void *dst
, const double *src
)
231 for (i
= 0; i
< type
.length
; ++i
)
232 write_elem(type
, dst
, i
, src
[i
]);
239 return (float)((double)random()/(double)RAND_MAX
);
244 random_vec(union lp_type type
, void *dst
)
247 for (i
= 0; i
< type
.length
; ++i
)
248 random_elem(type
, dst
, i
);
253 compare_vec(union lp_type type
, const void *res
, const void *ref
)
273 double scale
= lp_const_scale(type
);
277 for (i
= 0; i
< type
.length
; ++i
) {
278 double res_elem
= read_elem(type
, res
, i
);
279 double ref_elem
= read_elem(type
, ref
, i
);
280 double delta
= fabs(res_elem
- ref_elem
);
290 dump_vec(FILE *fp
, union lp_type type
, const void *src
)
293 for (i
= 0; i
< type
.length
; ++i
) {
300 value
= *((const float *)src
+ i
);
303 value
= *((const double *)src
+ i
);
309 fprintf(fp
, "%f", value
);
317 value
= *((const int8_t *)src
+ i
);
321 value
= *((const int16_t *)src
+ i
);
325 value
= *((const int32_t *)src
+ i
);
329 value
= *((const int64_t *)src
+ i
);
337 fprintf(fp
, format
, value
);
340 unsigned long long value
;
344 value
= *((const uint8_t *)src
+ i
);
348 value
= *((const uint16_t *)src
+ i
);
352 value
= *((const uint32_t *)src
+ i
);
356 value
= *((const uint64_t *)src
+ i
);
364 fprintf(fp
, format
, value
);
371 int main(int argc
, char **argv
)
373 unsigned verbose
= 0;
375 unsigned long n
= 1000;
379 for(i
= 1; i
< argc
; ++i
) {
380 if(strcmp(argv
[i
], "-v") == 0)
382 else if(strcmp(argv
[i
], "-o") == 0)
383 fp
= fopen(argv
[++i
], "wt");
389 /* Warm up the caches */
390 test_some(0, NULL
, 100);
392 write_tsv_header(fp
);
396 success
= test_some(verbose
, fp
, n
);
398 success
= test_all(verbose
, fp
);
403 return success
? 0 : 1;