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
37#include "gallivm/lp_bld.h"
38#include "gallivm/lp_bld_debug.h"
39#include "gallivm/lp_bld_init.h"
40#include "gallivm/lp_bld_arit.h"
41
42#include "lp_test.h"
43
44
45void
46write_tsv_header(FILE *fp)
47{
48   fprintf(fp,
49           "result\t"
50           "format\n");
51
52   fflush(fp);
53}
54
55
56typedef void (*unary_func_t)(float *out, const float *in);
57
58
59/**
60 * Describe a test case of one unary function.
61 */
62struct 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
92static float negf(float x)
93{
94   return -x;
95}
96
97
98static 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
110const float exp2_values[] = {
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};
127
128
129const float log2_values[] = {
130#if 0
131   /*
132    * Smallest denormalized number; meant just for experimentation, but not
133    * validation.
134    */
135   1.4012984643248171e-45,
136#endif
137   1e-007,
138   0.1,
139   0.5,
140   0.99,
141   1,
142   1.01,
143   1.1,
144   1.9,
145   1.99,
146   2,
147   4,
148   100000,
149   1e+018
150};
151
152
153static float rsqrtf(float x)
154{
155   return 1.0/sqrt(x);
156}
157
158
159const float rsqrt_values[] = {
160   -1, -1e-007,
161   1e-007, 1,
162   -4, -1,
163   1, 4,
164   -1e+035, -100000,
165   100000, 1e+035,
166};
167
168
169const float sincos_values[] = {
170   -5*M_PI/4,
171   -4*M_PI/4,
172   -4*M_PI/4,
173   -3*M_PI/4,
174   -2*M_PI/4,
175   -1*M_PI/4,
176    1*M_PI/4,
177    2*M_PI/4,
178    3*M_PI/4,
179    4*M_PI/4,
180    5*M_PI/4,
181};
182
183const float round_values[] = {
184      -10.0, -1, 0.0, 12.0,
185      -1.49, -0.25, 1.25, 2.51,
186      -0.99, -0.01, 0.01, 0.99,
187};
188
189static float fractf(float x)
190{
191   x -= floorf(x);
192   if (x >= 1.0f) {
193      // clamp to the largest number smaller than one
194      x = 1.0f - 0.5f*FLT_EPSILON;
195   }
196   return x;
197}
198
199
200const float fract_values[] = {
201   // http://en.wikipedia.org/wiki/IEEE_754-1985#Examples
202   0.0f,
203   -0.0f,
204   1.0f,
205   -1.0f,
206   0.5f,
207   -0.5f,
208   1.401298464324817e-45f, // smallest denormal
209   -1.401298464324817e-45f,
210   5.88e-39f, // middle denormal
211   1.18e-38f, // largest denormal
212   -1.18e-38f,
213   -1.62981451e-08f,
214   FLT_EPSILON,
215   -FLT_EPSILON,
216   1.0f - 0.5f*FLT_EPSILON,
217   -1.0f + FLT_EPSILON,
218   FLT_MAX,
219   -FLT_MAX
220};
221
222
223/*
224 * Unary test cases.
225 */
226
227static const struct unary_test_t
228unary_tests[] = {
229   {"neg", &lp_build_negate, &negf, exp2_values, Elements(exp2_values), 20.0 },
230   {"exp2", &lp_build_exp2, &exp2f, exp2_values, Elements(exp2_values), 20.0 },
231   {"log2", &lp_build_log2, &log2f, log2_values, Elements(log2_values), 20.0 },
232   {"exp", &lp_build_exp, &expf, exp2_values, Elements(exp2_values), 18.0 },
233   {"log", &lp_build_log, &logf, log2_values, Elements(log2_values), 20.0 },
234   {"rsqrt", &lp_build_rsqrt, &rsqrtf, rsqrt_values, Elements(rsqrt_values), 20.0 },
235   {"sin", &lp_build_sin, &sinf, sincos_values, Elements(sincos_values), 20.0 },
236   {"cos", &lp_build_cos, &cosf, sincos_values, Elements(sincos_values), 20.0 },
237   {"sgn", &lp_build_sgn, &sgnf, exp2_values, Elements(exp2_values), 20.0 },
238   {"round", &lp_build_round, &roundf, round_values, Elements(round_values), 24.0 },
239   {"trunc", &lp_build_trunc, &truncf, round_values, Elements(round_values), 24.0 },
240   {"floor", &lp_build_floor, &floorf, round_values, Elements(round_values), 24.0 },
241   {"ceil", &lp_build_ceil, &ceilf, round_values, Elements(round_values), 24.0 },
242   {"fract", &lp_build_fract_safe, &fractf, fract_values, Elements(fract_values), 24.0 },
243};
244
245
246/*
247 * Build LLVM function that exercises the unary operator builder.
248 */
249static LLVMValueRef
250build_unary_test_func(struct gallivm_state *gallivm,
251                      const struct unary_test_t *test)
252{
253   struct lp_type type = lp_type_float_vec(32, lp_native_vector_width);
254   LLVMContextRef context = gallivm->context;
255   LLVMModuleRef module = gallivm->module;
256   LLVMTypeRef vf32t = lp_build_vec_type(gallivm, type);
257   LLVMTypeRef args[2] = { LLVMPointerType(vf32t, 0), LLVMPointerType(vf32t, 0) };
258   LLVMValueRef func = LLVMAddFunction(module, test->name,
259                                       LLVMFunctionType(LLVMVoidTypeInContext(context),
260                                                        args, Elements(args), 0));
261   LLVMValueRef arg0 = LLVMGetParam(func, 0);
262   LLVMValueRef arg1 = LLVMGetParam(func, 1);
263   LLVMBuilderRef builder = gallivm->builder;
264   LLVMBasicBlockRef block = LLVMAppendBasicBlockInContext(context, func, "entry");
265   LLVMValueRef ret;
266
267   struct lp_build_context bld;
268
269   lp_build_context_init(&bld, gallivm, type);
270
271   LLVMSetFunctionCallConv(func, LLVMCCallConv);
272
273   LLVMPositionBuilderAtEnd(builder, block);
274
275   arg1 = LLVMBuildLoad(builder, arg1, "");
276
277   ret = test->builder(&bld, arg1);
278
279   LLVMBuildStore(builder, ret, arg0);
280
281   LLVMBuildRetVoid(builder);
282
283   gallivm_verify_function(gallivm, func);
284
285   return func;
286}
287
288
289/*
290 * Test one LLVM unary arithmetic builder function.
291 */
292static boolean
293test_unary(unsigned verbose, FILE *fp, const struct unary_test_t *test)
294{
295   struct gallivm_state *gallivm;
296   LLVMValueRef test_func;
297   unary_func_t test_func_jit;
298   boolean success = TRUE;
299   int i, j;
300   int length = lp_native_vector_width / 32;
301   float *in, *out;
302
303   in = align_malloc(length * 4, length * 4);
304   out = align_malloc(length * 4, length * 4);
305
306   /* random NaNs or 0s could wreak havoc */
307   for (i = 0; i < length; i++) {
308      in[i] = 1.0;
309   }
310
311   gallivm = gallivm_create();
312
313   test_func = build_unary_test_func(gallivm, test);
314
315   gallivm_compile_module(gallivm);
316
317   test_func_jit = (unary_func_t) gallivm_jit_function(gallivm, test_func);
318
319   for (j = 0; j < (test->num_values + length - 1) / length; j++) {
320      int num_vals = ((j + 1) * length <= test->num_values) ? length :
321                                                              test->num_values % length;
322
323      for (i = 0; i < num_vals; ++i) {
324         in[i] = test->values[i+j*length];
325      }
326
327      test_func_jit(out, in);
328      for (i = 0; i < num_vals; ++i) {
329         float ref = test->ref(in[i]);
330         double error, precision;
331         bool pass;
332
333         error = fabs(out[i] - ref);
334         precision = error ? -log2(error/fabs(ref)) : FLT_MANT_DIG;
335
336         pass = precision >= test->precision;
337
338         if (isnan(ref)) {
339            continue;
340         }
341
342         if (!pass || verbose) {
343            printf("%s(%.9g): ref = %.9g, out = %.9g, precision = %f bits, %s\n",
344                  test->name, in[i], ref, out[i], precision,
345                  pass ? "PASS" : "FAIL");
346         }
347
348         if (!pass) {
349            success = FALSE;
350         }
351      }
352   }
353
354   gallivm_free_function(gallivm, test_func, test_func_jit);
355
356   gallivm_destroy(gallivm);
357
358   align_free(in);
359   align_free(out);
360
361   return success;
362}
363
364
365boolean
366test_all(unsigned verbose, FILE *fp)
367{
368   boolean success = TRUE;
369   int i;
370
371   for (i = 0; i < Elements(unary_tests); ++i) {
372      if (!test_unary(verbose, fp, &unary_tests[i])) {
373         success = FALSE;
374      }
375   }
376
377   return success;
378}
379
380
381boolean
382test_some(unsigned verbose, FILE *fp,
383          unsigned long n)
384{
385   /*
386    * Not randomly generated test cases, so test all.
387    */
388
389   return test_all(verbose, fp);
390}
391
392
393boolean
394test_single(unsigned verbose, FILE *fp)
395{
396   return TRUE;
397}
398