1 /**************************************************************************
3 * Copyright 2011 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_pointer.h"
34 #include "util/u_memory.h"
35 #include "util/u_math.h"
36 #include "util/u_cpu_detect.h"
38 #include "gallivm/lp_bld.h"
39 #include "gallivm/lp_bld_debug.h"
40 #include "gallivm/lp_bld_init.h"
41 #include "gallivm/lp_bld_arit.h"
47 write_tsv_header(FILE *fp
)
57 typedef void (*unary_func_t
)(float *out
, const float *in
);
61 * Describe a test case of one unary function.
66 * Test name -- name of the mathematical function under test.
72 (*builder
)(struct lp_build_context
*bld
, LLVMValueRef a
);
75 * Reference (pure-C) function.
87 * Required precision in bits.
93 static float negf(float x
)
99 static float sgnf(float x
)
111 const float sgn_values
[] = {
133 const float exp2_values
[] = {
155 const float log2_values
[] = {
158 * Smallest denormalized number; meant just for experimentation, but not
161 1.4012984643248171e-45,
183 static float rcpf(float x
)
189 const float rcp_values
[] = {
196 5.88e-39f
, // denormal
197 #if (__STDC_VERSION__ >= 199901L)
203 static float rsqrtf(float x
)
205 return 1.0/(float)sqrt(x
);
209 const float rsqrt_values
[] = {
210 // http://msdn.microsoft.com/en-us/library/windows/desktop/bb147346.aspx
211 0.0, // must yield infinity
212 1.0, // must yield 1.0
215 5.88e-39f
, // denormal
216 #if (__STDC_VERSION__ >= 199901L)
222 const float sincos_values
[] = {
239 const float round_values
[] = {
240 -10.0, -1, 0.0, 12.0,
241 -1.49, -0.25, 1.25, 2.51,
242 -0.99, -0.01, 0.01, 0.99,
243 -1.5, -0.5, 0.5, 1.5,
244 1.401298464324817e-45f
, // smallest denormal
245 -1.401298464324817e-45f
,
248 1.62981451e15f
, // large number not representable as 32bit int
252 1.0f
- 0.5f
*FLT_EPSILON
,
258 static float fractf(float x
)
262 // clamp to the largest number smaller than one
263 x
= 1.0f
- 0.5f
*FLT_EPSILON
;
269 const float fract_values
[] = {
270 // http://en.wikipedia.org/wiki/IEEE_754-1985#Examples
277 1.401298464324817e-45f
, // smallest denormal
278 -1.401298464324817e-45f
,
279 5.88e-39f
, // middle denormal
280 1.18e-38f
, // largest denormal
285 1.0f
- 0.5f
*FLT_EPSILON
,
296 static const struct unary_test_t
298 {"abs", &lp_build_abs
, &fabsf
, sgn_values
, ARRAY_SIZE(sgn_values
), 20.0 },
299 {"neg", &lp_build_negate
, &negf
, sgn_values
, ARRAY_SIZE(sgn_values
), 20.0 },
300 {"sgn", &lp_build_sgn
, &sgnf
, sgn_values
, ARRAY_SIZE(sgn_values
), 20.0 },
301 {"exp2", &lp_build_exp2
, &exp2f
, exp2_values
, ARRAY_SIZE(exp2_values
), 18.0 },
302 {"log2", &lp_build_log2_safe
, &log2f
, log2_values
, ARRAY_SIZE(log2_values
), 20.0 },
303 {"exp", &lp_build_exp
, &expf
, exp2_values
, ARRAY_SIZE(exp2_values
), 18.0 },
304 {"log", &lp_build_log_safe
, &logf
, log2_values
, ARRAY_SIZE(log2_values
), 20.0 },
305 {"rcp", &lp_build_rcp
, &rcpf
, rcp_values
, ARRAY_SIZE(rcp_values
), 20.0 },
306 {"rsqrt", &lp_build_rsqrt
, &rsqrtf
, rsqrt_values
, ARRAY_SIZE(rsqrt_values
), 20.0 },
307 {"sin", &lp_build_sin
, &sinf
, sincos_values
, ARRAY_SIZE(sincos_values
), 20.0 },
308 {"cos", &lp_build_cos
, &cosf
, sincos_values
, ARRAY_SIZE(sincos_values
), 20.0 },
309 {"sgn", &lp_build_sgn
, &sgnf
, sgn_values
, ARRAY_SIZE(sgn_values
), 20.0 },
310 {"round", &lp_build_round
, &nearbyintf
, round_values
, ARRAY_SIZE(round_values
), 24.0 },
311 {"trunc", &lp_build_trunc
, &truncf
, round_values
, ARRAY_SIZE(round_values
), 24.0 },
312 {"floor", &lp_build_floor
, &floorf
, round_values
, ARRAY_SIZE(round_values
), 24.0 },
313 {"ceil", &lp_build_ceil
, &ceilf
, round_values
, ARRAY_SIZE(round_values
), 24.0 },
314 {"fract", &lp_build_fract_safe
, &fractf
, fract_values
, ARRAY_SIZE(fract_values
), 24.0 },
319 * Build LLVM function that exercises the unary operator builder.
322 build_unary_test_func(struct gallivm_state
*gallivm
,
323 const struct unary_test_t
*test
,
325 const char *test_name
)
327 struct lp_type type
= lp_type_float_vec(32, length
* 32);
328 LLVMContextRef context
= gallivm
->context
;
329 LLVMModuleRef module
= gallivm
->module
;
330 LLVMTypeRef vf32t
= lp_build_vec_type(gallivm
, type
);
331 LLVMTypeRef args
[2] = { LLVMPointerType(vf32t
, 0), LLVMPointerType(vf32t
, 0) };
332 LLVMValueRef func
= LLVMAddFunction(module
, test_name
,
333 LLVMFunctionType(LLVMVoidTypeInContext(context
),
334 args
, ARRAY_SIZE(args
), 0));
335 LLVMValueRef arg0
= LLVMGetParam(func
, 0);
336 LLVMValueRef arg1
= LLVMGetParam(func
, 1);
337 LLVMBuilderRef builder
= gallivm
->builder
;
338 LLVMBasicBlockRef block
= LLVMAppendBasicBlockInContext(context
, func
, "entry");
341 struct lp_build_context bld
;
343 lp_build_context_init(&bld
, gallivm
, type
);
345 LLVMSetFunctionCallConv(func
, LLVMCCallConv
);
347 LLVMPositionBuilderAtEnd(builder
, block
);
349 arg1
= LLVMBuildLoad(builder
, arg1
, "");
351 ret
= test
->builder(&bld
, arg1
);
353 LLVMBuildStore(builder
, ret
, arg0
);
355 LLVMBuildRetVoid(builder
);
357 gallivm_verify_function(gallivm
, func
);
364 * Flush denorms to zero.
367 flush_denorm_to_zero(float val
)
370 * If we have a denorm manually set it to (+-)0.
371 * This is because the reference may or may not do the right thing
372 * otherwise because we want the result according to treating all
373 * denormals as zero (FTZ/DAZ). Not using fpclassify because
374 * a) some compilers are stuck at c89 (msvc)
375 * b) not sure it reliably works with non-standard ftz/daz mode
376 * And, right now we only disable denorms with jited code on x86/sse
377 * (albeit this should be classified as a bug) so to get results which
378 * match we must only flush them to zero here in that case too.
384 #if defined(PIPE_ARCH_SSE)
385 if (util_cpu_caps
.has_sse
) {
386 if ((fi_val
.ui
& 0x7f800000) == 0) {
387 fi_val
.ui
&= 0xff800000;
396 * Test one LLVM unary arithmetic builder function.
399 test_unary(unsigned verbose
, FILE *fp
, const struct unary_test_t
*test
, unsigned length
)
402 util_snprintf(test_name
, sizeof test_name
, "%s.v%u", test
->name
, length
);
403 LLVMContextRef context
;
404 struct gallivm_state
*gallivm
;
405 LLVMValueRef test_func
;
406 unary_func_t test_func_jit
;
407 boolean success
= TRUE
;
411 in
= align_malloc(length
* 4, length
* 4);
412 out
= align_malloc(length
* 4, length
* 4);
414 /* random NaNs or 0s could wreak havoc */
415 for (i
= 0; i
< length
; i
++) {
419 context
= LLVMContextCreate();
420 gallivm
= gallivm_create("test_module", context
);
422 test_func
= build_unary_test_func(gallivm
, test
, length
, test_name
);
424 gallivm_compile_module(gallivm
);
426 test_func_jit
= (unary_func_t
) gallivm_jit_function(gallivm
, test_func
);
428 gallivm_free_ir(gallivm
);
430 for (j
= 0; j
< (test
->num_values
+ length
- 1) / length
; j
++) {
431 int num_vals
= ((j
+ 1) * length
<= test
->num_values
) ? length
:
432 test
->num_values
% length
;
434 for (i
= 0; i
< num_vals
; ++i
) {
435 in
[i
] = test
->values
[i
+j
*length
];
438 test_func_jit(out
, in
);
439 for (i
= 0; i
< num_vals
; ++i
) {
441 double error
, precision
;
442 boolean expected_pass
= TRUE
;
445 testval
= flush_denorm_to_zero(in
[i
]);
446 ref
= flush_denorm_to_zero(test
->ref(testval
));
448 if (util_inf_sign(ref
) && util_inf_sign(out
[i
]) == util_inf_sign(ref
)) {
451 error
= fabs(out
[i
] - ref
);
453 precision
= error
? -log2(error
/fabs(ref
)) : FLT_MANT_DIG
;
455 pass
= precision
>= test
->precision
;
461 if (test
->ref
== &nearbyintf
&& length
== 2 &&
462 ref
!= roundf(testval
)) {
463 /* FIXME: The generic (non SSE) path in lp_build_iround, which is
464 * always taken for length==2 regardless of native round support,
465 * does not round to even. */
466 expected_pass
= FALSE
;
469 if (test
->ref
== &expf
&& util_inf_sign(testval
) == -1) {
470 /* XXX: 64bits MSVCRT's expf(-inf) returns -inf instead of 0 */
471 #if defined(_MSC_VER) && defined(_WIN64)
472 expected_pass
= FALSE
;
476 if (pass
!= expected_pass
|| verbose
) {
477 printf("%s(%.9g): ref = %.9g, out = %.9g, precision = %f bits, %s%s\n",
478 test_name
, in
[i
], ref
, out
[i
], precision
,
479 pass
? "PASS" : "FAIL",
480 !expected_pass
? (pass
? " (unexpected)" : " (expected)" ): "");
484 if (pass
!= expected_pass
) {
490 gallivm_destroy(gallivm
);
491 LLVMContextDispose(context
);
501 test_all(unsigned verbose
, FILE *fp
)
503 boolean success
= TRUE
;
506 for (i
= 0; i
< ARRAY_SIZE(unary_tests
); ++i
) {
507 unsigned max_length
= lp_native_vector_width
/ 32;
509 for (length
= 1; length
<= max_length
; length
*= 2) {
510 if (!test_unary(verbose
, fp
, &unary_tests
[i
], length
)) {
521 test_some(unsigned verbose
, FILE *fp
,
525 * Not randomly generated test cases, so test all.
528 return test_all(verbose
, fp
);
533 test_single(unsigned verbose
, FILE *fp
)