mesa: added test for __NetBSD__
[mesa.git] / src / mesa / x86 / common_x86.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.1
4 *
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR 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(__linux__)
39 #include <linux/version.h>
40 #endif
41 #if defined(USE_SSE_ASM) && defined(__FreeBSD__)
42 #include <sys/types.h>
43 #include <sys/sysctl.h>
44 #endif
45
46 #include "common_x86_asm.h"
47 #include "imports.h"
48
49
50 int _mesa_x86_cpu_features = 0;
51
52 /* No reason for this to be public.
53 */
54 extern GLuint _ASMAPI _mesa_x86_has_cpuid(void);
55 extern void _ASMAPI _mesa_x86_cpuid(GLuint op, GLuint *reg_eax, GLuint *reg_ebx, GLuint *reg_ecx, GLuint *reg_edx);
56 extern GLuint _ASMAPI _mesa_x86_cpuid_eax(GLuint op);
57 extern GLuint _ASMAPI _mesa_x86_cpuid_ebx(GLuint op);
58 extern GLuint _ASMAPI _mesa_x86_cpuid_ecx(GLuint op);
59 extern GLuint _ASMAPI _mesa_x86_cpuid_edx(GLuint op);
60
61
62 #if defined(USE_SSE_ASM)
63 /*
64 * We must verify that the Streaming SIMD Extensions are truly supported
65 * on this processor before we go ahead and hook out the optimized code.
66 *
67 * However, I have been told by Alan Cox that all 2.4 (and later) Linux
68 * kernels provide full SSE support on all processors that expose SSE via
69 * the CPUID mechanism.
70 */
71 extern void _mesa_test_os_sse_support( void );
72 extern void _mesa_test_os_sse_exception_support( void );
73
74 #if defined(WIN32)
75 #ifndef STATUS_FLOAT_MULTIPLE_TRAPS
76 # define STATUS_FLOAT_MULTIPLE_TRAPS (0xC00002B5L)
77 #endif
78 static LONG WINAPI ExceptionFilter(LPEXCEPTION_POINTERS exp)
79 {
80 PEXCEPTION_RECORD rec = exp->ExceptionRecord;
81 PCONTEXT ctx = exp->ContextRecord;
82
83 if ( rec->ExceptionCode == EXCEPTION_ILLEGAL_INSTRUCTION ) {
84 _mesa_debug(NULL, "EXCEPTION_ILLEGAL_INSTRUCTION\n" );
85 _mesa_x86_cpu_features &= ~(X86_FEATURE_XMM);
86 } else if ( rec->ExceptionCode == STATUS_FLOAT_MULTIPLE_TRAPS ) {
87 _mesa_debug(NULL, "STATUS_FLOAT_MULTIPLE_TRAPS\n");
88 /* Windows seems to clear the exception flag itself, we just have to increment Eip */
89 } else {
90 _mesa_debug(NULL, "UNEXPECTED EXCEPTION (0x%08x), terminating!\n" );
91 return EXCEPTION_EXECUTE_HANDLER;
92 }
93
94 if ( (ctx->ContextFlags & CONTEXT_CONTROL) != CONTEXT_CONTROL ) {
95 _mesa_debug(NULL, "Context does not contain control registers, terminating!\n");
96 return EXCEPTION_EXECUTE_HANDLER;
97 }
98 ctx->Eip += 3;
99
100 return EXCEPTION_CONTINUE_EXECUTION;
101 }
102 #endif /* WIN32 */
103
104
105 static void check_os_sse_support( void )
106 {
107 #if defined(__FreeBSD__)
108 {
109 int ret, enabled;
110 unsigned int len;
111 len = sizeof(enabled);
112 ret = sysctlbyname("hw.instruction_sse", &enabled, &len, NULL, 0);
113 if (ret || !enabled)
114 _mesa_x86_cpu_features &= ~(X86_FEATURE_XMM);
115 }
116 #elif defined (__NetBSD__)
117 {
118 int ret, enabled;
119 size_t len = sizeof(enabled);
120 ret = sysctlbyname("machdep.sse", &enabled, &len, (void *)NULL, 0);
121 if (ret || !enabled)
122 _mesa_x86_cpu_features &= ~(X86_FEATURE_XMM);
123 }
124 #elif defined(WIN32)
125 LPTOP_LEVEL_EXCEPTION_FILTER oldFilter;
126
127 /* Install our ExceptionFilter */
128 oldFilter = SetUnhandledExceptionFilter( ExceptionFilter );
129
130 if ( cpu_has_xmm ) {
131 _mesa_debug(NULL, "Testing OS support for SSE...\n");
132
133 _mesa_test_os_sse_support();
134
135 if ( cpu_has_xmm ) {
136 _mesa_debug(NULL, "Yes.\n");
137 } else {
138 _mesa_debug(NULL, "No!\n");
139 }
140 }
141
142 if ( cpu_has_xmm ) {
143 _mesa_debug(NULL, "Testing OS support for SSE unmasked exceptions...\n");
144
145 _mesa_test_os_sse_exception_support();
146
147 if ( cpu_has_xmm ) {
148 _mesa_debug(NULL, "Yes.\n");
149 } else {
150 _mesa_debug(NULL, "No!\n");
151 }
152 }
153
154 /* Restore previous exception filter */
155 SetUnhandledExceptionFilter( oldFilter );
156
157 if ( cpu_has_xmm ) {
158 _mesa_debug(NULL, "Tests of OS support for SSE passed.\n");
159 } else {
160 _mesa_debug(NULL, "Tests of OS support for SSE failed!\n");
161 }
162 #else
163 /* Do nothing on other platforms for now.
164 */
165 _mesa_debug(NULL, "Not testing OS support for SSE, leaving enabled.\n");
166 #endif /* __FreeBSD__ */
167 }
168
169 #endif /* USE_SSE_ASM */
170
171
172 void _mesa_init_all_x86_transform_asm( void )
173 {
174 #ifdef USE_X86_ASM
175 _mesa_x86_cpu_features = 0;
176
177 if (!_mesa_x86_has_cpuid()) {
178 _mesa_debug(NULL, "CPUID not detected\n");
179 }
180 else {
181 GLuint cpu_features;
182 GLuint cpu_ext_features;
183 GLuint cpu_ext_info;
184 char cpu_vendor[13];
185 GLuint result;
186
187 /* get vendor name */
188 _mesa_x86_cpuid(0, &result, (GLuint *)(cpu_vendor + 0), (GLuint *)(cpu_vendor + 8), (GLuint *)(cpu_vendor + 4));
189 cpu_vendor[12] = '\0';
190
191 _mesa_debug(NULL, "CPU vendor: %s\n", cpu_vendor);
192
193 /* get cpu features */
194 cpu_features = _mesa_x86_cpuid_edx(1);
195
196 if (cpu_features & X86_CPU_FPU)
197 _mesa_x86_cpu_features |= X86_FEATURE_FPU;
198 if (cpu_features & X86_CPU_CMOV)
199 _mesa_x86_cpu_features |= X86_FEATURE_CMOV;
200
201 #ifdef USE_MMX_ASM
202 if (cpu_features & X86_CPU_MMX)
203 _mesa_x86_cpu_features |= X86_FEATURE_MMX;
204 #endif
205
206 #ifdef USE_SSE_ASM
207 if (cpu_features & X86_CPU_XMM)
208 _mesa_x86_cpu_features |= X86_FEATURE_XMM;
209 if (cpu_features & X86_CPU_XMM2)
210 _mesa_x86_cpu_features |= X86_FEATURE_XMM2;
211 #endif
212
213 /* query extended cpu features */
214 if ((cpu_ext_info = _mesa_x86_cpuid_eax(0x80000000)) > 0x80000000) {
215 if (cpu_ext_info >= 0x80000001) {
216
217 cpu_ext_features = _mesa_x86_cpuid_edx(0x80000001);
218
219 if (cpu_features & X86_CPU_MMX) {
220
221 #ifdef USE_3DNOW_ASM
222 if (cpu_ext_features & X86_CPUEXT_3DNOW)
223 _mesa_x86_cpu_features |= X86_FEATURE_3DNOW;
224 if (cpu_ext_features & X86_CPUEXT_3DNOW_EXT)
225 _mesa_x86_cpu_features |= X86_FEATURE_3DNOWEXT;
226 #endif
227
228 #ifdef USE_MMX_ASM
229 if (cpu_ext_features & X86_CPUEXT_MMX_EXT)
230 _mesa_x86_cpu_features |= X86_FEATURE_MMXEXT;
231 #endif
232 }
233 }
234
235 /* query cpu name */
236 if (cpu_ext_info >= 0x80000002) {
237 GLuint ofs;
238 char cpu_name[49];
239 for (ofs = 0; ofs < 3; ofs++)
240 _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));
241 cpu_name[48] = '\0'; /* the name should be NULL terminated, but just to be sure */
242
243 _mesa_debug(NULL, "CPU name: %s\n", cpu_name);
244 }
245 }
246
247 }
248
249 if ( _mesa_getenv( "MESA_NO_ASM" ) ) {
250 _mesa_x86_cpu_features = 0;
251 }
252
253 if ( _mesa_x86_cpu_features ) {
254 _mesa_init_x86_transform_asm();
255 }
256
257 #ifdef USE_MMX_ASM
258 if ( cpu_has_mmx ) {
259 if ( _mesa_getenv( "MESA_NO_MMX" ) == 0 ) {
260 _mesa_debug(NULL, "MMX cpu detected.\n");
261 } else {
262 _mesa_x86_cpu_features &= ~(X86_FEATURE_MMX);
263 }
264 }
265 #endif
266
267 #ifdef USE_3DNOW_ASM
268 if ( cpu_has_3dnow ) {
269 if ( _mesa_getenv( "MESA_NO_3DNOW" ) == 0 ) {
270 _mesa_debug(NULL, "3DNow! cpu detected.\n");
271 _mesa_init_3dnow_transform_asm();
272 } else {
273 _mesa_x86_cpu_features &= ~(X86_FEATURE_3DNOW);
274 }
275 }
276 #endif
277
278 #ifdef USE_SSE_ASM
279 if ( cpu_has_xmm ) {
280 if ( _mesa_getenv( "MESA_NO_SSE" ) == 0 ) {
281 _mesa_debug(NULL, "SSE cpu detected.\n");
282 if ( _mesa_getenv( "MESA_FORCE_SSE" ) == 0 ) {
283 check_os_sse_support();
284 }
285 if ( cpu_has_xmm ) {
286 _mesa_init_sse_transform_asm();
287 }
288 } else {
289 _mesa_debug(NULL, "SSE cpu detected, but switched off by user.\n");
290 _mesa_x86_cpu_features &= ~(X86_FEATURE_XMM);
291 }
292 }
293 #endif
294 #endif
295 }
296