llvmpipe: Test lp_test_arit with LP_NATIVE_VECTOR_WIDTH.
[mesa.git] / src / gallium / drivers / llvmpipe / lp_test_arit.c
1 /**************************************************************************
2 *
3 * Copyright 2011 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28
29 #include <limits.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 #include "util/u_pointer.h"
34 #include "util/u_memory.h"
35 #include "util/u_math.h"
36
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"
41
42 #include "lp_test.h"
43
44
45 void
46 write_tsv_header(FILE *fp)
47 {
48 fprintf(fp,
49 "result\t"
50 "format\n");
51
52 fflush(fp);
53 }
54
55
56 typedef float (*unary_func_t)(float);
57
58
59 /**
60 * Describe a test case of one unary function.
61 */
62 struct unary_test_t
63 {
64 /*
65 * Test name -- name of the mathematical function under test.
66 */
67
68 const char *name;
69
70 LLVMValueRef
71 (*builder)(struct lp_build_context *bld, LLVMValueRef a);
72
73 /*
74 * Reference (pure-C) function.
75 */
76 float
77 (*ref)(float a);
78
79 /*
80 * Test values.
81 */
82 const float *values;
83 unsigned num_values;
84
85 /*
86 * Required precision in bits.
87 */
88 double precision;
89 };
90
91
92 static float negf(float x)
93 {
94 return -x;
95 }
96
97
98 const float exp2_values[] = {
99 -60,
100 -4,
101 -2,
102 -1,
103 -1e-007,
104 0,
105 1e-007,
106 0.01,
107 0.1,
108 0.9,
109 0.99,
110 1,
111 2,
112 4,
113 60
114 };
115
116
117 const float log2_values[] = {
118 #if 0
119 /*
120 * Smallest denormalized number; meant just for experimentation, but not
121 * validation.
122 */
123 1.4012984643248171e-45,
124 #endif
125 1e-007,
126 0.1,
127 0.5,
128 0.99,
129 1,
130 1.01,
131 1.1,
132 1.9,
133 1.99,
134 2,
135 4,
136 100000,
137 1e+018
138 };
139
140
141 static float rsqrtf(float x)
142 {
143 return 1.0/sqrt(x);
144 }
145
146
147 const float rsqrt_values[] = {
148 -1, -1e-007,
149 1e-007, 1,
150 -4, -1,
151 1, 4,
152 -1e+035, -100000,
153 100000, 1e+035,
154 };
155
156
157 const float sincos_values[] = {
158 -5*M_PI/4,
159 -4*M_PI/4,
160 -4*M_PI/4,
161 -3*M_PI/4,
162 -2*M_PI/4,
163 -1*M_PI/4,
164 1*M_PI/4,
165 2*M_PI/4,
166 3*M_PI/4,
167 4*M_PI/4,
168 5*M_PI/4,
169 };
170
171
172 /*
173 * Unary test cases.
174 */
175
176 static const struct unary_test_t
177 unary_tests[] = {
178 {"neg", &lp_build_negate, &negf, exp2_values, Elements(exp2_values), 20.0 },
179 {"exp2", &lp_build_exp2, &exp2f, exp2_values, Elements(exp2_values), 20.0 },
180 {"log2", &lp_build_log2, &log2f, log2_values, Elements(log2_values), 10.0 }, // FIXME
181 {"exp", &lp_build_exp, &expf, exp2_values, Elements(exp2_values), 18.0 },
182 {"log", &lp_build_log, &logf, log2_values, Elements(log2_values), 10.0 }, // FIXME
183 {"rsqrt", &lp_build_rsqrt, &rsqrtf, rsqrt_values, Elements(rsqrt_values), 20.0 },
184 {"sin", &lp_build_sin, &sinf, sincos_values, Elements(sincos_values), 20.0 },
185 {"cos", &lp_build_cos, &cosf, sincos_values, Elements(sincos_values), 20.0 },
186 };
187
188
189 /*
190 * Build LLVM function that exercises the unary operator builder.
191 */
192 static LLVMValueRef
193 build_unary_test_func(struct gallivm_state *gallivm,
194 LLVMModuleRef module,
195 LLVMContextRef context,
196 const struct unary_test_t *test)
197 {
198 struct lp_type type = lp_type_float_vec(32);
199 LLVMTypeRef i32t = LLVMInt32TypeInContext(context);
200 LLVMTypeRef f32t = LLVMFloatTypeInContext(context);
201 LLVMTypeRef vf32t = lp_build_vec_type(gallivm, type);
202 LLVMTypeRef args[1] = { f32t };
203 LLVMValueRef func = LLVMAddFunction(module, test->name, LLVMFunctionType(f32t, args, Elements(args), 0));
204 LLVMValueRef arg1 = LLVMGetParam(func, 0);
205 LLVMBuilderRef builder = gallivm->builder;
206 LLVMBasicBlockRef block = LLVMAppendBasicBlockInContext(context, func, "entry");
207 LLVMValueRef index0 = LLVMConstInt(i32t, 0, 0);
208 LLVMValueRef ret;
209
210 struct lp_build_context bld;
211
212 lp_build_context_init(&bld, gallivm, lp_type_float_vec(32));
213
214 LLVMSetFunctionCallConv(func, LLVMCCallConv);
215
216 LLVMPositionBuilderAtEnd(builder, block);
217
218 /* scalar to vector */
219 arg1 = LLVMBuildInsertElement(builder, LLVMGetUndef(vf32t), arg1, index0, "");
220
221 ret = test->builder(&bld, arg1);
222
223 /* vector to scalar */
224 ret = LLVMBuildExtractElement(builder, ret, index0, "");
225
226 LLVMBuildRet(builder, ret);
227 return func;
228 }
229
230
231 /*
232 * Test one LLVM unary arithmetic builder function.
233 */
234 static boolean
235 test_unary(struct gallivm_state *gallivm, unsigned verbose, FILE *fp, const struct unary_test_t *test)
236 {
237 LLVMModuleRef module = gallivm->module;
238 LLVMValueRef test_func;
239 LLVMExecutionEngineRef engine = gallivm->engine;
240 LLVMContextRef context = gallivm->context;
241 char *error = NULL;
242 unary_func_t test_func_jit;
243 boolean success = TRUE;
244 int i;
245
246 test_func = build_unary_test_func(gallivm, module, context, test);
247
248 if (LLVMVerifyModule(module, LLVMPrintMessageAction, &error)) {
249 printf("LLVMVerifyModule: %s\n", error);
250 LLVMDumpModule(module);
251 abort();
252 }
253 LLVMDisposeMessage(error);
254
255 test_func_jit = (unary_func_t) pointer_to_func(LLVMGetPointerToGlobal(engine, test_func));
256
257 for (i = 0; i < test->num_values; ++i) {
258 float value = test->values[i];
259 float ref = test->ref(value);
260 float src = test_func_jit(value);
261
262 double error = fabs(src - ref);
263 double precision = error ? -log2(error/fabs(ref)) : FLT_MANT_DIG;
264
265 bool pass = precision >= test->precision;
266
267 if (isnan(ref)) {
268 continue;
269 }
270
271 if (!pass || verbose) {
272 printf("%s(%.9g): ref = %.9g, src = %.9g, precision = %f bits, %s\n",
273 test->name, value, ref, src, precision,
274 pass ? "PASS" : "FAIL");
275 }
276
277 if (!pass) {
278 success = FALSE;
279 }
280 }
281
282 LLVMFreeMachineCodeForFunction(engine, test_func);
283
284 return success;
285 }
286
287
288 boolean
289 test_all(struct gallivm_state *gallivm, unsigned verbose, FILE *fp)
290 {
291 boolean success = TRUE;
292 int i;
293
294 for (i = 0; i < Elements(unary_tests); ++i) {
295 if (!test_unary(gallivm, verbose, fp, &unary_tests[i])) {
296 success = FALSE;
297 }
298 }
299
300 return success;
301 }
302
303
304 boolean
305 test_some(struct gallivm_state *gallivm, unsigned verbose, FILE *fp,
306 unsigned long n)
307 {
308 /*
309 * Not randomly generated test cases, so test all.
310 */
311
312 return test_all(gallivm, verbose, fp);
313 }
314
315
316 boolean
317 test_single(struct gallivm_state *gallivm, unsigned verbose, FILE *fp)
318 {
319 return TRUE;
320 }