1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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"
41 write_tsv_header(FILE *fp)
54 #include "sse_mathfun.h"
56 typedef __m128 (*test_round_t)(__m128);
58 typedef LLVMValueRef (*lp_func_t)(struct lp_build_context *, LLVMValueRef);
62 add_test(struct gallivm_state *gallivm, const char *name, lp_func_t lp_func)
64 LLVMModuleRef module = gallivm->module;
65 LLVMContextRef context = gallivm->context;
66 LLVMBuilderRef builder = gallivm->builder;
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");
74 struct lp_build_context bld;
76 lp_build_context_init(&bld, gallivm, lp_float32_vec4_type());
78 LLVMSetFunctionCallConv(func, LLVMCCallConv);
80 LLVMPositionBuilderAtEnd(builder, block);
82 ret = lp_func(&bld, arg1);
84 LLVMBuildRet(builder, ret);
90 printv(char* string, v4sf value)
93 float *f = (float *)&v;
94 printf("%s: %10f %10f %10f %10f\n", string,
95 f[0], f[1], f[2], f[3]);
99 compare(v4sf x, v4sf y)
101 boolean success = TRUE;
102 float *xp = (float *) &x;
103 float *yp = (float *) &y;
104 if (xp[0] != yp[0] ||
108 printf(" Incorrect result! ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
118 test_round(struct gallivm_state *gallivm, unsigned verbose, FILE *fp)
120 LLVMModuleRef module = gallivm->module;
121 LLVMValueRef test_round = NULL, test_trunc, test_floor, test_ceil;
122 LLVMExecutionEngineRef engine = gallivm->engine;
124 test_round_t round_func, trunc_func, floor_func, ceil_func;
127 boolean success = TRUE;
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);
135 if(LLVMVerifyModule(module, LLVMPrintMessageAction, &error)) {
136 printf("LLVMVerifyModule: %s\n", error);
137 LLVMDumpModule(module);
140 LLVMDisposeMessage(error);
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));
147 memset(unpacked, 0, sizeof unpacked);
151 LLVMDumpModule(module);
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.
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}
164 float *xp = (float *) &x;
165 float *refp = (float *) &ref;
170 refp[0] = round(xp[0]);
171 refp[1] = round(xp[1]);
172 refp[2] = round(xp[2]);
173 refp[3] = round(xp[3]);
175 printv("C round(x) ", ref);
176 printv("LLVM round(x)", y);
177 success = success && compare(ref, y);
179 refp[0] = trunc(xp[0]);
180 refp[1] = trunc(xp[1]);
181 refp[2] = trunc(xp[2]);
182 refp[3] = trunc(xp[3]);
184 printv("C trunc(x) ", ref);
185 printv("LLVM trunc(x)", y);
186 success = success && compare(ref, y);
188 refp[0] = floor(xp[0]);
189 refp[1] = floor(xp[1]);
190 refp[2] = floor(xp[2]);
191 refp[3] = floor(xp[3]);
193 printv("C floor(x) ", ref);
194 printv("LLVM floor(x)", y);
195 success = success && compare(ref, y);
197 refp[0] = ceil(xp[0]);
198 refp[1] = ceil(xp[1]);
199 refp[2] = ceil(xp[2]);
200 refp[3] = ceil(xp[3]);
202 printv("C ceil(x) ", ref);
203 printv("LLVM ceil(x) ", y);
204 success = success && compare(ref, y);
207 LLVMFreeMachineCodeForFunction(engine, test_round);
208 LLVMFreeMachineCodeForFunction(engine, test_trunc);
209 LLVMFreeMachineCodeForFunction(engine, test_floor);
210 LLVMFreeMachineCodeForFunction(engine, test_ceil);
215 #else /* !PIPE_ARCH_SSE */
218 test_round(struct gallivm_state *gallivm, unsigned verbose, FILE *fp)
223 #endif /* !PIPE_ARCH_SSE */
227 test_all(struct gallivm_state *gallivm, unsigned verbose, FILE *fp)
229 return test_round(gallivm, verbose, fp);
234 test_some(struct gallivm_state *gallivm, unsigned verbose, FILE *fp,
237 return test_all(gallivm, verbose, fp);
241 test_single(struct gallivm_state *gallivm, unsigned verbose, FILE *fp)
243 printf("no test_single()");