llvmpipe: silence pointer type warnings
[mesa.git] / src / gallium / drivers / llvmpipe / lp_test_format.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 #include <stdlib.h>
30 #include <stdio.h>
31 #include <float.h>
32
33 #include "gallivm/lp_bld.h"
34 #include "gallivm/lp_bld_debug.h"
35 #include "gallivm/lp_bld_init.h"
36 #include <llvm-c/Analysis.h>
37 #include <llvm-c/Target.h>
38 #include <llvm-c/Transforms/Scalar.h>
39
40 #include "util/u_memory.h"
41 #include "util/u_pointer.h"
42 #include "util/u_string.h"
43 #include "util/u_format.h"
44 #include "util/u_format_tests.h"
45 #include "util/u_format_s3tc.h"
46
47 #include "gallivm/lp_bld_format.h"
48 #include "lp_test.h"
49
50
51 void
52 write_tsv_header(FILE *fp)
53 {
54 fprintf(fp,
55 "result\t"
56 "format\n");
57
58 fflush(fp);
59 }
60
61
62 static void
63 write_tsv_row(FILE *fp,
64 const struct util_format_description *desc,
65 boolean success)
66 {
67 fprintf(fp, "%s\t", success ? "pass" : "fail");
68
69 fprintf(fp, "%s\n", desc->name);
70
71 fflush(fp);
72 }
73
74
75 typedef void
76 (*fetch_ptr_t)(void *unpacked, const void *packed,
77 unsigned i, unsigned j);
78
79
80 static LLVMValueRef
81 add_fetch_rgba_test(unsigned verbose,
82 const struct util_format_description *desc,
83 struct lp_type type)
84 {
85 char name[256];
86 LLVMTypeRef args[4];
87 LLVMValueRef func;
88 LLVMValueRef packed_ptr;
89 LLVMValueRef rgba_ptr;
90 LLVMValueRef i;
91 LLVMValueRef j;
92 LLVMBasicBlockRef block;
93 LLVMBuilderRef builder;
94 LLVMValueRef rgba;
95
96 util_snprintf(name, sizeof name, "fetch_%s_%s", desc->short_name,
97 type.floating ? "float" : "unorm8");
98
99 args[0] = LLVMPointerType(lp_build_vec_type(type), 0);
100 args[1] = LLVMPointerType(LLVMInt8Type(), 0);
101 args[3] = args[2] = LLVMInt32Type();
102
103 func = LLVMAddFunction(lp_build_module, name,
104 LLVMFunctionType(LLVMVoidType(), args, Elements(args), 0));
105 LLVMSetFunctionCallConv(func, LLVMCCallConv);
106 rgba_ptr = LLVMGetParam(func, 0);
107 packed_ptr = LLVMGetParam(func, 1);
108 i = LLVMGetParam(func, 2);
109 j = LLVMGetParam(func, 3);
110
111 block = LLVMAppendBasicBlock(func, "entry");
112 builder = LLVMCreateBuilder();
113 LLVMPositionBuilderAtEnd(builder, block);
114
115 rgba = lp_build_fetch_rgba_aos(builder, desc, type, packed_ptr, i, j);
116
117 LLVMBuildStore(builder, rgba, rgba_ptr);
118
119 LLVMBuildRetVoid(builder);
120
121 LLVMDisposeBuilder(builder);
122
123 if (LLVMVerifyFunction(func, LLVMPrintMessageAction)) {
124 LLVMDumpValue(func);
125 abort();
126 }
127
128 LLVMRunFunctionPassManager(lp_build_pass, func);
129
130 if (verbose >= 1) {
131 LLVMDumpValue(func);
132 }
133
134 return func;
135 }
136
137
138 PIPE_ALIGN_STACK
139 static boolean
140 test_format_float(unsigned verbose, FILE *fp,
141 const struct util_format_description *desc)
142 {
143 LLVMValueRef fetch = NULL;
144 fetch_ptr_t fetch_ptr;
145 PIPE_ALIGN_VAR(16) float unpacked[4];
146 boolean first = TRUE;
147 boolean success = TRUE;
148 unsigned i, j, k, l;
149 void *f;
150
151 fetch = add_fetch_rgba_test(verbose, desc, lp_float32_vec4_type());
152
153 f = LLVMGetPointerToGlobal(lp_build_engine, fetch);
154 fetch_ptr = (fetch_ptr_t) pointer_to_func(f);
155
156 if (verbose >= 2) {
157 lp_disassemble(f);
158 }
159
160 for (l = 0; l < util_format_nr_test_cases; ++l) {
161 const struct util_format_test_case *test = &util_format_test_cases[l];
162
163 if (test->format == desc->format) {
164
165 if (first) {
166 printf("Testing %s (float) ...\n",
167 desc->name);
168 first = FALSE;
169 }
170
171 for (i = 0; i < desc->block.height; ++i) {
172 for (j = 0; j < desc->block.width; ++j) {
173 boolean match;
174
175 memset(unpacked, 0, sizeof unpacked);
176
177 fetch_ptr(unpacked, test->packed, j, i);
178
179 match = TRUE;
180 for(k = 0; k < 4; ++k)
181 if (fabs((float)test->unpacked[i][j][k] - unpacked[k]) > FLT_EPSILON)
182 match = FALSE;
183
184 if (!match) {
185 printf("FAILED\n");
186 printf(" Packed: %02x %02x %02x %02x\n",
187 test->packed[0], test->packed[1], test->packed[2], test->packed[3]);
188 printf(" Unpacked (%u,%u): %f %f %f %f obtained\n",
189 j, i,
190 unpacked[0], unpacked[1], unpacked[2], unpacked[3]);
191 printf(" %f %f %f %f expected\n",
192 test->unpacked[i][j][0],
193 test->unpacked[i][j][1],
194 test->unpacked[i][j][2],
195 test->unpacked[i][j][3]);
196 success = FALSE;
197 }
198 }
199 }
200 }
201 }
202
203 if (!success) {
204 if (verbose < 1) {
205 LLVMDumpValue(fetch);
206 }
207 }
208
209 LLVMFreeMachineCodeForFunction(lp_build_engine, fetch);
210 LLVMDeleteFunction(fetch);
211
212 if(fp)
213 write_tsv_row(fp, desc, success);
214
215 return success;
216 }
217
218
219 PIPE_ALIGN_STACK
220 static boolean
221 test_format_unorm8(unsigned verbose, FILE *fp,
222 const struct util_format_description *desc)
223 {
224 LLVMValueRef fetch = NULL;
225 fetch_ptr_t fetch_ptr;
226 uint8_t unpacked[4];
227 boolean first = TRUE;
228 boolean success = TRUE;
229 unsigned i, j, k, l;
230 void *f;
231
232 fetch = add_fetch_rgba_test(verbose, desc, lp_unorm8_vec4_type());
233
234 f = LLVMGetPointerToGlobal(lp_build_engine, fetch);
235 fetch_ptr = (fetch_ptr_t) pointer_to_func(f);
236
237 if (verbose >= 2) {
238 lp_disassemble(f);
239 }
240
241 for (l = 0; l < util_format_nr_test_cases; ++l) {
242 const struct util_format_test_case *test = &util_format_test_cases[l];
243
244 if (test->format == desc->format) {
245
246 if (first) {
247 printf("Testing %s (unorm8) ...\n",
248 desc->name);
249 first = FALSE;
250 }
251
252 for (i = 0; i < desc->block.height; ++i) {
253 for (j = 0; j < desc->block.width; ++j) {
254 boolean match;
255
256 memset(unpacked, 0, sizeof unpacked);
257
258 fetch_ptr(unpacked, test->packed, j, i);
259
260 match = TRUE;
261 for(k = 0; k < 4; ++k) {
262 int error = float_to_ubyte(test->unpacked[i][j][k]) - unpacked[k];
263 if (error < 0)
264 error = -error;
265 if (error > 1)
266 match = FALSE;
267 }
268
269 if (!match) {
270 printf("FAILED\n");
271 printf(" Packed: %02x %02x %02x %02x\n",
272 test->packed[0], test->packed[1], test->packed[2], test->packed[3]);
273 printf(" Unpacked (%u,%u): %02x %02x %02x %02x obtained\n",
274 j, i,
275 unpacked[0], unpacked[1], unpacked[2], unpacked[3]);
276 printf(" %02x %02x %02x %02x expected\n",
277 float_to_ubyte(test->unpacked[i][j][0]),
278 float_to_ubyte(test->unpacked[i][j][1]),
279 float_to_ubyte(test->unpacked[i][j][2]),
280 float_to_ubyte(test->unpacked[i][j][3]));
281 success = FALSE;
282 }
283 }
284 }
285 }
286 }
287
288 if (!success)
289 LLVMDumpValue(fetch);
290
291 LLVMFreeMachineCodeForFunction(lp_build_engine, fetch);
292 LLVMDeleteFunction(fetch);
293
294 if(fp)
295 write_tsv_row(fp, desc, success);
296
297 return success;
298 }
299
300
301
302
303 static boolean
304 test_one(unsigned verbose, FILE *fp,
305 const struct util_format_description *format_desc)
306 {
307 boolean success = TRUE;
308
309 if (!test_format_float(verbose, fp, format_desc)) {
310 success = FALSE;
311 }
312
313 if (!test_format_unorm8(verbose, fp, format_desc)) {
314 success = FALSE;
315 }
316
317 return success;
318 }
319
320
321 boolean
322 test_all(unsigned verbose, FILE *fp)
323 {
324 enum pipe_format format;
325 boolean success = TRUE;
326
327 util_format_s3tc_init();
328
329 for (format = 1; format < PIPE_FORMAT_COUNT; ++format) {
330 const struct util_format_description *format_desc;
331
332 format_desc = util_format_description(format);
333 if (!format_desc) {
334 continue;
335 }
336
337 /*
338 * TODO: test more
339 */
340
341 if (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) {
342 continue;
343 }
344
345 if (format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC &&
346 !util_format_s3tc_enabled) {
347 continue;
348 }
349
350 if (!test_one(verbose, fp, format_desc)) {
351 success = FALSE;
352 }
353 }
354
355 return success;
356 }
357
358
359 boolean
360 test_some(unsigned verbose, FILE *fp, unsigned long n)
361 {
362 return test_all(verbose, fp);
363 }
364
365
366 boolean
367 test_single(unsigned verbose, FILE *fp)
368 {
369 printf("no test_single()");
370 return TRUE;
371 }