gallivm: Fix 4 x unorm8 -> 4 x float conversion.
[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_type.h"
39 #include "gallivm/lp_bld_const.h"
40 #include "gallivm/lp_bld_conv.h"
41 #include "gallivm/lp_bld_debug.h"
42 #include "lp_test.h"
43
44
45 typedef void (*conv_test_ptr_t)(const void *src, const void *dst);
46
47
48 void
49 write_tsv_header(FILE *fp)
50 {
51 fprintf(fp,
52 "result\t"
53 "cycles_per_channel\t"
54 "src_type\t"
55 "dst_type\n");
56
57 fflush(fp);
58 }
59
60
61 static void
62 write_tsv_row(FILE *fp,
63 struct lp_type src_type,
64 struct lp_type dst_type,
65 double cycles,
66 boolean success)
67 {
68 fprintf(fp, "%s\t", success ? "pass" : "fail");
69
70 fprintf(fp, "%.1f\t", cycles / MAX2(src_type.length, dst_type.length));
71
72 dump_type(fp, src_type);
73 fprintf(fp, "\t");
74
75 dump_type(fp, dst_type);
76 fprintf(fp, "\n");
77
78 fflush(fp);
79 }
80
81
82 static void
83 dump_conv_types(FILE *fp,
84 struct lp_type src_type,
85 struct lp_type dst_type)
86 {
87 fprintf(fp, "src_type=");
88 dump_type(fp, src_type);
89
90 fprintf(fp, " dst_type=");
91 dump_type(fp, dst_type);
92
93 fprintf(fp, " ...\n");
94 fflush(fp);
95 }
96
97
98 static LLVMValueRef
99 add_conv_test(LLVMModuleRef module,
100 struct lp_type src_type, unsigned num_srcs,
101 struct lp_type dst_type, unsigned num_dsts)
102 {
103 LLVMTypeRef args[2];
104 LLVMValueRef func;
105 LLVMValueRef src_ptr;
106 LLVMValueRef dst_ptr;
107 LLVMBasicBlockRef block;
108 LLVMBuilderRef builder;
109 LLVMValueRef src[LP_MAX_VECTOR_LENGTH];
110 LLVMValueRef dst[LP_MAX_VECTOR_LENGTH];
111 unsigned i;
112
113 args[0] = LLVMPointerType(lp_build_vec_type(src_type), 0);
114 args[1] = LLVMPointerType(lp_build_vec_type(dst_type), 0);
115
116 func = LLVMAddFunction(module, "test", LLVMFunctionType(LLVMVoidType(), args, 2, 0));
117 LLVMSetFunctionCallConv(func, LLVMCCallConv);
118 src_ptr = LLVMGetParam(func, 0);
119 dst_ptr = LLVMGetParam(func, 1);
120
121 block = LLVMAppendBasicBlock(func, "entry");
122 builder = LLVMCreateBuilder();
123 LLVMPositionBuilderAtEnd(builder, block);
124
125 for(i = 0; i < num_srcs; ++i) {
126 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
127 LLVMValueRef ptr = LLVMBuildGEP(builder, src_ptr, &index, 1, "");
128 src[i] = LLVMBuildLoad(builder, ptr, "");
129 }
130
131 lp_build_conv(builder, src_type, dst_type, src, num_srcs, dst, num_dsts);
132
133 for(i = 0; i < num_dsts; ++i) {
134 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
135 LLVMValueRef ptr = LLVMBuildGEP(builder, dst_ptr, &index, 1, "");
136 LLVMBuildStore(builder, dst[i], ptr);
137 }
138
139 LLVMBuildRetVoid(builder);;
140
141 LLVMDisposeBuilder(builder);
142 return func;
143 }
144
145
146 PIPE_ALIGN_STACK
147 static boolean
148 test_one(unsigned verbose,
149 FILE *fp,
150 struct lp_type src_type,
151 struct lp_type dst_type)
152 {
153 LLVMModuleRef module = NULL;
154 LLVMValueRef func = NULL;
155 LLVMExecutionEngineRef engine = NULL;
156 LLVMModuleProviderRef provider = NULL;
157 LLVMPassManagerRef pass = NULL;
158 char *error = NULL;
159 conv_test_ptr_t conv_test_ptr;
160 boolean success;
161 const unsigned n = LP_TEST_NUM_SAMPLES;
162 int64_t cycles[LP_TEST_NUM_SAMPLES];
163 double cycles_avg = 0.0;
164 unsigned num_srcs;
165 unsigned num_dsts;
166 double eps;
167 unsigned i, j;
168 void *code;
169
170 if (src_type.width * src_type.length != dst_type.width * dst_type.length &&
171 src_type.length != dst_type.length) {
172 return TRUE;
173 }
174
175 if(verbose >= 1)
176 dump_conv_types(stdout, src_type, dst_type);
177
178 if (src_type.length > dst_type.length) {
179 num_srcs = 1;
180 num_dsts = src_type.length/dst_type.length;
181 }
182 else if (src_type.length < dst_type.length) {
183 num_dsts = 1;
184 num_srcs = dst_type.length/src_type.length;
185 }
186 else {
187 num_dsts = 1;
188 num_srcs = 1;
189 }
190
191 /* We must not loose or gain channels. Only precision */
192 assert(src_type.length * num_srcs == dst_type.length * num_dsts);
193
194 eps = MAX2(lp_const_eps(src_type), lp_const_eps(dst_type));
195
196 module = LLVMModuleCreateWithName("test");
197
198 func = add_conv_test(module, src_type, num_srcs, dst_type, num_dsts);
199
200 if(LLVMVerifyModule(module, LLVMPrintMessageAction, &error)) {
201 LLVMDumpModule(module);
202 abort();
203 }
204 LLVMDisposeMessage(error);
205
206 provider = LLVMCreateModuleProviderForExistingModule(module);
207 if (LLVMCreateJITCompiler(&engine, provider, 1, &error)) {
208 if(verbose < 1)
209 dump_conv_types(stderr, src_type, dst_type);
210 fprintf(stderr, "%s\n", error);
211 LLVMDisposeMessage(error);
212 abort();
213 }
214
215 #if 0
216 pass = LLVMCreatePassManager();
217 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engine), pass);
218 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
219 * but there are more on SVN. */
220 LLVMAddConstantPropagationPass(pass);
221 LLVMAddInstructionCombiningPass(pass);
222 LLVMAddPromoteMemoryToRegisterPass(pass);
223 LLVMAddGVNPass(pass);
224 LLVMAddCFGSimplificationPass(pass);
225 LLVMRunPassManager(pass, module);
226 #else
227 (void)pass;
228 #endif
229
230 if(verbose >= 2)
231 LLVMDumpModule(module);
232
233 code = LLVMGetPointerToGlobal(engine, func);
234 conv_test_ptr = (conv_test_ptr_t)pointer_to_func(code);
235
236 if(verbose >= 2)
237 lp_disassemble(code);
238
239 success = TRUE;
240 for(i = 0; i < n && success; ++i) {
241 unsigned src_stride = src_type.length*src_type.width/8;
242 unsigned dst_stride = dst_type.length*dst_type.width/8;
243 PIPE_ALIGN_VAR(16) uint8_t src[LP_MAX_VECTOR_LENGTH*LP_MAX_VECTOR_LENGTH];
244 PIPE_ALIGN_VAR(16) uint8_t dst[LP_MAX_VECTOR_LENGTH*LP_MAX_VECTOR_LENGTH];
245 double fref[LP_MAX_VECTOR_LENGTH*LP_MAX_VECTOR_LENGTH];
246 uint8_t ref[LP_MAX_VECTOR_LENGTH*LP_MAX_VECTOR_LENGTH];
247 int64_t start_counter = 0;
248 int64_t end_counter = 0;
249
250 for(j = 0; j < num_srcs; ++j) {
251 random_vec(src_type, src + j*src_stride);
252 read_vec(src_type, src + j*src_stride, fref + j*src_type.length);
253 }
254
255 for(j = 0; j < num_dsts; ++j) {
256 write_vec(dst_type, ref + j*dst_stride, fref + j*dst_type.length);
257 }
258
259 start_counter = rdtsc();
260 conv_test_ptr(src, dst);
261 end_counter = rdtsc();
262
263 cycles[i] = end_counter - start_counter;
264
265 for(j = 0; j < num_dsts; ++j) {
266 if(!compare_vec_with_eps(dst_type, dst + j*dst_stride, ref + j*dst_stride, eps))
267 success = FALSE;
268 }
269
270 if (!success || verbose >= 3) {
271 if(verbose < 1)
272 dump_conv_types(stderr, src_type, dst_type);
273 if (success) {
274 fprintf(stderr, "PASS\n");
275 }
276 else {
277 fprintf(stderr, "MISMATCH\n");
278 }
279
280 for(j = 0; j < num_srcs; ++j) {
281 fprintf(stderr, " Src%u: ", j);
282 dump_vec(stderr, src_type, src + j*src_stride);
283 fprintf(stderr, "\n");
284 }
285
286 #if 1
287 fprintf(stderr, " Ref: ");
288 for(j = 0; j < src_type.length*num_srcs; ++j)
289 fprintf(stderr, " %f", fref[j]);
290 fprintf(stderr, "\n");
291 #endif
292
293 for(j = 0; j < num_dsts; ++j) {
294 fprintf(stderr, " Dst%u: ", j);
295 dump_vec(stderr, dst_type, dst + j*dst_stride);
296 fprintf(stderr, "\n");
297
298 fprintf(stderr, " Ref%u: ", j);
299 dump_vec(stderr, dst_type, ref + j*dst_stride);
300 fprintf(stderr, "\n");
301 }
302 }
303 }
304
305 /*
306 * Unfortunately the output of cycle counter is not very reliable as it comes
307 * -- sometimes we get outliers (due IRQs perhaps?) which are
308 * better removed to avoid random or biased data.
309 */
310 {
311 double sum = 0.0, sum2 = 0.0;
312 double avg, std;
313 unsigned m;
314
315 for(i = 0; i < n; ++i) {
316 sum += cycles[i];
317 sum2 += cycles[i]*cycles[i];
318 }
319
320 avg = sum/n;
321 std = sqrtf((sum2 - n*avg*avg)/n);
322
323 m = 0;
324 sum = 0.0;
325 for(i = 0; i < n; ++i) {
326 if(fabs(cycles[i] - avg) <= 4.0*std) {
327 sum += cycles[i];
328 ++m;
329 }
330 }
331
332 cycles_avg = sum/m;
333
334 }
335
336 if(fp)
337 write_tsv_row(fp, src_type, dst_type, cycles_avg, success);
338
339 if (!success) {
340 static boolean firsttime = TRUE;
341 if(firsttime) {
342 if(verbose < 2)
343 LLVMDumpModule(module);
344 LLVMWriteBitcodeToFile(module, "conv.bc");
345 fprintf(stderr, "conv.bc written\n");
346 fprintf(stderr, "Invoke as \"llc -o - conv.bc\"\n");
347 firsttime = FALSE;
348 /* abort(); */
349 }
350 }
351
352 LLVMFreeMachineCodeForFunction(engine, func);
353
354 LLVMDisposeExecutionEngine(engine);
355 if(pass)
356 LLVMDisposePassManager(pass);
357
358 return success;
359 }
360
361
362 const struct lp_type conv_types[] = {
363 /* float, fixed, sign, norm, width, len */
364
365 { TRUE, FALSE, TRUE, TRUE, 32, 4 },
366 { TRUE, FALSE, TRUE, FALSE, 32, 4 },
367 { TRUE, FALSE, FALSE, TRUE, 32, 4 },
368 { TRUE, FALSE, FALSE, FALSE, 32, 4 },
369
370 /* TODO: test fixed formats too */
371
372 { FALSE, FALSE, TRUE, TRUE, 16, 8 },
373 { FALSE, FALSE, TRUE, FALSE, 16, 8 },
374 { FALSE, FALSE, FALSE, TRUE, 16, 8 },
375 { FALSE, FALSE, FALSE, FALSE, 16, 8 },
376
377 { FALSE, FALSE, TRUE, TRUE, 32, 4 },
378 { FALSE, FALSE, TRUE, FALSE, 32, 4 },
379 { FALSE, FALSE, FALSE, TRUE, 32, 4 },
380 { FALSE, FALSE, FALSE, FALSE, 32, 4 },
381
382 { FALSE, FALSE, TRUE, TRUE, 16, 8 },
383 { FALSE, FALSE, TRUE, FALSE, 16, 8 },
384 { FALSE, FALSE, FALSE, TRUE, 16, 8 },
385 { FALSE, FALSE, FALSE, FALSE, 16, 8 },
386
387 { FALSE, FALSE, TRUE, TRUE, 8, 16 },
388 { FALSE, FALSE, TRUE, FALSE, 8, 16 },
389 { FALSE, FALSE, FALSE, TRUE, 8, 16 },
390 { FALSE, FALSE, FALSE, FALSE, 8, 16 },
391
392 { FALSE, FALSE, TRUE, TRUE, 8, 4 },
393 { FALSE, FALSE, TRUE, FALSE, 8, 4 },
394 { FALSE, FALSE, FALSE, TRUE, 8, 4 },
395 { FALSE, FALSE, FALSE, FALSE, 8, 4 },
396 };
397
398
399 const unsigned num_types = sizeof(conv_types)/sizeof(conv_types[0]);
400
401
402 boolean
403 test_all(unsigned verbose, FILE *fp)
404 {
405 const struct lp_type *src_type;
406 const struct lp_type *dst_type;
407 boolean success = TRUE;
408
409 for(src_type = conv_types; src_type < &conv_types[num_types]; ++src_type) {
410 for(dst_type = conv_types; dst_type < &conv_types[num_types]; ++dst_type) {
411
412 if(src_type == dst_type)
413 continue;
414
415 if(src_type->norm != dst_type->norm)
416 continue;
417
418 if(!test_one(verbose, fp, *src_type, *dst_type))
419 success = FALSE;
420
421 }
422 }
423
424 return success;
425 }
426
427
428 boolean
429 test_some(unsigned verbose, FILE *fp, unsigned long n)
430 {
431 const struct lp_type *src_type;
432 const struct lp_type *dst_type;
433 unsigned long i;
434 boolean success = TRUE;
435
436 for(i = 0; i < n; ++i) {
437 src_type = &conv_types[rand() % num_types];
438
439 do {
440 dst_type = &conv_types[rand() % num_types];
441 } while (src_type == dst_type || src_type->norm != dst_type->norm);
442
443 if(!test_one(verbose, fp, *src_type, *dst_type))
444 success = FALSE;
445 }
446
447 return success;
448 }
449
450
451 boolean
452 test_single(unsigned verbose, FILE *fp)
453 {
454 /* float, fixed, sign, norm, width, len */
455 struct lp_type f32x4_type =
456 { TRUE, FALSE, TRUE, TRUE, 32, 4 };
457 struct lp_type ub8x4_type =
458 { FALSE, FALSE, FALSE, TRUE, 8, 16 };
459
460 boolean success;
461
462 success = test_one(verbose, fp, f32x4_type, ub8x4_type);
463
464 return success;
465 }