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>
46 #include "common_x86_asm.h"
50 int _mesa_x86_cpu_features
= 0;
52 /* No reason for this to be public.
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
);
62 #if defined(USE_SSE_ASM)
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.
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.
71 extern void _mesa_test_os_sse_support( void );
72 extern void _mesa_test_os_sse_exception_support( void );
75 #ifndef STATUS_FLOAT_MULTIPLE_TRAPS
76 # define STATUS_FLOAT_MULTIPLE_TRAPS (0xC00002B5L)
78 static LONG WINAPI
ExceptionFilter(LPEXCEPTION_POINTERS exp
)
80 PEXCEPTION_RECORD rec
= exp
->ExceptionRecord
;
81 PCONTEXT ctx
= exp
->ContextRecord
;
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 */
90 _mesa_debug(NULL
, "UNEXPECTED EXCEPTION (0x%08x), terminating!\n" );
91 return EXCEPTION_EXECUTE_HANDLER
;
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
;
100 return EXCEPTION_CONTINUE_EXECUTION
;
105 static void check_os_sse_support( void )
107 #if defined(__FreeBSD__)
111 len
= sizeof(enabled
);
112 ret
= sysctlbyname("hw.instruction_sse", &enabled
, &len
, NULL
, 0);
114 _mesa_x86_cpu_features
&= ~(X86_FEATURE_XMM
);
117 LPTOP_LEVEL_EXCEPTION_FILTER oldFilter
;
119 /* Install our ExceptionFilter */
120 oldFilter
= SetUnhandledExceptionFilter( ExceptionFilter
);
123 _mesa_debug(NULL
, "Testing OS support for SSE...\n");
125 _mesa_test_os_sse_support();
128 _mesa_debug(NULL
, "Yes.\n");
130 _mesa_debug(NULL
, "No!\n");
135 _mesa_debug(NULL
, "Testing OS support for SSE unmasked exceptions...\n");
137 _mesa_test_os_sse_exception_support();
140 _mesa_debug(NULL
, "Yes.\n");
142 _mesa_debug(NULL
, "No!\n");
146 /* Restore previous exception filter */
147 SetUnhandledExceptionFilter( oldFilter
);
150 _mesa_debug(NULL
, "Tests of OS support for SSE passed.\n");
152 _mesa_debug(NULL
, "Tests of OS support for SSE failed!\n");
155 /* Do nothing on other platforms for now.
157 _mesa_debug(NULL
, "Not testing OS support for SSE, leaving enabled.\n");
158 #endif /* __FreeBSD__ */
161 #endif /* USE_SSE_ASM */
164 void _mesa_init_all_x86_transform_asm( void )
167 _mesa_x86_cpu_features
= 0;
169 if (!_mesa_x86_has_cpuid()) {
170 _mesa_debug(NULL
, "CPUID not detected\n");
174 GLuint cpu_ext_features
;
179 /* get vendor name */
180 _mesa_x86_cpuid(0, &result
, (GLuint
*)(cpu_vendor
+ 0), (GLuint
*)(cpu_vendor
+ 8), (GLuint
*)(cpu_vendor
+ 4));
181 cpu_vendor
[12] = '\0';
183 _mesa_debug(NULL
, "CPU vendor: %s\n", cpu_vendor
);
185 /* get cpu features */
186 cpu_features
= _mesa_x86_cpuid_edx(1);
188 if (cpu_features
& X86_CPU_FPU
)
189 _mesa_x86_cpu_features
|= X86_FEATURE_FPU
;
190 if (cpu_features
& X86_CPU_CMOV
)
191 _mesa_x86_cpu_features
|= X86_FEATURE_CMOV
;
194 if (cpu_features
& X86_CPU_MMX
)
195 _mesa_x86_cpu_features
|= X86_FEATURE_MMX
;
199 if (cpu_features
& X86_CPU_XMM
)
200 _mesa_x86_cpu_features
|= X86_FEATURE_XMM
;
201 if (cpu_features
& X86_CPU_XMM2
)
202 _mesa_x86_cpu_features
|= X86_FEATURE_XMM2
;
205 /* query extended cpu features */
206 if ((cpu_ext_info
= _mesa_x86_cpuid_eax(0x80000000)) > 0x80000000) {
207 if (cpu_ext_info
>= 0x80000001) {
209 cpu_ext_features
= _mesa_x86_cpuid_edx(0x80000001);
211 if (cpu_features
& X86_CPU_MMX
) {
214 if (cpu_ext_features
& X86_CPUEXT_3DNOW
)
215 _mesa_x86_cpu_features
|= X86_FEATURE_3DNOW
;
216 if (cpu_ext_features
& X86_CPUEXT_3DNOW_EXT
)
217 _mesa_x86_cpu_features
|= X86_FEATURE_3DNOWEXT
;
221 if (cpu_ext_features
& X86_CPUEXT_MMX_EXT
)
222 _mesa_x86_cpu_features
|= X86_FEATURE_MMXEXT
;
228 if (cpu_ext_info
>= 0x80000002) {
231 for (ofs
= 0; ofs
< 3; ofs
++)
232 _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));
233 cpu_name
[48] = '\0'; /* the name should be NULL terminated, but just to be sure */
235 _mesa_debug(NULL
, "CPU name: %s\n", cpu_name
);
241 if ( _mesa_getenv( "MESA_NO_ASM" ) ) {
242 _mesa_x86_cpu_features
= 0;
245 if ( _mesa_x86_cpu_features
) {
246 _mesa_init_x86_transform_asm();
251 if ( _mesa_getenv( "MESA_NO_MMX" ) == 0 ) {
252 _mesa_debug(NULL
, "MMX cpu detected.\n");
254 _mesa_x86_cpu_features
&= ~(X86_FEATURE_MMX
);
260 if ( cpu_has_3dnow
) {
261 if ( _mesa_getenv( "MESA_NO_3DNOW" ) == 0 ) {
262 _mesa_debug(NULL
, "3DNow! cpu detected.\n");
263 _mesa_init_3dnow_transform_asm();
265 _mesa_x86_cpu_features
&= ~(X86_FEATURE_3DNOW
);
272 if ( _mesa_getenv( "MESA_NO_SSE" ) == 0 ) {
273 _mesa_debug(NULL
, "SSE cpu detected.\n");
274 if ( _mesa_getenv( "MESA_FORCE_SSE" ) == 0 ) {
275 check_os_sse_support();
278 _mesa_init_sse_transform_asm();
281 _mesa_debug(NULL
, "SSE cpu detected, but switched off by user.\n");
282 _mesa_x86_cpu_features
&= ~(X86_FEATURE_XMM
);