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 * Unit tests for type conversion.
33 * @author Jose Fonseca <jfonseca@vmware.com>
37 #include "util/u_pointer.h"
38 #include "gallivm/lp_bld_init.h"
39 #include "gallivm/lp_bld_type.h"
40 #include "gallivm/lp_bld_const.h"
41 #include "gallivm/lp_bld_conv.h"
42 #include "gallivm/lp_bld_debug.h"
46 typedef void (*conv_test_ptr_t
)(const void *src
, const void *dst
);
50 write_tsv_header(FILE *fp
)
54 "cycles_per_channel\t"
63 write_tsv_row(FILE *fp
,
64 struct lp_type src_type
,
65 struct lp_type dst_type
,
69 fprintf(fp
, "%s\t", success
? "pass" : "fail");
71 fprintf(fp
, "%.1f\t", cycles
/ MAX2(src_type
.length
, dst_type
.length
));
73 dump_type(fp
, src_type
);
76 dump_type(fp
, dst_type
);
84 dump_conv_types(FILE *fp
,
85 struct lp_type src_type
,
86 struct lp_type dst_type
)
88 fprintf(fp
, "src_type=");
89 dump_type(fp
, src_type
);
91 fprintf(fp
, " dst_type=");
92 dump_type(fp
, dst_type
);
94 fprintf(fp
, " ...\n");
100 add_conv_test(struct gallivm_state
*gallivm
,
101 struct lp_type src_type
, unsigned num_srcs
,
102 struct lp_type dst_type
, unsigned num_dsts
)
104 LLVMModuleRef module
= gallivm
->module
;
105 LLVMContextRef context
= gallivm
->context
;
106 LLVMBuilderRef builder
= gallivm
->builder
;
109 LLVMValueRef src_ptr
;
110 LLVMValueRef dst_ptr
;
111 LLVMBasicBlockRef block
;
112 LLVMValueRef src
[LP_MAX_VECTOR_LENGTH
];
113 LLVMValueRef dst
[LP_MAX_VECTOR_LENGTH
];
116 args
[0] = LLVMPointerType(lp_build_vec_type(gallivm
, src_type
), 0);
117 args
[1] = LLVMPointerType(lp_build_vec_type(gallivm
, dst_type
), 0);
119 func
= LLVMAddFunction(module
, "test",
120 LLVMFunctionType(LLVMVoidTypeInContext(context
),
122 LLVMSetFunctionCallConv(func
, LLVMCCallConv
);
123 src_ptr
= LLVMGetParam(func
, 0);
124 dst_ptr
= LLVMGetParam(func
, 1);
126 block
= LLVMAppendBasicBlockInContext(context
, func
, "entry");
127 LLVMPositionBuilderAtEnd(builder
, block
);
129 for(i
= 0; i
< num_srcs
; ++i
) {
130 LLVMValueRef index
= LLVMConstInt(LLVMInt32TypeInContext(context
), i
, 0);
131 LLVMValueRef ptr
= LLVMBuildGEP(builder
, src_ptr
, &index
, 1, "");
132 src
[i
] = LLVMBuildLoad(builder
, ptr
, "");
135 lp_build_conv(gallivm
, src_type
, dst_type
, src
, num_srcs
, dst
, num_dsts
);
137 for(i
= 0; i
< num_dsts
; ++i
) {
138 LLVMValueRef index
= LLVMConstInt(LLVMInt32TypeInContext(context
), i
, 0);
139 LLVMValueRef ptr
= LLVMBuildGEP(builder
, dst_ptr
, &index
, 1, "");
140 LLVMBuildStore(builder
, dst
[i
], ptr
);
143 LLVMBuildRetVoid(builder
);
145 gallivm_verify_function(gallivm
, func
);
153 test_one(unsigned verbose
,
155 struct lp_type src_type
,
156 struct lp_type dst_type
)
158 LLVMContextRef context
;
159 struct gallivm_state
*gallivm
;
160 LLVMValueRef func
= NULL
;
161 conv_test_ptr_t conv_test_ptr
;
163 const unsigned n
= LP_TEST_NUM_SAMPLES
;
164 int64_t cycles
[LP_TEST_NUM_SAMPLES
];
165 double cycles_avg
= 0.0;
171 if ((src_type
.width
>= dst_type
.width
&& src_type
.length
> dst_type
.length
) ||
172 (src_type
.width
<= dst_type
.width
&& src_type
.length
< dst_type
.length
)) {
177 * - fixed point 32 -> float 32
178 * - float 32 -> signed normalised integer 32
180 if ((src_type
.floating
&& !dst_type
.floating
&& dst_type
.sign
&& dst_type
.norm
&& src_type
.width
== dst_type
.width
) ||
181 (!src_type
.floating
&& dst_type
.floating
&& src_type
.fixed
&& src_type
.width
== dst_type
.width
)) {
186 * - fixed point 32 -> float 32
187 * - float 32 -> signed normalised integer 32
189 if ((src_type
.floating
&& !dst_type
.floating
&& dst_type
.sign
&& dst_type
.norm
&& src_type
.width
== dst_type
.width
) ||
190 (!src_type
.floating
&& dst_type
.floating
&& src_type
.fixed
&& src_type
.width
== dst_type
.width
)) {
195 dump_conv_types(stderr
, src_type
, dst_type
);
197 if (src_type
.length
> dst_type
.length
) {
199 num_dsts
= src_type
.length
/dst_type
.length
;
201 else if (src_type
.length
< dst_type
.length
) {
203 num_srcs
= dst_type
.length
/src_type
.length
;
210 /* We must not loose or gain channels. Only precision */
211 assert(src_type
.length
* num_srcs
== dst_type
.length
* num_dsts
);
213 eps
= MAX2(lp_const_eps(src_type
), lp_const_eps(dst_type
));
214 if (dst_type
.norm
&& dst_type
.sign
&& src_type
.sign
&& !src_type
.floating
) {
216 * This is quite inaccurate due to shift being used.
217 * I don't think it's possible to hit such conversions with
223 context
= LLVMContextCreate();
224 gallivm
= gallivm_create("test_module", context
);
226 func
= add_conv_test(gallivm
, src_type
, num_srcs
, dst_type
, num_dsts
);
228 gallivm_compile_module(gallivm
);
230 conv_test_ptr
= (conv_test_ptr_t
)gallivm_jit_function(gallivm
, func
);
232 gallivm_free_ir(gallivm
);
235 for(i
= 0; i
< n
&& success
; ++i
) {
236 unsigned src_stride
= src_type
.length
*src_type
.width
/8;
237 unsigned dst_stride
= dst_type
.length
*dst_type
.width
/8;
238 PIPE_ALIGN_VAR(LP_MIN_VECTOR_ALIGN
) uint8_t src
[LP_MAX_VECTOR_LENGTH
*LP_MAX_VECTOR_LENGTH
];
239 PIPE_ALIGN_VAR(LP_MIN_VECTOR_ALIGN
) uint8_t dst
[LP_MAX_VECTOR_LENGTH
*LP_MAX_VECTOR_LENGTH
];
240 double fref
[LP_MAX_VECTOR_LENGTH
*LP_MAX_VECTOR_LENGTH
];
241 uint8_t ref
[LP_MAX_VECTOR_LENGTH
*LP_MAX_VECTOR_LENGTH
];
242 int64_t start_counter
= 0;
243 int64_t end_counter
= 0;
245 for(j
= 0; j
< num_srcs
; ++j
) {
246 random_vec(src_type
, src
+ j
*src_stride
);
247 read_vec(src_type
, src
+ j
*src_stride
, fref
+ j
*src_type
.length
);
250 for(j
= 0; j
< num_dsts
; ++j
) {
251 write_vec(dst_type
, ref
+ j
*dst_stride
, fref
+ j
*dst_type
.length
);
254 start_counter
= rdtsc();
255 conv_test_ptr(src
, dst
);
256 end_counter
= rdtsc();
258 cycles
[i
] = end_counter
- start_counter
;
260 for(j
= 0; j
< num_dsts
; ++j
) {
261 if(!compare_vec_with_eps(dst_type
, dst
+ j
*dst_stride
, ref
+ j
*dst_stride
, eps
))
265 if (!success
|| verbose
>= 3) {
267 dump_conv_types(stderr
, src_type
, dst_type
);
269 fprintf(stderr
, "PASS\n");
272 fprintf(stderr
, "MISMATCH\n");
275 for(j
= 0; j
< num_srcs
; ++j
) {
276 fprintf(stderr
, " Src%u: ", j
);
277 dump_vec(stderr
, src_type
, src
+ j
*src_stride
);
278 fprintf(stderr
, "\n");
282 fprintf(stderr
, " Ref: ");
283 for(j
= 0; j
< src_type
.length
*num_srcs
; ++j
)
284 fprintf(stderr
, " %f", fref
[j
]);
285 fprintf(stderr
, "\n");
288 for(j
= 0; j
< num_dsts
; ++j
) {
289 fprintf(stderr
, " Dst%u: ", j
);
290 dump_vec(stderr
, dst_type
, dst
+ j
*dst_stride
);
291 fprintf(stderr
, "\n");
293 fprintf(stderr
, " Ref%u: ", j
);
294 dump_vec(stderr
, dst_type
, ref
+ j
*dst_stride
);
295 fprintf(stderr
, "\n");
301 * Unfortunately the output of cycle counter is not very reliable as it comes
302 * -- sometimes we get outliers (due IRQs perhaps?) which are
303 * better removed to avoid random or biased data.
306 double sum
= 0.0, sum2
= 0.0;
310 for(i
= 0; i
< n
; ++i
) {
312 sum2
+= cycles
[i
]*cycles
[i
];
316 std
= sqrtf((sum2
- n
*avg
*avg
)/n
);
320 for(i
= 0; i
< n
; ++i
) {
321 if(fabs(cycles
[i
] - avg
) <= 4.0*std
) {
332 write_tsv_row(fp
, src_type
, dst_type
, cycles_avg
, success
);
334 gallivm_destroy(gallivm
);
335 LLVMContextDispose(context
);
341 const struct lp_type conv_types
[] = {
342 /* float, fixed, sign, norm, width, len */
345 { TRUE
, FALSE
, TRUE
, TRUE
, 32, 4 },
346 { TRUE
, FALSE
, TRUE
, FALSE
, 32, 4 },
347 { TRUE
, FALSE
, FALSE
, TRUE
, 32, 4 },
348 { TRUE
, FALSE
, FALSE
, FALSE
, 32, 4 },
350 { TRUE
, FALSE
, TRUE
, TRUE
, 32, 8 },
351 { TRUE
, FALSE
, TRUE
, FALSE
, 32, 8 },
352 { TRUE
, FALSE
, FALSE
, TRUE
, 32, 8 },
353 { TRUE
, FALSE
, FALSE
, FALSE
, 32, 8 },
356 { FALSE
, TRUE
, TRUE
, TRUE
, 32, 4 },
357 { FALSE
, TRUE
, TRUE
, FALSE
, 32, 4 },
358 { FALSE
, TRUE
, FALSE
, TRUE
, 32, 4 },
359 { FALSE
, TRUE
, FALSE
, FALSE
, 32, 4 },
361 { FALSE
, TRUE
, TRUE
, TRUE
, 32, 8 },
362 { FALSE
, TRUE
, TRUE
, FALSE
, 32, 8 },
363 { FALSE
, TRUE
, FALSE
, TRUE
, 32, 8 },
364 { FALSE
, TRUE
, FALSE
, FALSE
, 32, 8 },
367 { FALSE
, FALSE
, TRUE
, TRUE
, 32, 4 },
368 { FALSE
, FALSE
, TRUE
, FALSE
, 32, 4 },
369 { FALSE
, FALSE
, FALSE
, TRUE
, 32, 4 },
370 { FALSE
, FALSE
, FALSE
, FALSE
, 32, 4 },
372 { FALSE
, FALSE
, TRUE
, TRUE
, 32, 8 },
373 { FALSE
, FALSE
, TRUE
, FALSE
, 32, 8 },
374 { FALSE
, FALSE
, FALSE
, TRUE
, 32, 8 },
375 { FALSE
, FALSE
, FALSE
, FALSE
, 32, 8 },
377 { FALSE
, FALSE
, TRUE
, TRUE
, 16, 8 },
378 { FALSE
, FALSE
, TRUE
, FALSE
, 16, 8 },
379 { FALSE
, FALSE
, FALSE
, TRUE
, 16, 8 },
380 { FALSE
, FALSE
, FALSE
, FALSE
, 16, 8 },
382 { FALSE
, FALSE
, TRUE
, TRUE
, 8, 16 },
383 { FALSE
, FALSE
, TRUE
, FALSE
, 8, 16 },
384 { FALSE
, FALSE
, FALSE
, TRUE
, 8, 16 },
385 { FALSE
, FALSE
, FALSE
, FALSE
, 8, 16 },
387 { FALSE
, FALSE
, TRUE
, TRUE
, 8, 4 },
388 { FALSE
, FALSE
, TRUE
, FALSE
, 8, 4 },
389 { FALSE
, FALSE
, FALSE
, TRUE
, 8, 4 },
390 { FALSE
, FALSE
, FALSE
, FALSE
, 8, 4 },
392 { FALSE
, FALSE
, FALSE
, TRUE
, 8, 8 },
396 const unsigned num_types
= ARRAY_SIZE(conv_types
);
400 test_all(unsigned verbose
, FILE *fp
)
402 const struct lp_type
*src_type
;
403 const struct lp_type
*dst_type
;
404 boolean success
= TRUE
;
407 for(src_type
= conv_types
; src_type
< &conv_types
[num_types
]; ++src_type
) {
408 for(dst_type
= conv_types
; dst_type
< &conv_types
[num_types
]; ++dst_type
) {
410 if(src_type
== dst_type
)
413 if(!test_one(verbose
, fp
, *src_type
, *dst_type
)){
420 fprintf(stderr
, "%d failures\n", error_count
);
427 test_some(unsigned verbose
, FILE *fp
,
430 const struct lp_type
*src_type
;
431 const struct lp_type
*dst_type
;
433 boolean success
= TRUE
;
435 for(i
= 0; i
< n
; ++i
) {
436 src_type
= &conv_types
[rand() % num_types
];
439 dst_type
= &conv_types
[rand() % num_types
];
440 } while (src_type
== dst_type
|| src_type
->norm
!= dst_type
->norm
);
442 if(!test_one(verbose
, fp
, *src_type
, *dst_type
))
451 test_single(unsigned verbose
, FILE *fp
)
453 /* float, fixed, sign, norm, width, len */
454 struct lp_type f32x4_type
=
455 { TRUE
, FALSE
, TRUE
, TRUE
, 32, 4 };
456 struct lp_type ub8x4_type
=
457 { FALSE
, FALSE
, FALSE
, TRUE
, 8, 16 };
461 success
= test_one(verbose
, fp
, f32x4_type
, ub8x4_type
);