nvfx: expose GLSL
[mesa/mesa-lb.git] / src / gallium / drivers / llvmpipe / lp_test_conv.c
blob958cc40538e92d6420e30432927136a43cc359fe
1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
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:
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.
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.
26 **************************************************************************/
29 /**
30 * @file
31 * Unit tests for type conversion.
33 * @author Jose Fonseca <jfonseca@vmware.com>
37 #include "gallivm/lp_bld_type.h"
38 #include "gallivm/lp_bld_const.h"
39 #include "gallivm/lp_bld_conv.h"
40 #include "gallivm/lp_bld_debug.h"
41 #include "lp_test.h"
44 typedef void (*conv_test_ptr_t)(const void *src, const void *dst);
47 void
48 write_tsv_header(FILE *fp)
50 fprintf(fp,
51 "result\t"
52 "cycles_per_channel\t"
53 "src_type\t"
54 "dst_type\n");
56 fflush(fp);
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)
67 fprintf(fp, "%s\t", success ? "pass" : "fail");
69 fprintf(fp, "%.1f\t", cycles / MAX2(src_type.length, dst_type.length));
71 dump_type(fp, src_type);
72 fprintf(fp, "\t");
74 dump_type(fp, dst_type);
75 fprintf(fp, "\n");
77 fflush(fp);
81 static void
82 dump_conv_types(FILE *fp,
83 struct lp_type src_type,
84 struct lp_type dst_type)
86 fprintf(fp, "src_type=");
87 dump_type(fp, src_type);
89 fprintf(fp, " dst_type=");
90 dump_type(fp, dst_type);
92 fprintf(fp, " ...\n");
93 fflush(fp);
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)
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;
112 args[0] = LLVMPointerType(lp_build_vec_type(src_type), 0);
113 args[1] = LLVMPointerType(lp_build_vec_type(dst_type), 0);
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);
120 block = LLVMAppendBasicBlock(func, "entry");
121 builder = LLVMCreateBuilder();
122 LLVMPositionBuilderAtEnd(builder, block);
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, "");
130 lp_build_conv(builder, src_type, dst_type, src, num_srcs, dst, num_dsts);
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);
138 LLVMBuildRetVoid(builder);;
140 LLVMDisposeBuilder(builder);
141 return func;
145 PIPE_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)
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;
168 if(verbose >= 1)
169 dump_conv_types(stdout, src_type, dst_type);
171 if(src_type.length > dst_type.length) {
172 num_srcs = 1;
173 num_dsts = src_type.length/dst_type.length;
175 else {
176 num_dsts = 1;
177 num_srcs = dst_type.length/src_type.length;
180 assert(src_type.width * src_type.length == dst_type.width * dst_type.length);
182 /* We must not loose or gain channels. Only precision */
183 assert(src_type.length * num_srcs == dst_type.length * num_dsts);
185 eps = MAX2(lp_const_eps(src_type), lp_const_eps(dst_type));
187 module = LLVMModuleCreateWithName("test");
189 func = add_conv_test(module, src_type, num_srcs, dst_type, num_dsts);
191 if(LLVMVerifyModule(module, LLVMPrintMessageAction, &error)) {
192 LLVMDumpModule(module);
193 abort();
195 LLVMDisposeMessage(error);
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();
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
221 if(verbose >= 2)
222 LLVMDumpModule(module);
224 conv_test_ptr = (conv_test_ptr_t)LLVMGetPointerToGlobal(engine, func);
226 if(verbose >= 2)
227 lp_disassemble(conv_test_ptr);
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 PIPE_ALIGN_VAR(16) uint8_t src[LP_MAX_VECTOR_LENGTH*LP_MAX_VECTOR_LENGTH];
234 PIPE_ALIGN_VAR(16) 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;
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);
245 for(j = 0; j < num_dsts; ++j) {
246 write_vec(dst_type, ref + j*dst_stride, fref + j*dst_type.length);
249 start_counter = rdtsc();
250 conv_test_ptr(src, dst);
251 end_counter = rdtsc();
253 cycles[i] = end_counter - start_counter;
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;
260 if (!success) {
261 if(verbose < 1)
262 dump_conv_types(stderr, src_type, dst_type);
263 fprintf(stderr, "MISMATCH\n");
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");
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
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");
283 fprintf(stderr, " Ref%u: ", j);
284 dump_vec(stderr, dst_type, ref + j*dst_stride);
285 fprintf(stderr, "\n");
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.
296 double sum = 0.0, sum2 = 0.0;
297 double avg, std;
298 unsigned m;
300 for(i = 0; i < n; ++i) {
301 sum += cycles[i];
302 sum2 += cycles[i]*cycles[i];
305 avg = sum/n;
306 std = sqrtf((sum2 - n*avg*avg)/n);
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;
317 cycles_avg = sum/m;
321 if(fp)
322 write_tsv_row(fp, src_type, dst_type, cycles_avg, success);
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(); */
337 LLVMFreeMachineCodeForFunction(engine, func);
339 LLVMDisposeExecutionEngine(engine);
340 if(pass)
341 LLVMDisposePassManager(pass);
343 return success;
347 const struct lp_type conv_types[] = {
348 /* float, fixed, sign, norm, width, len */
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 },
355 /* TODO: test fixed formats too */
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 },
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 },
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 },
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 },
379 const unsigned num_types = sizeof(conv_types)/sizeof(conv_types[0]);
382 boolean
383 test_all(unsigned verbose, FILE *fp)
385 const struct lp_type *src_type;
386 const struct lp_type *dst_type;
387 bool success = TRUE;
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) {
392 if(src_type == dst_type)
393 continue;
395 if(src_type->norm != dst_type->norm)
396 continue;
398 if(!test_one(verbose, fp, *src_type, *dst_type))
399 success = FALSE;
404 return success;
408 boolean
409 test_some(unsigned verbose, FILE *fp, unsigned long n)
411 const struct lp_type *src_type;
412 const struct lp_type *dst_type;
413 unsigned long i;
414 bool success = TRUE;
416 for(i = 0; i < n; ++i) {
417 src_type = &conv_types[rand() % num_types];
419 do {
420 dst_type = &conv_types[rand() % num_types];
421 } while (src_type == dst_type || src_type->norm != dst_type->norm);
423 if(!test_one(verbose, fp, *src_type, *dst_type))
424 success = FALSE;
427 return success;