2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2012 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__) || defined(__ppc64__))
30 #include <sys/sysctl.h> /* For AltiVec check */
31 #elif defined(__OpenBSD__) && defined(__powerpc__)
32 #include <sys/param.h>
33 #include <sys/sysctl.h> /* For AltiVec check */
34 #include <machine/cpu.h>
35 #elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
40 #define CPU_HAS_RDTSC 0x00000001
41 #define CPU_HAS_MMX 0x00000002
42 #define CPU_HAS_MMXEXT 0x00000004
43 #define CPU_HAS_3DNOW 0x00000010
44 #define CPU_HAS_3DNOWEXT 0x00000020
45 #define CPU_HAS_SSE 0x00000040
46 #define CPU_HAS_SSE2 0x00000080
47 #define CPU_HAS_ALTIVEC 0x00000100
49 #if SDL_ALTIVEC_BLITTERS && HAVE_SETJMP && !__MACOSX__ && !__OpenBSD__
50 /* This is the brute force way of detecting instruction sets...
51 the idea is borrowed from the libmpeg2 library - thanks!
53 static jmp_buf jmpbuf;
54 static void illegal_instruction(int sig)
58 #endif /* HAVE_SETJMP */
60 static __inline__ int CPU_haveCPUID(void)
63 #if defined(__GNUC__) && defined(i386)
65 " pushfl # Get original EFLAGS \n"
67 " movl %%eax,%%ecx \n"
68 " xorl $0x200000,%%eax # Flip ID bit in EFLAGS \n"
69 " pushl %%eax # Save new EFLAGS value on stack \n"
70 " popfl # Replace current EFLAGS value \n"
71 " pushfl # Get new EFLAGS \n"
72 " popl %%eax # Store new EFLAGS in EAX \n"
73 " xorl %%ecx,%%eax # Can not toggle ID bit, \n"
74 " jz 1f # Processor=80486 \n"
75 " movl $1,%0 # We have CPUID support \n"
81 #elif defined(__GNUC__) && defined(__x86_64__)
82 /* Technically, if this is being compiled under __x86_64__ then it has
83 CPUid by definition. But it's nice to be able to prove it. :) */
85 " pushfq # Get original EFLAGS \n"
87 " movq %%rax,%%rcx \n"
88 " xorl $0x200000,%%eax # Flip ID bit in EFLAGS \n"
89 " pushq %%rax # Save new EFLAGS value on stack \n"
90 " popfq # Replace current EFLAGS value \n"
91 " pushfq # Get new EFLAGS \n"
92 " popq %%rax # Store new EFLAGS in EAX \n"
93 " xorl %%ecx,%%eax # Can not toggle ID bit, \n"
94 " jz 1f # Processor=80486 \n"
95 " movl $1,%0 # We have CPUID support \n"
101 #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
103 pushfd ; Get original EFLAGS
106 xor eax, 200000h ; Flip ID bit in EFLAGS
107 push eax ; Save new EFLAGS value on stack
108 popfd ; Replace current EFLAGS value
109 pushfd ; Get new EFLAGS
110 pop eax ; Store new EFLAGS in EAX
111 xor eax, ecx ; Can not toggle ID bit,
112 jz done ; Processor=80486
113 mov has_CPUID,1 ; We have CPUID support
116 #elif defined(__sun) && defined(__i386)
121 " xorl $0x200000,%eax \n"
128 " movl $1,-8(%ebp) \n"
131 #elif defined(__sun) && defined(__amd64)
136 " xorl $0x200000,%eax \n"
143 " movl $1,-8(%rbp) \n"
150 static __inline__ int CPU_getCPUIDFeatures(void)
153 #if defined(__GNUC__) && defined(i386)
155 " xorl %%eax,%%eax # Set up for CPUID instruction \n"
157 " cpuid # Get and save vendor ID \n"
159 " cmpl $1,%%eax # Make sure 1 is valid input for CPUID\n"
160 " jl 1f # We dont have the CPUID instruction\n"
161 " xorl %%eax,%%eax \n"
164 " cpuid # Get family/model/stepping/features\n"
170 : "%eax", "%ecx", "%edx"
172 #elif defined(__GNUC__) && defined(__x86_64__)
174 " xorl %%eax,%%eax # Set up for CPUID instruction \n"
176 " cpuid # Get and save vendor ID \n"
178 " cmpl $1,%%eax # Make sure 1 is valid input for CPUID\n"
179 " jl 1f # We dont have the CPUID instruction\n"
180 " xorl %%eax,%%eax \n"
183 " cpuid # Get family/model/stepping/features\n"
189 : "%rax", "%rcx", "%rdx"
191 #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
193 xor eax, eax ; Set up for CPUID instruction
195 cpuid ; Get and save vendor ID
197 cmp eax, 1 ; Make sure 1 is valid input for CPUID
198 jl done ; We dont have the CPUID instruction
202 cpuid ; Get family/model/stepping/features
207 #elif defined(__sun) && (defined(__i386) || defined(__amd64))
221 " movl %edx,-8(%ebp) \n"
223 " movl %edx,-8(%rbp) \n"
230 static __inline__ int CPU_getCPUIDFeaturesExt(void)
233 #if defined(__GNUC__) && defined(i386)
235 " movl $0x80000000,%%eax # Query for extended functions \n"
237 " cpuid # Get extended function limit \n"
239 " cmpl $0x80000001,%%eax \n"
240 " jl 1f # Nope, we dont have function 800000001h\n"
241 " movl $0x80000001,%%eax # Setup extended function 800000001h\n"
243 " cpuid # and get the information \n"
249 : "%eax", "%ecx", "%edx"
251 #elif defined(__GNUC__) && defined (__x86_64__)
253 " movl $0x80000000,%%eax # Query for extended functions \n"
255 " cpuid # Get extended function limit \n"
257 " cmpl $0x80000001,%%eax \n"
258 " jl 1f # Nope, we dont have function 800000001h\n"
259 " movl $0x80000001,%%eax # Setup extended function 800000001h\n"
261 " cpuid # and get the information \n"
267 : "%rax", "%rcx", "%rdx"
269 #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
271 mov eax,80000000h ; Query for extended functions
273 cpuid ; Get extended function limit
276 jl done ; Nope, we dont have function 800000001h
277 mov eax,80000001h ; Setup extended function 800000001h
279 cpuid ; and get the information
284 #elif defined(__sun) && ( defined(__i386) || defined(__amd64) )
286 " movl $0x80000000,%eax \n"
290 " cmpl $0x80000001,%eax \n"
292 " movl $0x80000001,%eax \n"
297 " movl %edx,-8(%ebp) \n"
299 " movl %edx,-8(%rbp) \n"
307 static __inline__ int CPU_haveRDTSC(void)
309 if ( CPU_haveCPUID() ) {
310 return (CPU_getCPUIDFeatures() & 0x00000010);
315 static __inline__ int CPU_haveMMX(void)
317 if ( CPU_haveCPUID() ) {
318 return (CPU_getCPUIDFeatures() & 0x00800000);
323 static __inline__ int CPU_haveMMXExt(void)
325 if ( CPU_haveCPUID() ) {
326 return (CPU_getCPUIDFeaturesExt() & 0x00400000);
331 static __inline__ int CPU_have3DNow(void)
333 if ( CPU_haveCPUID() ) {
334 return (CPU_getCPUIDFeaturesExt() & 0x80000000);
339 static __inline__ int CPU_have3DNowExt(void)
341 if ( CPU_haveCPUID() ) {
342 return (CPU_getCPUIDFeaturesExt() & 0x40000000);
347 static __inline__ int CPU_haveSSE(void)
349 if ( CPU_haveCPUID() ) {
350 return (CPU_getCPUIDFeatures() & 0x02000000);
355 static __inline__ int CPU_haveSSE2(void)
357 if ( CPU_haveCPUID() ) {
358 return (CPU_getCPUIDFeatures() & 0x04000000);
363 static __inline__ int CPU_haveAltiVec(void)
365 volatile int altivec = 0;
366 #if (defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))) || (defined(__OpenBSD__) && defined(__powerpc__))
368 int selectors[2] = { CTL_MACHDEP, CPU_ALTIVEC };
370 int selectors[2] = { CTL_HW, HW_VECTORUNIT };
372 int hasVectorUnit = 0;
373 size_t length = sizeof(hasVectorUnit);
374 int error = sysctl(selectors, 2, &hasVectorUnit, &length, NULL, 0);
376 altivec = (hasVectorUnit != 0);
377 #elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
378 void (*handler)(int sig);
379 handler = signal(SIGILL, illegal_instruction);
380 if ( setjmp(jmpbuf) == 0 ) {
381 asm volatile ("mtspr 256, %0\n\t"
382 "vand %%v0, %%v0, %%v0"
387 signal(SIGILL, handler);
392 static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
394 static Uint32 SDL_GetCPUFeatures(void)
396 if ( SDL_CPUFeatures == 0xFFFFFFFF ) {
398 if ( CPU_haveRDTSC() ) {
399 SDL_CPUFeatures |= CPU_HAS_RDTSC;
401 if ( CPU_haveMMX() ) {
402 SDL_CPUFeatures |= CPU_HAS_MMX;
404 if ( CPU_haveMMXExt() ) {
405 SDL_CPUFeatures |= CPU_HAS_MMXEXT;
407 if ( CPU_have3DNow() ) {
408 SDL_CPUFeatures |= CPU_HAS_3DNOW;
410 if ( CPU_have3DNowExt() ) {
411 SDL_CPUFeatures |= CPU_HAS_3DNOWEXT;
413 if ( CPU_haveSSE() ) {
414 SDL_CPUFeatures |= CPU_HAS_SSE;
416 if ( CPU_haveSSE2() ) {
417 SDL_CPUFeatures |= CPU_HAS_SSE2;
419 if ( CPU_haveAltiVec() ) {
420 SDL_CPUFeatures |= CPU_HAS_ALTIVEC;
423 return SDL_CPUFeatures;
426 SDL_bool SDL_HasRDTSC(void)
428 if ( SDL_GetCPUFeatures() & CPU_HAS_RDTSC ) {
434 SDL_bool SDL_HasMMX(void)
436 if ( SDL_GetCPUFeatures() & CPU_HAS_MMX ) {
442 SDL_bool SDL_HasMMXExt(void)
444 if ( SDL_GetCPUFeatures() & CPU_HAS_MMXEXT ) {
450 SDL_bool SDL_Has3DNow(void)
452 if ( SDL_GetCPUFeatures() & CPU_HAS_3DNOW ) {
458 SDL_bool SDL_Has3DNowExt(void)
460 if ( SDL_GetCPUFeatures() & CPU_HAS_3DNOWEXT ) {
466 SDL_bool SDL_HasSSE(void)
468 if ( SDL_GetCPUFeatures() & CPU_HAS_SSE ) {
474 SDL_bool SDL_HasSSE2(void)
476 if ( SDL_GetCPUFeatures() & CPU_HAS_SSE2 ) {
482 SDL_bool SDL_HasAltiVec(void)
484 if ( SDL_GetCPUFeatures() & CPU_HAS_ALTIVEC ) {
496 printf("RDTSC: %d\n", SDL_HasRDTSC());
497 printf("MMX: %d\n", SDL_HasMMX());
498 printf("MMXExt: %d\n", SDL_HasMMXExt());
499 printf("3DNow: %d\n", SDL_Has3DNow());
500 printf("3DNowExt: %d\n", SDL_Has3DNowExt());
501 printf("SSE: %d\n", SDL_HasSSE());
502 printf("SSE2: %d\n", SDL_HasSSE2());
503 printf("AltiVec: %d\n", SDL_HasAltiVec());
507 #endif /* TEST_MAIN */