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_init.h"
39 #include "gallivm/lp_bld_arit.h"
45 write_tsv_header(FILE *fp
)
55 typedef float (*unary_func_t
)(float);
59 * Describe a test case of one unary function.
64 * Test name -- name of the mathematical function under test.
70 (*builder
)(struct lp_build_context
*bld
, LLVMValueRef a
);
73 * Reference (pure-C) function.
86 const float exp2_values
[] = {
101 const float log2_values
[] = {
104 * Smallest denormalized number; meant just for experimentation, but not
107 1.4012984643248171e-45,
119 static float rsqrtf(float x
)
125 const float rsqrt_values
[] = {
135 const float sincos_values
[] = {
154 static const struct unary_test_t unary_tests
[] = {
155 {"exp2", &lp_build_exp2
, &exp2f
, exp2_values
, Elements(exp2_values
)},
156 {"log2", &lp_build_log2
, &log2f
, log2_values
, Elements(log2_values
)},
157 {"exp", &lp_build_exp
, &expf
, exp2_values
, Elements(exp2_values
)},
158 {"log", &lp_build_log
, &logf
, log2_values
, Elements(log2_values
)},
159 {"rsqrt", &lp_build_rsqrt
, &rsqrtf
, rsqrt_values
, Elements(rsqrt_values
)},
160 {"sin", &lp_build_sin
, &sinf
, sincos_values
, Elements(sincos_values
)},
161 {"cos", &lp_build_cos
, &cosf
, sincos_values
, Elements(sincos_values
)},
166 * Build LLVM function that exercises the unary operator builder.
169 build_unary_test_func(struct gallivm_state
*gallivm
,
170 LLVMModuleRef module
,
171 LLVMContextRef context
,
172 const struct unary_test_t
*test
)
174 LLVMTypeRef i32t
= LLVMInt32TypeInContext(context
);
175 LLVMTypeRef f32t
= LLVMFloatTypeInContext(context
);
176 LLVMTypeRef v4f32t
= LLVMVectorType(f32t
, 4);
177 LLVMTypeRef args
[1] = { f32t
};
178 LLVMValueRef func
= LLVMAddFunction(module
, test
->name
, LLVMFunctionType(f32t
, args
, Elements(args
), 0));
179 LLVMValueRef arg1
= LLVMGetParam(func
, 0);
180 LLVMBuilderRef builder
= gallivm
->builder
;
181 LLVMBasicBlockRef block
= LLVMAppendBasicBlockInContext(context
, func
, "entry");
182 LLVMValueRef index0
= LLVMConstInt(i32t
, 0, 0);
185 struct lp_build_context bld
;
187 lp_build_context_init(&bld
, gallivm
, lp_float32_vec4_type());
189 LLVMSetFunctionCallConv(func
, LLVMCCallConv
);
191 LLVMPositionBuilderAtEnd(builder
, block
);
193 /* scalar to vector */
194 arg1
= LLVMBuildInsertElement(builder
, LLVMGetUndef(v4f32t
), arg1
, index0
, "");
196 ret
= test
->builder(&bld
, arg1
);
198 /* vector to scalar */
199 ret
= LLVMBuildExtractElement(builder
, ret
, index0
, "");
201 LLVMBuildRet(builder
, ret
);
207 * Test one LLVM unary arithmetic builder function.
210 test_unary(struct gallivm_state
*gallivm
, unsigned verbose
, FILE *fp
, const struct unary_test_t
*test
)
212 LLVMModuleRef module
= gallivm
->module
;
213 LLVMValueRef test_func
;
214 LLVMExecutionEngineRef engine
= gallivm
->engine
;
215 LLVMContextRef context
= gallivm
->context
;
217 unary_func_t test_func_jit
;
218 boolean success
= TRUE
;
221 test_func
= build_unary_test_func(gallivm
, module
, context
, test
);
223 if (LLVMVerifyModule(module
, LLVMPrintMessageAction
, &error
)) {
224 printf("LLVMVerifyModule: %s\n", error
);
225 LLVMDumpModule(module
);
228 LLVMDisposeMessage(error
);
230 test_func_jit
= (unary_func_t
) pointer_to_func(LLVMGetPointerToGlobal(engine
, test_func
));
232 for (i
= 0; i
< test
->num_values
; ++i
) {
233 float value
= test
->values
[i
];
234 float ref
= test
->ref(value
);
235 float src
= test_func_jit(value
);
237 double error
= fabs(src
- ref
);
238 double precision
= error
? -log2(error
/fabs(ref
)) : FLT_MANT_DIG
;
240 bool pass
= precision
>= 20.0;
246 if (!pass
|| verbose
) {
247 printf("%s(%.9g): ref = %.9g, src = %.9g, precision = %f bits, %s\n",
248 test
->name
, value
, ref
, src
, precision
,
249 pass
? "PASS" : "FAIL");
257 LLVMFreeMachineCodeForFunction(engine
, test_func
);
264 test_all(struct gallivm_state
*gallivm
, unsigned verbose
, FILE *fp
)
266 boolean success
= TRUE
;
269 for (i
= 0; i
< Elements(unary_tests
); ++i
) {
270 if (!test_unary(gallivm
, verbose
, fp
, &unary_tests
[i
])) {
280 test_some(struct gallivm_state
*gallivm
, unsigned verbose
, FILE *fp
,
284 * Not randomly generated test cases, so test all.
287 return test_all(gallivm
, verbose
, fp
);
292 test_single(struct gallivm_state
*gallivm
, unsigned verbose
, FILE *fp
)