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"
37 #include "gallivm/lp_bld.h"
38 #include "gallivm/lp_bld_debug.h"
39 #include "gallivm/lp_bld_init.h"
40 #include "gallivm/lp_bld_arit.h"
46 write_tsv_header(FILE *fp
)
56 typedef void (*unary_func_t
)(float *out
, const float *in
);
60 * Describe a test case of one unary function.
65 * Test name -- name of the mathematical function under test.
71 (*builder
)(struct lp_build_context
*bld
, LLVMValueRef a
);
74 * Reference (pure-C) function.
86 * Required precision in bits.
92 static float negf(float x
)
98 static float sgnf(float x
)
110 const float exp2_values
[] = {
129 const float log2_values
[] = {
132 * Smallest denormalized number; meant just for experimentation, but not
135 1.4012984643248171e-45,
153 static float rcpf(float x
)
159 const float rcp_values
[] = {
166 5.88e-39f
, // denormal
167 #if (__STDC_VERSION__ >= 199901L)
173 static float rsqrtf(float x
)
175 return 1.0/(float)sqrt(x
);
179 const float rsqrt_values
[] = {
180 // http://msdn.microsoft.com/en-us/library/windows/desktop/bb147346.aspx
181 0.0, // must yield infinity
182 1.0, // must yield 1.0
185 5.88e-39f
, // denormal
186 #if (__STDC_VERSION__ >= 199901L)
192 const float sincos_values
[] = {
206 const float round_values
[] = {
207 -10.0, -1, 0.0, 12.0,
208 -1.49, -0.25, 1.25, 2.51,
209 -0.99, -0.01, 0.01, 0.99,
210 1.401298464324817e-45f
, // smallest denormal
211 -1.401298464324817e-45f
,
214 1.62981451e15f
, // large number not representable as 32bit int
218 1.0f
- 0.5f
*FLT_EPSILON
,
224 static float fractf(float x
)
228 // clamp to the largest number smaller than one
229 x
= 1.0f
- 0.5f
*FLT_EPSILON
;
235 const float fract_values
[] = {
236 // http://en.wikipedia.org/wiki/IEEE_754-1985#Examples
243 1.401298464324817e-45f
, // smallest denormal
244 -1.401298464324817e-45f
,
245 5.88e-39f
, // middle denormal
246 1.18e-38f
, // largest denormal
251 1.0f
- 0.5f
*FLT_EPSILON
,
262 static const struct unary_test_t
264 {"neg", &lp_build_negate
, &negf
, exp2_values
, Elements(exp2_values
), 20.0 },
265 {"exp2", &lp_build_exp2
, &exp2f
, exp2_values
, Elements(exp2_values
), 20.0 },
266 {"log2", &lp_build_log2
, &log2f
, log2_values
, Elements(log2_values
), 20.0 },
267 {"exp", &lp_build_exp
, &expf
, exp2_values
, Elements(exp2_values
), 18.0 },
268 {"log", &lp_build_log
, &logf
, log2_values
, Elements(log2_values
), 20.0 },
269 {"rcp", &lp_build_rcp
, &rcpf
, rcp_values
, Elements(rcp_values
), 20.0 },
270 {"rsqrt", &lp_build_rsqrt
, &rsqrtf
, rsqrt_values
, Elements(rsqrt_values
), 20.0 },
271 {"sin", &lp_build_sin
, &sinf
, sincos_values
, Elements(sincos_values
), 20.0 },
272 {"cos", &lp_build_cos
, &cosf
, sincos_values
, Elements(sincos_values
), 20.0 },
273 {"sgn", &lp_build_sgn
, &sgnf
, exp2_values
, Elements(exp2_values
), 20.0 },
274 {"round", &lp_build_round
, &roundf
, round_values
, Elements(round_values
), 24.0 },
275 {"trunc", &lp_build_trunc
, &truncf
, round_values
, Elements(round_values
), 24.0 },
276 {"floor", &lp_build_floor
, &floorf
, round_values
, Elements(round_values
), 24.0 },
277 {"ceil", &lp_build_ceil
, &ceilf
, round_values
, Elements(round_values
), 24.0 },
278 {"fract", &lp_build_fract_safe
, &fractf
, fract_values
, Elements(fract_values
), 24.0 },
283 * Build LLVM function that exercises the unary operator builder.
286 build_unary_test_func(struct gallivm_state
*gallivm
,
287 const struct unary_test_t
*test
)
289 struct lp_type type
= lp_type_float_vec(32, lp_native_vector_width
);
290 LLVMContextRef context
= gallivm
->context
;
291 LLVMModuleRef module
= gallivm
->module
;
292 LLVMTypeRef vf32t
= lp_build_vec_type(gallivm
, type
);
293 LLVMTypeRef args
[2] = { LLVMPointerType(vf32t
, 0), LLVMPointerType(vf32t
, 0) };
294 LLVMValueRef func
= LLVMAddFunction(module
, test
->name
,
295 LLVMFunctionType(LLVMVoidTypeInContext(context
),
296 args
, Elements(args
), 0));
297 LLVMValueRef arg0
= LLVMGetParam(func
, 0);
298 LLVMValueRef arg1
= LLVMGetParam(func
, 1);
299 LLVMBuilderRef builder
= gallivm
->builder
;
300 LLVMBasicBlockRef block
= LLVMAppendBasicBlockInContext(context
, func
, "entry");
303 struct lp_build_context bld
;
305 lp_build_context_init(&bld
, gallivm
, type
);
307 LLVMSetFunctionCallConv(func
, LLVMCCallConv
);
309 LLVMPositionBuilderAtEnd(builder
, block
);
311 arg1
= LLVMBuildLoad(builder
, arg1
, "");
313 ret
= test
->builder(&bld
, arg1
);
315 LLVMBuildStore(builder
, ret
, arg0
);
317 LLVMBuildRetVoid(builder
);
319 gallivm_verify_function(gallivm
, func
);
326 * Test one LLVM unary arithmetic builder function.
329 test_unary(unsigned verbose
, FILE *fp
, const struct unary_test_t
*test
)
331 struct gallivm_state
*gallivm
;
332 LLVMValueRef test_func
;
333 unary_func_t test_func_jit
;
334 boolean success
= TRUE
;
336 int length
= lp_native_vector_width
/ 32;
339 in
= align_malloc(length
* 4, length
* 4);
340 out
= align_malloc(length
* 4, length
* 4);
342 /* random NaNs or 0s could wreak havoc */
343 for (i
= 0; i
< length
; i
++) {
347 gallivm
= gallivm_create();
349 test_func
= build_unary_test_func(gallivm
, test
);
351 gallivm_compile_module(gallivm
);
353 test_func_jit
= (unary_func_t
) gallivm_jit_function(gallivm
, test_func
);
355 for (j
= 0; j
< (test
->num_values
+ length
- 1) / length
; j
++) {
356 int num_vals
= ((j
+ 1) * length
<= test
->num_values
) ? length
:
357 test
->num_values
% length
;
359 for (i
= 0; i
< num_vals
; ++i
) {
360 in
[i
] = test
->values
[i
+j
*length
];
363 test_func_jit(out
, in
);
364 for (i
= 0; i
< num_vals
; ++i
) {
365 float ref
= test
->ref(in
[i
]);
366 double error
, precision
;
369 if (util_inf_sign(ref
) && util_inf_sign(out
[i
]) == util_inf_sign(ref
)) {
372 error
= fabs(out
[i
] - ref
);
374 precision
= error
? -log2(error
/fabs(ref
)) : FLT_MANT_DIG
;
376 pass
= precision
>= test
->precision
;
382 if (!pass
|| verbose
) {
383 printf("%s(%.9g): ref = %.9g, out = %.9g, precision = %f bits, %s\n",
384 test
->name
, in
[i
], ref
, out
[i
], precision
,
385 pass
? "PASS" : "FAIL");
394 gallivm_free_function(gallivm
, test_func
, test_func_jit
);
396 gallivm_destroy(gallivm
);
406 test_all(unsigned verbose
, FILE *fp
)
408 boolean success
= TRUE
;
411 for (i
= 0; i
< Elements(unary_tests
); ++i
) {
412 if (!test_unary(verbose
, fp
, &unary_tests
[i
])) {
422 test_some(unsigned verbose
, FILE *fp
,
426 * Not randomly generated test cases, so test all.
429 return test_all(verbose
, fp
);
434 test_single(unsigned verbose
, FILE *fp
)