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