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