0dcb5422887d36df0848f229267763009c3e5780
[mesa.git] / src / gallium / drivers / llvmpipe / lp_test_conv.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28
29 /**
30 * @file
31 * Unit tests for type conversion.
32 *
33 * @author Jose Fonseca <jfonseca@vmware.com>
34 */
35
36
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"
43 #include "lp_test.h"
44
45
46 typedef void (*conv_test_ptr_t)(const void *src, const void *dst);
47
48
49 void
50 write_tsv_header(FILE *fp)
51 {
52 fprintf(fp,
53 "result\t"
54 "cycles_per_channel\t"
55 "src_type\t"
56 "dst_type\n");
57
58 fflush(fp);
59 }
60
61
62 static void
63 write_tsv_row(FILE *fp,
64 struct lp_type src_type,
65 struct lp_type dst_type,
66 double cycles,
67 boolean success)
68 {
69 fprintf(fp, "%s\t", success ? "pass" : "fail");
70
71 fprintf(fp, "%.1f\t", cycles / MAX2(src_type.length, dst_type.length));
72
73 dump_type(fp, src_type);
74 fprintf(fp, "\t");
75
76 dump_type(fp, dst_type);
77 fprintf(fp, "\n");
78
79 fflush(fp);
80 }
81
82
83 static void
84 dump_conv_types(FILE *fp,
85 struct lp_type src_type,
86 struct lp_type dst_type)
87 {
88 fprintf(fp, "src_type=");
89 dump_type(fp, src_type);
90
91 fprintf(fp, " dst_type=");
92 dump_type(fp, dst_type);
93
94 fprintf(fp, " ...\n");
95 fflush(fp);
96 }
97
98
99 static LLVMValueRef
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)
103 {
104 LLVMModuleRef module = gallivm->module;
105 LLVMContextRef context = gallivm->context;
106 LLVMBuilderRef builder = gallivm->builder;
107 LLVMTypeRef args[2];
108 LLVMValueRef func;
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];
114 unsigned i;
115
116 args[0] = LLVMPointerType(lp_build_vec_type(gallivm, src_type), 0);
117 args[1] = LLVMPointerType(lp_build_vec_type(gallivm, dst_type), 0);
118
119 func = LLVMAddFunction(module, "test",
120 LLVMFunctionType(LLVMVoidTypeInContext(context),
121 args, 2, 0));
122 LLVMSetFunctionCallConv(func, LLVMCCallConv);
123 src_ptr = LLVMGetParam(func, 0);
124 dst_ptr = LLVMGetParam(func, 1);
125
126 block = LLVMAppendBasicBlockInContext(context, func, "entry");
127 LLVMPositionBuilderAtEnd(builder, block);
128
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, "");
133 }
134
135 lp_build_conv(gallivm, src_type, dst_type, src, num_srcs, dst, num_dsts);
136
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);
141 }
142
143 LLVMBuildRetVoid(builder);;
144
145 return func;
146 }
147
148
149 PIPE_ALIGN_STACK
150 static boolean
151 test_one(struct gallivm_state *gallivm, unsigned verbose,
152 FILE *fp,
153 struct lp_type src_type,
154 struct lp_type dst_type)
155 {
156 LLVMModuleRef module = gallivm->module;
157 LLVMExecutionEngineRef engine = gallivm->engine;
158 LLVMValueRef func = NULL;
159 char *error = NULL;
160 conv_test_ptr_t conv_test_ptr;
161 boolean success;
162 const unsigned n = LP_TEST_NUM_SAMPLES;
163 int64_t cycles[LP_TEST_NUM_SAMPLES];
164 double cycles_avg = 0.0;
165 unsigned num_srcs;
166 unsigned num_dsts;
167 double eps;
168 unsigned i, j;
169 void *code;
170
171 if (src_type.width * src_type.length != dst_type.width * dst_type.length &&
172 src_type.length != dst_type.length) {
173 return TRUE;
174 }
175
176 /* Known failures
177 * - fixed point 32 -> float 32
178 * - float 32 -> signed normalised integer 32
179 */
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)) {
182 return TRUE;
183 }
184
185 if(verbose >= 1)
186 dump_conv_types(stdout, src_type, dst_type);
187
188 if (src_type.length > dst_type.length) {
189 num_srcs = 1;
190 num_dsts = src_type.length/dst_type.length;
191 }
192 else if (src_type.length < dst_type.length) {
193 num_dsts = 1;
194 num_srcs = dst_type.length/src_type.length;
195 }
196 else {
197 num_dsts = 1;
198 num_srcs = 1;
199 }
200
201 /* We must not loose or gain channels. Only precision */
202 assert(src_type.length * num_srcs == dst_type.length * num_dsts);
203
204 eps = MAX2(lp_const_eps(src_type), lp_const_eps(dst_type));
205
206 func = add_conv_test(gallivm, src_type, num_srcs, dst_type, num_dsts);
207
208 if(LLVMVerifyModule(module, LLVMPrintMessageAction, &error)) {
209 LLVMDumpModule(module);
210 abort();
211 }
212 LLVMDisposeMessage(error);
213
214 if(verbose >= 2)
215 LLVMDumpModule(module);
216
217 code = LLVMGetPointerToGlobal(engine, func);
218 conv_test_ptr = (conv_test_ptr_t)pointer_to_func(code);
219
220 if(verbose >= 2)
221 lp_disassemble(code);
222
223 success = TRUE;
224 for(i = 0; i < n && success; ++i) {
225 unsigned src_stride = src_type.length*src_type.width/8;
226 unsigned dst_stride = dst_type.length*dst_type.width/8;
227 PIPE_ALIGN_VAR(16) uint8_t src[LP_MAX_VECTOR_LENGTH*LP_MAX_VECTOR_LENGTH];
228 PIPE_ALIGN_VAR(16) uint8_t dst[LP_MAX_VECTOR_LENGTH*LP_MAX_VECTOR_LENGTH];
229 double fref[LP_MAX_VECTOR_LENGTH*LP_MAX_VECTOR_LENGTH];
230 uint8_t ref[LP_MAX_VECTOR_LENGTH*LP_MAX_VECTOR_LENGTH];
231 int64_t start_counter = 0;
232 int64_t end_counter = 0;
233
234 for(j = 0; j < num_srcs; ++j) {
235 random_vec(src_type, src + j*src_stride);
236 read_vec(src_type, src + j*src_stride, fref + j*src_type.length);
237 }
238
239 for(j = 0; j < num_dsts; ++j) {
240 write_vec(dst_type, ref + j*dst_stride, fref + j*dst_type.length);
241 }
242
243 start_counter = rdtsc();
244 conv_test_ptr(src, dst);
245 end_counter = rdtsc();
246
247 cycles[i] = end_counter - start_counter;
248
249 for(j = 0; j < num_dsts; ++j) {
250 if(!compare_vec_with_eps(dst_type, dst + j*dst_stride, ref + j*dst_stride, eps))
251 success = FALSE;
252 }
253
254 if (!success || verbose >= 3) {
255 if(verbose < 1)
256 dump_conv_types(stderr, src_type, dst_type);
257 if (success) {
258 fprintf(stderr, "PASS\n");
259 }
260 else {
261 fprintf(stderr, "MISMATCH\n");
262 }
263
264 for(j = 0; j < num_srcs; ++j) {
265 fprintf(stderr, " Src%u: ", j);
266 dump_vec(stderr, src_type, src + j*src_stride);
267 fprintf(stderr, "\n");
268 }
269
270 #if 1
271 fprintf(stderr, " Ref: ");
272 for(j = 0; j < src_type.length*num_srcs; ++j)
273 fprintf(stderr, " %f", fref[j]);
274 fprintf(stderr, "\n");
275 #endif
276
277 for(j = 0; j < num_dsts; ++j) {
278 fprintf(stderr, " Dst%u: ", j);
279 dump_vec(stderr, dst_type, dst + j*dst_stride);
280 fprintf(stderr, "\n");
281
282 fprintf(stderr, " Ref%u: ", j);
283 dump_vec(stderr, dst_type, ref + j*dst_stride);
284 fprintf(stderr, "\n");
285 }
286 }
287 }
288
289 /*
290 * Unfortunately the output of cycle counter is not very reliable as it comes
291 * -- sometimes we get outliers (due IRQs perhaps?) which are
292 * better removed to avoid random or biased data.
293 */
294 {
295 double sum = 0.0, sum2 = 0.0;
296 double avg, std;
297 unsigned m;
298
299 for(i = 0; i < n; ++i) {
300 sum += cycles[i];
301 sum2 += cycles[i]*cycles[i];
302 }
303
304 avg = sum/n;
305 std = sqrtf((sum2 - n*avg*avg)/n);
306
307 m = 0;
308 sum = 0.0;
309 for(i = 0; i < n; ++i) {
310 if(fabs(cycles[i] - avg) <= 4.0*std) {
311 sum += cycles[i];
312 ++m;
313 }
314 }
315
316 cycles_avg = sum/m;
317
318 }
319
320 if(fp)
321 write_tsv_row(fp, src_type, dst_type, cycles_avg, success);
322
323 if (!success) {
324 static boolean firsttime = TRUE;
325 if(firsttime) {
326 if(verbose < 2)
327 LLVMDumpModule(module);
328 LLVMWriteBitcodeToFile(module, "conv.bc");
329 fprintf(stderr, "conv.bc written\n");
330 fprintf(stderr, "Invoke as \"llc -o - conv.bc\"\n");
331 firsttime = FALSE;
332 /* abort(); */
333 }
334 }
335
336 LLVMFreeMachineCodeForFunction(engine, func);
337
338 return success;
339 }
340
341
342 const struct lp_type conv_types[] = {
343 /* float, fixed, sign, norm, width, len */
344
345 /* Float */
346 { TRUE, FALSE, TRUE, TRUE, 32, 4 },
347 { TRUE, FALSE, TRUE, FALSE, 32, 4 },
348 { TRUE, FALSE, FALSE, TRUE, 32, 4 },
349 { TRUE, FALSE, FALSE, FALSE, 32, 4 },
350
351 /* Fixed */
352 { FALSE, TRUE, TRUE, TRUE, 32, 4 },
353 { FALSE, TRUE, TRUE, FALSE, 32, 4 },
354 { FALSE, TRUE, FALSE, TRUE, 32, 4 },
355 { FALSE, TRUE, FALSE, FALSE, 32, 4 },
356
357 /* Integer */
358 { FALSE, FALSE, TRUE, TRUE, 32, 4 },
359 { FALSE, FALSE, TRUE, FALSE, 32, 4 },
360 { FALSE, FALSE, FALSE, TRUE, 32, 4 },
361 { FALSE, FALSE, FALSE, FALSE, 32, 4 },
362
363 { FALSE, FALSE, TRUE, TRUE, 16, 8 },
364 { FALSE, FALSE, TRUE, FALSE, 16, 8 },
365 { FALSE, FALSE, FALSE, TRUE, 16, 8 },
366 { FALSE, FALSE, FALSE, FALSE, 16, 8 },
367
368 { FALSE, FALSE, TRUE, TRUE, 8, 16 },
369 { FALSE, FALSE, TRUE, FALSE, 8, 16 },
370 { FALSE, FALSE, FALSE, TRUE, 8, 16 },
371 { FALSE, FALSE, FALSE, FALSE, 8, 16 },
372
373 { FALSE, FALSE, TRUE, TRUE, 8, 4 },
374 { FALSE, FALSE, TRUE, FALSE, 8, 4 },
375 { FALSE, FALSE, FALSE, TRUE, 8, 4 },
376 { FALSE, FALSE, FALSE, FALSE, 8, 4 },
377 };
378
379
380 const unsigned num_types = sizeof(conv_types)/sizeof(conv_types[0]);
381
382
383 boolean
384 test_all(struct gallivm_state *gallivm, unsigned verbose, FILE *fp)
385 {
386 const struct lp_type *src_type;
387 const struct lp_type *dst_type;
388 boolean success = TRUE;
389 int error_count = 0;
390
391 for(src_type = conv_types; src_type < &conv_types[num_types]; ++src_type) {
392 for(dst_type = conv_types; dst_type < &conv_types[num_types]; ++dst_type) {
393
394 if(src_type == dst_type)
395 continue;
396
397 if(!test_one(gallivm, verbose, fp, *src_type, *dst_type)){
398 success = FALSE;
399 ++error_count;
400 }
401 }
402 }
403
404 fprintf(stderr, "%d failures\n", error_count);
405
406 return success;
407 }
408
409
410 boolean
411 test_some(struct gallivm_state *gallivm, unsigned verbose, FILE *fp,
412 unsigned long n)
413 {
414 const struct lp_type *src_type;
415 const struct lp_type *dst_type;
416 unsigned long i;
417 boolean success = TRUE;
418
419 for(i = 0; i < n; ++i) {
420 src_type = &conv_types[rand() % num_types];
421
422 do {
423 dst_type = &conv_types[rand() % num_types];
424 } while (src_type == dst_type || src_type->norm != dst_type->norm);
425
426 if(!test_one(gallivm, verbose, fp, *src_type, *dst_type))
427 success = FALSE;
428 }
429
430 return success;
431 }
432
433
434 boolean
435 test_single(struct gallivm_state *gallivm, unsigned verbose, FILE *fp)
436 {
437 /* float, fixed, sign, norm, width, len */
438 struct lp_type f32x4_type =
439 { TRUE, FALSE, TRUE, TRUE, 32, 4 };
440 struct lp_type ub8x4_type =
441 { FALSE, FALSE, FALSE, TRUE, 8, 16 };
442
443 boolean success;
444
445 success = test_one(gallivm, verbose, fp, f32x4_type, ub8x4_type);
446
447 return success;
448 }