util: reference surfaces and sampler views in blitter when saving them
[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_printf.h"
34 #include "gallivm/lp_bld_arit.h"
35
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>
40
41 #include "lp_test.h"
42
43
44 void
45 write_tsv_header(FILE *fp)
46 {
47 fprintf(fp,
48 "result\t"
49 "format\n");
50
51 fflush(fp);
52 }
53
54
55 #ifdef PIPE_ARCH_SSE
56
57 #define USE_SSE2
58 #include "sse_mathfun.h"
59
60 typedef __m128 (*test_sincos_t)(__m128);
61
62 static LLVMValueRef
63 add_sincos_test(LLVMModuleRef module, boolean sin)
64 {
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");
71 LLVMValueRef ret;
72 struct lp_build_context bld;
73
74 bld.builder = builder;
75 bld.type.floating = 1;
76 bld.type.width = 32;
77 bld.type.length = 4;
78
79 LLVMSetFunctionCallConv(func, LLVMCCallConv);
80
81 LLVMPositionBuilderAtEnd(builder, block);
82 ret = sin ? lp_build_sin(&bld, arg1) : lp_build_cos(&bld, arg1);
83 LLVMBuildRet(builder, ret);
84 LLVMDisposeBuilder(builder);
85 return func;
86 }
87
88 static void
89 printv(char* string, v4sf value)
90 {
91 v4sf v = 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]);
96 }
97
98 PIPE_ALIGN_STACK
99 static boolean
100 test_sincos(unsigned verbose, FILE *fp)
101 {
102 LLVMModuleRef module = NULL;
103 LLVMValueRef test_sin = NULL, test_cos = NULL;
104 LLVMExecutionEngineRef engine = NULL;
105 LLVMModuleProviderRef provider = NULL;
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 unsigned packed;
112 boolean success = TRUE;
113
114 module = LLVMModuleCreateWithName("test");
115
116 test_sin = add_sincos_test(module, TRUE);
117 test_cos = add_sincos_test(module, FALSE);
118
119 if(LLVMVerifyModule(module, LLVMPrintMessageAction, &error)) {
120 printf("LLVMVerifyModule: %s\n", error);
121 LLVMDumpModule(module);
122 abort();
123 }
124 LLVMDisposeMessage(error);
125
126 provider = LLVMCreateModuleProviderForExistingModule(module);
127 if (LLVMCreateJITCompiler(&engine, provider, 1, &error)) {
128 fprintf(stderr, "%s\n", error);
129 LLVMDisposeMessage(error);
130 abort();
131 }
132
133 #if 0
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);
144 #else
145 (void)pass;
146 #endif
147
148 sin_func = (test_sincos_t)LLVMGetPointerToGlobal(engine, test_sin);
149 cos_func = (test_sincos_t)LLVMGetPointerToGlobal(engine, test_cos);
150
151 memset(unpacked, 0, sizeof unpacked);
152 packed = 0;
153
154
155 // LLVMDumpModule(module);
156 {
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));
162 }
163
164 LLVMFreeMachineCodeForFunction(engine, test_sin);
165 LLVMFreeMachineCodeForFunction(engine, test_cos);
166
167 LLVMDisposeExecutionEngine(engine);
168 if(pass)
169 LLVMDisposePassManager(pass);
170
171 return success;
172 }
173
174 #else /* !PIPE_ARCH_SSE */
175
176 static boolean
177 test_sincos(unsigned verbose, FILE *fp)
178 {
179 return TRUE;
180 }
181
182 #endif /* !PIPE_ARCH_SSE */
183
184
185 boolean
186 test_all(unsigned verbose, FILE *fp)
187 {
188 boolean success = TRUE;
189
190 test_sincos(verbose, fp);
191
192 return success;
193 }
194
195
196 boolean
197 test_some(unsigned verbose, FILE *fp, unsigned long n)
198 {
199 return test_all(verbose, fp);
200 }
201
202 boolean
203 test_single(unsigned verbose, FILE *fp)
204 {
205 printf("no test_single()");
206 return TRUE;
207 }