Move compiler.h and imports.h/c from src/mesa/main into src/util
[mesa.git] / src / mesa / x86 / common_x86.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2006 Brian Paul 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 "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file common_x86.c
27 *
28 * Check CPU capabilities & initialize optimized funtions for this particular
29 * processor.
30 *
31 * Changed by Andre Werthmann for using the new SSE functions.
32 *
33 * \author Holger Waechtler <holger@akaflieg.extern.tu-berlin.de>
34 * \author Andre Werthmann <wertmann@cs.uni-potsdam.de>
35 */
36
37 /* XXX these includes should probably go into imports.h or glheader.h */
38 #if defined(USE_SSE_ASM) && defined(__FreeBSD__)
39 #include <sys/types.h>
40 #include <sys/sysctl.h>
41 #endif
42 #if defined(USE_SSE_ASM) && (defined(__OpenBSD__) || defined(__NetBSD__))
43 #include <sys/param.h>
44 #include <sys/sysctl.h>
45 #include <machine/cpu.h>
46 #endif
47 #if defined(USE_X86_64_ASM)
48 #include <cpuid.h>
49 #if !defined(bit_SSE4_1) && defined(bit_SSE41)
50 /* XXX: clang defines bit_SSE41 instead of bit_SSE4_1 */
51 #define bit_SSE4_1 bit_SSE41
52 #elif !defined(bit_SSE4_1) && !defined(bit_SSE41)
53 #define bit_SSE4_1 0x00080000
54 #endif
55 #endif
56
57 #include "main/errors.h"
58 #include "util/imports.h"
59 #include "common_x86_asm.h"
60
61
62 /** Bitmask of X86_FEATURE_x bits */
63 int _mesa_x86_cpu_features = 0x0;
64
65 static int detection_debug = GL_FALSE;
66
67 /* No reason for this to be public.
68 */
69 extern GLuint _mesa_x86_has_cpuid(void);
70 extern void _mesa_x86_cpuid(GLuint op, GLuint *reg_eax, GLuint *reg_ebx, GLuint *reg_ecx, GLuint *reg_edx);
71 extern GLuint _mesa_x86_cpuid_eax(GLuint op);
72 extern GLuint _mesa_x86_cpuid_ebx(GLuint op);
73 extern GLuint _mesa_x86_cpuid_ecx(GLuint op);
74 extern GLuint _mesa_x86_cpuid_edx(GLuint op);
75
76
77 #if defined(USE_SSE_ASM)
78 /*
79 * We must verify that the Streaming SIMD Extensions are truly supported
80 * on this processor before we go ahead and hook out the optimized code.
81 *
82 * However, I have been told by Alan Cox that all 2.4 (and later) Linux
83 * kernels provide full SSE support on all processors that expose SSE via
84 * the CPUID mechanism.
85 */
86
87 /* These are assembly functions: */
88 extern void _mesa_test_os_sse_support( void );
89 extern void _mesa_test_os_sse_exception_support( void );
90
91
92 #if defined(_WIN32)
93 #ifndef STATUS_FLOAT_MULTIPLE_TRAPS
94 # define STATUS_FLOAT_MULTIPLE_TRAPS (0xC00002B5L)
95 #endif
96 static LONG WINAPI ExceptionFilter(LPEXCEPTION_POINTERS exp)
97 {
98 PEXCEPTION_RECORD rec = exp->ExceptionRecord;
99 PCONTEXT ctx = exp->ContextRecord;
100
101 if ( rec->ExceptionCode == EXCEPTION_ILLEGAL_INSTRUCTION ) {
102 _mesa_debug(NULL, "EXCEPTION_ILLEGAL_INSTRUCTION\n" );
103 _mesa_x86_cpu_features &= ~(X86_FEATURE_XMM);
104 } else if ( rec->ExceptionCode == STATUS_FLOAT_MULTIPLE_TRAPS ) {
105 _mesa_debug(NULL, "STATUS_FLOAT_MULTIPLE_TRAPS\n");
106 /* Windows seems to clear the exception flag itself, we just have to increment Eip */
107 } else {
108 _mesa_debug(NULL, "UNEXPECTED EXCEPTION (0x%08x), terminating!\n" );
109 return EXCEPTION_EXECUTE_HANDLER;
110 }
111
112 if ( (ctx->ContextFlags & CONTEXT_CONTROL) != CONTEXT_CONTROL ) {
113 _mesa_debug(NULL, "Context does not contain control registers, terminating!\n");
114 return EXCEPTION_EXECUTE_HANDLER;
115 }
116 ctx->Eip += 3;
117
118 return EXCEPTION_CONTINUE_EXECUTION;
119 }
120 #endif /* _WIN32 */
121
122
123 /**
124 * Check if SSE is supported.
125 * If not, turn off the X86_FEATURE_XMM flag in _mesa_x86_cpu_features.
126 */
127 void _mesa_check_os_sse_support( void )
128 {
129 #if defined(__FreeBSD__)
130 {
131 int ret, enabled;
132 unsigned int len;
133 len = sizeof(enabled);
134 ret = sysctlbyname("hw.instruction_sse", &enabled, &len, NULL, 0);
135 if (ret || !enabled)
136 _mesa_x86_cpu_features &= ~(X86_FEATURE_XMM);
137 }
138 #elif defined (__NetBSD__)
139 {
140 int ret, enabled;
141 size_t len = sizeof(enabled);
142 ret = sysctlbyname("machdep.sse", &enabled, &len, (void *)NULL, 0);
143 if (ret || !enabled)
144 _mesa_x86_cpu_features &= ~(X86_FEATURE_XMM);
145 }
146 #elif defined(__OpenBSD__)
147 {
148 int mib[2];
149 int ret, enabled;
150 size_t len = sizeof(enabled);
151
152 mib[0] = CTL_MACHDEP;
153 mib[1] = CPU_SSE;
154
155 ret = sysctl(mib, 2, &enabled, &len, NULL, 0);
156 if (ret || !enabled)
157 _mesa_x86_cpu_features &= ~(X86_FEATURE_XMM);
158 }
159 #elif defined(_WIN32)
160 LPTOP_LEVEL_EXCEPTION_FILTER oldFilter;
161
162 /* Install our ExceptionFilter */
163 oldFilter = SetUnhandledExceptionFilter( ExceptionFilter );
164
165 if ( cpu_has_xmm ) {
166 _mesa_debug(NULL, "Testing OS support for SSE...\n");
167
168 _mesa_test_os_sse_support();
169
170 if ( cpu_has_xmm ) {
171 _mesa_debug(NULL, "Yes.\n");
172 } else {
173 _mesa_debug(NULL, "No!\n");
174 }
175 }
176
177 if ( cpu_has_xmm ) {
178 _mesa_debug(NULL, "Testing OS support for SSE unmasked exceptions...\n");
179
180 _mesa_test_os_sse_exception_support();
181
182 if ( cpu_has_xmm ) {
183 _mesa_debug(NULL, "Yes.\n");
184 } else {
185 _mesa_debug(NULL, "No!\n");
186 }
187 }
188
189 /* Restore previous exception filter */
190 SetUnhandledExceptionFilter( oldFilter );
191
192 if ( cpu_has_xmm ) {
193 _mesa_debug(NULL, "Tests of OS support for SSE passed.\n");
194 } else {
195 _mesa_debug(NULL, "Tests of OS support for SSE failed!\n");
196 }
197 #else
198 /* Do nothing on other platforms for now.
199 */
200 if (detection_debug)
201 _mesa_debug(NULL, "Not testing OS support for SSE, leaving enabled.\n");
202 #endif /* __FreeBSD__ */
203 }
204
205 #endif /* USE_SSE_ASM */
206
207
208 /**
209 * Initialize the _mesa_x86_cpu_features bitfield.
210 * This is a no-op if called more than once.
211 */
212 void
213 _mesa_get_x86_features(void)
214 {
215 static int called = 0;
216
217 if (called)
218 return;
219
220 called = 1;
221
222 #ifdef USE_X86_ASM
223 _mesa_x86_cpu_features = 0x0;
224
225 if (getenv( "MESA_NO_ASM")) {
226 return;
227 }
228
229 if (!_mesa_x86_has_cpuid()) {
230 _mesa_debug(NULL, "CPUID not detected\n");
231 }
232 else {
233 GLuint cpu_features, cpu_features_ecx;
234 GLuint cpu_ext_features;
235 GLuint cpu_ext_info;
236 char cpu_vendor[13];
237 GLuint result;
238
239 /* get vendor name */
240 _mesa_x86_cpuid(0, &result, (GLuint *)(cpu_vendor + 0), (GLuint *)(cpu_vendor + 8), (GLuint *)(cpu_vendor + 4));
241 cpu_vendor[12] = '\0';
242
243 if (detection_debug)
244 _mesa_debug(NULL, "CPU vendor: %s\n", cpu_vendor);
245
246 /* get cpu features */
247 cpu_features = _mesa_x86_cpuid_edx(1);
248 cpu_features_ecx = _mesa_x86_cpuid_ecx(1);
249
250 if (cpu_features & X86_CPU_FPU)
251 _mesa_x86_cpu_features |= X86_FEATURE_FPU;
252 if (cpu_features & X86_CPU_CMOV)
253 _mesa_x86_cpu_features |= X86_FEATURE_CMOV;
254
255 #ifdef USE_MMX_ASM
256 if (cpu_features & X86_CPU_MMX)
257 _mesa_x86_cpu_features |= X86_FEATURE_MMX;
258 #endif
259
260 #ifdef USE_SSE_ASM
261 if (cpu_features & X86_CPU_XMM)
262 _mesa_x86_cpu_features |= X86_FEATURE_XMM;
263 if (cpu_features & X86_CPU_XMM2)
264 _mesa_x86_cpu_features |= X86_FEATURE_XMM2;
265 if (cpu_features_ecx & X86_CPU_SSE4_1)
266 _mesa_x86_cpu_features |= X86_FEATURE_SSE4_1;
267 #endif
268
269 /* query extended cpu features */
270 if ((cpu_ext_info = _mesa_x86_cpuid_eax(0x80000000)) > 0x80000000) {
271 if (cpu_ext_info >= 0x80000001) {
272
273 cpu_ext_features = _mesa_x86_cpuid_edx(0x80000001);
274
275 if (cpu_features & X86_CPU_MMX) {
276
277 #ifdef USE_3DNOW_ASM
278 if (cpu_ext_features & X86_CPUEXT_3DNOW)
279 _mesa_x86_cpu_features |= X86_FEATURE_3DNOW;
280 if (cpu_ext_features & X86_CPUEXT_3DNOW_EXT)
281 _mesa_x86_cpu_features |= X86_FEATURE_3DNOWEXT;
282 #endif
283
284 #ifdef USE_MMX_ASM
285 if (cpu_ext_features & X86_CPUEXT_MMX_EXT)
286 _mesa_x86_cpu_features |= X86_FEATURE_MMXEXT;
287 #endif
288 }
289 }
290
291 /* query cpu name */
292 if (cpu_ext_info >= 0x80000002) {
293 GLuint ofs;
294 char cpu_name[49];
295 for (ofs = 0; ofs < 3; ofs++)
296 _mesa_x86_cpuid(0x80000002+ofs, (GLuint *)(cpu_name + (16*ofs)+0), (GLuint *)(cpu_name + (16*ofs)+4), (GLuint *)(cpu_name + (16*ofs)+8), (GLuint *)(cpu_name + (16*ofs)+12));
297 cpu_name[48] = '\0'; /* the name should be NULL terminated, but just to be sure */
298
299 if (detection_debug)
300 _mesa_debug(NULL, "CPU name: %s\n", cpu_name);
301 }
302 }
303
304 }
305
306 #ifdef USE_MMX_ASM
307 if ( cpu_has_mmx ) {
308 if ( getenv( "MESA_NO_MMX" ) == 0 ) {
309 if (detection_debug)
310 _mesa_debug(NULL, "MMX cpu detected.\n");
311 } else {
312 _mesa_x86_cpu_features &= ~(X86_FEATURE_MMX);
313 }
314 }
315 #endif
316
317 #ifdef USE_3DNOW_ASM
318 if ( cpu_has_3dnow ) {
319 if ( getenv( "MESA_NO_3DNOW" ) == 0 ) {
320 if (detection_debug)
321 _mesa_debug(NULL, "3DNow! cpu detected.\n");
322 } else {
323 _mesa_x86_cpu_features &= ~(X86_FEATURE_3DNOW);
324 }
325 }
326 #endif
327
328 #ifdef USE_SSE_ASM
329 if ( cpu_has_xmm ) {
330 if ( getenv( "MESA_NO_SSE" ) == 0 ) {
331 if (detection_debug)
332 _mesa_debug(NULL, "SSE cpu detected.\n");
333 if ( getenv( "MESA_FORCE_SSE" ) == 0 ) {
334 _mesa_check_os_sse_support();
335 }
336 } else {
337 _mesa_debug(NULL, "SSE cpu detected, but switched off by user.\n");
338 _mesa_x86_cpu_features &= ~(X86_FEATURE_XMM);
339 }
340 }
341 #endif
342
343 #elif defined(USE_X86_64_ASM)
344 {
345 unsigned int eax, ebx, ecx, edx;
346
347 /* Always available on x86-64. */
348 _mesa_x86_cpu_features |= X86_FEATURE_XMM | X86_FEATURE_XMM2;
349
350 if (!__get_cpuid(1, &eax, &ebx, &ecx, &edx))
351 return;
352
353 if (ecx & bit_SSE4_1)
354 _mesa_x86_cpu_features |= X86_FEATURE_SSE4_1;
355 }
356 #endif /* USE_X86_64_ASM */
357
358 (void) detection_debug;
359 }