Merge branch 'llvm-cliptest-viewport'
[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 lp_build_context_init(&bld, builder, lp_float32_vec4_type());
76
77 LLVMSetFunctionCallConv(func, LLVMCCallConv);
78
79 LLVMPositionBuilderAtEnd(builder, block);
80 ret = sin ? lp_build_sin(&bld, arg1) : lp_build_cos(&bld, arg1);
81 LLVMBuildRet(builder, ret);
82 LLVMDisposeBuilder(builder);
83 return func;
84 }
85
86 static void
87 printv(char* string, v4sf value)
88 {
89 v4sf v = value;
90 uint32_t *p = (uint32_t *) &v;
91 float *f = (float *)&v;
92 printf("%s: %f(%x) %f(%x) %f(%x) %f(%x)\n", string,
93 f[0], p[0], f[1], p[1], f[2], p[2], f[3], p[3]);
94 }
95
96 PIPE_ALIGN_STACK
97 static boolean
98 test_sincos(unsigned verbose, FILE *fp)
99 {
100 LLVMModuleRef module = NULL;
101 LLVMValueRef test_sin = NULL, test_cos = NULL;
102 LLVMExecutionEngineRef engine = lp_build_engine;
103 LLVMPassManagerRef pass = NULL;
104 char *error = NULL;
105 test_sincos_t sin_func;
106 test_sincos_t cos_func;
107 float unpacked[4];
108 boolean success = TRUE;
109
110 module = LLVMModuleCreateWithName("test");
111
112 test_sin = add_sincos_test(module, TRUE);
113 test_cos = add_sincos_test(module, FALSE);
114
115 if(LLVMVerifyModule(module, LLVMPrintMessageAction, &error)) {
116 printf("LLVMVerifyModule: %s\n", error);
117 LLVMDumpModule(module);
118 abort();
119 }
120 LLVMDisposeMessage(error);
121
122 #if 0
123 pass = LLVMCreatePassManager();
124 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engine), pass);
125 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
126 * but there are more on SVN. */
127 LLVMAddConstantPropagationPass(pass);
128 LLVMAddInstructionCombiningPass(pass);
129 LLVMAddPromoteMemoryToRegisterPass(pass);
130 LLVMAddGVNPass(pass);
131 LLVMAddCFGSimplificationPass(pass);
132 LLVMRunPassManager(pass, module);
133 #else
134 (void)pass;
135 #endif
136
137 sin_func = (test_sincos_t) pointer_to_func(LLVMGetPointerToGlobal(engine, test_sin));
138 cos_func = (test_sincos_t) pointer_to_func(LLVMGetPointerToGlobal(engine, test_cos));
139
140 memset(unpacked, 0, sizeof unpacked);
141
142
143 // LLVMDumpModule(module);
144 {
145 v4sf src = {3.14159/4.0, -3.14159/4.0, 1.0, -1.0};
146 printv("ref ",sin_ps(src));
147 printv("llvm", sin_func(src));
148 printv("ref ",cos_ps(src));
149 printv("llvm",cos_func(src));
150 }
151
152 LLVMFreeMachineCodeForFunction(engine, test_sin);
153 LLVMFreeMachineCodeForFunction(engine, test_cos);
154
155 if(pass)
156 LLVMDisposePassManager(pass);
157
158 return success;
159 }
160
161 #else /* !PIPE_ARCH_SSE */
162
163 static boolean
164 test_sincos(unsigned verbose, FILE *fp)
165 {
166 return TRUE;
167 }
168
169 #endif /* !PIPE_ARCH_SSE */
170
171
172 boolean
173 test_all(unsigned verbose, FILE *fp)
174 {
175 boolean success = TRUE;
176
177 test_sincos(verbose, fp);
178
179 return success;
180 }
181
182
183 boolean
184 test_some(unsigned verbose, FILE *fp, unsigned long n)
185 {
186 return test_all(verbose, fp);
187 }
188
189 boolean
190 test_single(unsigned verbose, FILE *fp)
191 {
192 printf("no test_single()");
193 return TRUE;
194 }