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 #ifdef _MSC_VER
297 #define WRAP(func) \
298 static float \
299 wrap_ ## func(float x) \
300 { \
301 return func(x); \
302 }
303 WRAP(expf)
304 WRAP(logf)
305 WRAP(sinf)
306 WRAP(cosf)
307 WRAP(floorf)
308 WRAP(ceilf)
309 #define expf wrap_expf
310 #define logf wrap_logf
311 #define sinf wrap_sinf
312 #define cosf wrap_cosf
313 #define floorf wrap_floorf
314 #define ceilf wrap_ceilf
315 #endif
316
317 static const struct unary_test_t
318 unary_tests[] = {
319 {"abs", &lp_build_abs, &fabsf, sgn_values, ARRAY_SIZE(sgn_values), 20.0 },
320 {"neg", &lp_build_negate, &negf, sgn_values, ARRAY_SIZE(sgn_values), 20.0 },
321 {"sgn", &lp_build_sgn, &sgnf, sgn_values, ARRAY_SIZE(sgn_values), 20.0 },
322 {"exp2", &lp_build_exp2, &exp2f, exp2_values, ARRAY_SIZE(exp2_values), 18.0 },
323 {"log2", &lp_build_log2_safe, &log2f, log2_values, ARRAY_SIZE(log2_values), 20.0 },
324 {"exp", &lp_build_exp, &expf, exp2_values, ARRAY_SIZE(exp2_values), 18.0 },
325 {"log", &lp_build_log_safe, &logf, log2_values, ARRAY_SIZE(log2_values), 20.0 },
326 {"rcp", &lp_build_rcp, &rcpf, rcp_values, ARRAY_SIZE(rcp_values), 20.0 },
327 {"rsqrt", &lp_build_rsqrt, &rsqrtf, rsqrt_values, ARRAY_SIZE(rsqrt_values), 20.0 },
328 {"sin", &lp_build_sin, &sinf, sincos_values, ARRAY_SIZE(sincos_values), 20.0 },
329 {"cos", &lp_build_cos, &cosf, sincos_values, ARRAY_SIZE(sincos_values), 20.0 },
330 {"sgn", &lp_build_sgn, &sgnf, sgn_values, ARRAY_SIZE(sgn_values), 20.0 },
331 {"round", &lp_build_round, &nearbyintf, round_values, ARRAY_SIZE(round_values), 24.0 },
332 {"trunc", &lp_build_trunc, &truncf, round_values, ARRAY_SIZE(round_values), 24.0 },
333 {"floor", &lp_build_floor, &floorf, round_values, ARRAY_SIZE(round_values), 24.0 },
334 {"ceil", &lp_build_ceil, &ceilf, round_values, ARRAY_SIZE(round_values), 24.0 },
335 {"fract", &lp_build_fract_safe, &fractf, fract_values, ARRAY_SIZE(fract_values), 24.0 },
336 };
337
338
339 /*
340 * Build LLVM function that exercises the unary operator builder.
341 */
342 static LLVMValueRef
build_unary_test_func(struct gallivm_state * gallivm,const struct unary_test_t * test,unsigned length,const char * test_name)343 build_unary_test_func(struct gallivm_state *gallivm,
344 const struct unary_test_t *test,
345 unsigned length,
346 const char *test_name)
347 {
348 struct lp_type type = lp_type_float_vec(32, length * 32);
349 LLVMContextRef context = gallivm->context;
350 LLVMModuleRef module = gallivm->module;
351 LLVMTypeRef vf32t = lp_build_vec_type(gallivm, type);
352 LLVMTypeRef args[2] = { LLVMPointerType(vf32t, 0), LLVMPointerType(vf32t, 0) };
353 LLVMValueRef func = LLVMAddFunction(module, test_name,
354 LLVMFunctionType(LLVMVoidTypeInContext(context),
355 args, ARRAY_SIZE(args), 0));
356 LLVMValueRef arg0 = LLVMGetParam(func, 0);
357 LLVMValueRef arg1 = LLVMGetParam(func, 1);
358 LLVMBuilderRef builder = gallivm->builder;
359 LLVMBasicBlockRef block = LLVMAppendBasicBlockInContext(context, func, "entry");
360 LLVMValueRef ret;
361
362 struct lp_build_context bld;
363
364 lp_build_context_init(&bld, gallivm, type);
365
366 LLVMSetFunctionCallConv(func, LLVMCCallConv);
367
368 LLVMPositionBuilderAtEnd(builder, block);
369
370 arg1 = LLVMBuildLoad(builder, arg1, "");
371
372 ret = test->builder(&bld, arg1);
373
374 LLVMBuildStore(builder, ret, arg0);
375
376 LLVMBuildRetVoid(builder);
377
378 gallivm_verify_function(gallivm, func);
379
380 return func;
381 }
382
383
384 /*
385 * Flush denorms to zero.
386 */
387 static float
flush_denorm_to_zero(float val)388 flush_denorm_to_zero(float val)
389 {
390 /*
391 * If we have a denorm manually set it to (+-)0.
392 * This is because the reference may or may not do the right thing
393 * otherwise because we want the result according to treating all
394 * denormals as zero (FTZ/DAZ). Not using fpclassify because
395 * a) some compilers are stuck at c89 (msvc)
396 * b) not sure it reliably works with non-standard ftz/daz mode
397 * And, right now we only disable denorms with jited code on x86/sse
398 * (albeit this should be classified as a bug) so to get results which
399 * match we must only flush them to zero here in that case too.
400 */
401 union fi fi_val;
402
403 fi_val.f = val;
404
405 #if defined(PIPE_ARCH_SSE)
406 if (util_get_cpu_caps()->has_sse) {
407 if ((fi_val.ui & 0x7f800000) == 0) {
408 fi_val.ui &= 0xff800000;
409 }
410 }
411 #endif
412
413 return fi_val.f;
414 }
415
416 /*
417 * Test one LLVM unary arithmetic builder function.
418 */
419 static boolean
test_unary(unsigned verbose,FILE * fp,const struct unary_test_t * test,unsigned length)420 test_unary(unsigned verbose, FILE *fp, const struct unary_test_t *test, unsigned length)
421 {
422 char test_name[128];
423 snprintf(test_name, sizeof test_name, "%s.v%u", test->name, length);
424 LLVMContextRef context;
425 struct gallivm_state *gallivm;
426 LLVMValueRef test_func;
427 unary_func_t test_func_jit;
428 boolean success = TRUE;
429 int i, j;
430 float *in, *out;
431
432 in = align_malloc(length * 4, length * 4);
433 out = align_malloc(length * 4, length * 4);
434
435 /* random NaNs or 0s could wreak havoc */
436 for (i = 0; i < length; i++) {
437 in[i] = 1.0;
438 }
439
440 context = LLVMContextCreate();
441 gallivm = gallivm_create("test_module", context, NULL);
442
443 test_func = build_unary_test_func(gallivm, test, length, test_name);
444
445 gallivm_compile_module(gallivm);
446
447 test_func_jit = (unary_func_t) gallivm_jit_function(gallivm, test_func);
448
449 gallivm_free_ir(gallivm);
450
451 for (j = 0; j < (test->num_values + length - 1) / length; j++) {
452 int num_vals = ((j + 1) * length <= test->num_values) ? length :
453 test->num_values % length;
454
455 for (i = 0; i < num_vals; ++i) {
456 in[i] = test->values[i+j*length];
457 }
458
459 test_func_jit(out, in);
460 for (i = 0; i < num_vals; ++i) {
461 float testval, ref;
462 double error, precision;
463 boolean expected_pass = TRUE;
464 bool pass;
465
466 testval = flush_denorm_to_zero(in[i]);
467 ref = flush_denorm_to_zero(test->ref(testval));
468
469 if (util_inf_sign(ref) && util_inf_sign(out[i]) == util_inf_sign(ref)) {
470 error = 0;
471 } else {
472 error = fabs(out[i] - ref);
473 }
474 precision = error ? -log2(error/fabs(ref)) : FLT_MANT_DIG;
475
476 pass = precision >= test->precision;
477
478 if (isnan(ref)) {
479 continue;
480 }
481
482 if (!util_get_cpu_caps()->has_neon &&
483 test->ref == &nearbyintf && length == 2 &&
484 ref != roundf(testval)) {
485 /* FIXME: The generic (non SSE) path in lp_build_iround, which is
486 * always taken for length==2 regardless of native round support,
487 * does not round to even. */
488 expected_pass = FALSE;
489 }
490
491 if (test->ref == &expf && util_inf_sign(testval) == -1) {
492 /* Some older 64-bit MSVCRT versions return -inf instead of 0
493 * for expf(-inf). As detecting the VC runtime version is
494 * non-trivial, just ignore the test result. */
495 #if defined(_MSC_VER) && defined(_WIN64)
496 expected_pass = pass;
497 #endif
498 }
499
500 if (pass != expected_pass || verbose) {
501 printf("%s(%.9g): ref = %.9g, out = %.9g, precision = %f bits, %s%s\n",
502 test_name, in[i], ref, out[i], precision,
503 pass ? "PASS" : "FAIL",
504 !expected_pass ? (pass ? " (unexpected)" : " (expected)" ): "");
505 fflush(stdout);
506 }
507
508 if (pass != expected_pass) {
509 success = FALSE;
510 }
511 }
512 }
513
514 gallivm_destroy(gallivm);
515 LLVMContextDispose(context);
516
517 align_free(in);
518 align_free(out);
519
520 return success;
521 }
522
523
524 boolean
test_all(unsigned verbose,FILE * fp)525 test_all(unsigned verbose, FILE *fp)
526 {
527 boolean success = TRUE;
528 int i;
529
530 for (i = 0; i < ARRAY_SIZE(unary_tests); ++i) {
531 unsigned max_length = lp_native_vector_width / 32;
532 unsigned length;
533 for (length = 1; length <= max_length; length *= 2) {
534 if (!test_unary(verbose, fp, &unary_tests[i], length)) {
535 success = FALSE;
536 }
537 }
538 }
539
540 return success;
541 }
542
543
544 boolean
test_some(unsigned verbose,FILE * fp,unsigned long n)545 test_some(unsigned verbose, FILE *fp,
546 unsigned long n)
547 {
548 /*
549 * Not randomly generated test cases, so test all.
550 */
551
552 return test_all(verbose, fp);
553 }
554
555
556 boolean
test_single(unsigned verbose,FILE * fp)557 test_single(unsigned verbose, FILE *fp)
558 {
559 return TRUE;
560 }
561