llvmpipe: Fix MSVC build.
[mesa.git] / src / gallium / drivers / llvmpipe / lp_test_sincos.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 "gallivm/lp_bld.h"
33 #include "gallivm/lp_bld_init.h"
34 #include "gallivm/lp_bld_arit.h"
35 #include "util/u_pointer.h"
36
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>
41
42 #include "lp_test.h"
43
44
45 void
46 write_tsv_header(FILE *fp)
47 {
48 fprintf(fp,
49 "result\t"
50 "format\n");
51
52 fflush(fp);
53 }
54
55
56 #ifdef PIPE_ARCH_SSE
57
58 #define USE_SSE2
59 #include "sse_mathfun.h"
60
61 typedef __m128 (*test_sincos_t)(__m128);
62
63 static LLVMValueRef
64 add_sincos_test(LLVMModuleRef module, boolean sin)
65 {
66 LLVMTypeRef v4sf = LLVMVectorType(LLVMFloatType(), 4);
67 LLVMTypeRef args[1] = { v4sf };
68 LLVMValueRef func = LLVMAddFunction(module, "sincos", LLVMFunctionType(v4sf, args, 1, 0));
69 LLVMValueRef arg1 = LLVMGetParam(func, 0);
70 LLVMBuilderRef builder = LLVMCreateBuilder();
71 LLVMBasicBlockRef block = LLVMAppendBasicBlock(func, "entry");
72 LLVMValueRef ret;
73 struct lp_build_context bld;
74
75 bld.builder = builder;
76 bld.type.floating = 1;
77 bld.type.width = 32;
78 bld.type.length = 4;
79
80 LLVMSetFunctionCallConv(func, LLVMCCallConv);
81
82 LLVMPositionBuilderAtEnd(builder, block);
83 ret = sin ? lp_build_sin(&bld, arg1) : lp_build_cos(&bld, arg1);
84 LLVMBuildRet(builder, ret);
85 LLVMDisposeBuilder(builder);
86 return func;
87 }
88
89 static void
90 printv(char* string, v4sf value)
91 {
92 v4sf v = value;
93 uint32_t *p = (uint32_t *) &v;
94 float *f = (float *)&v;
95 printf("%s: %f(%x) %f(%x) %f(%x) %f(%x)\n", string,
96 f[0], p[0], f[1], p[1], f[2], p[2], f[3], p[3]);
97 }
98
99 PIPE_ALIGN_STACK
100 static boolean
101 test_sincos(unsigned verbose, FILE *fp)
102 {
103 LLVMModuleRef module = NULL;
104 LLVMValueRef test_sin = NULL, test_cos = NULL;
105 LLVMExecutionEngineRef engine = lp_build_engine;
106 LLVMPassManagerRef pass = NULL;
107 char *error = NULL;
108 test_sincos_t sin_func;
109 test_sincos_t cos_func;
110 float unpacked[4];
111 boolean success = TRUE;
112
113 module = LLVMModuleCreateWithName("test");
114
115 test_sin = add_sincos_test(module, TRUE);
116 test_cos = add_sincos_test(module, FALSE);
117
118 if(LLVMVerifyModule(module, LLVMPrintMessageAction, &error)) {
119 printf("LLVMVerifyModule: %s\n", error);
120 LLVMDumpModule(module);
121 abort();
122 }
123 LLVMDisposeMessage(error);
124
125 #if 0
126 pass = LLVMCreatePassManager();
127 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engine), pass);
128 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
129 * but there are more on SVN. */
130 LLVMAddConstantPropagationPass(pass);
131 LLVMAddInstructionCombiningPass(pass);
132 LLVMAddPromoteMemoryToRegisterPass(pass);
133 LLVMAddGVNPass(pass);
134 LLVMAddCFGSimplificationPass(pass);
135 LLVMRunPassManager(pass, module);
136 #else
137 (void)pass;
138 #endif
139
140 sin_func = (test_sincos_t) pointer_to_func(LLVMGetPointerToGlobal(engine, test_sin));
141 cos_func = (test_sincos_t) pointer_to_func(LLVMGetPointerToGlobal(engine, test_cos));
142
143 memset(unpacked, 0, sizeof unpacked);
144
145
146 // LLVMDumpModule(module);
147 {
148 v4sf src = {3.14159/4.0, -3.14159/4.0, 1.0, -1.0};
149 printv("ref ",sin_ps(src));
150 printv("llvm", sin_func(src));
151 printv("ref ",cos_ps(src));
152 printv("llvm",cos_func(src));
153 }
154
155 LLVMFreeMachineCodeForFunction(engine, test_sin);
156 LLVMFreeMachineCodeForFunction(engine, test_cos);
157
158 if(pass)
159 LLVMDisposePassManager(pass);
160
161 return success;
162 }
163
164 #else /* !PIPE_ARCH_SSE */
165
166 static boolean
167 test_sincos(unsigned verbose, FILE *fp)
168 {
169 return TRUE;
170 }
171
172 #endif /* !PIPE_ARCH_SSE */
173
174
175 boolean
176 test_all(unsigned verbose, FILE *fp)
177 {
178 boolean success = TRUE;
179
180 test_sincos(verbose, fp);
181
182 return success;
183 }
184
185
186 boolean
187 test_some(unsigned verbose, FILE *fp, unsigned long n)
188 {
189 return test_all(verbose, fp);
190 }
191
192 boolean
193 test_single(unsigned verbose, FILE *fp)
194 {
195 printf("no test_single()");
196 return TRUE;
197 }