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
write_tsv_header(FILE * fp)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
negf(float x)93 static float negf(float x)
94 {
95 return -x;
96 }
97
98
sgnf(float x)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 sgn_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 exp2_values[] = {
134 -INFINITY,
135 -60,
136 -4,
137 -2,
138 -1,
139 -1e-007,
140 0,
141 1e-007,
142 0.01,
143 0.1,
144 0.9,
145 0.99,
146 1,
147 2,
148 4,
149 60,
150 INFINITY,
151 NAN
152 };
153
154
155 const float log2_values[] = {
156 #if 0
157 /*
158 * Smallest denormalized number; meant just for experimentation, but not
159 * validation.
160 */
161 1.4012984643248171e-45,
162 #endif
163 -INFINITY,
164 0,
165 1e-007,
166 0.1,
167 0.5,
168 0.99,
169 1,
170 1.01,
171 1.1,
172 1.9,
173 1.99,
174 2,
175 4,
176 100000,
177 1e+018,
178 INFINITY,
179 NAN
180 };
181
182
rcpf(float x)183 static float rcpf(float x)
184 {
185 return 1.0/x;
186 }
187
188
189 const float rcp_values[] = {
190 -0.0, 0.0,
191 -1.0, 1.0,
192 -1e-007, 1e-007,
193 -4.0, 4.0,
194 -1e+035, -100000,
195 100000, 1e+035,
196 5.88e-39f, // denormal
197 #if (__STDC_VERSION__ >= 199901L)
198 INFINITY, -INFINITY,
199 #endif
200 };
201
202
rsqrtf(float x)203 static float rsqrtf(float x)
204 {
205 return 1.0/(float)sqrt(x);
206 }
207
208
209 const float rsqrt_values[] = {
210 // http://msdn.microsoft.com/en-us/library/windows/desktop/bb147346.aspx
211 0.0, // must yield infinity
212 1.0, // must yield 1.0
213 1e-007, 4.0,
214 100000, 1e+035,
215 5.88e-39f, // denormal
216 #if (__STDC_VERSION__ >= 199901L)
217 INFINITY,
218 #endif
219 };
220
221
222 const float sincos_values[] = {
223 -INFINITY,
224 -5*M_PI/4,
225 -4*M_PI/4,
226 -4*M_PI/4,
227 -3*M_PI/4,
228 -2*M_PI/4,
229 -1*M_PI/4,
230 1*M_PI/4,
231 2*M_PI/4,
232 3*M_PI/4,
233 4*M_PI/4,
234 5*M_PI/4,
235 INFINITY,
236 NAN
237 };
238
239 const float round_values[] = {
240 -10.0, -1, 0.0, 12.0,
241 -1.49, -0.25, 1.25, 2.51,
242 -0.99, -0.01, 0.01, 0.99,
243 -1.5, -0.5, 0.5, 1.5,
244 1.401298464324817e-45f, // smallest denormal
245 -1.401298464324817e-45f,
246 1.62981451e-08f,
247 -1.62981451e-08f,
248 1.62981451e15f, // large number not representable as 32bit int
249 -1.62981451e15f,
250 FLT_EPSILON,
251 -FLT_EPSILON,
252 1.0f - 0.5f*FLT_EPSILON,
253 -1.0f + FLT_EPSILON,
254 FLT_MAX,
255 -FLT_MAX
256 };
257
fractf(float x)258 static float fractf(float x)
259 {
260 x -= floorf(x);
261 if (x >= 1.0f) {
262 // clamp to the largest number smaller than one
263 x = 1.0f - 0.5f*FLT_EPSILON;
264 }
265 return x;
266 }
267
268
269 const float fract_values[] = {
270 // http://en.wikipedia.org/wiki/IEEE_754-1985#Examples
271 0.0f,
272 -0.0f,
273 1.0f,
274 -1.0f,
275 0.5f,
276 -0.5f,
277 1.401298464324817e-45f, // smallest denormal
278 -1.401298464324817e-45f,
279 5.88e-39f, // middle denormal
280 1.18e-38f, // largest denormal
281 -1.18e-38f,
282 -1.62981451e-08f,
283 FLT_EPSILON,
284 -FLT_EPSILON,
285 1.0f - 0.5f*FLT_EPSILON,
286 -1.0f + FLT_EPSILON,
287 FLT_MAX,
288 -FLT_MAX
289 };
290
291
292 /*
293 * Unary test cases.
294 */
295
296 static const struct unary_test_t
297 unary_tests[] = {
298 {"abs", &lp_build_abs, &fabsf, sgn_values, ARRAY_SIZE(sgn_values), 20.0 },
299 {"neg", &lp_build_negate, &negf, sgn_values, ARRAY_SIZE(sgn_values), 20.0 },
300 {"sgn", &lp_build_sgn, &sgnf, sgn_values, ARRAY_SIZE(sgn_values), 20.0 },
301 {"exp2", &lp_build_exp2, &exp2f, exp2_values, ARRAY_SIZE(exp2_values), 18.0 },
302 {"log2", &lp_build_log2_safe, &log2f, log2_values, ARRAY_SIZE(log2_values), 20.0 },
303 {"exp", &lp_build_exp, &expf, exp2_values, ARRAY_SIZE(exp2_values), 18.0 },
304 {"log", &lp_build_log_safe, &logf, log2_values, ARRAY_SIZE(log2_values), 20.0 },
305 {"rcp", &lp_build_rcp, &rcpf, rcp_values, ARRAY_SIZE(rcp_values), 20.0 },
306 {"rsqrt", &lp_build_rsqrt, &rsqrtf, rsqrt_values, ARRAY_SIZE(rsqrt_values), 20.0 },
307 {"sin", &lp_build_sin, &sinf, sincos_values, ARRAY_SIZE(sincos_values), 20.0 },
308 {"cos", &lp_build_cos, &cosf, sincos_values, ARRAY_SIZE(sincos_values), 20.0 },
309 {"sgn", &lp_build_sgn, &sgnf, sgn_values, ARRAY_SIZE(sgn_values), 20.0 },
310 {"round", &lp_build_round, &nearbyintf, round_values, ARRAY_SIZE(round_values), 24.0 },
311 {"trunc", &lp_build_trunc, &truncf, round_values, ARRAY_SIZE(round_values), 24.0 },
312 {"floor", &lp_build_floor, &floorf, round_values, ARRAY_SIZE(round_values), 24.0 },
313 {"ceil", &lp_build_ceil, &ceilf, round_values, ARRAY_SIZE(round_values), 24.0 },
314 {"fract", &lp_build_fract_safe, &fractf, fract_values, ARRAY_SIZE(fract_values), 24.0 },
315 };
316
317
318 /*
319 * Build LLVM function that exercises the unary operator builder.
320 */
321 static LLVMValueRef
build_unary_test_func(struct gallivm_state * gallivm,const struct unary_test_t * test,unsigned length,const char * test_name)322 build_unary_test_func(struct gallivm_state *gallivm,
323 const struct unary_test_t *test,
324 unsigned length,
325 const char *test_name)
326 {
327 struct lp_type type = lp_type_float_vec(32, length * 32);
328 LLVMContextRef context = gallivm->context;
329 LLVMModuleRef module = gallivm->module;
330 LLVMTypeRef vf32t = lp_build_vec_type(gallivm, type);
331 LLVMTypeRef args[2] = { LLVMPointerType(vf32t, 0), LLVMPointerType(vf32t, 0) };
332 LLVMValueRef func = LLVMAddFunction(module, test_name,
333 LLVMFunctionType(LLVMVoidTypeInContext(context),
334 args, ARRAY_SIZE(args), 0));
335 LLVMValueRef arg0 = LLVMGetParam(func, 0);
336 LLVMValueRef arg1 = LLVMGetParam(func, 1);
337 LLVMBuilderRef builder = gallivm->builder;
338 LLVMBasicBlockRef block = LLVMAppendBasicBlockInContext(context, func, "entry");
339 LLVMValueRef ret;
340
341 struct lp_build_context bld;
342
343 lp_build_context_init(&bld, gallivm, type);
344
345 LLVMSetFunctionCallConv(func, LLVMCCallConv);
346
347 LLVMPositionBuilderAtEnd(builder, block);
348
349 arg1 = LLVMBuildLoad(builder, arg1, "");
350
351 ret = test->builder(&bld, arg1);
352
353 LLVMBuildStore(builder, ret, arg0);
354
355 LLVMBuildRetVoid(builder);
356
357 gallivm_verify_function(gallivm, func);
358
359 return func;
360 }
361
362
363 /*
364 * Flush denorms to zero.
365 */
366 static float
flush_denorm_to_zero(float val)367 flush_denorm_to_zero(float val)
368 {
369 /*
370 * If we have a denorm manually set it to (+-)0.
371 * This is because the reference may or may not do the right thing
372 * otherwise because we want the result according to treating all
373 * denormals as zero (FTZ/DAZ). Not using fpclassify because
374 * a) some compilers are stuck at c89 (msvc)
375 * b) not sure it reliably works with non-standard ftz/daz mode
376 * And, right now we only disable denorms with jited code on x86/sse
377 * (albeit this should be classified as a bug) so to get results which
378 * match we must only flush them to zero here in that case too.
379 */
380 union fi fi_val;
381
382 fi_val.f = val;
383
384 #if defined(PIPE_ARCH_SSE)
385 if (util_cpu_caps.has_sse) {
386 if ((fi_val.ui & 0x7f800000) == 0) {
387 fi_val.ui &= 0xff800000;
388 }
389 }
390 #endif
391
392 return fi_val.f;
393 }
394
395 /*
396 * Test one LLVM unary arithmetic builder function.
397 */
398 static boolean
test_unary(unsigned verbose,FILE * fp,const struct unary_test_t * test,unsigned length)399 test_unary(unsigned verbose, FILE *fp, const struct unary_test_t *test, unsigned length)
400 {
401 char test_name[128];
402 snprintf(test_name, sizeof test_name, "%s.v%u", test->name, length);
403 LLVMContextRef context;
404 struct gallivm_state *gallivm;
405 LLVMValueRef test_func;
406 unary_func_t test_func_jit;
407 boolean success = TRUE;
408 int i, j;
409 float *in, *out;
410
411 in = align_malloc(length * 4, length * 4);
412 out = align_malloc(length * 4, length * 4);
413
414 /* random NaNs or 0s could wreak havoc */
415 for (i = 0; i < length; i++) {
416 in[i] = 1.0;
417 }
418
419 context = LLVMContextCreate();
420 gallivm = gallivm_create("test_module", context, NULL);
421
422 test_func = build_unary_test_func(gallivm, test, length, test_name);
423
424 gallivm_compile_module(gallivm);
425
426 test_func_jit = (unary_func_t) gallivm_jit_function(gallivm, test_func);
427
428 gallivm_free_ir(gallivm);
429
430 for (j = 0; j < (test->num_values + length - 1) / length; j++) {
431 int num_vals = ((j + 1) * length <= test->num_values) ? length :
432 test->num_values % length;
433
434 for (i = 0; i < num_vals; ++i) {
435 in[i] = test->values[i+j*length];
436 }
437
438 test_func_jit(out, in);
439 for (i = 0; i < num_vals; ++i) {
440 float testval, ref;
441 double error, precision;
442 boolean expected_pass = TRUE;
443 bool pass;
444
445 testval = flush_denorm_to_zero(in[i]);
446 ref = flush_denorm_to_zero(test->ref(testval));
447
448 if (util_inf_sign(ref) && util_inf_sign(out[i]) == util_inf_sign(ref)) {
449 error = 0;
450 } else {
451 error = fabs(out[i] - ref);
452 }
453 precision = error ? -log2(error/fabs(ref)) : FLT_MANT_DIG;
454
455 pass = precision >= test->precision;
456
457 if (isnan(ref)) {
458 continue;
459 }
460
461 if (!util_cpu_caps.has_neon &&
462 test->ref == &nearbyintf && length == 2 &&
463 ref != roundf(testval)) {
464 /* FIXME: The generic (non SSE) path in lp_build_iround, which is
465 * always taken for length==2 regardless of native round support,
466 * does not round to even. */
467 expected_pass = FALSE;
468 }
469
470 if (test->ref == &expf && util_inf_sign(testval) == -1) {
471 /* Some older 64-bit MSVCRT versions return -inf instead of 0
472 * for expf(-inf). As detecting the VC runtime version is
473 * non-trivial, just ignore the test result. */
474 #if defined(_MSC_VER) && defined(_WIN64)
475 expected_pass = pass;
476 #endif
477 }
478
479 if (pass != expected_pass || verbose) {
480 printf("%s(%.9g): ref = %.9g, out = %.9g, precision = %f bits, %s%s\n",
481 test_name, in[i], ref, out[i], precision,
482 pass ? "PASS" : "FAIL",
483 !expected_pass ? (pass ? " (unexpected)" : " (expected)" ): "");
484 fflush(stdout);
485 }
486
487 if (pass != expected_pass) {
488 success = FALSE;
489 }
490 }
491 }
492
493 gallivm_destroy(gallivm);
494 LLVMContextDispose(context);
495
496 align_free(in);
497 align_free(out);
498
499 return success;
500 }
501
502
503 boolean
test_all(unsigned verbose,FILE * fp)504 test_all(unsigned verbose, FILE *fp)
505 {
506 boolean success = TRUE;
507 int i;
508
509 for (i = 0; i < ARRAY_SIZE(unary_tests); ++i) {
510 unsigned max_length = lp_native_vector_width / 32;
511 unsigned length;
512 for (length = 1; length <= max_length; length *= 2) {
513 if (!test_unary(verbose, fp, &unary_tests[i], length)) {
514 success = FALSE;
515 }
516 }
517 }
518
519 return success;
520 }
521
522
523 boolean
test_some(unsigned verbose,FILE * fp,unsigned long n)524 test_some(unsigned verbose, FILE *fp,
525 unsigned long n)
526 {
527 /*
528 * Not randomly generated test cases, so test all.
529 */
530
531 return test_all(verbose, fp);
532 }
533
534
535 boolean
test_single(unsigned verbose,FILE * fp)536 test_single(unsigned verbose, FILE *fp)
537 {
538 return TRUE;
539 }
540