llvmpipe: Make the code portable for MinGW.
[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 "lp_bld_type.h"
38 #include "lp_bld_const.h"
39 #include "lp_bld_conv.h"
40 #include "lp_bld_debug.h"
41 #include "lp_test.h"
42
43
44 typedef void (*conv_test_ptr_t)(const void *src, const void *dst);
45
46
47 void
48 write_tsv_header(FILE *fp)
49 {
50 fprintf(fp,
51 "result\t"
52 "cycles_per_channel\t"
53 "src_type\t"
54 "dst_type\n");
55
56 fflush(fp);
57 }
58
59
60 static void
61 write_tsv_row(FILE *fp,
62 struct lp_type src_type,
63 struct lp_type dst_type,
64 double cycles,
65 boolean success)
66 {
67 fprintf(fp, "%s\t", success ? "pass" : "fail");
68
69 fprintf(fp, "%.1f\t", cycles / MAX2(src_type.length, dst_type.length));
70
71 dump_type(fp, src_type);
72 fprintf(fp, "\t");
73
74 dump_type(fp, dst_type);
75 fprintf(fp, "\n");
76
77 fflush(fp);
78 }
79
80
81 static void
82 dump_conv_types(FILE *fp,
83 struct lp_type src_type,
84 struct lp_type dst_type)
85 {
86 fprintf(fp, "src_type=");
87 dump_type(fp, src_type);
88
89 fprintf(fp, " dst_type=");
90 dump_type(fp, dst_type);
91
92 fprintf(fp, " ...\n");
93 fflush(fp);
94 }
95
96
97 static LLVMValueRef
98 add_conv_test(LLVMModuleRef module,
99 struct lp_type src_type, unsigned num_srcs,
100 struct lp_type dst_type, unsigned num_dsts)
101 {
102 LLVMTypeRef args[2];
103 LLVMValueRef func;
104 LLVMValueRef src_ptr;
105 LLVMValueRef dst_ptr;
106 LLVMBasicBlockRef block;
107 LLVMBuilderRef builder;
108 LLVMValueRef src[LP_MAX_VECTOR_LENGTH];
109 LLVMValueRef dst[LP_MAX_VECTOR_LENGTH];
110 unsigned i;
111
112 args[0] = LLVMPointerType(lp_build_vec_type(src_type), 0);
113 args[1] = LLVMPointerType(lp_build_vec_type(dst_type), 0);
114
115 func = LLVMAddFunction(module, "test", LLVMFunctionType(LLVMVoidType(), args, 2, 0));
116 LLVMSetFunctionCallConv(func, LLVMCCallConv);
117 src_ptr = LLVMGetParam(func, 0);
118 dst_ptr = LLVMGetParam(func, 1);
119
120 block = LLVMAppendBasicBlock(func, "entry");
121 builder = LLVMCreateBuilder();
122 LLVMPositionBuilderAtEnd(builder, block);
123
124 for(i = 0; i < num_srcs; ++i) {
125 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
126 LLVMValueRef ptr = LLVMBuildGEP(builder, src_ptr, &index, 1, "");
127 src[i] = LLVMBuildLoad(builder, ptr, "");
128 }
129
130 lp_build_conv(builder, src_type, dst_type, src, num_srcs, dst, num_dsts);
131
132 for(i = 0; i < num_dsts; ++i) {
133 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
134 LLVMValueRef ptr = LLVMBuildGEP(builder, dst_ptr, &index, 1, "");
135 LLVMBuildStore(builder, dst[i], ptr);
136 }
137
138 LLVMBuildRetVoid(builder);;
139
140 LLVMDisposeBuilder(builder);
141 return func;
142 }
143
144
145 static boolean
146 test_one(unsigned verbose,
147 FILE *fp,
148 struct lp_type src_type,
149 struct lp_type dst_type)
150 {
151 LLVMModuleRef module = NULL;
152 LLVMValueRef func = NULL;
153 LLVMExecutionEngineRef engine = NULL;
154 LLVMModuleProviderRef provider = NULL;
155 LLVMPassManagerRef pass = NULL;
156 char *error = NULL;
157 conv_test_ptr_t conv_test_ptr;
158 boolean success;
159 const unsigned n = 32;
160 int64_t cycles[n];
161 double cycles_avg = 0.0;
162 unsigned num_srcs;
163 unsigned num_dsts;
164 double eps;
165 unsigned i, j;
166
167 if(verbose >= 1)
168 dump_conv_types(stdout, src_type, dst_type);
169
170 if(src_type.length > dst_type.length) {
171 num_srcs = 1;
172 num_dsts = src_type.length/dst_type.length;
173 }
174 else {
175 num_dsts = 1;
176 num_srcs = dst_type.length/src_type.length;
177 }
178
179 assert(src_type.width * src_type.length == dst_type.width * dst_type.length);
180
181 /* We must not loose or gain channels. Only precision */
182 assert(src_type.length * num_srcs == dst_type.length * num_dsts);
183
184 eps = MAX2(lp_const_eps(src_type), lp_const_eps(dst_type));
185
186 module = LLVMModuleCreateWithName("test");
187
188 func = add_conv_test(module, src_type, num_srcs, dst_type, num_dsts);
189
190 if(LLVMVerifyModule(module, LLVMPrintMessageAction, &error)) {
191 LLVMDumpModule(module);
192 abort();
193 }
194 LLVMDisposeMessage(error);
195
196 provider = LLVMCreateModuleProviderForExistingModule(module);
197 if (LLVMCreateJITCompiler(&engine, provider, 1, &error)) {
198 if(verbose < 1)
199 dump_conv_types(stderr, src_type, dst_type);
200 fprintf(stderr, "%s\n", error);
201 LLVMDisposeMessage(error);
202 abort();
203 }
204
205 #if 0
206 pass = LLVMCreatePassManager();
207 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engine), pass);
208 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
209 * but there are more on SVN. */
210 LLVMAddConstantPropagationPass(pass);
211 LLVMAddInstructionCombiningPass(pass);
212 LLVMAddPromoteMemoryToRegisterPass(pass);
213 LLVMAddGVNPass(pass);
214 LLVMAddCFGSimplificationPass(pass);
215 LLVMRunPassManager(pass, module);
216 #else
217 (void)pass;
218 #endif
219
220 if(verbose >= 2)
221 LLVMDumpModule(module);
222
223 conv_test_ptr = (conv_test_ptr_t)LLVMGetPointerToGlobal(engine, func);
224
225 if(verbose >= 2)
226 lp_disassemble(conv_test_ptr);
227
228 success = TRUE;
229 for(i = 0; i < n && success; ++i) {
230 unsigned src_stride = src_type.length*src_type.width/8;
231 unsigned dst_stride = dst_type.length*dst_type.width/8;
232 uint8_t src[LP_MAX_VECTOR_LENGTH*LP_MAX_VECTOR_LENGTH];
233 uint8_t dst[LP_MAX_VECTOR_LENGTH*LP_MAX_VECTOR_LENGTH];
234 double fref[LP_MAX_VECTOR_LENGTH*LP_MAX_VECTOR_LENGTH];
235 uint8_t ref[LP_MAX_VECTOR_LENGTH*LP_MAX_VECTOR_LENGTH];
236 int64_t start_counter = 0;
237 int64_t end_counter = 0;
238
239 for(j = 0; j < num_srcs; ++j) {
240 random_vec(src_type, src + j*src_stride);
241 read_vec(src_type, src + j*src_stride, fref + j*src_type.length);
242 }
243
244 for(j = 0; j < num_dsts; ++j) {
245 write_vec(dst_type, ref + j*dst_stride, fref + j*dst_type.length);
246 }
247
248 start_counter = rdtsc();
249 conv_test_ptr(src, dst);
250 end_counter = rdtsc();
251
252 cycles[i] = end_counter - start_counter;
253
254 for(j = 0; j < num_dsts; ++j) {
255 if(!compare_vec_with_eps(dst_type, dst + j*dst_stride, ref + j*dst_stride, eps))
256 success = FALSE;
257 }
258
259 if (!success) {
260 if(verbose < 1)
261 dump_conv_types(stderr, src_type, dst_type);
262 fprintf(stderr, "MISMATCH\n");
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 LLVMDisposeExecutionEngine(engine);
339 if(pass)
340 LLVMDisposePassManager(pass);
341
342 return success;
343 }
344
345
346 const struct lp_type conv_types[] = {
347 /* float, fixed, sign, norm, width, len */
348
349 { TRUE, FALSE, TRUE, TRUE, 32, 4 },
350 { TRUE, FALSE, TRUE, FALSE, 32, 4 },
351 { TRUE, FALSE, FALSE, TRUE, 32, 4 },
352 { TRUE, FALSE, FALSE, FALSE, 32, 4 },
353
354 /* TODO: test fixed formats too */
355
356 { FALSE, FALSE, TRUE, TRUE, 16, 8 },
357 { FALSE, FALSE, TRUE, FALSE, 16, 8 },
358 { FALSE, FALSE, FALSE, TRUE, 16, 8 },
359 { FALSE, FALSE, FALSE, FALSE, 16, 8 },
360
361 { FALSE, FALSE, TRUE, TRUE, 32, 4 },
362 { FALSE, FALSE, TRUE, FALSE, 32, 4 },
363 { FALSE, FALSE, FALSE, TRUE, 32, 4 },
364 { FALSE, FALSE, FALSE, FALSE, 32, 4 },
365
366 { FALSE, FALSE, TRUE, TRUE, 16, 8 },
367 { FALSE, FALSE, TRUE, FALSE, 16, 8 },
368 { FALSE, FALSE, FALSE, TRUE, 16, 8 },
369 { FALSE, FALSE, FALSE, FALSE, 16, 8 },
370
371 { FALSE, FALSE, TRUE, TRUE, 8, 16 },
372 { FALSE, FALSE, TRUE, FALSE, 8, 16 },
373 { FALSE, FALSE, FALSE, TRUE, 8, 16 },
374 { FALSE, FALSE, FALSE, FALSE, 8, 16 },
375 };
376
377
378 const unsigned num_types = sizeof(conv_types)/sizeof(conv_types[0]);
379
380
381 boolean
382 test_all(unsigned verbose, FILE *fp)
383 {
384 const struct lp_type *src_type;
385 const struct lp_type *dst_type;
386 bool success = TRUE;
387
388 for(src_type = conv_types; src_type < &conv_types[num_types]; ++src_type) {
389 for(dst_type = conv_types; dst_type < &conv_types[num_types]; ++dst_type) {
390
391 if(src_type == dst_type)
392 continue;
393
394 if(src_type->norm != dst_type->norm)
395 continue;
396
397 if(!test_one(verbose, fp, *src_type, *dst_type))
398 success = FALSE;
399
400 }
401 }
402
403 return success;
404 }
405
406
407 boolean
408 test_some(unsigned verbose, FILE *fp, unsigned long n)
409 {
410 const struct lp_type *src_type;
411 const struct lp_type *dst_type;
412 unsigned long i;
413 bool success = TRUE;
414
415 for(i = 0; i < n; ++i) {
416 src_type = &conv_types[rand() % num_types];
417
418 do {
419 dst_type = &conv_types[rand() % num_types];
420 } while (src_type == dst_type || src_type->norm != dst_type->norm);
421
422 if(!test_one(verbose, fp, *src_type, *dst_type))
423 success = FALSE;
424 }
425
426 return success;
427 }