llvmpipe: s/bool/boolean/ in test progs
[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_init.h"
35 #include <llvm-c/Analysis.h>
36 #include <llvm-c/Target.h>
37 #include <llvm-c/Transforms/Scalar.h>
38
39 #include "util/u_memory.h"
40 #include "util/u_format.h"
41 #include "util/u_format_tests.h"
42 #include "util/u_format_s3tc.h"
43
44 #include "gallivm/lp_bld_format.h"
45 #include "lp_test.h"
46
47
48 void
49 write_tsv_header(FILE *fp)
50 {
51 fprintf(fp,
52 "result\t"
53 "format\n");
54
55 fflush(fp);
56 }
57
58
59 static void
60 write_tsv_row(FILE *fp,
61 const struct util_format_description *desc,
62 boolean success)
63 {
64 fprintf(fp, "%s\t", success ? "pass" : "fail");
65
66 fprintf(fp, "%s\n", desc->name);
67
68 fflush(fp);
69 }
70
71
72 typedef void
73 (*fetch_ptr_t)(float *, const void *packed,
74 unsigned i, unsigned j);
75
76
77 static LLVMValueRef
78 add_fetch_rgba_test(LLVMModuleRef lp_build_module,
79 const struct util_format_description *desc)
80 {
81 LLVMTypeRef args[4];
82 LLVMValueRef func;
83 LLVMValueRef packed_ptr;
84 LLVMValueRef rgba_ptr;
85 LLVMValueRef i;
86 LLVMValueRef j;
87 LLVMBasicBlockRef block;
88 LLVMBuilderRef builder;
89 LLVMValueRef rgba;
90
91 args[0] = LLVMPointerType(LLVMVectorType(LLVMFloatType(), 4), 0);
92 args[1] = LLVMPointerType(LLVMInt8Type(), 0);
93 args[3] = args[2] = LLVMInt32Type();
94
95 func = LLVMAddFunction(lp_build_module, "fetch", LLVMFunctionType(LLVMVoidType(), args, Elements(args), 0));
96 LLVMSetFunctionCallConv(func, LLVMCCallConv);
97 rgba_ptr = LLVMGetParam(func, 0);
98 packed_ptr = LLVMGetParam(func, 1);
99 i = LLVMGetParam(func, 2);
100 j = LLVMGetParam(func, 3);
101
102 block = LLVMAppendBasicBlock(func, "entry");
103 builder = LLVMCreateBuilder();
104 LLVMPositionBuilderAtEnd(builder, block);
105
106 rgba = lp_build_fetch_rgba_aos(builder, desc, packed_ptr, i, j);
107
108 LLVMBuildStore(builder, rgba, rgba_ptr);
109
110 LLVMBuildRetVoid(builder);
111
112 LLVMDisposeBuilder(builder);
113 return func;
114 }
115
116
117 PIPE_ALIGN_STACK
118 static boolean
119 test_format(unsigned verbose, FILE *fp,
120 const struct util_format_description *desc,
121 const struct util_format_test_case *test)
122 {
123 LLVMValueRef fetch = NULL;
124 LLVMPassManagerRef pass = NULL;
125 fetch_ptr_t fetch_ptr;
126 float unpacked[4];
127 boolean success;
128 unsigned i, j, k;
129
130 fetch = add_fetch_rgba_test(lp_build_module, desc);
131
132 if (LLVMVerifyFunction(fetch, LLVMPrintMessageAction)) {
133 LLVMDumpValue(fetch);
134 abort();
135 }
136
137 #if 0
138 pass = LLVMCreatePassManager();
139 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(lp_build_engine), pass);
140 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
141 * but there are more on SVN. */
142 LLVMAddConstantPropagationPass(pass);
143 LLVMAddInstructionCombiningPass(pass);
144 LLVMAddPromoteMemoryToRegisterPass(pass);
145 LLVMAddGVNPass(pass);
146 LLVMAddCFGSimplificationPass(pass);
147 LLVMRunPassManager(pass, lp_build_module);
148 #else
149 (void)pass;
150 #endif
151
152 fetch_ptr = (fetch_ptr_t) LLVMGetPointerToGlobal(lp_build_engine, fetch);
153
154 for (i = 0; i < desc->block.height; ++i) {
155 for (j = 0; j < desc->block.width; ++j) {
156
157 memset(unpacked, 0, sizeof unpacked);
158
159 fetch_ptr(unpacked, test->packed, j, i);
160
161 success = TRUE;
162 for(k = 0; k < 4; ++k)
163 if (fabs((float)test->unpacked[i][j][k] - unpacked[k]) > FLT_EPSILON)
164 success = FALSE;
165
166 if (!success) {
167 printf("FAILED\n");
168 printf(" Packed: %02x %02x %02x %02x\n",
169 test->packed[0], test->packed[1], test->packed[2], test->packed[3]);
170 printf(" Unpacked (%u,%u): %f %f %f %f obtained\n",
171 j, i,
172 unpacked[0], unpacked[1], unpacked[2], unpacked[3]);
173 printf(" %f %f %f %f expected\n",
174 test->unpacked[i][j][0],
175 test->unpacked[i][j][1],
176 test->unpacked[i][j][2],
177 test->unpacked[i][j][3]);
178 }
179 }
180 }
181
182 if (!success)
183 LLVMDumpValue(fetch);
184
185 LLVMFreeMachineCodeForFunction(lp_build_engine, fetch);
186 LLVMDeleteFunction(fetch);
187
188 if(pass)
189 LLVMDisposePassManager(pass);
190
191 if(fp)
192 write_tsv_row(fp, desc, success);
193
194 return success;
195 }
196
197
198
199 static boolean
200 test_one(unsigned verbose, FILE *fp,
201 const struct util_format_description *format_desc)
202 {
203 unsigned i;
204 boolean first = TRUE;
205 boolean success = TRUE;
206
207 for (i = 0; i < util_format_nr_test_cases; ++i) {
208 const struct util_format_test_case *test = &util_format_test_cases[i];
209
210 if (test->format == format_desc->format) {
211
212 if (first) {
213 printf("Testing %s ...\n",
214 format_desc->name);
215 first = FALSE;
216 }
217
218 if (!test_format(verbose, fp, format_desc, test)) {
219 success = FALSE;
220 }
221 }
222 }
223
224 return success;
225 }
226
227
228 boolean
229 test_all(unsigned verbose, FILE *fp)
230 {
231 enum pipe_format format;
232 boolean success = TRUE;
233
234 for (format = 1; format < PIPE_FORMAT_COUNT; ++format) {
235 const struct util_format_description *format_desc;
236
237 format_desc = util_format_description(format);
238 if (!format_desc) {
239 continue;
240 }
241
242 /*
243 * TODO: test more
244 */
245
246 if (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) {
247 continue;
248 }
249
250 if (format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC &&
251 !util_format_s3tc_enabled) {
252 continue;
253 }
254
255 if (!test_one(verbose, fp, format_desc)) {
256 success = FALSE;
257 }
258 }
259
260 return success;
261 }
262
263
264 boolean
265 test_some(unsigned verbose, FILE *fp, unsigned long n)
266 {
267 return test_all(verbose, fp);
268 }