tests: Updated tests to properly handle NaN for half floats.
[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 "util/u_memory.h"
34 #include "util/u_pointer.h"
35 #include "util/u_string.h"
36 #include "util/u_format.h"
37 #include "util/u_format_tests.h"
38 #include "util/u_format_s3tc.h"
39
40 #include "gallivm/lp_bld.h"
41 #include "gallivm/lp_bld_debug.h"
42 #include "gallivm/lp_bld_format.h"
43 #include "gallivm/lp_bld_init.h"
44
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)(void *unpacked, const void *packed,
74 unsigned i, unsigned j);
75
76
77 static LLVMValueRef
78 add_fetch_rgba_test(struct gallivm_state *gallivm, unsigned verbose,
79 const struct util_format_description *desc,
80 struct lp_type type)
81 {
82 char name[256];
83 LLVMContextRef context = gallivm->context;
84 LLVMModuleRef module = gallivm->module;
85 LLVMBuilderRef builder = gallivm->builder;
86 LLVMPassManagerRef passmgr = gallivm->passmgr;
87 LLVMTypeRef args[4];
88 LLVMValueRef func;
89 LLVMValueRef packed_ptr;
90 LLVMValueRef offset = LLVMConstNull(LLVMInt32TypeInContext(context));
91 LLVMValueRef rgba_ptr;
92 LLVMValueRef i;
93 LLVMValueRef j;
94 LLVMBasicBlockRef block;
95 LLVMValueRef rgba;
96
97 util_snprintf(name, sizeof name, "fetch_%s_%s", desc->short_name,
98 type.floating ? "float" : "unorm8");
99
100 args[0] = LLVMPointerType(lp_build_vec_type(gallivm, type), 0);
101 args[1] = LLVMPointerType(LLVMInt8TypeInContext(context), 0);
102 args[3] = args[2] = LLVMInt32TypeInContext(context);
103
104 func = LLVMAddFunction(module, name,
105 LLVMFunctionType(LLVMVoidTypeInContext(context),
106 args, Elements(args), 0));
107 LLVMSetFunctionCallConv(func, LLVMCCallConv);
108 rgba_ptr = LLVMGetParam(func, 0);
109 packed_ptr = LLVMGetParam(func, 1);
110 i = LLVMGetParam(func, 2);
111 j = LLVMGetParam(func, 3);
112
113 block = LLVMAppendBasicBlockInContext(context, func, "entry");
114 LLVMPositionBuilderAtEnd(builder, block);
115
116 rgba = lp_build_fetch_rgba_aos(gallivm, desc, type,
117 packed_ptr, offset, i, j);
118
119 LLVMBuildStore(builder, rgba, rgba_ptr);
120
121 LLVMBuildRetVoid(builder);
122
123 if (LLVMVerifyFunction(func, LLVMPrintMessageAction)) {
124 LLVMDumpValue(func);
125 abort();
126 }
127
128 LLVMRunFunctionPassManager(passmgr, 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(struct gallivm_state *gallivm, unsigned verbose, FILE *fp,
141 const struct util_format_description *desc)
142 {
143 LLVMValueRef fetch = NULL;
144 LLVMExecutionEngineRef engine = gallivm->engine;
145 fetch_ptr_t fetch_ptr;
146 PIPE_ALIGN_VAR(16) float unpacked[4];
147 boolean first = TRUE;
148 boolean success = TRUE;
149 unsigned i, j, k, l;
150 void *f;
151
152 fetch = add_fetch_rgba_test(gallivm, verbose, desc, lp_float32_vec4_type());
153
154 f = LLVMGetPointerToGlobal(engine, fetch);
155 fetch_ptr = (fetch_ptr_t) pointer_to_func(f);
156
157 if (verbose >= 2) {
158 lp_disassemble(f);
159 }
160
161 for (l = 0; l < util_format_nr_test_cases; ++l) {
162 const struct util_format_test_case *test = &util_format_test_cases[l];
163
164 if (test->format == desc->format) {
165
166 if (first) {
167 printf("Testing %s (float) ...\n",
168 desc->name);
169 first = FALSE;
170 }
171
172 for (i = 0; i < desc->block.height; ++i) {
173 for (j = 0; j < desc->block.width; ++j) {
174 boolean match;
175
176 memset(unpacked, 0, sizeof unpacked);
177
178 fetch_ptr(unpacked, test->packed, j, i);
179
180 match = TRUE;
181 for(k = 0; k < 4; ++k)
182 if (fabs((float)test->unpacked[i][j][k] - unpacked[k]) > FLT_EPSILON)
183 match = FALSE;
184
185 if (!match) {
186 printf("FAILED\n");
187 printf(" Packed: %02x %02x %02x %02x\n",
188 test->packed[0], test->packed[1], test->packed[2], test->packed[3]);
189 printf(" Unpacked (%u,%u): %f %f %f %f obtained\n",
190 j, i,
191 unpacked[0], unpacked[1], unpacked[2], unpacked[3]);
192 printf(" %f %f %f %f expected\n",
193 test->unpacked[i][j][0],
194 test->unpacked[i][j][1],
195 test->unpacked[i][j][2],
196 test->unpacked[i][j][3]);
197 success = FALSE;
198 }
199 }
200 }
201 }
202 }
203
204 if (!success) {
205 if (verbose < 1) {
206 LLVMDumpValue(fetch);
207 }
208 }
209
210 LLVMFreeMachineCodeForFunction(engine, fetch);
211 LLVMDeleteFunction(fetch);
212
213 if(fp)
214 write_tsv_row(fp, desc, success);
215
216 return success;
217 }
218
219
220 PIPE_ALIGN_STACK
221 static boolean
222 test_format_unorm8(struct gallivm_state *gallivm,
223 unsigned verbose, FILE *fp,
224 const struct util_format_description *desc)
225 {
226 LLVMValueRef fetch = NULL;
227 fetch_ptr_t fetch_ptr;
228 uint8_t unpacked[4];
229 boolean first = TRUE;
230 boolean success = TRUE;
231 unsigned i, j, k, l;
232 void *f;
233
234 fetch = add_fetch_rgba_test(gallivm, verbose, desc, lp_unorm8_vec4_type());
235
236 f = LLVMGetPointerToGlobal(gallivm->engine, fetch);
237 fetch_ptr = (fetch_ptr_t) pointer_to_func(f);
238
239 if (verbose >= 2) {
240 lp_disassemble(f);
241 }
242
243 for (l = 0; l < util_format_nr_test_cases; ++l) {
244 const struct util_format_test_case *test = &util_format_test_cases[l];
245
246 if (test->format == desc->format) {
247
248 if (first) {
249 printf("Testing %s (unorm8) ...\n",
250 desc->name);
251 first = FALSE;
252 }
253
254 for (i = 0; i < desc->block.height; ++i) {
255 for (j = 0; j < desc->block.width; ++j) {
256 boolean match;
257
258 memset(unpacked, 0, sizeof unpacked);
259
260 fetch_ptr(unpacked, test->packed, j, i);
261
262 match = TRUE;
263 for(k = 0; k < 4; ++k) {
264 int error = float_to_ubyte(test->unpacked[i][j][k]) - unpacked[k];
265
266 if (util_is_double_nan(test->unpacked[i][j][k]))
267 continue;
268
269 if (error < 0)
270 error = -error;
271
272 if (error > 1)
273 match = FALSE;
274 }
275
276 if (!match) {
277 printf("FAILED\n");
278 printf(" Packed: %02x %02x %02x %02x\n",
279 test->packed[0], test->packed[1], test->packed[2], test->packed[3]);
280 printf(" Unpacked (%u,%u): %02x %02x %02x %02x obtained\n",
281 j, i,
282 unpacked[0], unpacked[1], unpacked[2], unpacked[3]);
283 printf(" %02x %02x %02x %02x expected\n",
284 float_to_ubyte(test->unpacked[i][j][0]),
285 float_to_ubyte(test->unpacked[i][j][1]),
286 float_to_ubyte(test->unpacked[i][j][2]),
287 float_to_ubyte(test->unpacked[i][j][3]));
288 success = FALSE;
289 }
290 }
291 }
292 }
293 }
294
295 if (!success)
296 LLVMDumpValue(fetch);
297
298 LLVMFreeMachineCodeForFunction(gallivm->engine, fetch);
299 LLVMDeleteFunction(fetch);
300
301 if(fp)
302 write_tsv_row(fp, desc, success);
303
304 return success;
305 }
306
307
308
309
310 static boolean
311 test_one(struct gallivm_state *gallivm,
312 unsigned verbose, FILE *fp,
313 const struct util_format_description *format_desc)
314 {
315 boolean success = TRUE;
316
317 if (!test_format_float(gallivm, verbose, fp, format_desc)) {
318 success = FALSE;
319 }
320
321 if (!test_format_unorm8(gallivm, verbose, fp, format_desc)) {
322 success = FALSE;
323 }
324
325 return success;
326 }
327
328
329 boolean
330 test_all(struct gallivm_state *gallivm, unsigned verbose, FILE *fp)
331 {
332 enum pipe_format format;
333 boolean success = TRUE;
334
335 util_format_s3tc_init();
336
337 for (format = 1; format < PIPE_FORMAT_COUNT; ++format) {
338 const struct util_format_description *format_desc;
339
340 format_desc = util_format_description(format);
341 if (!format_desc) {
342 continue;
343 }
344
345
346 /*
347 * TODO: test more
348 */
349
350 if (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) {
351 continue;
352 }
353
354 if (util_format_is_pure_integer(format))
355 continue;
356
357 if (format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC &&
358 !util_format_s3tc_enabled) {
359 continue;
360 }
361
362 if (!test_one(gallivm, verbose, fp, format_desc)) {
363 success = FALSE;
364 }
365 }
366
367 return success;
368 }
369
370
371 boolean
372 test_some(struct gallivm_state *gallivm, unsigned verbose, FILE *fp,
373 unsigned long n)
374 {
375 return test_all(gallivm, verbose, fp);
376 }
377
378
379 boolean
380 test_single(struct gallivm_state *gallivm, unsigned verbose, FILE *fp)
381 {
382 printf("no test_single()");
383 return TRUE;
384 }