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>
45 write_tsv_header(FILE *fp
)
58 #include "sse_mathfun.h"
60 typedef __m128 (*test_sincos_t
)(__m128
);
63 add_sincos_test(LLVMModuleRef module
, boolean sin
)
65 LLVMTypeRef v4sf
= LLVMVectorType(LLVMFloatType(), 4);
66 LLVMTypeRef args
[1] = { v4sf
};
67 LLVMValueRef func
= LLVMAddFunction(module
, "sincos", LLVMFunctionType(v4sf
, args
, 1, 0));
68 LLVMValueRef arg1
= LLVMGetParam(func
, 0);
69 LLVMBuilderRef builder
= LLVMCreateBuilder();
70 LLVMBasicBlockRef block
= LLVMAppendBasicBlock(func
, "entry");
72 struct lp_build_context bld
;
74 bld
.builder
= builder
;
75 bld
.type
.floating
= 1;
79 LLVMSetFunctionCallConv(func
, LLVMCCallConv
);
81 LLVMPositionBuilderAtEnd(builder
, block
);
82 ret
= sin
? lp_build_sin(&bld
, arg1
) : lp_build_cos(&bld
, arg1
);
83 LLVMBuildRet(builder
, ret
);
84 LLVMDisposeBuilder(builder
);
89 printv(char* string
, v4sf value
)
92 uint32_t *p
= (uint32_t *) &v
;
93 float *f
= (float *)&v
;
94 printf("%s: %f(%x) %f(%x) %f(%x) %f(%x)\n", string
,
95 f
[0], p
[0], f
[1], p
[1], f
[2], p
[2], f
[3], p
[3]);
100 test_sincos(unsigned verbose
, FILE *fp
)
102 LLVMModuleRef module
= NULL
;
103 LLVMValueRef test_sin
= NULL
, test_cos
= NULL
;
104 LLVMExecutionEngineRef engine
= NULL
;
105 LLVMModuleProviderRef provider
= NULL
;
106 LLVMPassManagerRef pass
= NULL
;
108 test_sincos_t sin_func
;
109 test_sincos_t cos_func
;
112 boolean success
= TRUE
;
114 module
= LLVMModuleCreateWithName("test");
116 test_sin
= add_sincos_test(module
, TRUE
);
117 test_cos
= add_sincos_test(module
, FALSE
);
119 if(LLVMVerifyModule(module
, LLVMPrintMessageAction
, &error
)) {
120 printf("LLVMVerifyModule: %s\n", error
);
121 LLVMDumpModule(module
);
124 LLVMDisposeMessage(error
);
126 provider
= LLVMCreateModuleProviderForExistingModule(module
);
127 if (LLVMCreateJITCompiler(&engine
, provider
, 1, &error
)) {
128 fprintf(stderr
, "%s\n", error
);
129 LLVMDisposeMessage(error
);
134 pass
= LLVMCreatePassManager();
135 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engine
), pass
);
136 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
137 * but there are more on SVN. */
138 LLVMAddConstantPropagationPass(pass
);
139 LLVMAddInstructionCombiningPass(pass
);
140 LLVMAddPromoteMemoryToRegisterPass(pass
);
141 LLVMAddGVNPass(pass
);
142 LLVMAddCFGSimplificationPass(pass
);
143 LLVMRunPassManager(pass
, module
);
148 sin_func
= (test_sincos_t
)LLVMGetPointerToGlobal(engine
, test_sin
);
149 cos_func
= (test_sincos_t
)LLVMGetPointerToGlobal(engine
, test_cos
);
151 memset(unpacked
, 0, sizeof unpacked
);
155 // LLVMDumpModule(module);
157 v4sf src
= {3.14159/4.0, -3.14159/4.0, 1.0, -1.0};
158 printv("ref ",sin_ps(src
));
159 printv("llvm", sin_func(src
));
160 printv("ref ",cos_ps(src
));
161 printv("llvm",cos_func(src
));
164 LLVMFreeMachineCodeForFunction(engine
, test_sin
);
165 LLVMFreeMachineCodeForFunction(engine
, test_cos
);
167 LLVMDisposeExecutionEngine(engine
);
169 LLVMDisposePassManager(pass
);
174 #else /* !PIPE_ARCH_SSE */
177 test_sincos(unsigned verbose
, FILE *fp
)
182 #endif /* !PIPE_ARCH_SSE */
186 test_all(unsigned verbose
, FILE *fp
)
188 boolean success
= TRUE
;
190 test_sincos(verbose
, fp
);
197 test_some(unsigned verbose
, FILE *fp
, unsigned long n
)
199 return test_all(verbose
, fp
);
203 test_single(unsigned verbose
, FILE *fp
)
205 printf("no test_single()");