Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / gallium / drivers / llvmpipe / lp_test_round.c
1 /**************************************************************************
2  *
3  * Copyright 2010 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 <stdlib.h>
30 #include <stdio.h>
31
32 #include "util/u_pointer.h"
33 #include "gallivm/lp_bld.h"
34 #include "gallivm/lp_bld_init.h"
35 #include "gallivm/lp_bld_arit.h"
36
37 #include "lp_test.h"
38
39
40 void
41 write_tsv_header(FILE *fp)
42 {
43    fprintf(fp,
44            "result\t"
45            "format\n");
46
47    fflush(fp);
48 }
49
50
51 #ifdef PIPE_ARCH_SSE
52
53 #define USE_SSE2
54 #include "sse_mathfun.h"
55
56 typedef __m128 (*test_round_t)(__m128);
57
58 typedef LLVMValueRef (*lp_func_t)(struct lp_build_context *, LLVMValueRef);
59
60
61 static LLVMValueRef
62 add_test(struct gallivm_state *gallivm, const char *name, lp_func_t lp_func)
63 {
64    LLVMModuleRef module = gallivm->module;
65    LLVMContextRef context = gallivm->context;
66    LLVMBuilderRef builder = gallivm->builder;
67
68    LLVMTypeRef v4sf = LLVMVectorType(LLVMFloatTypeInContext(context), 4);
69    LLVMTypeRef args[1] = { v4sf };
70    LLVMValueRef func = LLVMAddFunction(module, name, LLVMFunctionType(v4sf, args, 1, 0));
71    LLVMValueRef arg1 = LLVMGetParam(func, 0);
72    LLVMBasicBlockRef block = LLVMAppendBasicBlockInContext(context, func, "entry");
73    LLVMValueRef ret;
74    struct lp_build_context bld;
75
76    lp_build_context_init(&bld, gallivm, lp_float32_vec4_type());
77
78    LLVMSetFunctionCallConv(func, LLVMCCallConv);
79
80    LLVMPositionBuilderAtEnd(builder, block);
81
82    ret = lp_func(&bld, arg1);
83
84    LLVMBuildRet(builder, ret);
85
86    return func;
87 }
88
89 static void
90 printv(char* string, v4sf value)
91 {
92    v4sf v = value;
93    float *f = (float *)&v;
94    printf("%s: %10f %10f %10f %10f\n", string,
95            f[0], f[1], f[2], f[3]);
96 }
97
98 static boolean
99 compare(v4sf x, v4sf y)
100 {
101    boolean success = TRUE;
102    float *xp = (float *) &x;
103    float *yp = (float *) &y;
104    if (xp[0] != yp[0] ||
105        xp[1] != yp[1] ||
106        xp[2] != yp[2] ||
107        xp[3] != yp[3]) {
108       printf(" Incorrect result! ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
109       success = FALSE;
110    }
111    return success;
112 }
113
114
115
116 PIPE_ALIGN_STACK
117 static boolean
118 test_round(struct gallivm_state *gallivm, unsigned verbose, FILE *fp)
119 {
120    LLVMModuleRef module = gallivm->module;
121    LLVMValueRef test_round = NULL, test_trunc, test_floor, test_ceil;
122    LLVMExecutionEngineRef engine = gallivm->engine;
123    char *error = NULL;
124    test_round_t round_func, trunc_func, floor_func, ceil_func;
125    float unpacked[4];
126    unsigned packed;
127    boolean success = TRUE;
128    int i;
129
130    test_round = add_test(gallivm, "round", lp_build_round);
131    test_trunc = add_test(gallivm, "trunc", lp_build_trunc);
132    test_floor = add_test(gallivm, "floor", lp_build_floor);
133    test_ceil = add_test(gallivm, "ceil", lp_build_ceil);
134
135    if(LLVMVerifyModule(module, LLVMPrintMessageAction, &error)) {
136       printf("LLVMVerifyModule: %s\n", error);
137       LLVMDumpModule(module);
138       abort();
139    }
140    LLVMDisposeMessage(error);
141
142    round_func = (test_round_t) pointer_to_func(LLVMGetPointerToGlobal(engine, test_round));
143    trunc_func = (test_round_t) pointer_to_func(LLVMGetPointerToGlobal(engine, test_trunc));
144    floor_func = (test_round_t) pointer_to_func(LLVMGetPointerToGlobal(engine, test_floor));
145    ceil_func = (test_round_t) pointer_to_func(LLVMGetPointerToGlobal(engine, test_ceil));
146
147    memset(unpacked, 0, sizeof unpacked);
148    packed = 0;
149
150    if (0)
151       LLVMDumpModule(module);
152
153    for (i = 0; i < 3; i++) {
154       /* NOTE: There are several acceptable rules for x.5 rounding: ceiling,
155        * nearest even, etc. So we avoid testing such corner cases here.
156        */
157       v4sf xvals[3] = {
158          {-10.0, -1, 0, 12.0},
159          {-1.49, -0.25, 1.25, 2.51},
160          {-0.99, -0.01, 0.01, 0.99}
161       };
162       v4sf x = xvals[i];
163       v4sf y, ref;
164       float *xp = (float *) &x;
165       float *refp = (float *) &ref;
166
167       printf("\n");
168       printv("x            ", x);
169
170       refp[0] = round(xp[0]);
171       refp[1] = round(xp[1]);
172       refp[2] = round(xp[2]);
173       refp[3] = round(xp[3]);
174       y = round_func(x);
175       printv("C round(x)   ", ref);
176       printv("LLVM round(x)", y);
177       success = success && compare(ref, y);
178
179       refp[0] = trunc(xp[0]);
180       refp[1] = trunc(xp[1]);
181       refp[2] = trunc(xp[2]);
182       refp[3] = trunc(xp[3]);
183       y = trunc_func(x);
184       printv("C trunc(x)   ", ref);
185       printv("LLVM trunc(x)", y);
186       success = success && compare(ref, y);
187
188       refp[0] = floor(xp[0]);
189       refp[1] = floor(xp[1]);
190       refp[2] = floor(xp[2]);
191       refp[3] = floor(xp[3]);
192       y = floor_func(x);
193       printv("C floor(x)   ", ref);
194       printv("LLVM floor(x)", y);
195       success = success && compare(ref, y);
196
197       refp[0] = ceil(xp[0]);
198       refp[1] = ceil(xp[1]);
199       refp[2] = ceil(xp[2]);
200       refp[3] = ceil(xp[3]);
201       y = ceil_func(x);
202       printv("C ceil(x)    ", ref);
203       printv("LLVM ceil(x) ", y);
204       success = success && compare(ref, y);
205    }
206
207    LLVMFreeMachineCodeForFunction(engine, test_round);
208    LLVMFreeMachineCodeForFunction(engine, test_trunc);
209    LLVMFreeMachineCodeForFunction(engine, test_floor);
210    LLVMFreeMachineCodeForFunction(engine, test_ceil);
211
212    return success;
213 }
214
215 #else /* !PIPE_ARCH_SSE */
216
217 static boolean
218 test_round(struct gallivm_state *gallivm, unsigned verbose, FILE *fp)
219 {
220    return TRUE;
221 }
222
223 #endif /* !PIPE_ARCH_SSE */
224
225
226 boolean
227 test_all(struct gallivm_state *gallivm, unsigned verbose, FILE *fp)
228 {
229    return test_round(gallivm, verbose, fp);
230 }
231
232
233 boolean
234 test_some(struct gallivm_state *gallivm, unsigned verbose, FILE *fp,
235           unsigned long n)
236 {
237    return test_all(gallivm, verbose, fp);
238 }
239
240 boolean
241 test_single(struct gallivm_state *gallivm, unsigned verbose, FILE *fp)
242 {
243    printf("no test_single()");
244    return TRUE;
245 }