gallivm/llvmpipe: squash merge of the llvm-context branch
[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 if(verbose >= 1)
177 dump_conv_types(stdout, src_type, dst_type);
178
179 if (src_type.length > dst_type.length) {
180 num_srcs = 1;
181 num_dsts = src_type.length/dst_type.length;
182 }
183 else if (src_type.length < dst_type.length) {
184 num_dsts = 1;
185 num_srcs = dst_type.length/src_type.length;
186 }
187 else {
188 num_dsts = 1;
189 num_srcs = 1;
190 }
191
192 /* We must not loose or gain channels. Only precision */
193 assert(src_type.length * num_srcs == dst_type.length * num_dsts);
194
195 eps = MAX2(lp_const_eps(src_type), lp_const_eps(dst_type));
196
197 func = add_conv_test(gallivm, src_type, num_srcs, dst_type, num_dsts);
198
199 if(LLVMVerifyModule(module, LLVMPrintMessageAction, &error)) {
200 LLVMDumpModule(module);
201 abort();
202 }
203 LLVMDisposeMessage(error);
204
205 if(verbose >= 2)
206 LLVMDumpModule(module);
207
208 code = LLVMGetPointerToGlobal(engine, func);
209 conv_test_ptr = (conv_test_ptr_t)pointer_to_func(code);
210
211 if(verbose >= 2)
212 lp_disassemble(code);
213
214 success = TRUE;
215 for(i = 0; i < n && success; ++i) {
216 unsigned src_stride = src_type.length*src_type.width/8;
217 unsigned dst_stride = dst_type.length*dst_type.width/8;
218 PIPE_ALIGN_VAR(16) uint8_t src[LP_MAX_VECTOR_LENGTH*LP_MAX_VECTOR_LENGTH];
219 PIPE_ALIGN_VAR(16) uint8_t dst[LP_MAX_VECTOR_LENGTH*LP_MAX_VECTOR_LENGTH];
220 double fref[LP_MAX_VECTOR_LENGTH*LP_MAX_VECTOR_LENGTH];
221 uint8_t ref[LP_MAX_VECTOR_LENGTH*LP_MAX_VECTOR_LENGTH];
222 int64_t start_counter = 0;
223 int64_t end_counter = 0;
224
225 for(j = 0; j < num_srcs; ++j) {
226 random_vec(src_type, src + j*src_stride);
227 read_vec(src_type, src + j*src_stride, fref + j*src_type.length);
228 }
229
230 for(j = 0; j < num_dsts; ++j) {
231 write_vec(dst_type, ref + j*dst_stride, fref + j*dst_type.length);
232 }
233
234 start_counter = rdtsc();
235 conv_test_ptr(src, dst);
236 end_counter = rdtsc();
237
238 cycles[i] = end_counter - start_counter;
239
240 for(j = 0; j < num_dsts; ++j) {
241 if(!compare_vec_with_eps(dst_type, dst + j*dst_stride, ref + j*dst_stride, eps))
242 success = FALSE;
243 }
244
245 if (!success || verbose >= 3) {
246 if(verbose < 1)
247 dump_conv_types(stderr, src_type, dst_type);
248 if (success) {
249 fprintf(stderr, "PASS\n");
250 }
251 else {
252 fprintf(stderr, "MISMATCH\n");
253 }
254
255 for(j = 0; j < num_srcs; ++j) {
256 fprintf(stderr, " Src%u: ", j);
257 dump_vec(stderr, src_type, src + j*src_stride);
258 fprintf(stderr, "\n");
259 }
260
261 #if 1
262 fprintf(stderr, " Ref: ");
263 for(j = 0; j < src_type.length*num_srcs; ++j)
264 fprintf(stderr, " %f", fref[j]);
265 fprintf(stderr, "\n");
266 #endif
267
268 for(j = 0; j < num_dsts; ++j) {
269 fprintf(stderr, " Dst%u: ", j);
270 dump_vec(stderr, dst_type, dst + j*dst_stride);
271 fprintf(stderr, "\n");
272
273 fprintf(stderr, " Ref%u: ", j);
274 dump_vec(stderr, dst_type, ref + j*dst_stride);
275 fprintf(stderr, "\n");
276 }
277 }
278 }
279
280 /*
281 * Unfortunately the output of cycle counter is not very reliable as it comes
282 * -- sometimes we get outliers (due IRQs perhaps?) which are
283 * better removed to avoid random or biased data.
284 */
285 {
286 double sum = 0.0, sum2 = 0.0;
287 double avg, std;
288 unsigned m;
289
290 for(i = 0; i < n; ++i) {
291 sum += cycles[i];
292 sum2 += cycles[i]*cycles[i];
293 }
294
295 avg = sum/n;
296 std = sqrtf((sum2 - n*avg*avg)/n);
297
298 m = 0;
299 sum = 0.0;
300 for(i = 0; i < n; ++i) {
301 if(fabs(cycles[i] - avg) <= 4.0*std) {
302 sum += cycles[i];
303 ++m;
304 }
305 }
306
307 cycles_avg = sum/m;
308
309 }
310
311 if(fp)
312 write_tsv_row(fp, src_type, dst_type, cycles_avg, success);
313
314 if (!success) {
315 static boolean firsttime = TRUE;
316 if(firsttime) {
317 if(verbose < 2)
318 LLVMDumpModule(module);
319 LLVMWriteBitcodeToFile(module, "conv.bc");
320 fprintf(stderr, "conv.bc written\n");
321 fprintf(stderr, "Invoke as \"llc -o - conv.bc\"\n");
322 firsttime = FALSE;
323 /* abort(); */
324 }
325 }
326
327 LLVMFreeMachineCodeForFunction(engine, func);
328
329 return success;
330 }
331
332
333 const struct lp_type conv_types[] = {
334 /* float, fixed, sign, norm, width, len */
335
336 { TRUE, FALSE, TRUE, TRUE, 32, 4 },
337 { TRUE, FALSE, TRUE, FALSE, 32, 4 },
338 { TRUE, FALSE, FALSE, TRUE, 32, 4 },
339 { TRUE, FALSE, FALSE, FALSE, 32, 4 },
340
341 /* TODO: test fixed formats too */
342
343 { FALSE, FALSE, TRUE, TRUE, 16, 8 },
344 { FALSE, FALSE, TRUE, FALSE, 16, 8 },
345 { FALSE, FALSE, FALSE, TRUE, 16, 8 },
346 { FALSE, FALSE, FALSE, FALSE, 16, 8 },
347
348 { FALSE, FALSE, TRUE, TRUE, 32, 4 },
349 { FALSE, FALSE, TRUE, FALSE, 32, 4 },
350 { FALSE, FALSE, FALSE, TRUE, 32, 4 },
351 { FALSE, FALSE, FALSE, FALSE, 32, 4 },
352
353 { FALSE, FALSE, TRUE, TRUE, 16, 8 },
354 { FALSE, FALSE, TRUE, FALSE, 16, 8 },
355 { FALSE, FALSE, FALSE, TRUE, 16, 8 },
356 { FALSE, FALSE, FALSE, FALSE, 16, 8 },
357
358 { FALSE, FALSE, TRUE, TRUE, 8, 16 },
359 { FALSE, FALSE, TRUE, FALSE, 8, 16 },
360 { FALSE, FALSE, FALSE, TRUE, 8, 16 },
361 { FALSE, FALSE, FALSE, FALSE, 8, 16 },
362
363 { FALSE, FALSE, TRUE, TRUE, 8, 4 },
364 { FALSE, FALSE, TRUE, FALSE, 8, 4 },
365 { FALSE, FALSE, FALSE, TRUE, 8, 4 },
366 { FALSE, FALSE, FALSE, FALSE, 8, 4 },
367 };
368
369
370 const unsigned num_types = sizeof(conv_types)/sizeof(conv_types[0]);
371
372
373 boolean
374 test_all(struct gallivm_state *gallivm, unsigned verbose, FILE *fp)
375 {
376 const struct lp_type *src_type;
377 const struct lp_type *dst_type;
378 boolean success = TRUE;
379
380 for(src_type = conv_types; src_type < &conv_types[num_types]; ++src_type) {
381 for(dst_type = conv_types; dst_type < &conv_types[num_types]; ++dst_type) {
382
383 if(src_type == dst_type)
384 continue;
385
386 if(src_type->norm != dst_type->norm)
387 continue;
388
389 if(!test_one(gallivm, verbose, fp, *src_type, *dst_type))
390 success = FALSE;
391
392 }
393 }
394
395 return success;
396 }
397
398
399 boolean
400 test_some(struct gallivm_state *gallivm, unsigned verbose, FILE *fp,
401 unsigned long n)
402 {
403 const struct lp_type *src_type;
404 const struct lp_type *dst_type;
405 unsigned long i;
406 boolean success = TRUE;
407
408 for(i = 0; i < n; ++i) {
409 src_type = &conv_types[rand() % num_types];
410
411 do {
412 dst_type = &conv_types[rand() % num_types];
413 } while (src_type == dst_type || src_type->norm != dst_type->norm);
414
415 if(!test_one(gallivm, verbose, fp, *src_type, *dst_type))
416 success = FALSE;
417 }
418
419 return success;
420 }
421
422
423 boolean
424 test_single(struct gallivm_state *gallivm, unsigned verbose, FILE *fp)
425 {
426 /* float, fixed, sign, norm, width, len */
427 struct lp_type f32x4_type =
428 { TRUE, FALSE, TRUE, TRUE, 32, 4 };
429 struct lp_type ub8x4_type =
430 { FALSE, FALSE, FALSE, TRUE, 8, 16 };
431
432 boolean success;
433
434 success = test_one(gallivm, verbose, fp, f32x4_type, ub8x4_type);
435
436 return success;
437 }