llvmpipe: minor cleanup of sse2 for calc_fixed_position
[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 {"neg", &lp_build_negate, &negf, exp2_values, Elements(exp2_values), 20.0 },
276 {"exp2", &lp_build_exp2, &exp2f, exp2_values, Elements(exp2_values), 20.0 },
277 {"log2", &lp_build_log2_safe, &log2f, log2_values, Elements(log2_values), 20.0 },
278 {"exp", &lp_build_exp, &expf, exp2_values, Elements(exp2_values), 18.0 },
279 {"log", &lp_build_log_safe, &logf, log2_values, Elements(log2_values), 20.0 },
280 {"rcp", &lp_build_rcp, &rcpf, rcp_values, Elements(rcp_values), 20.0 },
281 {"rsqrt", &lp_build_rsqrt, &rsqrtf, rsqrt_values, Elements(rsqrt_values), 20.0 },
282 {"sin", &lp_build_sin, &sinf, sincos_values, Elements(sincos_values), 20.0 },
283 {"cos", &lp_build_cos, &cosf, sincos_values, Elements(sincos_values), 20.0 },
284 {"sgn", &lp_build_sgn, &sgnf, exp2_values, Elements(exp2_values), 20.0 },
285 {"round", &lp_build_round, &roundf, round_values, Elements(round_values), 24.0 },
286 {"trunc", &lp_build_trunc, &truncf, round_values, Elements(round_values), 24.0 },
287 {"floor", &lp_build_floor, &floorf, round_values, Elements(round_values), 24.0 },
288 {"ceil", &lp_build_ceil, &ceilf, round_values, Elements(round_values), 24.0 },
289 {"fract", &lp_build_fract_safe, &fractf, fract_values, Elements(fract_values), 24.0 },
290 };
291
292
293 /*
294 * Build LLVM function that exercises the unary operator builder.
295 */
296 static LLVMValueRef
297 build_unary_test_func(struct gallivm_state *gallivm,
298 const struct unary_test_t *test)
299 {
300 struct lp_type type = lp_type_float_vec(32, lp_native_vector_width);
301 LLVMContextRef context = gallivm->context;
302 LLVMModuleRef module = gallivm->module;
303 LLVMTypeRef vf32t = lp_build_vec_type(gallivm, type);
304 LLVMTypeRef args[2] = { LLVMPointerType(vf32t, 0), LLVMPointerType(vf32t, 0) };
305 LLVMValueRef func = LLVMAddFunction(module, test->name,
306 LLVMFunctionType(LLVMVoidTypeInContext(context),
307 args, Elements(args), 0));
308 LLVMValueRef arg0 = LLVMGetParam(func, 0);
309 LLVMValueRef arg1 = LLVMGetParam(func, 1);
310 LLVMBuilderRef builder = gallivm->builder;
311 LLVMBasicBlockRef block = LLVMAppendBasicBlockInContext(context, func, "entry");
312 LLVMValueRef ret;
313
314 struct lp_build_context bld;
315
316 lp_build_context_init(&bld, gallivm, type);
317
318 LLVMSetFunctionCallConv(func, LLVMCCallConv);
319
320 LLVMPositionBuilderAtEnd(builder, block);
321
322 arg1 = LLVMBuildLoad(builder, arg1, "");
323
324 ret = test->builder(&bld, arg1);
325
326 LLVMBuildStore(builder, ret, arg0);
327
328 LLVMBuildRetVoid(builder);
329
330 gallivm_verify_function(gallivm, func);
331
332 return func;
333 }
334
335
336 /*
337 * Flush denorms to zero.
338 */
339 static float
340 flush_denorm_to_zero(float val)
341 {
342 /*
343 * If we have a denorm manually set it to (+-)0.
344 * This is because the reference may or may not do the right thing
345 * otherwise because we want the result according to treating all
346 * denormals as zero (FTZ/DAZ). Not using fpclassify because
347 * a) some compilers are stuck at c89 (msvc)
348 * b) not sure it reliably works with non-standard ftz/daz mode
349 * And, right now we only disable denorms with jited code on x86/sse
350 * (albeit this should be classified as a bug) so to get results which
351 * match we must only flush them to zero here in that case too.
352 */
353 union fi fi_val;
354
355 fi_val.f = val;
356
357 #if defined(PIPE_ARCH_SSE)
358 if (util_cpu_caps.has_sse) {
359 if ((fi_val.ui & 0x7f800000) == 0) {
360 fi_val.ui &= 0xff800000;
361 }
362 }
363 #endif
364
365 return fi_val.f;
366 }
367
368 /*
369 * Test one LLVM unary arithmetic builder function.
370 */
371 static boolean
372 test_unary(unsigned verbose, FILE *fp, const struct unary_test_t *test)
373 {
374 struct gallivm_state *gallivm;
375 LLVMValueRef test_func;
376 unary_func_t test_func_jit;
377 boolean success = TRUE;
378 int i, j;
379 int length = lp_native_vector_width / 32;
380 float *in, *out;
381
382 in = align_malloc(length * 4, length * 4);
383 out = align_malloc(length * 4, length * 4);
384
385 /* random NaNs or 0s could wreak havoc */
386 for (i = 0; i < length; i++) {
387 in[i] = 1.0;
388 }
389
390 gallivm = gallivm_create("test_module", LLVMGetGlobalContext());
391
392 test_func = build_unary_test_func(gallivm, test);
393
394 gallivm_compile_module(gallivm);
395
396 test_func_jit = (unary_func_t) gallivm_jit_function(gallivm, test_func);
397
398 gallivm_free_ir(gallivm);
399
400 for (j = 0; j < (test->num_values + length - 1) / length; j++) {
401 int num_vals = ((j + 1) * length <= test->num_values) ? length :
402 test->num_values % length;
403
404 for (i = 0; i < num_vals; ++i) {
405 in[i] = test->values[i+j*length];
406 }
407
408 test_func_jit(out, in);
409 for (i = 0; i < num_vals; ++i) {
410 float testval, ref;
411 double error, precision;
412 bool pass;
413
414 testval = flush_denorm_to_zero(in[i]);
415 ref = flush_denorm_to_zero(test->ref(testval));
416
417 if (util_inf_sign(ref) && util_inf_sign(out[i]) == util_inf_sign(ref)) {
418 error = 0;
419 } else {
420 error = fabs(out[i] - ref);
421 }
422 precision = error ? -log2(error/fabs(ref)) : FLT_MANT_DIG;
423
424 pass = precision >= test->precision;
425
426 if (isnan(ref)) {
427 continue;
428 }
429
430 if (!pass || verbose) {
431 printf("%s(%.9g): ref = %.9g, out = %.9g, precision = %f bits, %s\n",
432 test->name, in[i], ref, out[i], precision,
433 pass ? "PASS" : "FAIL");
434 fflush(stdout);
435 }
436
437 if (!pass) {
438 success = FALSE;
439 }
440 }
441 }
442
443 gallivm_destroy(gallivm);
444
445 align_free(in);
446 align_free(out);
447
448 return success;
449 }
450
451
452 boolean
453 test_all(unsigned verbose, FILE *fp)
454 {
455 boolean success = TRUE;
456 int i;
457
458 for (i = 0; i < Elements(unary_tests); ++i) {
459 if (!test_unary(verbose, fp, &unary_tests[i])) {
460 success = FALSE;
461 }
462 }
463
464 return success;
465 }
466
467
468 boolean
469 test_some(unsigned verbose, FILE *fp,
470 unsigned long n)
471 {
472 /*
473 * Not randomly generated test cases, so test all.
474 */
475
476 return test_all(verbose, fp);
477 }
478
479
480 boolean
481 test_single(unsigned verbose, FILE *fp)
482 {
483 return TRUE;
484 }