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 exp2_values
[] = {
133 const float log2_values
[] = {
136 * Smallest denormalized number; meant just for experimentation, but not
139 1.4012984643248171e-45,
161 static float rcpf(float x
)
167 const float rcp_values
[] = {
174 5.88e-39f
, // denormal
175 #if (__STDC_VERSION__ >= 199901L)
181 static float rsqrtf(float x
)
183 return 1.0/(float)sqrt(x
);
187 const float rsqrt_values
[] = {
188 // http://msdn.microsoft.com/en-us/library/windows/desktop/bb147346.aspx
189 0.0, // must yield infinity
190 1.0, // must yield 1.0
193 5.88e-39f
, // denormal
194 #if (__STDC_VERSION__ >= 199901L)
200 const float sincos_values
[] = {
217 const float round_values
[] = {
218 -10.0, -1, 0.0, 12.0,
219 -1.49, -0.25, 1.25, 2.51,
220 -0.99, -0.01, 0.01, 0.99,
221 -1.5, -0.5, 0.5, 1.5,
222 1.401298464324817e-45f
, // smallest denormal
223 -1.401298464324817e-45f
,
226 1.62981451e15f
, // large number not representable as 32bit int
230 1.0f
- 0.5f
*FLT_EPSILON
,
236 static float fractf(float x
)
240 // clamp to the largest number smaller than one
241 x
= 1.0f
- 0.5f
*FLT_EPSILON
;
247 const float fract_values
[] = {
248 // http://en.wikipedia.org/wiki/IEEE_754-1985#Examples
255 1.401298464324817e-45f
, // smallest denormal
256 -1.401298464324817e-45f
,
257 5.88e-39f
, // middle denormal
258 1.18e-38f
, // largest denormal
263 1.0f
- 0.5f
*FLT_EPSILON
,
274 static const struct unary_test_t
276 {"abs", &lp_build_abs
, &fabsf
, exp2_values
, Elements(exp2_values
), 20.0 },
277 {"neg", &lp_build_negate
, &negf
, exp2_values
, Elements(exp2_values
), 20.0 },
278 {"exp2", &lp_build_exp2
, &exp2f
, exp2_values
, Elements(exp2_values
), 20.0 },
279 {"log2", &lp_build_log2_safe
, &log2f
, log2_values
, Elements(log2_values
), 20.0 },
280 {"exp", &lp_build_exp
, &expf
, exp2_values
, Elements(exp2_values
), 18.0 },
281 {"log", &lp_build_log_safe
, &logf
, log2_values
, Elements(log2_values
), 20.0 },
282 {"rcp", &lp_build_rcp
, &rcpf
, rcp_values
, Elements(rcp_values
), 20.0 },
283 {"rsqrt", &lp_build_rsqrt
, &rsqrtf
, rsqrt_values
, Elements(rsqrt_values
), 20.0 },
284 {"sin", &lp_build_sin
, &sinf
, sincos_values
, Elements(sincos_values
), 20.0 },
285 {"cos", &lp_build_cos
, &cosf
, sincos_values
, Elements(sincos_values
), 20.0 },
286 {"sgn", &lp_build_sgn
, &sgnf
, exp2_values
, Elements(exp2_values
), 20.0 },
287 {"round", &lp_build_round
, &nearbyintf
, round_values
, Elements(round_values
), 24.0 },
288 {"trunc", &lp_build_trunc
, &truncf
, round_values
, Elements(round_values
), 24.0 },
289 {"floor", &lp_build_floor
, &floorf
, round_values
, Elements(round_values
), 24.0 },
290 {"ceil", &lp_build_ceil
, &ceilf
, round_values
, Elements(round_values
), 24.0 },
291 {"fract", &lp_build_fract_safe
, &fractf
, fract_values
, Elements(fract_values
), 24.0 },
296 * Build LLVM function that exercises the unary operator builder.
299 build_unary_test_func(struct gallivm_state
*gallivm
,
300 const struct unary_test_t
*test
)
302 struct lp_type type
= lp_type_float_vec(32, lp_native_vector_width
);
303 LLVMContextRef context
= gallivm
->context
;
304 LLVMModuleRef module
= gallivm
->module
;
305 LLVMTypeRef vf32t
= lp_build_vec_type(gallivm
, type
);
306 LLVMTypeRef args
[2] = { LLVMPointerType(vf32t
, 0), LLVMPointerType(vf32t
, 0) };
307 LLVMValueRef func
= LLVMAddFunction(module
, test
->name
,
308 LLVMFunctionType(LLVMVoidTypeInContext(context
),
309 args
, Elements(args
), 0));
310 LLVMValueRef arg0
= LLVMGetParam(func
, 0);
311 LLVMValueRef arg1
= LLVMGetParam(func
, 1);
312 LLVMBuilderRef builder
= gallivm
->builder
;
313 LLVMBasicBlockRef block
= LLVMAppendBasicBlockInContext(context
, func
, "entry");
316 struct lp_build_context bld
;
318 lp_build_context_init(&bld
, gallivm
, type
);
320 LLVMSetFunctionCallConv(func
, LLVMCCallConv
);
322 LLVMPositionBuilderAtEnd(builder
, block
);
324 arg1
= LLVMBuildLoad(builder
, arg1
, "");
326 ret
= test
->builder(&bld
, arg1
);
328 LLVMBuildStore(builder
, ret
, arg0
);
330 LLVMBuildRetVoid(builder
);
332 gallivm_verify_function(gallivm
, func
);
339 * Flush denorms to zero.
342 flush_denorm_to_zero(float val
)
345 * If we have a denorm manually set it to (+-)0.
346 * This is because the reference may or may not do the right thing
347 * otherwise because we want the result according to treating all
348 * denormals as zero (FTZ/DAZ). Not using fpclassify because
349 * a) some compilers are stuck at c89 (msvc)
350 * b) not sure it reliably works with non-standard ftz/daz mode
351 * And, right now we only disable denorms with jited code on x86/sse
352 * (albeit this should be classified as a bug) so to get results which
353 * match we must only flush them to zero here in that case too.
359 #if defined(PIPE_ARCH_SSE)
360 if (util_cpu_caps
.has_sse
) {
361 if ((fi_val
.ui
& 0x7f800000) == 0) {
362 fi_val
.ui
&= 0xff800000;
371 * Test one LLVM unary arithmetic builder function.
374 test_unary(unsigned verbose
, FILE *fp
, const struct unary_test_t
*test
)
376 struct gallivm_state
*gallivm
;
377 LLVMValueRef test_func
;
378 unary_func_t test_func_jit
;
379 boolean success
= TRUE
;
381 int length
= lp_native_vector_width
/ 32;
384 in
= align_malloc(length
* 4, length
* 4);
385 out
= align_malloc(length
* 4, length
* 4);
387 /* random NaNs or 0s could wreak havoc */
388 for (i
= 0; i
< length
; i
++) {
392 gallivm
= gallivm_create("test_module", LLVMGetGlobalContext());
394 test_func
= build_unary_test_func(gallivm
, test
);
396 gallivm_compile_module(gallivm
);
398 test_func_jit
= (unary_func_t
) gallivm_jit_function(gallivm
, test_func
);
400 gallivm_free_ir(gallivm
);
402 for (j
= 0; j
< (test
->num_values
+ length
- 1) / length
; j
++) {
403 int num_vals
= ((j
+ 1) * length
<= test
->num_values
) ? length
:
404 test
->num_values
% length
;
406 for (i
= 0; i
< num_vals
; ++i
) {
407 in
[i
] = test
->values
[i
+j
*length
];
410 test_func_jit(out
, in
);
411 for (i
= 0; i
< num_vals
; ++i
) {
413 double error
, precision
;
416 testval
= flush_denorm_to_zero(in
[i
]);
417 ref
= flush_denorm_to_zero(test
->ref(testval
));
419 if (util_inf_sign(ref
) && util_inf_sign(out
[i
]) == util_inf_sign(ref
)) {
422 error
= fabs(out
[i
] - ref
);
424 precision
= error
? -log2(error
/fabs(ref
)) : FLT_MANT_DIG
;
426 pass
= precision
>= test
->precision
;
432 if (!pass
|| verbose
) {
433 printf("%s(%.9g): ref = %.9g, out = %.9g, precision = %f bits, %s\n",
434 test
->name
, in
[i
], ref
, out
[i
], precision
,
435 pass
? "PASS" : "FAIL");
445 gallivm_destroy(gallivm
);
455 test_all(unsigned verbose
, FILE *fp
)
457 boolean success
= TRUE
;
460 for (i
= 0; i
< Elements(unary_tests
); ++i
) {
461 if (!test_unary(verbose
, fp
, &unary_tests
[i
])) {
471 test_some(unsigned verbose
, FILE *fp
,
475 * Not randomly generated test cases, so test all.
478 return test_all(verbose
, fp
);
483 test_single(unsigned verbose
, FILE *fp
)