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 "gallivm/lp_bld.h"
33 #include "gallivm/lp_bld_printf.h"
34 #include "gallivm/lp_bld_arit.h"
36 #include <llvm-c/Analysis.h>
37 #include <llvm-c/ExecutionEngine.h>
38 #include <llvm-c/Target.h>
39 #include <llvm-c/Transforms/Scalar.h>
44 struct sincos_test_case
{
49 write_tsv_header(FILE *fp
)
62 #include "sse_mathfun.h"
64 typedef __m128 (*test_sincos_t
)(__m128
);
67 add_sincos_test(LLVMModuleRef module
, bool sin
)
69 LLVMTypeRef v4sf
= LLVMVectorType(LLVMFloatType(), 4);
70 LLVMTypeRef args
[1] = { v4sf
};
71 LLVMValueRef func
= LLVMAddFunction(module
, "sincos", 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
);
86 ret
= sin
? lp_build_sin(&bld
, arg1
) : lp_build_cos(&bld
, arg1
);
87 LLVMBuildRet(builder
, ret
);
88 LLVMDisposeBuilder(builder
);
93 printv(char* string
, v4sf value
)
96 uint32_t *p
= (uint32_t *) &v
;
97 float *f
= (float *)&v
;
98 printf("%s: %f(%x) %f(%x) %f(%x) %f(%x)\n", string
,
99 f
[0], p
[0], f
[1], p
[1], f
[2], p
[2], f
[3], p
[3]);
104 test_sincos(unsigned verbose
, FILE *fp
, const struct sincos_test_case
*testcase
)
106 LLVMModuleRef module
= NULL
;
107 LLVMValueRef test_sin
= NULL
, test_cos
= NULL
;
108 LLVMExecutionEngineRef engine
= NULL
;
109 LLVMModuleProviderRef provider
= NULL
;
110 LLVMPassManagerRef pass
= NULL
;
112 test_sincos_t sin_func
;
113 test_sincos_t cos_func
;
116 boolean success
= TRUE
;
118 module
= LLVMModuleCreateWithName("test");
120 test_sin
= add_sincos_test(module
, TRUE
);
121 test_cos
= add_sincos_test(module
, FALSE
);
123 if(LLVMVerifyModule(module
, LLVMPrintMessageAction
, &error
)) {
124 printf("LLVMVerifyModule: %s\n", error
);
125 LLVMDumpModule(module
);
128 LLVMDisposeMessage(error
);
130 provider
= LLVMCreateModuleProviderForExistingModule(module
);
131 if (LLVMCreateJITCompiler(&engine
, provider
, 1, &error
)) {
132 fprintf(stderr
, "%s\n", error
);
133 LLVMDisposeMessage(error
);
138 pass
= LLVMCreatePassManager();
139 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engine
), pass
);
140 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
141 * but there are more on SVN. */
142 LLVMAddConstantPropagationPass(pass
);
143 LLVMAddInstructionCombiningPass(pass
);
144 LLVMAddPromoteMemoryToRegisterPass(pass
);
145 LLVMAddGVNPass(pass
);
146 LLVMAddCFGSimplificationPass(pass
);
147 LLVMRunPassManager(pass
, module
);
152 sin_func
= (test_sincos_t
)LLVMGetPointerToGlobal(engine
, test_sin
);
153 cos_func
= (test_sincos_t
)LLVMGetPointerToGlobal(engine
, test_cos
);
155 memset(unpacked
, 0, sizeof unpacked
);
159 // LLVMDumpModule(module);
161 v4sf src
= {3.14159/4.0, -3.14159/4.0, 1.0, -1.0};
162 printv("ref ",sin_ps(src
));
163 printv("llvm", sin_func(src
));
164 printv("ref ",cos_ps(src
));
165 printv("llvm",cos_func(src
));
168 LLVMFreeMachineCodeForFunction(engine
, test_sin
);
169 LLVMFreeMachineCodeForFunction(engine
, test_cos
);
171 LLVMDisposeExecutionEngine(engine
);
173 LLVMDisposePassManager(pass
);
178 #else /* !PIPE_ARCH_SSE */
181 test_sincos(unsigned verbose
, FILE *fp
, const struct sincos_test_case
*testcase
)
186 #endif /* !PIPE_ARCH_SSE */
190 test_all(unsigned verbose
, FILE *fp
)
194 test_sincos(verbose
, fp
, NULL
);
201 test_some(unsigned verbose
, FILE *fp
, unsigned long n
)
203 return test_all(verbose
, fp
);