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