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