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