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