2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2006 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 /* CPU feature detection for SDL */
27 #include "SDL_cpuinfo.h"
29 #if defined(__MACOSX__) && defined(__ppc__)
30 #include <sys/sysctl.h> /* For AltiVec check */
31 #elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
36 #define CPU_HAS_RDTSC 0x00000001
37 #define CPU_HAS_MMX 0x00000002
38 #define CPU_HAS_MMXEXT 0x00000004
39 #define CPU_HAS_3DNOW 0x00000010
40 #define CPU_HAS_3DNOWEXT 0x00000020
41 #define CPU_HAS_SSE 0x00000040
42 #define CPU_HAS_SSE2 0x00000080
43 #define CPU_HAS_ALTIVEC 0x00000100
45 #if SDL_ALTIVEC_BLITTERS && HAVE_SETJMP && !__MACOSX__
46 /* This is the brute force way of detecting instruction sets...
47 the idea is borrowed from the libmpeg2 library - thanks!
49 static jmp_buf jmpbuf
;
50 static void illegal_instruction(int sig
)
54 #endif /* HAVE_SETJMP */
56 static __inline__
int CPU_haveCPUID(void)
59 #if defined(__GNUC__) && defined(i386)
61 " pushfl # Get original EFLAGS \n"
63 " movl %%eax,%%ecx \n"
64 " xorl $0x200000,%%eax # Flip ID bit in EFLAGS \n"
65 " pushl %%eax # Save new EFLAGS value on stack \n"
66 " popfl # Replace current EFLAGS value \n"
67 " pushfl # Get new EFLAGS \n"
68 " popl %%eax # Store new EFLAGS in EAX \n"
69 " xorl %%ecx,%%eax # Can not toggle ID bit, \n"
70 " jz 1f # Processor=80486 \n"
71 " movl $1,%0 # We have CPUID support \n"
77 #elif defined(__GNUC__) && defined(__x86_64__)
78 /* Technically, if this is being compiled under __x86_64__ then it has
79 CPUid by definition. But it's nice to be able to prove it. :) */
81 " pushfq # Get original EFLAGS \n"
83 " movq %%rax,%%rcx \n"
84 " xorl $0x200000,%%eax # Flip ID bit in EFLAGS \n"
85 " pushq %%rax # Save new EFLAGS value on stack \n"
86 " popfq # Replace current EFLAGS value \n"
87 " pushfq # Get new EFLAGS \n"
88 " popq %%rax # Store new EFLAGS in EAX \n"
89 " xorl %%ecx,%%eax # Can not toggle ID bit, \n"
90 " jz 1f # Processor=80486 \n"
91 " movl $1,%0 # We have CPUID support \n"
97 #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
99 pushfd
; Get original EFLAGS
102 xor eax
, 200000h
; Flip ID bit in EFLAGS
103 push eax
; Save
new EFLAGS value on stack
104 popfd
; Replace current EFLAGS value
105 pushfd
; Get
new EFLAGS
106 pop eax
; Store
new EFLAGS in EAX
107 xor eax
, ecx
; Can
not toggle ID bit
,
108 jz done
; Processor
=80486
109 mov has_CPUID
,1 ; We have CPUID support
112 #elif defined(__sun) && defined(__i386)
117 " xorl $0x200000,%eax \n"
124 " movl $1,-8(%ebp) \n"
127 #elif defined(__sun) && defined(__amd64)
132 " xorl $0x200000,%eax \n"
139 " movl $1,-8(%rbp) \n"
146 static __inline__
int CPU_getCPUIDFeatures(void)
149 #if defined(__GNUC__) && ( defined(i386) || defined(__x86_64__) )
151 " movl %%ebx,%%edi\n"
152 " xorl %%eax,%%eax # Set up for CPUID instruction \n"
153 " cpuid # Get and save vendor ID \n"
154 " cmpl $1,%%eax # Make sure 1 is valid input for CPUID\n"
155 " jl 1f # We dont have the CPUID instruction\n"
156 " xorl %%eax,%%eax \n"
158 " cpuid # Get family/model/stepping/features\n"
161 " movl %%edi,%%ebx\n"
164 : "%eax", "%ecx", "%edx", "%edi"
166 #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
168 xor eax
, eax
; Set up
for CPUID instruction
169 cpuid
; Get
and save vendor ID
170 cmp eax
, 1 ; Make sure
1 is valid input
for CPUID
171 jl done
; We dont have the CPUID instruction
174 cpuid
; Get family
/model
/stepping
/features
178 #elif defined(__sun) && (defined(__i386) || defined(__amd64))
189 " movl %edx,-8(%ebp) \n"
191 " movl %edx,-8(%rbp) \n"
194 " movl %edi,%ebx\n" );
199 static __inline__
int CPU_getCPUIDFeaturesExt(void)
202 #if defined(__GNUC__) && (defined(i386) || defined (__x86_64__) )
204 " movl %%ebx,%%edi\n"
205 " movl $0x80000000,%%eax # Query for extended functions \n"
206 " cpuid # Get extended function limit \n"
207 " cmpl $0x80000001,%%eax \n"
208 " jl 1f # Nope, we dont have function 800000001h\n"
209 " movl $0x80000001,%%eax # Setup extended function 800000001h\n"
210 " cpuid # and get the information \n"
213 " movl %%edi,%%ebx\n"
216 : "%eax", "%ecx", "%edx", "%edi"
218 #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
220 mov eax
,80000000h
; Query
for extended functions
221 cpuid
; Get extended function limit
223 jl done
; Nope
, we dont have function
800000001h
224 mov eax
,80000001h
; Setup extended function
800000001h
225 cpuid
; and get the information
229 #elif defined(__sun) && ( defined(__i386) || defined(__amd64) )
232 " movl $0x80000000,%eax \n"
234 " cmpl $0x80000001,%eax \n"
236 " movl $0x80000001,%eax \n"
239 " movl %edx,-8(%ebp) \n"
241 " movl %edx,-8(%rbp) \n"
250 static __inline__
int CPU_haveRDTSC(void)
252 if ( CPU_haveCPUID() ) {
253 return (CPU_getCPUIDFeatures() & 0x00000010);
258 static __inline__
int CPU_haveMMX(void)
260 if ( CPU_haveCPUID() ) {
261 return (CPU_getCPUIDFeatures() & 0x00800000);
266 static __inline__
int CPU_haveMMXExt(void)
268 if ( CPU_haveCPUID() ) {
269 return (CPU_getCPUIDFeaturesExt() & 0x00400000);
274 static __inline__
int CPU_have3DNow(void)
276 if ( CPU_haveCPUID() ) {
277 return (CPU_getCPUIDFeaturesExt() & 0x80000000);
282 static __inline__
int CPU_have3DNowExt(void)
284 if ( CPU_haveCPUID() ) {
285 return (CPU_getCPUIDFeaturesExt() & 0x40000000);
290 static __inline__
int CPU_haveSSE(void)
292 if ( CPU_haveCPUID() ) {
293 return (CPU_getCPUIDFeatures() & 0x02000000);
298 static __inline__
int CPU_haveSSE2(void)
300 if ( CPU_haveCPUID() ) {
301 return (CPU_getCPUIDFeatures() & 0x04000000);
306 static __inline__
int CPU_haveAltiVec(void)
308 volatile int altivec
= 0;
309 #if defined(__MACOSX__) && defined(__ppc__)
310 int selectors
[2] = { CTL_HW
, HW_VECTORUNIT
};
311 int hasVectorUnit
= 0;
312 size_t length
= sizeof(hasVectorUnit
);
313 int error
= sysctl(selectors
, 2, &hasVectorUnit
, &length
, NULL
, 0);
315 altivec
= (hasVectorUnit
!= 0);
316 #elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
317 void (*handler
)(int sig
);
318 handler
= signal(SIGILL
, illegal_instruction
);
319 if ( setjmp(jmpbuf
) == 0 ) {
320 asm volatile ("mtspr 256, %0\n\t"
321 "vand %%v0, %%v0, %%v0"
326 signal(SIGILL
, handler
);
331 static Uint32 SDL_CPUFeatures
= 0xFFFFFFFF;
333 static Uint32
SDL_GetCPUFeatures(void)
335 if ( SDL_CPUFeatures
== 0xFFFFFFFF ) {
337 if ( CPU_haveRDTSC() ) {
338 SDL_CPUFeatures
|= CPU_HAS_RDTSC
;
340 if ( CPU_haveMMX() ) {
341 SDL_CPUFeatures
|= CPU_HAS_MMX
;
343 if ( CPU_haveMMXExt() ) {
344 SDL_CPUFeatures
|= CPU_HAS_MMXEXT
;
346 if ( CPU_have3DNow() ) {
347 SDL_CPUFeatures
|= CPU_HAS_3DNOW
;
349 if ( CPU_have3DNowExt() ) {
350 SDL_CPUFeatures
|= CPU_HAS_3DNOWEXT
;
352 if ( CPU_haveSSE() ) {
353 SDL_CPUFeatures
|= CPU_HAS_SSE
;
355 if ( CPU_haveSSE2() ) {
356 SDL_CPUFeatures
|= CPU_HAS_SSE2
;
358 if ( CPU_haveAltiVec() ) {
359 SDL_CPUFeatures
|= CPU_HAS_ALTIVEC
;
362 return SDL_CPUFeatures
;
365 SDL_bool
SDL_HasRDTSC(void)
367 if ( SDL_GetCPUFeatures() & CPU_HAS_RDTSC
) {
373 SDL_bool
SDL_HasMMX(void)
375 if ( SDL_GetCPUFeatures() & CPU_HAS_MMX
) {
381 SDL_bool
SDL_HasMMXExt(void)
383 if ( SDL_GetCPUFeatures() & CPU_HAS_MMXEXT
) {
389 SDL_bool
SDL_Has3DNow(void)
391 if ( SDL_GetCPUFeatures() & CPU_HAS_3DNOW
) {
397 SDL_bool
SDL_Has3DNowExt(void)
399 if ( SDL_GetCPUFeatures() & CPU_HAS_3DNOWEXT
) {
405 SDL_bool
SDL_HasSSE(void)
407 if ( SDL_GetCPUFeatures() & CPU_HAS_SSE
) {
413 SDL_bool
SDL_HasSSE2(void)
415 if ( SDL_GetCPUFeatures() & CPU_HAS_SSE2
) {
421 SDL_bool
SDL_HasAltiVec(void)
423 if ( SDL_GetCPUFeatures() & CPU_HAS_ALTIVEC
) {
435 printf("RDTSC: %d\n", SDL_HasRDTSC());
436 printf("MMX: %d\n", SDL_HasMMX());
437 printf("MMXExt: %d\n", SDL_HasMMXExt());
438 printf("3DNow: %d\n", SDL_Has3DNow());
439 printf("3DNowExt: %d\n", SDL_Has3DNowExt());
440 printf("SSE: %d\n", SDL_HasSSE());
441 printf("SSE2: %d\n", SDL_HasSSE2());
442 printf("AltiVec: %d\n", SDL_HasAltiVec());
446 #endif /* TEST_MAIN */