llvmpipe: Unit test for sin/cos that compares against reference implementation.
[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 struct sincos_test_case {
45 };
46
47
48 void
49 write_tsv_header(FILE *fp)
50 {
51 fprintf(fp,
52 "result\t"
53 "format\n");
54
55 fflush(fp);
56 }
57
58
59 #ifdef PIPE_ARCH_SSE
60
61 #define USE_SSE2
62 #include "sse_mathfun.h"
63
64 typedef __m128 (*test_sincos_t)(__m128);
65
66 static LLVMValueRef
67 add_sincos_test(LLVMModuleRef module, bool sin)
68 {
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");
75 LLVMValueRef ret;
76 struct lp_build_context bld;
77
78 bld.builder = builder;
79 bld.type.floating = 1;
80 bld.type.width = 32;
81 bld.type.length = 4;
82
83 LLVMSetFunctionCallConv(func, LLVMCCallConv);
84
85 LLVMPositionBuilderAtEnd(builder, block);
86 ret = sin ? lp_build_sin(&bld, arg1) : lp_build_cos(&bld, arg1);
87 LLVMBuildRet(builder, ret);
88 LLVMDisposeBuilder(builder);
89 return func;
90 }
91
92 static void
93 printv(char* string, v4sf value)
94 {
95 v4sf v = 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]);
100 }
101
102 PIPE_ALIGN_STACK
103 static boolean
104 test_sincos(unsigned verbose, FILE *fp, const struct sincos_test_case *testcase)
105 {
106 LLVMModuleRef module = NULL;
107 LLVMValueRef test_sin = NULL, test_cos = NULL;
108 LLVMExecutionEngineRef engine = NULL;
109 LLVMModuleProviderRef provider = NULL;
110 LLVMPassManagerRef pass = NULL;
111 char *error = NULL;
112 test_sincos_t sin_func;
113 test_sincos_t cos_func;
114 float unpacked[4];
115 unsigned packed;
116 boolean success = TRUE;
117
118 module = LLVMModuleCreateWithName("test");
119
120 test_sin = add_sincos_test(module, TRUE);
121 test_cos = add_sincos_test(module, FALSE);
122
123 if(LLVMVerifyModule(module, LLVMPrintMessageAction, &error)) {
124 printf("LLVMVerifyModule: %s\n", error);
125 LLVMDumpModule(module);
126 abort();
127 }
128 LLVMDisposeMessage(error);
129
130 provider = LLVMCreateModuleProviderForExistingModule(module);
131 if (LLVMCreateJITCompiler(&engine, provider, 1, &error)) {
132 fprintf(stderr, "%s\n", error);
133 LLVMDisposeMessage(error);
134 abort();
135 }
136
137 #if 0
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);
148 #else
149 (void)pass;
150 #endif
151
152 sin_func = (test_sincos_t)LLVMGetPointerToGlobal(engine, test_sin);
153 cos_func = (test_sincos_t)LLVMGetPointerToGlobal(engine, test_cos);
154
155 memset(unpacked, 0, sizeof unpacked);
156 packed = 0;
157
158
159 // LLVMDumpModule(module);
160 {
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));
166 }
167
168 LLVMFreeMachineCodeForFunction(engine, test_sin);
169 LLVMFreeMachineCodeForFunction(engine, test_cos);
170
171 LLVMDisposeExecutionEngine(engine);
172 if(pass)
173 LLVMDisposePassManager(pass);
174
175 return success;
176 }
177
178 #else /* !PIPE_ARCH_SSE */
179
180 static boolean
181 test_sincos(unsigned verbose, FILE *fp, const struct sincos_test_case *testcase)
182 {
183 return TRUE;
184 }
185
186 #endif /* !PIPE_ARCH_SSE */
187
188
189 boolean
190 test_all(unsigned verbose, FILE *fp)
191 {
192 bool success = TRUE;
193
194 test_sincos(verbose, fp, NULL);
195
196 return success;
197 }
198
199
200 boolean
201 test_some(unsigned verbose, FILE *fp, unsigned long n)
202 {
203 return test_all(verbose, fp);
204 }