Merge remote-tracking branch 'public/master' into vulkan
[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 #include "util/u_cpu_detect.h"
37
38 #include "gallivm/lp_bld.h"
39 #include "gallivm/lp_bld_debug.h"
40 #include "gallivm/lp_bld_init.h"
41 #include "gallivm/lp_bld_arit.h"
42
43 #include "lp_test.h"
44
45
46 void
47 write_tsv_header(FILE *fp)
48 {
49 fprintf(fp,
50 "result\t"
51 "format\n");
52
53 fflush(fp);
54 }
55
56
57 typedef void (*unary_func_t)(float *out, const float *in);
58
59
60 /**
61 * Describe a test case of one unary function.
62 */
63 struct unary_test_t
64 {
65 /*
66 * Test name -- name of the mathematical function under test.
67 */
68
69 const char *name;
70
71 LLVMValueRef
72 (*builder)(struct lp_build_context *bld, LLVMValueRef a);
73
74 /*
75 * Reference (pure-C) function.
76 */
77 float
78 (*ref)(float a);
79
80 /*
81 * Test values.
82 */
83 const float *values;
84 unsigned num_values;
85
86 /*
87 * Required precision in bits.
88 */
89 double precision;
90 };
91
92
93 static float negf(float x)
94 {
95 return -x;
96 }
97
98
99 static float sgnf(float x)
100 {
101 if (x > 0.0f) {
102 return 1.0f;
103 }
104 if (x < 0.0f) {
105 return -1.0f;
106 }
107 return 0.0f;
108 }
109
110
111 const float exp2_values[] = {
112 -INFINITY,
113 -60,
114 -4,
115 -2,
116 -1,
117 -1e-007,
118 0,
119 1e-007,
120 0.01,
121 0.1,
122 0.9,
123 0.99,
124 1,
125 2,
126 4,
127 60,
128 INFINITY,
129 NAN
130 };
131
132
133 const float log2_values[] = {
134 #if 0
135 /*
136 * Smallest denormalized number; meant just for experimentation, but not
137 * validation.
138 */
139 1.4012984643248171e-45,
140 #endif
141 -INFINITY,
142 0,
143 1e-007,
144 0.1,
145 0.5,
146 0.99,
147 1,
148 1.01,
149 1.1,
150 1.9,
151 1.99,
152 2,
153 4,
154 100000,
155 1e+018,
156 INFINITY,
157 NAN
158 };
159
160
161 static float rcpf(float x)
162 {
163 return 1.0/x;
164 }
165
166
167 const float rcp_values[] = {
168 -0.0, 0.0,
169 -1.0, 1.0,
170 -1e-007, 1e-007,
171 -4.0, 4.0,
172 -1e+035, -100000,
173 100000, 1e+035,
174 5.88e-39f, // denormal
175 #if (__STDC_VERSION__ >= 199901L)
176 INFINITY, -INFINITY,
177 #endif
178 };
179
180
181 static float rsqrtf(float x)
182 {
183 return 1.0/(float)sqrt(x);
184 }
185
186
187 const float rsqrt_values[] = {
188 // http://msdn.microsoft.com/en-us/library/windows/desktop/bb147346.aspx
189 0.0, // must yield infinity
190 1.0, // must yield 1.0
191 1e-007, 4.0,
192 100000, 1e+035,
193 5.88e-39f, // denormal
194 #if (__STDC_VERSION__ >= 199901L)
195 INFINITY,
196 #endif
197 };
198
199
200 const float sincos_values[] = {
201 -INFINITY,
202 -5*M_PI/4,
203 -4*M_PI/4,
204 -4*M_PI/4,
205 -3*M_PI/4,
206 -2*M_PI/4,
207 -1*M_PI/4,
208 1*M_PI/4,
209 2*M_PI/4,
210 3*M_PI/4,
211 4*M_PI/4,
212 5*M_PI/4,
213 INFINITY,
214 NAN
215 };
216
217 const float round_values[] = {
218 -10.0, -1, 0.0, 12.0,
219 -1.49, -0.25, 1.25, 2.51,
220 -0.99, -0.01, 0.01, 0.99,
221 1.401298464324817e-45f, // smallest denormal
222 -1.401298464324817e-45f,
223 1.62981451e-08f,
224 -1.62981451e-08f,
225 1.62981451e15f, // large number not representable as 32bit int
226 -1.62981451e15f,
227 FLT_EPSILON,
228 -FLT_EPSILON,
229 1.0f - 0.5f*FLT_EPSILON,
230 -1.0f + FLT_EPSILON,
231 FLT_MAX,
232 -FLT_MAX
233 };
234
235 static float fractf(float x)
236 {
237 x -= floorf(x);
238 if (x >= 1.0f) {
239 // clamp to the largest number smaller than one
240 x = 1.0f - 0.5f*FLT_EPSILON;
241 }
242 return x;
243 }
244
245
246 const float fract_values[] = {
247 // http://en.wikipedia.org/wiki/IEEE_754-1985#Examples
248 0.0f,
249 -0.0f,
250 1.0f,
251 -1.0f,
252 0.5f,
253 -0.5f,
254 1.401298464324817e-45f, // smallest denormal
255 -1.401298464324817e-45f,
256 5.88e-39f, // middle denormal
257 1.18e-38f, // largest denormal
258 -1.18e-38f,
259 -1.62981451e-08f,
260 FLT_EPSILON,
261 -FLT_EPSILON,
262 1.0f - 0.5f*FLT_EPSILON,
263 -1.0f + FLT_EPSILON,
264 FLT_MAX,
265 -FLT_MAX
266 };
267
268
269 /*
270 * Unary test cases.
271 */
272
273 static const struct unary_test_t
274 unary_tests[] = {
275 {"abs", &lp_build_abs, &fabsf, exp2_values, Elements(exp2_values), 20.0 },
276 {"neg", &lp_build_negate, &negf, exp2_values, Elements(exp2_values), 20.0 },
277 {"exp2", &lp_build_exp2, &exp2f, exp2_values, Elements(exp2_values), 20.0 },
278 {"log2", &lp_build_log2_safe, &log2f, log2_values, Elements(log2_values), 20.0 },
279 {"exp", &lp_build_exp, &expf, exp2_values, Elements(exp2_values), 18.0 },
280 {"log", &lp_build_log_safe, &logf, log2_values, Elements(log2_values), 20.0 },
281 {"rcp", &lp_build_rcp, &rcpf, rcp_values, Elements(rcp_values), 20.0 },
282 {"rsqrt", &lp_build_rsqrt, &rsqrtf, rsqrt_values, Elements(rsqrt_values), 20.0 },
283 {"sin", &lp_build_sin, &sinf, sincos_values, Elements(sincos_values), 20.0 },
284 {"cos", &lp_build_cos, &cosf, sincos_values, Elements(sincos_values), 20.0 },
285 {"sgn", &lp_build_sgn, &sgnf, exp2_values, Elements(exp2_values), 20.0 },
286 {"round", &lp_build_round, &roundf, round_values, Elements(round_values), 24.0 },
287 {"trunc", &lp_build_trunc, &truncf, round_values, Elements(round_values), 24.0 },
288 {"floor", &lp_build_floor, &floorf, round_values, Elements(round_values), 24.0 },
289 {"ceil", &lp_build_ceil, &ceilf, round_values, Elements(round_values), 24.0 },
290 {"fract", &lp_build_fract_safe, &fractf, fract_values, Elements(fract_values), 24.0 },
291 };
292
293
294 /*
295 * Build LLVM function that exercises the unary operator builder.
296 */
297 static LLVMValueRef
298 build_unary_test_func(struct gallivm_state *gallivm,
299 const struct unary_test_t *test)
300 {
301 struct lp_type type = lp_type_float_vec(32, lp_native_vector_width);
302 LLVMContextRef context = gallivm->context;
303 LLVMModuleRef module = gallivm->module;
304 LLVMTypeRef vf32t = lp_build_vec_type(gallivm, type);
305 LLVMTypeRef args[2] = { LLVMPointerType(vf32t, 0), LLVMPointerType(vf32t, 0) };
306 LLVMValueRef func = LLVMAddFunction(module, test->name,
307 LLVMFunctionType(LLVMVoidTypeInContext(context),
308 args, Elements(args), 0));
309 LLVMValueRef arg0 = LLVMGetParam(func, 0);
310 LLVMValueRef arg1 = LLVMGetParam(func, 1);
311 LLVMBuilderRef builder = gallivm->builder;
312 LLVMBasicBlockRef block = LLVMAppendBasicBlockInContext(context, func, "entry");
313 LLVMValueRef ret;
314
315 struct lp_build_context bld;
316
317 lp_build_context_init(&bld, gallivm, type);
318
319 LLVMSetFunctionCallConv(func, LLVMCCallConv);
320
321 LLVMPositionBuilderAtEnd(builder, block);
322
323 arg1 = LLVMBuildLoad(builder, arg1, "");
324
325 ret = test->builder(&bld, arg1);
326
327 LLVMBuildStore(builder, ret, arg0);
328
329 LLVMBuildRetVoid(builder);
330
331 gallivm_verify_function(gallivm, func);
332
333 return func;
334 }
335
336
337 /*
338 * Flush denorms to zero.
339 */
340 static float
341 flush_denorm_to_zero(float val)
342 {
343 /*
344 * If we have a denorm manually set it to (+-)0.
345 * This is because the reference may or may not do the right thing
346 * otherwise because we want the result according to treating all
347 * denormals as zero (FTZ/DAZ). Not using fpclassify because
348 * a) some compilers are stuck at c89 (msvc)
349 * b) not sure it reliably works with non-standard ftz/daz mode
350 * And, right now we only disable denorms with jited code on x86/sse
351 * (albeit this should be classified as a bug) so to get results which
352 * match we must only flush them to zero here in that case too.
353 */
354 union fi fi_val;
355
356 fi_val.f = val;
357
358 #if defined(PIPE_ARCH_SSE)
359 if (util_cpu_caps.has_sse) {
360 if ((fi_val.ui & 0x7f800000) == 0) {
361 fi_val.ui &= 0xff800000;
362 }
363 }
364 #endif
365
366 return fi_val.f;
367 }
368
369 /*
370 * Test one LLVM unary arithmetic builder function.
371 */
372 static boolean
373 test_unary(unsigned verbose, FILE *fp, const struct unary_test_t *test)
374 {
375 struct gallivm_state *gallivm;
376 LLVMValueRef test_func;
377 unary_func_t test_func_jit;
378 boolean success = TRUE;
379 int i, j;
380 int length = lp_native_vector_width / 32;
381 float *in, *out;
382
383 in = align_malloc(length * 4, length * 4);
384 out = align_malloc(length * 4, length * 4);
385
386 /* random NaNs or 0s could wreak havoc */
387 for (i = 0; i < length; i++) {
388 in[i] = 1.0;
389 }
390
391 gallivm = gallivm_create("test_module", LLVMGetGlobalContext());
392
393 test_func = build_unary_test_func(gallivm, test);
394
395 gallivm_compile_module(gallivm);
396
397 test_func_jit = (unary_func_t) gallivm_jit_function(gallivm, test_func);
398
399 gallivm_free_ir(gallivm);
400
401 for (j = 0; j < (test->num_values + length - 1) / length; j++) {
402 int num_vals = ((j + 1) * length <= test->num_values) ? length :
403 test->num_values % length;
404
405 for (i = 0; i < num_vals; ++i) {
406 in[i] = test->values[i+j*length];
407 }
408
409 test_func_jit(out, in);
410 for (i = 0; i < num_vals; ++i) {
411 float testval, ref;
412 double error, precision;
413 bool pass;
414
415 testval = flush_denorm_to_zero(in[i]);
416 ref = flush_denorm_to_zero(test->ref(testval));
417
418 if (util_inf_sign(ref) && util_inf_sign(out[i]) == util_inf_sign(ref)) {
419 error = 0;
420 } else {
421 error = fabs(out[i] - ref);
422 }
423 precision = error ? -log2(error/fabs(ref)) : FLT_MANT_DIG;
424
425 pass = precision >= test->precision;
426
427 if (isnan(ref)) {
428 continue;
429 }
430
431 if (!pass || verbose) {
432 printf("%s(%.9g): ref = %.9g, out = %.9g, precision = %f bits, %s\n",
433 test->name, in[i], ref, out[i], precision,
434 pass ? "PASS" : "FAIL");
435 fflush(stdout);
436 }
437
438 if (!pass) {
439 success = FALSE;
440 }
441 }
442 }
443
444 gallivm_destroy(gallivm);
445
446 align_free(in);
447 align_free(out);
448
449 return success;
450 }
451
452
453 boolean
454 test_all(unsigned verbose, FILE *fp)
455 {
456 boolean success = TRUE;
457 int i;
458
459 for (i = 0; i < Elements(unary_tests); ++i) {
460 if (!test_unary(verbose, fp, &unary_tests[i])) {
461 success = FALSE;
462 }
463 }
464
465 return success;
466 }
467
468
469 boolean
470 test_some(unsigned verbose, FILE *fp,
471 unsigned long n)
472 {
473 /*
474 * Not randomly generated test cases, so test all.
475 */
476
477 return test_all(verbose, fp);
478 }
479
480
481 boolean
482 test_single(unsigned verbose, FILE *fp)
483 {
484 return TRUE;
485 }