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 rsqrtf(float x
)
159 const float rsqrt_values
[] = {
169 const float sincos_values
[] = {
183 const float round_values
[] = {
184 -10.0, -1, 0.0, 12.0,
185 -1.49, -0.25, 1.25, 2.51,
186 -0.99, -0.01, 0.01, 0.99,
189 static float fractf(float x
)
193 // clamp to the largest number smaller than one
194 x
= 1.0f
- 0.5f
*FLT_EPSILON
;
200 const float fract_values
[] = {
201 // http://en.wikipedia.org/wiki/IEEE_754-1985#Examples
208 1.401298464324817e-45f
, // smallest denormal
209 -1.401298464324817e-45f
,
210 5.88e-39f
, // middle denormal
211 1.18e-38f
, // largest denormal
216 1.0f
- 0.5f
*FLT_EPSILON
,
227 static const struct unary_test_t
229 {"neg", &lp_build_negate
, &negf
, exp2_values
, Elements(exp2_values
), 20.0 },
230 {"exp2", &lp_build_exp2
, &exp2f
, exp2_values
, Elements(exp2_values
), 20.0 },
231 {"log2", &lp_build_log2
, &log2f
, log2_values
, Elements(log2_values
), 20.0 },
232 {"exp", &lp_build_exp
, &expf
, exp2_values
, Elements(exp2_values
), 18.0 },
233 {"log", &lp_build_log
, &logf
, log2_values
, Elements(log2_values
), 20.0 },
234 {"rsqrt", &lp_build_rsqrt
, &rsqrtf
, rsqrt_values
, Elements(rsqrt_values
), 20.0 },
235 {"sin", &lp_build_sin
, &sinf
, sincos_values
, Elements(sincos_values
), 20.0 },
236 {"cos", &lp_build_cos
, &cosf
, sincos_values
, Elements(sincos_values
), 20.0 },
237 {"sgn", &lp_build_sgn
, &sgnf
, exp2_values
, Elements(exp2_values
), 20.0 },
238 {"round", &lp_build_round
, &roundf
, round_values
, Elements(round_values
), 24.0 },
239 {"trunc", &lp_build_trunc
, &truncf
, round_values
, Elements(round_values
), 24.0 },
240 {"floor", &lp_build_floor
, &floorf
, round_values
, Elements(round_values
), 24.0 },
241 {"ceil", &lp_build_ceil
, &ceilf
, round_values
, Elements(round_values
), 24.0 },
242 {"fract", &lp_build_fract_safe
, &fractf
, fract_values
, Elements(fract_values
), 24.0 },
247 * Build LLVM function that exercises the unary operator builder.
250 build_unary_test_func(struct gallivm_state
*gallivm
,
251 const struct unary_test_t
*test
)
253 struct lp_type type
= lp_type_float_vec(32, lp_native_vector_width
);
254 LLVMContextRef context
= gallivm
->context
;
255 LLVMModuleRef module
= gallivm
->module
;
256 LLVMTypeRef vf32t
= lp_build_vec_type(gallivm
, type
);
257 LLVMTypeRef args
[2] = { LLVMPointerType(vf32t
, 0), LLVMPointerType(vf32t
, 0) };
258 LLVMValueRef func
= LLVMAddFunction(module
, test
->name
,
259 LLVMFunctionType(LLVMVoidTypeInContext(context
),
260 args
, Elements(args
), 0));
261 LLVMValueRef arg0
= LLVMGetParam(func
, 0);
262 LLVMValueRef arg1
= LLVMGetParam(func
, 1);
263 LLVMBuilderRef builder
= gallivm
->builder
;
264 LLVMBasicBlockRef block
= LLVMAppendBasicBlockInContext(context
, func
, "entry");
267 struct lp_build_context bld
;
269 lp_build_context_init(&bld
, gallivm
, type
);
271 LLVMSetFunctionCallConv(func
, LLVMCCallConv
);
273 LLVMPositionBuilderAtEnd(builder
, block
);
275 arg1
= LLVMBuildLoad(builder
, arg1
, "");
277 ret
= test
->builder(&bld
, arg1
);
279 LLVMBuildStore(builder
, ret
, arg0
);
281 LLVMBuildRetVoid(builder
);
283 gallivm_verify_function(gallivm
, func
);
290 * Test one LLVM unary arithmetic builder function.
293 test_unary(unsigned verbose
, FILE *fp
, const struct unary_test_t
*test
)
295 struct gallivm_state
*gallivm
;
296 LLVMValueRef test_func
;
297 unary_func_t test_func_jit
;
298 boolean success
= TRUE
;
300 int length
= lp_native_vector_width
/ 32;
303 in
= align_malloc(length
* 4, length
* 4);
304 out
= align_malloc(length
* 4, length
* 4);
306 /* random NaNs or 0s could wreak havoc */
307 for (i
= 0; i
< length
; i
++) {
311 gallivm
= gallivm_create();
313 test_func
= build_unary_test_func(gallivm
, test
);
315 gallivm_compile_module(gallivm
);
317 test_func_jit
= (unary_func_t
) gallivm_jit_function(gallivm
, test_func
);
319 for (j
= 0; j
< (test
->num_values
+ length
- 1) / length
; j
++) {
320 int num_vals
= ((j
+ 1) * length
<= test
->num_values
) ? length
:
321 test
->num_values
% length
;
323 for (i
= 0; i
< num_vals
; ++i
) {
324 in
[i
] = test
->values
[i
+j
*length
];
327 test_func_jit(out
, in
);
328 for (i
= 0; i
< num_vals
; ++i
) {
329 float ref
= test
->ref(in
[i
]);
330 double error
, precision
;
333 error
= fabs(out
[i
] - ref
);
334 precision
= error
? -log2(error
/fabs(ref
)) : FLT_MANT_DIG
;
336 pass
= precision
>= test
->precision
;
342 if (!pass
|| verbose
) {
343 printf("%s(%.9g): ref = %.9g, out = %.9g, precision = %f bits, %s\n",
344 test
->name
, in
[i
], ref
, out
[i
], precision
,
345 pass
? "PASS" : "FAIL");
354 gallivm_free_function(gallivm
, test_func
, test_func_jit
);
356 gallivm_destroy(gallivm
);
366 test_all(unsigned verbose
, FILE *fp
)
368 boolean success
= TRUE
;
371 for (i
= 0; i
< Elements(unary_tests
); ++i
) {
372 if (!test_unary(verbose
, fp
, &unary_tests
[i
])) {
382 test_some(unsigned verbose
, FILE *fp
,
386 * Not randomly generated test cases, so test all.
389 return test_all(verbose
, fp
);
394 test_single(unsigned verbose
, FILE *fp
)