llvmpipe: Initialize bld ctx via lp_build_context_init instead of ad-hoc and broken...
[mesa.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 <llvm-c/Analysis.h>
38 #include <llvm-c/ExecutionEngine.h>
39 #include <llvm-c/Target.h>
40 #include <llvm-c/Transforms/Scalar.h>
41
42 #include "lp_test.h"
43
44
45 void
46 write_tsv_header(FILE *fp)
47 {
48 fprintf(fp,
49 "result\t"
50 "format\n");
51
52 fflush(fp);
53 }
54
55
56 #ifdef PIPE_ARCH_SSE
57
58 #define USE_SSE2
59 #include "sse_mathfun.h"
60
61 typedef __m128 (*test_round_t)(__m128);
62
63 typedef LLVMValueRef (*lp_func_t)(struct lp_build_context *, LLVMValueRef);
64
65
66 static LLVMValueRef
67 add_test(LLVMModuleRef module, const char *name, lp_func_t lp_func)
68 {
69 LLVMTypeRef v4sf = LLVMVectorType(LLVMFloatType(), 4);
70 LLVMTypeRef args[1] = { v4sf };
71 LLVMValueRef func = LLVMAddFunction(module, name, LLVMFunctionType(v4sf, args, 1, 0));
72 LLVMValueRef arg1 = LLVMGetParam(func, 0);
73 LLVMBuilderRef builder = LLVMCreateBuilder();
74 LLVMBasicBlockRef block = LLVMAppendBasicBlock(func, "entry");
75 LLVMValueRef ret;
76 struct lp_build_context bld;
77
78 lp_build_context_init(&bld, builder, lp_float32_vec4_type());
79
80 LLVMSetFunctionCallConv(func, LLVMCCallConv);
81
82 LLVMPositionBuilderAtEnd(builder, block);
83
84 ret = lp_func(&bld, arg1);
85
86 LLVMBuildRet(builder, ret);
87 LLVMDisposeBuilder(builder);
88 return func;
89 }
90
91 static void
92 printv(char* string, v4sf value)
93 {
94 v4sf v = value;
95 float *f = (float *)&v;
96 printf("%s: %10f %10f %10f %10f\n", string,
97 f[0], f[1], f[2], f[3]);
98 }
99
100 static void
101 compare(v4sf x, v4sf y)
102 {
103 float *xp = (float *) &x;
104 float *yp = (float *) &y;
105 if (xp[0] != yp[0] ||
106 xp[1] != yp[1] ||
107 xp[2] != yp[2] ||
108 xp[3] != yp[3]) {
109 printf(" Incorrect result! ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
110 }
111 }
112
113
114
115 PIPE_ALIGN_STACK
116 static boolean
117 test_round(unsigned verbose, FILE *fp)
118 {
119 LLVMModuleRef module = NULL;
120 LLVMValueRef test_round = NULL, test_trunc, test_floor, test_ceil;
121 LLVMExecutionEngineRef engine = lp_build_engine;
122 LLVMPassManagerRef pass = NULL;
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 module = LLVMModuleCreateWithName("test");
131
132 test_round = add_test(module, "round", lp_build_round);
133 test_trunc = add_test(module, "trunc", lp_build_trunc);
134 test_floor = add_test(module, "floor", lp_build_floor);
135 test_ceil = add_test(module, "ceil", lp_build_ceil);
136
137 if(LLVMVerifyModule(module, LLVMPrintMessageAction, &error)) {
138 printf("LLVMVerifyModule: %s\n", error);
139 LLVMDumpModule(module);
140 abort();
141 }
142 LLVMDisposeMessage(error);
143
144 #if 0
145 pass = LLVMCreatePassManager();
146 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engine), pass);
147 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
148 * but there are more on SVN. */
149 LLVMAddConstantPropagationPass(pass);
150 LLVMAddInstructionCombiningPass(pass);
151 LLVMAddPromoteMemoryToRegisterPass(pass);
152 LLVMAddGVNPass(pass);
153 LLVMAddCFGSimplificationPass(pass);
154 LLVMRunPassManager(pass, module);
155 #else
156 (void)pass;
157 #endif
158
159 round_func = (test_round_t) pointer_to_func(LLVMGetPointerToGlobal(engine, test_round));
160 trunc_func = (test_round_t) pointer_to_func(LLVMGetPointerToGlobal(engine, test_trunc));
161 floor_func = (test_round_t) pointer_to_func(LLVMGetPointerToGlobal(engine, test_floor));
162 ceil_func = (test_round_t) pointer_to_func(LLVMGetPointerToGlobal(engine, test_ceil));
163
164 memset(unpacked, 0, sizeof unpacked);
165 packed = 0;
166
167 if (0)
168 LLVMDumpModule(module);
169
170 for (i = 0; i < 3; i++) {
171 v4sf xvals[3] = {
172 {-10.0, -1, 0, 12.0},
173 {-1.5, -0.25, 1.25, 2.5},
174 {-0.99, -0.01, 0.01, 0.99}
175 };
176 v4sf x = xvals[i];
177 v4sf y, ref;
178 float *xp = (float *) &x;
179 float *refp = (float *) &ref;
180
181 printf("\n");
182 printv("x ", x);
183
184 refp[0] = round(xp[0]);
185 refp[1] = round(xp[1]);
186 refp[2] = round(xp[2]);
187 refp[3] = round(xp[3]);
188 y = round_func(x);
189 printv("C round(x) ", ref);
190 printv("LLVM round(x)", y);
191 compare(ref, y);
192
193 refp[0] = trunc(xp[0]);
194 refp[1] = trunc(xp[1]);
195 refp[2] = trunc(xp[2]);
196 refp[3] = trunc(xp[3]);
197 y = trunc_func(x);
198 printv("C trunc(x) ", ref);
199 printv("LLVM trunc(x)", y);
200 compare(ref, y);
201
202 refp[0] = floor(xp[0]);
203 refp[1] = floor(xp[1]);
204 refp[2] = floor(xp[2]);
205 refp[3] = floor(xp[3]);
206 y = floor_func(x);
207 printv("C floor(x) ", ref);
208 printv("LLVM floor(x)", y);
209 compare(ref, y);
210
211 refp[0] = ceil(xp[0]);
212 refp[1] = ceil(xp[1]);
213 refp[2] = ceil(xp[2]);
214 refp[3] = ceil(xp[3]);
215 y = ceil_func(x);
216 printv("C ceil(x) ", ref);
217 printv("LLVM ceil(x) ", y);
218 compare(ref, y);
219 }
220
221 LLVMFreeMachineCodeForFunction(engine, test_round);
222 LLVMFreeMachineCodeForFunction(engine, test_trunc);
223 LLVMFreeMachineCodeForFunction(engine, test_floor);
224 LLVMFreeMachineCodeForFunction(engine, test_ceil);
225
226 LLVMDisposeExecutionEngine(engine);
227 if(pass)
228 LLVMDisposePassManager(pass);
229
230 return success;
231 }
232
233 #else /* !PIPE_ARCH_SSE */
234
235 static boolean
236 test_round(unsigned verbose, FILE *fp)
237 {
238 return TRUE;
239 }
240
241 #endif /* !PIPE_ARCH_SSE */
242
243
244 boolean
245 test_all(unsigned verbose, FILE *fp)
246 {
247 boolean success = TRUE;
248
249 test_round(verbose, fp);
250
251 return success;
252 }
253
254
255 boolean
256 test_some(unsigned verbose, FILE *fp, unsigned long n)
257 {
258 return test_all(verbose, fp);
259 }
260
261 boolean
262 test_single(unsigned verbose, FILE *fp)
263 {
264 printf("no test_single()");
265 return TRUE;
266 }