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_printf.h"
35 #include "gallivm/lp_bld_arit.h"
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>
46 write_tsv_header(FILE *fp
)
59 #include "sse_mathfun.h"
61 typedef __m128 (*test_round_t
)(__m128
);
63 typedef LLVMValueRef (*lp_func_t
)(struct lp_build_context
*, LLVMValueRef
);
67 add_test(LLVMModuleRef module
, const char *name
, lp_func_t lp_func
)
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");
76 struct lp_build_context bld
;
78 bld
.builder
= builder
;
79 bld
.type
.floating
= 1;
83 LLVMSetFunctionCallConv(func
, LLVMCCallConv
);
85 LLVMPositionBuilderAtEnd(builder
, block
);
87 ret
= lp_func(&bld
, arg1
);
89 LLVMBuildRet(builder
, ret
);
90 LLVMDisposeBuilder(builder
);
95 printv(char* string
, v4sf value
)
98 float *f
= (float *)&v
;
99 printf("%s: %10f %10f %10f %10f\n", string
,
100 f
[0], f
[1], f
[2], f
[3]);
104 compare(v4sf x
, v4sf y
)
106 float *xp
= (float *) &x
;
107 float *yp
= (float *) &y
;
108 if (xp
[0] != yp
[0] ||
112 printf(" Incorrect result! ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
120 test_round(unsigned verbose
, FILE *fp
)
122 LLVMModuleRef module
= NULL
;
123 LLVMValueRef test_round
= NULL
, test_trunc
, test_floor
, test_ceil
;
124 LLVMExecutionEngineRef engine
= NULL
;
125 LLVMModuleProviderRef provider
= NULL
;
126 LLVMPassManagerRef pass
= NULL
;
128 test_round_t round_func
, trunc_func
, floor_func
, ceil_func
;
131 boolean success
= TRUE
;
134 module
= LLVMModuleCreateWithName("test");
136 test_round
= add_test(module
, "round", lp_build_round
);
137 test_trunc
= add_test(module
, "trunc", lp_build_trunc
);
138 test_floor
= add_test(module
, "floor", lp_build_floor
);
139 test_ceil
= add_test(module
, "ceil", lp_build_ceil
);
141 if(LLVMVerifyModule(module
, LLVMPrintMessageAction
, &error
)) {
142 printf("LLVMVerifyModule: %s\n", error
);
143 LLVMDumpModule(module
);
146 LLVMDisposeMessage(error
);
148 provider
= LLVMCreateModuleProviderForExistingModule(module
);
149 if (LLVMCreateJITCompiler(&engine
, provider
, 1, &error
)) {
150 fprintf(stderr
, "%s\n", error
);
151 LLVMDisposeMessage(error
);
156 pass
= LLVMCreatePassManager();
157 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engine
), pass
);
158 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
159 * but there are more on SVN. */
160 LLVMAddConstantPropagationPass(pass
);
161 LLVMAddInstructionCombiningPass(pass
);
162 LLVMAddPromoteMemoryToRegisterPass(pass
);
163 LLVMAddGVNPass(pass
);
164 LLVMAddCFGSimplificationPass(pass
);
165 LLVMRunPassManager(pass
, module
);
170 round_func
= (test_round_t
) pointer_to_func(LLVMGetPointerToGlobal(engine
, test_round
));
171 trunc_func
= (test_round_t
) pointer_to_func(LLVMGetPointerToGlobal(engine
, test_trunc
));
172 floor_func
= (test_round_t
) pointer_to_func(LLVMGetPointerToGlobal(engine
, test_floor
));
173 ceil_func
= (test_round_t
) pointer_to_func(LLVMGetPointerToGlobal(engine
, test_ceil
));
175 memset(unpacked
, 0, sizeof unpacked
);
179 LLVMDumpModule(module
);
181 for (i
= 0; i
< 3; i
++) {
183 {-10.0, -1, 0, 12.0},
184 {-1.5, -0.25, 1.25, 2.5},
185 {-0.99, -0.01, 0.01, 0.99}
189 float *xp
= (float *) &x
;
190 float *refp
= (float *) &ref
;
195 refp
[0] = round(xp
[0]);
196 refp
[1] = round(xp
[1]);
197 refp
[2] = round(xp
[2]);
198 refp
[3] = round(xp
[3]);
200 printv("C round(x) ", ref
);
201 printv("LLVM round(x)", y
);
204 refp
[0] = trunc(xp
[0]);
205 refp
[1] = trunc(xp
[1]);
206 refp
[2] = trunc(xp
[2]);
207 refp
[3] = trunc(xp
[3]);
209 printv("C trunc(x) ", ref
);
210 printv("LLVM trunc(x)", y
);
213 refp
[0] = floor(xp
[0]);
214 refp
[1] = floor(xp
[1]);
215 refp
[2] = floor(xp
[2]);
216 refp
[3] = floor(xp
[3]);
218 printv("C floor(x) ", ref
);
219 printv("LLVM floor(x)", y
);
222 refp
[0] = ceil(xp
[0]);
223 refp
[1] = ceil(xp
[1]);
224 refp
[2] = ceil(xp
[2]);
225 refp
[3] = ceil(xp
[3]);
227 printv("C ceil(x) ", ref
);
228 printv("LLVM ceil(x) ", y
);
232 LLVMFreeMachineCodeForFunction(engine
, test_round
);
233 LLVMFreeMachineCodeForFunction(engine
, test_trunc
);
234 LLVMFreeMachineCodeForFunction(engine
, test_floor
);
235 LLVMFreeMachineCodeForFunction(engine
, test_ceil
);
237 LLVMDisposeExecutionEngine(engine
);
239 LLVMDisposePassManager(pass
);
244 #else /* !PIPE_ARCH_SSE */
247 test_round(unsigned verbose
, FILE *fp
)
252 #endif /* !PIPE_ARCH_SSE */
256 test_all(unsigned verbose
, FILE *fp
)
258 boolean success
= TRUE
;
260 test_round(verbose
, fp
);
267 test_some(unsigned verbose
, FILE *fp
, unsigned long n
)
269 return test_all(verbose
, fp
);
273 test_single(unsigned verbose
, FILE *fp
)
275 printf("no test_single()");