2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 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)
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(__GNUC__) && defined(__x86_64__)
168 " movq %%rbx,%%rdi\n"
169 " xorl %%eax,%%eax # Set up for CPUID instruction \n"
170 " cpuid # Get and save vendor ID \n"
171 " cmpl $1,%%eax # Make sure 1 is valid input for CPUID\n"
172 " jl 1f # We dont have the CPUID instruction\n"
173 " xorl %%eax,%%eax \n"
175 " cpuid # Get family/model/stepping/features\n"
178 " movq %%rdi,%%rbx\n"
181 : "%rax", "%rbx", "%rcx", "%rdx", "%rdi"
183 #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
185 xor eax, eax ; Set up for CPUID instruction
186 cpuid ; Get and save vendor ID
187 cmp eax, 1 ; Make sure 1 is valid input for CPUID
188 jl done ; We dont have the CPUID instruction
191 cpuid ; Get family/model/stepping/features
195 #elif defined(__sun) && (defined(__i386) || defined(__amd64))
206 " movl %edx,-8(%ebp) \n"
208 " movl %edx,-8(%rbp) \n"
211 " movl %edi,%ebx\n" );
216 static __inline__ int CPU_getCPUIDFeaturesExt(void)
219 #if defined(__GNUC__) && defined(i386)
221 " movl %%ebx,%%edi\n"
222 " movl $0x80000000,%%eax # Query for extended functions \n"
223 " cpuid # Get extended function limit \n"
224 " cmpl $0x80000001,%%eax \n"
225 " jl 1f # Nope, we dont have function 800000001h\n"
226 " movl $0x80000001,%%eax # Setup extended function 800000001h\n"
227 " cpuid # and get the information \n"
230 " movl %%edi,%%ebx\n"
233 : "%eax", "%ecx", "%edx", "%edi"
235 #elif defined(__GNUC__) && defined (__x86_64__)
237 " movq %%rbx,%%rdi\n"
238 " movl $0x80000000,%%eax # Query for extended functions \n"
239 " cpuid # Get extended function limit \n"
240 " cmpl $0x80000001,%%eax \n"
241 " jl 1f # Nope, we dont have function 800000001h\n"
242 " movl $0x80000001,%%eax # Setup extended function 800000001h\n"
243 " cpuid # and get the information \n"
246 " movq %%rdi,%%rbx\n"
249 : "%rax", "%rbx", "%rcx", "%rdx", "%rdi"
251 #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
253 mov eax,80000000h ; Query for extended functions
254 cpuid ; Get extended function limit
256 jl done ; Nope, we dont have function 800000001h
257 mov eax,80000001h ; Setup extended function 800000001h
258 cpuid ; and get the information
262 #elif defined(__sun) && ( defined(__i386) || defined(__amd64) )
265 " movl $0x80000000,%eax \n"
267 " cmpl $0x80000001,%eax \n"
269 " movl $0x80000001,%eax \n"
272 " movl %edx,-8(%ebp) \n"
274 " movl %edx,-8(%rbp) \n"
283 static __inline__ int CPU_haveRDTSC(void)
285 if ( CPU_haveCPUID() ) {
286 return (CPU_getCPUIDFeatures() & 0x00000010);
291 static __inline__ int CPU_haveMMX(void)
293 if ( CPU_haveCPUID() ) {
294 return (CPU_getCPUIDFeatures() & 0x00800000);
299 static __inline__ int CPU_haveMMXExt(void)
301 if ( CPU_haveCPUID() ) {
302 return (CPU_getCPUIDFeaturesExt() & 0x00400000);
307 static __inline__ int CPU_have3DNow(void)
309 if ( CPU_haveCPUID() ) {
310 return (CPU_getCPUIDFeaturesExt() & 0x80000000);
315 static __inline__ int CPU_have3DNowExt(void)
317 if ( CPU_haveCPUID() ) {
318 return (CPU_getCPUIDFeaturesExt() & 0x40000000);
323 static __inline__ int CPU_haveSSE(void)
325 if ( CPU_haveCPUID() ) {
326 return (CPU_getCPUIDFeatures() & 0x02000000);
331 static __inline__ int CPU_haveSSE2(void)
333 if ( CPU_haveCPUID() ) {
334 return (CPU_getCPUIDFeatures() & 0x04000000);
339 static __inline__ int CPU_haveAltiVec(void)
341 volatile int altivec = 0;
342 #if defined(__MACOSX__) && defined(__ppc__)
343 int selectors[2] = { CTL_HW, HW_VECTORUNIT };
344 int hasVectorUnit = 0;
345 size_t length = sizeof(hasVectorUnit);
346 int error = sysctl(selectors, 2, &hasVectorUnit, &length, NULL, 0);
348 altivec = (hasVectorUnit != 0);
349 #elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
350 void (*handler)(int sig);
351 handler = signal(SIGILL, illegal_instruction);
352 if ( setjmp(jmpbuf) == 0 ) {
353 asm volatile ("mtspr 256, %0\n\t"
354 "vand %%v0, %%v0, %%v0"
359 signal(SIGILL, handler);
364 static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
366 static Uint32 SDL_GetCPUFeatures(void)
368 if ( SDL_CPUFeatures == 0xFFFFFFFF ) {
370 if ( CPU_haveRDTSC() ) {
371 SDL_CPUFeatures |= CPU_HAS_RDTSC;
373 if ( CPU_haveMMX() ) {
374 SDL_CPUFeatures |= CPU_HAS_MMX;
376 if ( CPU_haveMMXExt() ) {
377 SDL_CPUFeatures |= CPU_HAS_MMXEXT;
379 if ( CPU_have3DNow() ) {
380 SDL_CPUFeatures |= CPU_HAS_3DNOW;
382 if ( CPU_have3DNowExt() ) {
383 SDL_CPUFeatures |= CPU_HAS_3DNOWEXT;
385 if ( CPU_haveSSE() ) {
386 SDL_CPUFeatures |= CPU_HAS_SSE;
388 if ( CPU_haveSSE2() ) {
389 SDL_CPUFeatures |= CPU_HAS_SSE2;
391 if ( CPU_haveAltiVec() ) {
392 SDL_CPUFeatures |= CPU_HAS_ALTIVEC;
395 return SDL_CPUFeatures;
398 SDL_bool SDL_HasRDTSC(void)
400 if ( SDL_GetCPUFeatures() & CPU_HAS_RDTSC ) {
406 SDL_bool SDL_HasMMX(void)
408 if ( SDL_GetCPUFeatures() & CPU_HAS_MMX ) {
414 SDL_bool SDL_HasMMXExt(void)
416 if ( SDL_GetCPUFeatures() & CPU_HAS_MMXEXT ) {
422 SDL_bool SDL_Has3DNow(void)
424 if ( SDL_GetCPUFeatures() & CPU_HAS_3DNOW ) {
430 SDL_bool SDL_Has3DNowExt(void)
432 if ( SDL_GetCPUFeatures() & CPU_HAS_3DNOWEXT ) {
438 SDL_bool SDL_HasSSE(void)
440 if ( SDL_GetCPUFeatures() & CPU_HAS_SSE ) {
446 SDL_bool SDL_HasSSE2(void)
448 if ( SDL_GetCPUFeatures() & CPU_HAS_SSE2 ) {
454 SDL_bool SDL_HasAltiVec(void)
456 if ( SDL_GetCPUFeatures() & CPU_HAS_ALTIVEC ) {
468 printf("RDTSC: %d\n", SDL_HasRDTSC());
469 printf("MMX: %d\n", SDL_HasMMX());
470 printf("MMXExt: %d\n", SDL_HasMMXExt());
471 printf("3DNow: %d\n", SDL_Has3DNow());
472 printf("3DNowExt: %d\n", SDL_Has3DNowExt());
473 printf("SSE: %d\n", SDL_HasSSE());
474 printf("SSE2: %d\n", SDL_HasSSE2());
475 printf("AltiVec: %d\n", SDL_HasAltiVec());
479 #endif /* TEST_MAIN */