2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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.
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(__FreeBSD__)
39 #include <sys/types.h>
40 #include <sys/sysctl.h>
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>
47 #if defined(USE_X86_64_ASM)
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
59 #include "main/errors.h"
61 #include "common_x86_asm.h"
64 /** Bitmask of X86_FEATURE_x bits */
65 int _mesa_x86_cpu_features
= 0x0;
67 static int detection_debug
= GL_FALSE
;
69 /* No reason for this to be public.
71 extern GLuint
_mesa_x86_has_cpuid(void);
72 extern void _mesa_x86_cpuid(GLuint op
, GLuint
*reg_eax
, GLuint
*reg_ebx
, GLuint
*reg_ecx
, GLuint
*reg_edx
);
73 extern GLuint
_mesa_x86_cpuid_eax(GLuint op
);
74 extern GLuint
_mesa_x86_cpuid_ebx(GLuint op
);
75 extern GLuint
_mesa_x86_cpuid_ecx(GLuint op
);
76 extern GLuint
_mesa_x86_cpuid_edx(GLuint op
);
79 #if defined(USE_SSE_ASM)
81 * We must verify that the Streaming SIMD Extensions are truly supported
82 * on this processor before we go ahead and hook out the optimized code.
84 * However, I have been told by Alan Cox that all 2.4 (and later) Linux
85 * kernels provide full SSE support on all processors that expose SSE via
86 * the CPUID mechanism.
89 /* These are assembly functions: */
90 extern void _mesa_test_os_sse_support( void );
91 extern void _mesa_test_os_sse_exception_support( void );
95 #ifndef STATUS_FLOAT_MULTIPLE_TRAPS
96 # define STATUS_FLOAT_MULTIPLE_TRAPS (0xC00002B5L)
98 static LONG WINAPI
ExceptionFilter(LPEXCEPTION_POINTERS exp
)
100 PEXCEPTION_RECORD rec
= exp
->ExceptionRecord
;
101 PCONTEXT ctx
= exp
->ContextRecord
;
103 if ( rec
->ExceptionCode
== EXCEPTION_ILLEGAL_INSTRUCTION
) {
104 _mesa_debug(NULL
, "EXCEPTION_ILLEGAL_INSTRUCTION\n" );
105 _mesa_x86_cpu_features
&= ~(X86_FEATURE_XMM
);
106 } else if ( rec
->ExceptionCode
== STATUS_FLOAT_MULTIPLE_TRAPS
) {
107 _mesa_debug(NULL
, "STATUS_FLOAT_MULTIPLE_TRAPS\n");
108 /* Windows seems to clear the exception flag itself, we just have to increment Eip */
110 _mesa_debug(NULL
, "UNEXPECTED EXCEPTION (0x%08x), terminating!\n" );
111 return EXCEPTION_EXECUTE_HANDLER
;
114 if ( (ctx
->ContextFlags
& CONTEXT_CONTROL
) != CONTEXT_CONTROL
) {
115 _mesa_debug(NULL
, "Context does not contain control registers, terminating!\n");
116 return EXCEPTION_EXECUTE_HANDLER
;
120 return EXCEPTION_CONTINUE_EXECUTION
;
126 * Check if SSE is supported.
127 * If not, turn off the X86_FEATURE_XMM flag in _mesa_x86_cpu_features.
129 void _mesa_check_os_sse_support( void )
131 #if defined(__FreeBSD__)
135 len
= sizeof(enabled
);
136 ret
= sysctlbyname("hw.instruction_sse", &enabled
, &len
, NULL
, 0);
138 _mesa_x86_cpu_features
&= ~(X86_FEATURE_XMM
);
140 #elif defined (__NetBSD__)
143 size_t len
= sizeof(enabled
);
144 ret
= sysctlbyname("machdep.sse", &enabled
, &len
, (void *)NULL
, 0);
146 _mesa_x86_cpu_features
&= ~(X86_FEATURE_XMM
);
148 #elif defined(__OpenBSD__)
152 size_t len
= sizeof(enabled
);
154 mib
[0] = CTL_MACHDEP
;
157 ret
= sysctl(mib
, 2, &enabled
, &len
, NULL
, 0);
159 _mesa_x86_cpu_features
&= ~(X86_FEATURE_XMM
);
161 #elif defined(_WIN32)
162 LPTOP_LEVEL_EXCEPTION_FILTER oldFilter
;
164 /* Install our ExceptionFilter */
165 oldFilter
= SetUnhandledExceptionFilter( ExceptionFilter
);
168 _mesa_debug(NULL
, "Testing OS support for SSE...\n");
170 _mesa_test_os_sse_support();
173 _mesa_debug(NULL
, "Yes.\n");
175 _mesa_debug(NULL
, "No!\n");
180 _mesa_debug(NULL
, "Testing OS support for SSE unmasked exceptions...\n");
182 _mesa_test_os_sse_exception_support();
185 _mesa_debug(NULL
, "Yes.\n");
187 _mesa_debug(NULL
, "No!\n");
191 /* Restore previous exception filter */
192 SetUnhandledExceptionFilter( oldFilter
);
195 _mesa_debug(NULL
, "Tests of OS support for SSE passed.\n");
197 _mesa_debug(NULL
, "Tests of OS support for SSE failed!\n");
200 /* Do nothing on other platforms for now.
203 _mesa_debug(NULL
, "Not testing OS support for SSE, leaving enabled.\n");
204 #endif /* __FreeBSD__ */
207 #endif /* USE_SSE_ASM */
211 * Initialize the _mesa_x86_cpu_features bitfield.
212 * This is a no-op if called more than once.
215 _mesa_get_x86_features(void)
217 static int called
= 0;
225 _mesa_x86_cpu_features
= 0x0;
227 if (getenv( "MESA_NO_ASM")) {
231 if (!_mesa_x86_has_cpuid()) {
232 _mesa_debug(NULL
, "CPUID not detected\n");
235 GLuint cpu_features
, cpu_features_ecx
;
236 GLuint cpu_ext_features
;
241 /* get vendor name */
242 _mesa_x86_cpuid(0, &result
, (GLuint
*)(cpu_vendor
+ 0), (GLuint
*)(cpu_vendor
+ 8), (GLuint
*)(cpu_vendor
+ 4));
243 cpu_vendor
[12] = '\0';
246 _mesa_debug(NULL
, "CPU vendor: %s\n", cpu_vendor
);
248 /* get cpu features */
249 cpu_features
= _mesa_x86_cpuid_edx(1);
250 cpu_features_ecx
= _mesa_x86_cpuid_ecx(1);
252 if (cpu_features
& X86_CPU_FPU
)
253 _mesa_x86_cpu_features
|= X86_FEATURE_FPU
;
254 if (cpu_features
& X86_CPU_CMOV
)
255 _mesa_x86_cpu_features
|= X86_FEATURE_CMOV
;
258 if (cpu_features
& X86_CPU_MMX
)
259 _mesa_x86_cpu_features
|= X86_FEATURE_MMX
;
263 if (cpu_features
& X86_CPU_XMM
)
264 _mesa_x86_cpu_features
|= X86_FEATURE_XMM
;
265 if (cpu_features
& X86_CPU_XMM2
)
266 _mesa_x86_cpu_features
|= X86_FEATURE_XMM2
;
267 if (cpu_features_ecx
& X86_CPU_SSE4_1
)
268 _mesa_x86_cpu_features
|= X86_FEATURE_SSE4_1
;
271 /* query extended cpu features */
272 if ((cpu_ext_info
= _mesa_x86_cpuid_eax(0x80000000)) > 0x80000000) {
273 if (cpu_ext_info
>= 0x80000001) {
275 cpu_ext_features
= _mesa_x86_cpuid_edx(0x80000001);
277 if (cpu_features
& X86_CPU_MMX
) {
280 if (cpu_ext_features
& X86_CPUEXT_3DNOW
)
281 _mesa_x86_cpu_features
|= X86_FEATURE_3DNOW
;
282 if (cpu_ext_features
& X86_CPUEXT_3DNOW_EXT
)
283 _mesa_x86_cpu_features
|= X86_FEATURE_3DNOWEXT
;
287 if (cpu_ext_features
& X86_CPUEXT_MMX_EXT
)
288 _mesa_x86_cpu_features
|= X86_FEATURE_MMXEXT
;
294 if (cpu_ext_info
>= 0x80000002) {
297 for (ofs
= 0; ofs
< 3; ofs
++)
298 _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));
299 cpu_name
[48] = '\0'; /* the name should be NULL terminated, but just to be sure */
302 _mesa_debug(NULL
, "CPU name: %s\n", cpu_name
);
310 if ( getenv( "MESA_NO_MMX" ) == 0 ) {
312 _mesa_debug(NULL
, "MMX cpu detected.\n");
314 _mesa_x86_cpu_features
&= ~(X86_FEATURE_MMX
);
320 if ( cpu_has_3dnow
) {
321 if ( getenv( "MESA_NO_3DNOW" ) == 0 ) {
323 _mesa_debug(NULL
, "3DNow! cpu detected.\n");
325 _mesa_x86_cpu_features
&= ~(X86_FEATURE_3DNOW
);
332 if ( getenv( "MESA_NO_SSE" ) == 0 ) {
334 _mesa_debug(NULL
, "SSE cpu detected.\n");
335 if ( getenv( "MESA_FORCE_SSE" ) == 0 ) {
336 _mesa_check_os_sse_support();
339 _mesa_debug(NULL
, "SSE cpu detected, but switched off by user.\n");
340 _mesa_x86_cpu_features
&= ~(X86_FEATURE_XMM
);
345 #elif defined(USE_X86_64_ASM)
347 unsigned int eax
, ebx
, ecx
, edx
;
349 /* Always available on x86-64. */
350 _mesa_x86_cpu_features
|= X86_FEATURE_XMM
| X86_FEATURE_XMM2
;
352 if (!__get_cpuid(1, &eax
, &ebx
, &ecx
, &edx
))
355 if (ecx
& bit_SSE4_1
)
356 _mesa_x86_cpu_features
|= X86_FEATURE_SSE4_1
;
358 #endif /* USE_X86_64_ASM */
360 (void) detection_debug
;