radeonsi: remove unused variable si_state_dsa::db_render_control
[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 void (*unary_func_t)(float *out, const float *in);
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 static float sgnf(float x)
99 {
100 if (x > 0.0f) {
101 return 1.0f;
102 }
103 if (x < 0.0f) {
104 return -1.0f;
105 }
106 return 0.0f;
107 }
108
109
110 const float exp2_values[] = {
111 -INFINITY,
112 -60,
113 -4,
114 -2,
115 -1,
116 -1e-007,
117 0,
118 1e-007,
119 0.01,
120 0.1,
121 0.9,
122 0.99,
123 1,
124 2,
125 4,
126 60,
127 INFINITY,
128 NAN
129 };
130
131
132 const float log2_values[] = {
133 #if 0
134 /*
135 * Smallest denormalized number; meant just for experimentation, but not
136 * validation.
137 */
138 1.4012984643248171e-45,
139 #endif
140 -INFINITY,
141 0,
142 1e-007,
143 0.1,
144 0.5,
145 0.99,
146 1,
147 1.01,
148 1.1,
149 1.9,
150 1.99,
151 2,
152 4,
153 100000,
154 1e+018,
155 INFINITY,
156 NAN
157 };
158
159
160 static float rcpf(float x)
161 {
162 return 1.0/x;
163 }
164
165
166 const float rcp_values[] = {
167 -0.0, 0.0,
168 -1.0, 1.0,
169 -1e-007, 1e-007,
170 -4.0, 4.0,
171 -1e+035, -100000,
172 100000, 1e+035,
173 5.88e-39f, // denormal
174 #if (__STDC_VERSION__ >= 199901L)
175 INFINITY, -INFINITY,
176 #endif
177 };
178
179
180 static float rsqrtf(float x)
181 {
182 return 1.0/(float)sqrt(x);
183 }
184
185
186 const float rsqrt_values[] = {
187 // http://msdn.microsoft.com/en-us/library/windows/desktop/bb147346.aspx
188 0.0, // must yield infinity
189 1.0, // must yield 1.0
190 1e-007, 4.0,
191 100000, 1e+035,
192 5.88e-39f, // denormal
193 #if (__STDC_VERSION__ >= 199901L)
194 INFINITY,
195 #endif
196 };
197
198
199 const float sincos_values[] = {
200 -INFINITY,
201 -5*M_PI/4,
202 -4*M_PI/4,
203 -4*M_PI/4,
204 -3*M_PI/4,
205 -2*M_PI/4,
206 -1*M_PI/4,
207 1*M_PI/4,
208 2*M_PI/4,
209 3*M_PI/4,
210 4*M_PI/4,
211 5*M_PI/4,
212 INFINITY,
213 NAN
214 };
215
216 const float round_values[] = {
217 -10.0, -1, 0.0, 12.0,
218 -1.49, -0.25, 1.25, 2.51,
219 -0.99, -0.01, 0.01, 0.99,
220 1.401298464324817e-45f, // smallest denormal
221 -1.401298464324817e-45f,
222 1.62981451e-08f,
223 -1.62981451e-08f,
224 1.62981451e15f, // large number not representable as 32bit int
225 -1.62981451e15f,
226 FLT_EPSILON,
227 -FLT_EPSILON,
228 1.0f - 0.5f*FLT_EPSILON,
229 -1.0f + FLT_EPSILON,
230 FLT_MAX,
231 -FLT_MAX
232 };
233
234 static float fractf(float x)
235 {
236 x -= floorf(x);
237 if (x >= 1.0f) {
238 // clamp to the largest number smaller than one
239 x = 1.0f - 0.5f*FLT_EPSILON;
240 }
241 return x;
242 }
243
244
245 const float fract_values[] = {
246 // http://en.wikipedia.org/wiki/IEEE_754-1985#Examples
247 0.0f,
248 -0.0f,
249 1.0f,
250 -1.0f,
251 0.5f,
252 -0.5f,
253 1.401298464324817e-45f, // smallest denormal
254 -1.401298464324817e-45f,
255 5.88e-39f, // middle denormal
256 1.18e-38f, // largest denormal
257 -1.18e-38f,
258 -1.62981451e-08f,
259 FLT_EPSILON,
260 -FLT_EPSILON,
261 1.0f - 0.5f*FLT_EPSILON,
262 -1.0f + FLT_EPSILON,
263 FLT_MAX,
264 -FLT_MAX
265 };
266
267
268 /*
269 * Unary test cases.
270 */
271
272 static const struct unary_test_t
273 unary_tests[] = {
274 {"neg", &lp_build_negate, &negf, exp2_values, Elements(exp2_values), 20.0 },
275 {"exp2", &lp_build_exp2, &exp2f, exp2_values, Elements(exp2_values), 20.0 },
276 {"log2", &lp_build_log2_safe, &log2f, log2_values, Elements(log2_values), 20.0 },
277 {"exp", &lp_build_exp, &expf, exp2_values, Elements(exp2_values), 18.0 },
278 {"log", &lp_build_log_safe, &logf, log2_values, Elements(log2_values), 20.0 },
279 {"rcp", &lp_build_rcp, &rcpf, rcp_values, Elements(rcp_values), 20.0 },
280 {"rsqrt", &lp_build_rsqrt, &rsqrtf, rsqrt_values, Elements(rsqrt_values), 20.0 },
281 {"sin", &lp_build_sin, &sinf, sincos_values, Elements(sincos_values), 20.0 },
282 {"cos", &lp_build_cos, &cosf, sincos_values, Elements(sincos_values), 20.0 },
283 {"sgn", &lp_build_sgn, &sgnf, exp2_values, Elements(exp2_values), 20.0 },
284 {"round", &lp_build_round, &roundf, round_values, Elements(round_values), 24.0 },
285 {"trunc", &lp_build_trunc, &truncf, round_values, Elements(round_values), 24.0 },
286 {"floor", &lp_build_floor, &floorf, round_values, Elements(round_values), 24.0 },
287 {"ceil", &lp_build_ceil, &ceilf, round_values, Elements(round_values), 24.0 },
288 {"fract", &lp_build_fract_safe, &fractf, fract_values, Elements(fract_values), 24.0 },
289 };
290
291
292 /*
293 * Build LLVM function that exercises the unary operator builder.
294 */
295 static LLVMValueRef
296 build_unary_test_func(struct gallivm_state *gallivm,
297 const struct unary_test_t *test)
298 {
299 struct lp_type type = lp_type_float_vec(32, lp_native_vector_width);
300 LLVMContextRef context = gallivm->context;
301 LLVMModuleRef module = gallivm->module;
302 LLVMTypeRef vf32t = lp_build_vec_type(gallivm, type);
303 LLVMTypeRef args[2] = { LLVMPointerType(vf32t, 0), LLVMPointerType(vf32t, 0) };
304 LLVMValueRef func = LLVMAddFunction(module, test->name,
305 LLVMFunctionType(LLVMVoidTypeInContext(context),
306 args, Elements(args), 0));
307 LLVMValueRef arg0 = LLVMGetParam(func, 0);
308 LLVMValueRef arg1 = LLVMGetParam(func, 1);
309 LLVMBuilderRef builder = gallivm->builder;
310 LLVMBasicBlockRef block = LLVMAppendBasicBlockInContext(context, func, "entry");
311 LLVMValueRef ret;
312
313 struct lp_build_context bld;
314
315 lp_build_context_init(&bld, gallivm, type);
316
317 LLVMSetFunctionCallConv(func, LLVMCCallConv);
318
319 LLVMPositionBuilderAtEnd(builder, block);
320
321 arg1 = LLVMBuildLoad(builder, arg1, "");
322
323 ret = test->builder(&bld, arg1);
324
325 LLVMBuildStore(builder, ret, arg0);
326
327 LLVMBuildRetVoid(builder);
328
329 gallivm_verify_function(gallivm, func);
330
331 return func;
332 }
333
334
335 /*
336 * Test one LLVM unary arithmetic builder function.
337 */
338 static boolean
339 test_unary(unsigned verbose, FILE *fp, const struct unary_test_t *test)
340 {
341 struct gallivm_state *gallivm;
342 LLVMValueRef test_func;
343 unary_func_t test_func_jit;
344 boolean success = TRUE;
345 int i, j;
346 int length = lp_native_vector_width / 32;
347 float *in, *out;
348
349 in = align_malloc(length * 4, length * 4);
350 out = align_malloc(length * 4, length * 4);
351
352 /* random NaNs or 0s could wreak havoc */
353 for (i = 0; i < length; i++) {
354 in[i] = 1.0;
355 }
356
357 gallivm = gallivm_create("test_module", LLVMGetGlobalContext());
358
359 test_func = build_unary_test_func(gallivm, test);
360
361 gallivm_compile_module(gallivm);
362
363 test_func_jit = (unary_func_t) gallivm_jit_function(gallivm, test_func);
364
365 gallivm_free_ir(gallivm);
366
367 for (j = 0; j < (test->num_values + length - 1) / length; j++) {
368 int num_vals = ((j + 1) * length <= test->num_values) ? length :
369 test->num_values % length;
370
371 for (i = 0; i < num_vals; ++i) {
372 in[i] = test->values[i+j*length];
373 }
374
375 test_func_jit(out, in);
376 for (i = 0; i < num_vals; ++i) {
377 float ref = test->ref(in[i]);
378 double error, precision;
379 bool pass;
380
381 if (util_inf_sign(ref) && util_inf_sign(out[i]) == util_inf_sign(ref)) {
382 error = 0;
383 } else {
384 error = fabs(out[i] - ref);
385 }
386 precision = error ? -log2(error/fabs(ref)) : FLT_MANT_DIG;
387
388 pass = precision >= test->precision;
389
390 if (isnan(ref)) {
391 continue;
392 }
393
394 if (!pass || verbose) {
395 printf("%s(%.9g): ref = %.9g, out = %.9g, precision = %f bits, %s\n",
396 test->name, in[i], ref, out[i], precision,
397 pass ? "PASS" : "FAIL");
398 fflush(stdout);
399 }
400
401 if (!pass) {
402 success = FALSE;
403 }
404 }
405 }
406
407 gallivm_destroy(gallivm);
408
409 align_free(in);
410 align_free(out);
411
412 return success;
413 }
414
415
416 boolean
417 test_all(unsigned verbose, FILE *fp)
418 {
419 boolean success = TRUE;
420 int i;
421
422 for (i = 0; i < Elements(unary_tests); ++i) {
423 if (!test_unary(verbose, fp, &unary_tests[i])) {
424 success = FALSE;
425 }
426 }
427
428 return success;
429 }
430
431
432 boolean
433 test_some(unsigned verbose, FILE *fp,
434 unsigned long n)
435 {
436 /*
437 * Not randomly generated test cases, so test all.
438 */
439
440 return test_all(verbose, fp);
441 }
442
443
444 boolean
445 test_single(unsigned verbose, FILE *fp)
446 {
447 return TRUE;
448 }