1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
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
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 **************************************************************************/
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"
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"
49 write_tsv_header(FILE *fp
)
60 write_tsv_row(FILE *fp
,
61 const struct util_format_description
*desc
,
64 fprintf(fp
, "%s\t", success
? "pass" : "fail");
66 fprintf(fp
, "%s\n", desc
->name
);
73 (*fetch_ptr_t
)(void *unpacked
, const void *packed
,
74 unsigned i
, unsigned j
);
78 add_fetch_rgba_test(struct gallivm_state
*gallivm
, unsigned verbose
,
79 const struct util_format_description
*desc
,
83 LLVMContextRef context
= gallivm
->context
;
84 LLVMModuleRef module
= gallivm
->module
;
85 LLVMBuilderRef builder
= gallivm
->builder
;
88 LLVMValueRef packed_ptr
;
89 LLVMValueRef offset
= LLVMConstNull(LLVMInt32TypeInContext(context
));
90 LLVMValueRef rgba_ptr
;
93 LLVMBasicBlockRef block
;
96 util_snprintf(name
, sizeof name
, "fetch_%s_%s", desc
->short_name
,
97 type
.floating
? "float" : "unorm8");
99 args
[0] = LLVMPointerType(lp_build_vec_type(gallivm
, type
), 0);
100 args
[1] = LLVMPointerType(LLVMInt8TypeInContext(context
), 0);
101 args
[3] = args
[2] = LLVMInt32TypeInContext(context
);
103 func
= LLVMAddFunction(module
, name
,
104 LLVMFunctionType(LLVMVoidTypeInContext(context
),
105 args
, Elements(args
), 0));
106 LLVMSetFunctionCallConv(func
, LLVMCCallConv
);
107 rgba_ptr
= LLVMGetParam(func
, 0);
108 packed_ptr
= LLVMGetParam(func
, 1);
109 i
= LLVMGetParam(func
, 2);
110 j
= LLVMGetParam(func
, 3);
112 block
= LLVMAppendBasicBlockInContext(context
, func
, "entry");
113 LLVMPositionBuilderAtEnd(builder
, block
);
115 rgba
= lp_build_fetch_rgba_aos(gallivm
, desc
, type
, TRUE
,
116 packed_ptr
, offset
, i
, j
);
118 LLVMBuildStore(builder
, rgba
, rgba_ptr
);
120 LLVMBuildRetVoid(builder
);
122 gallivm_verify_function(gallivm
, func
);
130 test_format_float(unsigned verbose
, FILE *fp
,
131 const struct util_format_description
*desc
)
133 struct gallivm_state
*gallivm
;
134 LLVMValueRef fetch
= NULL
;
135 fetch_ptr_t fetch_ptr
;
136 PIPE_ALIGN_VAR(16) uint8_t packed
[UTIL_FORMAT_MAX_PACKED_BYTES
];
137 PIPE_ALIGN_VAR(16) float unpacked
[4];
138 boolean first
= TRUE
;
139 boolean success
= TRUE
;
142 gallivm
= gallivm_create("test_module_float", LLVMGetGlobalContext());
144 fetch
= add_fetch_rgba_test(gallivm
, verbose
, desc
, lp_float32_vec4_type());
146 gallivm_compile_module(gallivm
);
148 fetch_ptr
= (fetch_ptr_t
) gallivm_jit_function(gallivm
, fetch
);
150 gallivm_free_ir(gallivm
);
152 for (l
= 0; l
< util_format_nr_test_cases
; ++l
) {
153 const struct util_format_test_case
*test
= &util_format_test_cases
[l
];
155 if (test
->format
== desc
->format
) {
158 printf("Testing %s (float) ...\n",
164 /* To ensure it's 16-byte aligned */
165 memcpy(packed
, test
->packed
, sizeof packed
);
167 for (i
= 0; i
< desc
->block
.height
; ++i
) {
168 for (j
= 0; j
< desc
->block
.width
; ++j
) {
169 boolean match
= TRUE
;
171 memset(unpacked
, 0, sizeof unpacked
);
173 fetch_ptr(unpacked
, packed
, j
, i
);
175 for(k
= 0; k
< 4; ++k
) {
176 if (util_double_inf_sign(test
->unpacked
[i
][j
][k
]) != util_inf_sign(unpacked
[k
])) {
180 if (util_is_double_nan(test
->unpacked
[i
][j
][k
]) != util_is_nan(unpacked
[k
])) {
184 if (!util_is_double_inf_or_nan(test
->unpacked
[i
][j
][k
]) &&
185 fabs((float)test
->unpacked
[i
][j
][k
] - unpacked
[k
]) > FLT_EPSILON
) {
192 printf(" Packed: %02x %02x %02x %02x\n",
193 test
->packed
[0], test
->packed
[1], test
->packed
[2], test
->packed
[3]);
194 printf(" Unpacked (%u,%u): %.9g %.9g %.9g %.9g obtained\n",
196 unpacked
[0], unpacked
[1], unpacked
[2], unpacked
[3]);
197 printf(" %.9g %.9g %.9g %.9g expected\n",
198 test
->unpacked
[i
][j
][0],
199 test
->unpacked
[i
][j
][1],
200 test
->unpacked
[i
][j
][2],
201 test
->unpacked
[i
][j
][3]);
210 gallivm_destroy(gallivm
);
213 write_tsv_row(fp
, desc
, success
);
221 test_format_unorm8(unsigned verbose
, FILE *fp
,
222 const struct util_format_description
*desc
)
224 struct gallivm_state
*gallivm
;
225 LLVMValueRef fetch
= NULL
;
226 fetch_ptr_t fetch_ptr
;
227 PIPE_ALIGN_VAR(16) uint8_t packed
[UTIL_FORMAT_MAX_PACKED_BYTES
];
229 boolean first
= TRUE
;
230 boolean success
= TRUE
;
233 gallivm
= gallivm_create("test_module_unorm8", LLVMGetGlobalContext());
235 fetch
= add_fetch_rgba_test(gallivm
, verbose
, desc
, lp_unorm8_vec4_type());
237 gallivm_compile_module(gallivm
);
239 fetch_ptr
= (fetch_ptr_t
) gallivm_jit_function(gallivm
, fetch
);
241 gallivm_free_ir(gallivm
);
243 for (l
= 0; l
< util_format_nr_test_cases
; ++l
) {
244 const struct util_format_test_case
*test
= &util_format_test_cases
[l
];
246 if (test
->format
== desc
->format
) {
249 printf("Testing %s (unorm8) ...\n",
254 /* To ensure it's 16-byte aligned */
255 /* Could skip this and use unaligned lp_build_fetch_rgba_aos */
256 memcpy(packed
, test
->packed
, sizeof packed
);
258 for (i
= 0; i
< desc
->block
.height
; ++i
) {
259 for (j
= 0; j
< desc
->block
.width
; ++j
) {
262 memset(unpacked
, 0, sizeof unpacked
);
264 fetch_ptr(unpacked
, packed
, j
, i
);
267 for(k
= 0; k
< 4; ++k
) {
268 int error
= float_to_ubyte(test
->unpacked
[i
][j
][k
]) - unpacked
[k
];
270 if (util_is_double_nan(test
->unpacked
[i
][j
][k
]))
282 printf(" Packed: %02x %02x %02x %02x\n",
283 test
->packed
[0], test
->packed
[1], test
->packed
[2], test
->packed
[3]);
284 printf(" Unpacked (%u,%u): %02x %02x %02x %02x obtained\n",
286 unpacked
[0], unpacked
[1], unpacked
[2], unpacked
[3]);
287 printf(" %02x %02x %02x %02x expected\n",
288 float_to_ubyte(test
->unpacked
[i
][j
][0]),
289 float_to_ubyte(test
->unpacked
[i
][j
][1]),
290 float_to_ubyte(test
->unpacked
[i
][j
][2]),
291 float_to_ubyte(test
->unpacked
[i
][j
][3]));
300 gallivm_destroy(gallivm
);
303 write_tsv_row(fp
, desc
, success
);
312 test_one(unsigned verbose
, FILE *fp
,
313 const struct util_format_description
*format_desc
)
315 boolean success
= TRUE
;
317 if (!test_format_float(verbose
, fp
, format_desc
)) {
321 if (!test_format_unorm8(verbose
, fp
, format_desc
)) {
330 test_all(unsigned verbose
, FILE *fp
)
332 enum pipe_format format
;
333 boolean success
= TRUE
;
335 util_format_s3tc_init();
337 for (format
= 1; format
< PIPE_FORMAT_COUNT
; ++format
) {
338 const struct util_format_description
*format_desc
;
340 format_desc
= util_format_description(format
);
350 if (format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
) {
354 if (util_format_is_pure_integer(format
))
357 if (format_desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
&&
358 !util_format_s3tc_enabled
) {
362 if (!test_one(verbose
, fp
, format_desc
)) {
372 test_some(unsigned verbose
, FILE *fp
,
375 return test_all(verbose
, fp
);
380 test_single(unsigned verbose
, FILE *fp
)
382 printf("no test_single()");