2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
28 * Check CPU capabilities & initialize optimized funtions for this particular
31 * Changed by Andre Werthmann for using the new SSE functions.
33 * \author Holger Waechtler <holger@akaflieg.extern.tu-berlin.de>
34 * \author Andre Werthmann <wertmann@cs.uni-potsdam.de>
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>
41 #if defined(USE_SSE_ASM) && defined(__FreeBSD__)
42 #include <sys/types.h>
43 #include <sys/sysctl.h>
45 #if defined(USE_SSE_ASM) && defined(__OpenBSD__)
46 #include <sys/param.h>
47 #include <sys/sysctl.h>
48 #include <machine/cpu.h>
51 #include "main/imports.h"
52 #include "common_x86_asm.h"
55 int _mesa_x86_cpu_features
= 0;
57 /* No reason for this to be public.
59 extern GLuint _ASMAPI
_mesa_x86_has_cpuid(void);
60 extern void _ASMAPI
_mesa_x86_cpuid(GLuint op
, GLuint
*reg_eax
, GLuint
*reg_ebx
, GLuint
*reg_ecx
, GLuint
*reg_edx
);
61 extern GLuint _ASMAPI
_mesa_x86_cpuid_eax(GLuint op
);
62 extern GLuint _ASMAPI
_mesa_x86_cpuid_ebx(GLuint op
);
63 extern GLuint _ASMAPI
_mesa_x86_cpuid_ecx(GLuint op
);
64 extern GLuint _ASMAPI
_mesa_x86_cpuid_edx(GLuint op
);
67 #if defined(USE_SSE_ASM)
69 * We must verify that the Streaming SIMD Extensions are truly supported
70 * on this processor before we go ahead and hook out the optimized code.
72 * However, I have been told by Alan Cox that all 2.4 (and later) Linux
73 * kernels provide full SSE support on all processors that expose SSE via
74 * the CPUID mechanism.
76 extern void _mesa_test_os_sse_support( void );
77 extern void _mesa_test_os_sse_exception_support( void );
80 #ifndef STATUS_FLOAT_MULTIPLE_TRAPS
81 # define STATUS_FLOAT_MULTIPLE_TRAPS (0xC00002B5L)
83 static LONG WINAPI
ExceptionFilter(LPEXCEPTION_POINTERS exp
)
85 PEXCEPTION_RECORD rec
= exp
->ExceptionRecord
;
86 PCONTEXT ctx
= exp
->ContextRecord
;
88 if ( rec
->ExceptionCode
== EXCEPTION_ILLEGAL_INSTRUCTION
) {
89 _mesa_debug(NULL
, "EXCEPTION_ILLEGAL_INSTRUCTION\n" );
90 _mesa_x86_cpu_features
&= ~(X86_FEATURE_XMM
);
91 } else if ( rec
->ExceptionCode
== STATUS_FLOAT_MULTIPLE_TRAPS
) {
92 _mesa_debug(NULL
, "STATUS_FLOAT_MULTIPLE_TRAPS\n");
93 /* Windows seems to clear the exception flag itself, we just have to increment Eip */
95 _mesa_debug(NULL
, "UNEXPECTED EXCEPTION (0x%08x), terminating!\n" );
96 return EXCEPTION_EXECUTE_HANDLER
;
99 if ( (ctx
->ContextFlags
& CONTEXT_CONTROL
) != CONTEXT_CONTROL
) {
100 _mesa_debug(NULL
, "Context does not contain control registers, terminating!\n");
101 return EXCEPTION_EXECUTE_HANDLER
;
105 return EXCEPTION_CONTINUE_EXECUTION
;
110 static void check_os_sse_support( void )
112 #if defined(__FreeBSD__)
116 len
= sizeof(enabled
);
117 ret
= sysctlbyname("hw.instruction_sse", &enabled
, &len
, NULL
, 0);
119 _mesa_x86_cpu_features
&= ~(X86_FEATURE_XMM
);
121 #elif defined (__NetBSD__)
124 size_t len
= sizeof(enabled
);
125 ret
= sysctlbyname("machdep.sse", &enabled
, &len
, (void *)NULL
, 0);
127 _mesa_x86_cpu_features
&= ~(X86_FEATURE_XMM
);
129 #elif defined(__OpenBSD__)
133 size_t len
= sizeof(enabled
);
135 mib
[0] = CTL_MACHDEP
;
138 ret
= sysctl(mib
, 2, &enabled
, &len
, NULL
, 0);
140 _mesa_x86_cpu_features
&= ~(X86_FEATURE_XMM
);
143 LPTOP_LEVEL_EXCEPTION_FILTER oldFilter
;
145 /* Install our ExceptionFilter */
146 oldFilter
= SetUnhandledExceptionFilter( ExceptionFilter
);
149 _mesa_debug(NULL
, "Testing OS support for SSE...\n");
151 _mesa_test_os_sse_support();
154 _mesa_debug(NULL
, "Yes.\n");
156 _mesa_debug(NULL
, "No!\n");
161 _mesa_debug(NULL
, "Testing OS support for SSE unmasked exceptions...\n");
163 _mesa_test_os_sse_exception_support();
166 _mesa_debug(NULL
, "Yes.\n");
168 _mesa_debug(NULL
, "No!\n");
172 /* Restore previous exception filter */
173 SetUnhandledExceptionFilter( oldFilter
);
176 _mesa_debug(NULL
, "Tests of OS support for SSE passed.\n");
178 _mesa_debug(NULL
, "Tests of OS support for SSE failed!\n");
181 /* Do nothing on other platforms for now.
183 _mesa_debug(NULL
, "Not testing OS support for SSE, leaving enabled.\n");
184 #endif /* __FreeBSD__ */
187 #endif /* USE_SSE_ASM */
190 void _mesa_init_all_x86_transform_asm( void )
193 _mesa_x86_cpu_features
= 0;
195 if (!_mesa_x86_has_cpuid()) {
196 _mesa_debug(NULL
, "CPUID not detected\n");
200 GLuint cpu_ext_features
;
205 /* get vendor name */
206 _mesa_x86_cpuid(0, &result
, (GLuint
*)(cpu_vendor
+ 0), (GLuint
*)(cpu_vendor
+ 8), (GLuint
*)(cpu_vendor
+ 4));
207 cpu_vendor
[12] = '\0';
209 _mesa_debug(NULL
, "CPU vendor: %s\n", cpu_vendor
);
211 /* get cpu features */
212 cpu_features
= _mesa_x86_cpuid_edx(1);
214 if (cpu_features
& X86_CPU_FPU
)
215 _mesa_x86_cpu_features
|= X86_FEATURE_FPU
;
216 if (cpu_features
& X86_CPU_CMOV
)
217 _mesa_x86_cpu_features
|= X86_FEATURE_CMOV
;
220 if (cpu_features
& X86_CPU_MMX
)
221 _mesa_x86_cpu_features
|= X86_FEATURE_MMX
;
225 if (cpu_features
& X86_CPU_XMM
)
226 _mesa_x86_cpu_features
|= X86_FEATURE_XMM
;
227 if (cpu_features
& X86_CPU_XMM2
)
228 _mesa_x86_cpu_features
|= X86_FEATURE_XMM2
;
231 /* query extended cpu features */
232 if ((cpu_ext_info
= _mesa_x86_cpuid_eax(0x80000000)) > 0x80000000) {
233 if (cpu_ext_info
>= 0x80000001) {
235 cpu_ext_features
= _mesa_x86_cpuid_edx(0x80000001);
237 if (cpu_features
& X86_CPU_MMX
) {
240 if (cpu_ext_features
& X86_CPUEXT_3DNOW
)
241 _mesa_x86_cpu_features
|= X86_FEATURE_3DNOW
;
242 if (cpu_ext_features
& X86_CPUEXT_3DNOW_EXT
)
243 _mesa_x86_cpu_features
|= X86_FEATURE_3DNOWEXT
;
247 if (cpu_ext_features
& X86_CPUEXT_MMX_EXT
)
248 _mesa_x86_cpu_features
|= X86_FEATURE_MMXEXT
;
254 if (cpu_ext_info
>= 0x80000002) {
257 for (ofs
= 0; ofs
< 3; ofs
++)
258 _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));
259 cpu_name
[48] = '\0'; /* the name should be NULL terminated, but just to be sure */
261 _mesa_debug(NULL
, "CPU name: %s\n", cpu_name
);
267 if ( _mesa_getenv( "MESA_NO_ASM" ) ) {
268 _mesa_x86_cpu_features
= 0;
271 if ( _mesa_x86_cpu_features
) {
272 _mesa_init_x86_transform_asm();
277 if ( _mesa_getenv( "MESA_NO_MMX" ) == 0 ) {
278 _mesa_debug(NULL
, "MMX cpu detected.\n");
280 _mesa_x86_cpu_features
&= ~(X86_FEATURE_MMX
);
286 if ( cpu_has_3dnow
) {
287 if ( _mesa_getenv( "MESA_NO_3DNOW" ) == 0 ) {
288 _mesa_debug(NULL
, "3DNow! cpu detected.\n");
289 _mesa_init_3dnow_transform_asm();
291 _mesa_x86_cpu_features
&= ~(X86_FEATURE_3DNOW
);
298 if ( _mesa_getenv( "MESA_NO_SSE" ) == 0 ) {
299 _mesa_debug(NULL
, "SSE cpu detected.\n");
300 if ( _mesa_getenv( "MESA_FORCE_SSE" ) == 0 ) {
301 check_os_sse_support();
304 _mesa_init_sse_transform_asm();
307 _mesa_debug(NULL
, "SSE cpu detected, but switched off by user.\n");
308 _mesa_x86_cpu_features
&= ~(X86_FEATURE_XMM
);