• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_memory.h"
34 #include "util/u_math.h"
35 #include "util/u_cpu_detect.h"
36 
37 #include "gallivm/lp_bld.h"
38 #include "gallivm/lp_bld_init.h"
39 #include "gallivm/lp_bld_arit.h"
40 
41 #include "lp_test.h"
42 
43 
44 void
write_tsv_header(FILE * fp)45 write_tsv_header(FILE *fp)
46 {
47    fprintf(fp,
48            "result\t"
49            "format\n");
50 
51    fflush(fp);
52 }
53 
54 
55 typedef void (*unary_func_t)(float *out, const float *in);
56 
57 
58 /**
59  * Describe a test case of one unary function.
60  */
61 struct unary_test_t
62 {
63    /*
64     * Test name -- name of the mathematical function under test.
65     */
66 
67    const char *name;
68 
69    LLVMValueRef
70    (*builder)(struct lp_build_context *bld, LLVMValueRef a);
71 
72    /*
73     * Reference (pure-C) function.
74     */
75    float
76    (*ref)(float a);
77 
78    /*
79     * Test values.
80     */
81    const float *values;
82    unsigned num_values;
83 
84    /*
85     * Required precision in bits.
86     */
87    double precision;
88 };
89 
90 
negf(float x)91 static float negf(float x)
92 {
93    return -x;
94 }
95 
96 
sgnf(float x)97 static float sgnf(float x)
98 {
99    if (x > 0.0f) {
100       return 1.0f;
101    }
102    if (x < 0.0f) {
103       return -1.0f;
104    }
105    return 0.0f;
106 }
107 
108 
109 const float sgn_values[] = {
110    -INFINITY,
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    INFINITY,
127    NAN
128 };
129 
130 
131 const float exp2_values[] = {
132    -INFINITY,
133    -60,
134    -4,
135    -2,
136    -1,
137    -1e-007,
138    0,
139    1e-007,
140    0.01,
141    0.1,
142    0.9,
143    0.99,
144    1,
145    2,
146    4,
147    60,
148    INFINITY,
149    NAN
150 };
151 
152 
153 const float log2_values[] = {
154 #if 0
155    /*
156     * Smallest denormalized number; meant just for experimentation, but not
157     * validation.
158     */
159    1.4012984643248171e-45,
160 #endif
161    -INFINITY,
162    0,
163    1e-007,
164    0.1,
165    0.5,
166    0.99,
167    1,
168    1.01,
169    1.1,
170    1.9,
171    1.99,
172    2,
173    4,
174    100000,
175    1e+018,
176    INFINITY,
177    NAN
178 };
179 
180 
rcpf(float x)181 static float rcpf(float x)
182 {
183    return 1.0/x;
184 }
185 
186 
187 const float rcp_values[] = {
188    -0.0, 0.0,
189    -1.0, 1.0,
190    -1e-007, 1e-007,
191    -4.0, 4.0,
192    -1e+035, -100000,
193    100000, 1e+035,
194    5.88e-39f, // denormal
195    INFINITY, -INFINITY,
196 };
197 
198 
rsqrtf(float x)199 static float rsqrtf(float x)
200 {
201    return 1.0/(float)sqrt(x);
202 }
203 
204 
205 const float rsqrt_values[] = {
206    // http://msdn.microsoft.com/en-us/library/windows/desktop/bb147346.aspx
207    0.0, // must yield infinity
208    1.0, // must yield 1.0
209    1e-007, 4.0,
210    100000, 1e+035,
211    5.88e-39f, // denormal
212    INFINITY,
213 };
214 
215 
216 const float sincos_values[] = {
217    -INFINITY,
218    -5*M_PI/4,
219    -4*M_PI/4,
220    -4*M_PI/4,
221    -3*M_PI/4,
222    -2*M_PI/4,
223    -1*M_PI/4,
224    1*M_PI/4,
225    2*M_PI/4,
226    3*M_PI/4,
227    4*M_PI/4,
228    5*M_PI/4,
229    INFINITY,
230    NAN
231 };
232 
233 const float round_values[] = {
234       -10.0, -1, 0.0, 12.0,
235       -1.49, -0.25, 1.25, 2.51,
236       -0.99, -0.01, 0.01, 0.99,
237       -1.5, -0.5, 0.5, 1.5,
238       1.401298464324817e-45f, // smallest denormal
239       -1.401298464324817e-45f,
240       1.62981451e-08f,
241       -1.62981451e-08f,
242       1.62981451e15f, // large number not representable as 32bit int
243       -1.62981451e15f,
244       FLT_EPSILON,
245       -FLT_EPSILON,
246       1.0f - 0.5f*FLT_EPSILON,
247       -1.0f + FLT_EPSILON,
248       FLT_MAX,
249       -FLT_MAX
250 };
251 
fractf(float x)252 static float fractf(float x)
253 {
254    x -= floorf(x);
255    if (x >= 1.0f) {
256       // clamp to the largest number smaller than one
257       x = 1.0f - 0.5f*FLT_EPSILON;
258    }
259    return x;
260 }
261 
262 
263 const float fract_values[] = {
264    // http://en.wikipedia.org/wiki/IEEE_754-1985#Examples
265    0.0f,
266    -0.0f,
267    1.0f,
268    -1.0f,
269    0.5f,
270    -0.5f,
271    1.401298464324817e-45f, // smallest denormal
272    -1.401298464324817e-45f,
273    5.88e-39f, // middle denormal
274    1.18e-38f, // largest denormal
275    -1.18e-38f,
276    -1.62981451e-08f,
277    FLT_EPSILON,
278    -FLT_EPSILON,
279    1.0f - 0.5f*FLT_EPSILON,
280    -1.0f + FLT_EPSILON,
281    FLT_MAX,
282    -FLT_MAX
283 };
284 
285 
286 /*
287  * Unary test cases.
288  */
289 
290 #ifdef _MSC_VER
291 #define WRAP(func) \
292 static float \
293 wrap_ ## func(float x) \
294 { \
295    return func(x); \
296 }
297 WRAP(log2f)
298 WRAP(expf)
299 WRAP(logf)
300 WRAP(sinf)
301 WRAP(cosf)
302 WRAP(nearbyintf)
303 WRAP(floorf)
304 WRAP(ceilf)
305 #define log2f wrap_log2f
306 #define expf wrap_expf
307 #define logf wrap_logf
308 #define sinf wrap_sinf
309 #define cosf wrap_cosf
310 #define nearbyintf wrap_nearbyintf
311 #define floorf wrap_floorf
312 #define ceilf wrap_ceilf
313 #endif
314 
315 static const struct unary_test_t
316 unary_tests[] = {
317    {"abs", &lp_build_abs, &fabsf, sgn_values, ARRAY_SIZE(sgn_values), 20.0 },
318    {"neg", &lp_build_negate, &negf, sgn_values, ARRAY_SIZE(sgn_values), 20.0 },
319    {"sgn", &lp_build_sgn, &sgnf, sgn_values, ARRAY_SIZE(sgn_values), 20.0 },
320    {"exp2", &lp_build_exp2, &exp2f, exp2_values, ARRAY_SIZE(exp2_values), 18.0 },
321    {"log2", &lp_build_log2_safe, &log2f, log2_values, ARRAY_SIZE(log2_values), 20.0 },
322    {"exp", &lp_build_exp, &expf, exp2_values, ARRAY_SIZE(exp2_values), 18.0 },
323    {"log", &lp_build_log_safe, &logf, log2_values, ARRAY_SIZE(log2_values), 20.0 },
324    {"rcp", &lp_build_rcp, &rcpf, rcp_values, ARRAY_SIZE(rcp_values), 20.0 },
325    {"rsqrt", &lp_build_rsqrt, &rsqrtf, rsqrt_values, ARRAY_SIZE(rsqrt_values), 20.0 },
326    {"sin", &lp_build_sin, &sinf, sincos_values, ARRAY_SIZE(sincos_values), 20.0 },
327    {"cos", &lp_build_cos, &cosf, sincos_values, ARRAY_SIZE(sincos_values), 20.0 },
328    {"sgn", &lp_build_sgn, &sgnf, sgn_values, ARRAY_SIZE(sgn_values), 20.0 },
329    {"round", &lp_build_round, &nearbyintf, round_values, ARRAY_SIZE(round_values), 24.0 },
330    {"trunc", &lp_build_trunc, &truncf, round_values, ARRAY_SIZE(round_values), 24.0 },
331    {"floor", &lp_build_floor, &floorf, round_values, ARRAY_SIZE(round_values), 24.0 },
332    {"ceil", &lp_build_ceil, &ceilf, round_values, ARRAY_SIZE(round_values), 24.0 },
333    {"fract", &lp_build_fract_safe, &fractf, fract_values, ARRAY_SIZE(fract_values), 24.0 },
334 };
335 
336 
337 /*
338  * Build LLVM function that exercises the unary operator builder.
339  */
340 static LLVMValueRef
build_unary_test_func(struct gallivm_state * gallivm,const struct unary_test_t * test,unsigned length,const char * test_name)341 build_unary_test_func(struct gallivm_state *gallivm,
342                       const struct unary_test_t *test,
343                       unsigned length,
344                       const char *test_name)
345 {
346    struct lp_type type = lp_type_float_vec(32, length * 32);
347    LLVMContextRef context = gallivm->context;
348    LLVMModuleRef module = gallivm->module;
349    LLVMTypeRef vf32t = lp_build_vec_type(gallivm, type);
350    LLVMTypeRef args[2] = { LLVMPointerType(vf32t, 0), LLVMPointerType(vf32t, 0) };
351    LLVMValueRef func = LLVMAddFunction(module, test_name,
352                                        LLVMFunctionType(LLVMVoidTypeInContext(context),
353                                                         args, ARRAY_SIZE(args), 0));
354    LLVMValueRef arg0 = LLVMGetParam(func, 0);
355    LLVMValueRef arg1 = LLVMGetParam(func, 1);
356    LLVMBuilderRef builder = gallivm->builder;
357    LLVMBasicBlockRef block = LLVMAppendBasicBlockInContext(context, func, "entry");
358    LLVMValueRef ret;
359 
360    struct lp_build_context bld;
361 
362    lp_build_context_init(&bld, gallivm, type);
363 
364    LLVMSetFunctionCallConv(func, LLVMCCallConv);
365 
366    LLVMPositionBuilderAtEnd(builder, block);
367 
368    arg1 = LLVMBuildLoad2(builder, vf32t, arg1, "");
369 
370    ret = test->builder(&bld, arg1);
371 
372    LLVMBuildStore(builder, ret, arg0);
373 
374    LLVMBuildRetVoid(builder);
375 
376    gallivm_verify_function(gallivm, func);
377 
378    return func;
379 }
380 
381 
382 /*
383  * Flush denorms to zero.
384  */
385 static float
flush_denorm_to_zero(float val)386 flush_denorm_to_zero(float val)
387 {
388    /*
389     * If we have a denorm manually set it to (+-)0.
390     * This is because the reference may or may not do the right thing
391     * otherwise because we want the result according to treating all
392     * denormals as zero (FTZ/DAZ). Not using fpclassify because
393     * a) some compilers are stuck at c89 (msvc)
394     * b) not sure it reliably works with non-standard ftz/daz mode
395     * And, right now we only disable denorms with jited code on x86/sse
396     * (albeit this should be classified as a bug) so to get results which
397     * match we must only flush them to zero here in that case too.
398     */
399    union fi fi_val;
400 
401    fi_val.f = val;
402 
403 #if DETECT_ARCH_SSE
404    if (util_get_cpu_caps()->has_sse) {
405       if ((fi_val.ui & 0x7f800000) == 0) {
406          fi_val.ui &= 0xff800000;
407       }
408    }
409 #endif
410 
411    return fi_val.f;
412 }
413 
414 /*
415  * Test one LLVM unary arithmetic builder function.
416  */
417 static bool
test_unary(unsigned verbose,FILE * fp,const struct unary_test_t * test,unsigned length)418 test_unary(unsigned verbose, FILE *fp, const struct unary_test_t *test, unsigned length)
419 {
420    char test_name[128];
421    snprintf(test_name, sizeof test_name, "%s.v%u", test->name, length);
422    lp_context_ref context;
423    struct gallivm_state *gallivm;
424    LLVMValueRef test_func;
425    unary_func_t test_func_jit;
426    bool success = true;
427    int i, j;
428    float *in, *out;
429 
430    in = align_malloc(length * 4, length * 4);
431    out = align_malloc(length * 4, length * 4);
432 
433    /* random NaNs or 0s could wreak havoc */
434    for (i = 0; i < length; i++) {
435       in[i] = 1.0;
436    }
437 
438    lp_context_create(&context);
439    gallivm = gallivm_create("test_module", &context, NULL);
440 
441    test_func = build_unary_test_func(gallivm, test, length, test_name);
442 
443    gallivm_compile_module(gallivm);
444 
445    test_func_jit = (unary_func_t) gallivm_jit_function(gallivm, test_func, test_name);
446 
447    gallivm_free_ir(gallivm);
448 
449    for (j = 0; j < (test->num_values + length - 1) / length; j++) {
450       int num_vals = ((j + 1) * length <= test->num_values) ? length :
451                                                               test->num_values % length;
452 
453       for (i = 0; i < num_vals; ++i) {
454          in[i] = test->values[i+j*length];
455       }
456 
457       test_func_jit(out, in);
458       for (i = 0; i < num_vals; ++i) {
459          float testval, ref;
460          double error, precision;
461          bool expected_pass = true;
462          bool pass;
463 
464          testval = flush_denorm_to_zero(in[i]);
465          ref = flush_denorm_to_zero(test->ref(testval));
466 
467          if (util_inf_sign(ref) && util_inf_sign(out[i]) == util_inf_sign(ref)) {
468             error = 0;
469          } else {
470             error = fabs(out[i] - ref);
471          }
472          precision = error ? -log2(error/fabs(ref)) : FLT_MANT_DIG;
473 
474          pass = precision >= test->precision;
475 
476          if (isnan(ref)) {
477             continue;
478          }
479 
480          if (test->ref == &nearbyintf && length == 2 &&
481              !util_get_cpu_caps()->has_neon &&
482              util_get_cpu_caps()->family != CPU_S390X &&
483              !(util_get_cpu_caps()->has_sse4_1 && LLVM_VERSION_MAJOR >= 8) &&
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    lp_context_destroy(&context);
516 
517    align_free(in);
518    align_free(out);
519 
520    return success;
521 }
522 
523 
524 bool
test_all(unsigned verbose,FILE * fp)525 test_all(unsigned verbose, FILE *fp)
526 {
527    bool 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 bool
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 bool
test_single(unsigned verbose,FILE * fp)557 test_single(unsigned verbose, FILE *fp)
558 {
559    return true;
560 }
561