2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2004 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 /* CPU feature detection for SDL */
30 #ifdef unix /* FIXME: Better setjmp detection? */
37 #include "SDL_cpuinfo.h"
40 #include <sys/sysctl.h> /* For AltiVec check */
43 #define CPU_HAS_RDTSC 0x00000001
44 #define CPU_HAS_MMX 0x00000002
45 #define CPU_HAS_MMXEXT 0x00000004
46 #define CPU_HAS_3DNOW 0x00000010
47 #define CPU_HAS_3DNOWEXT 0x00000020
48 #define CPU_HAS_SSE 0x00000040
49 #define CPU_HAS_SSE2 0x00000080
50 #define CPU_HAS_ALTIVEC 0x00000100
53 /* This is the brute force way of detecting instruction sets...
54 the idea is borrowed from the libmpeg2 library - thanks!
56 static jmp_buf jmpbuf;
57 static void illegal_instruction(int sig)
63 static __inline__ int CPU_haveCPUID()
66 #if defined(__GNUC__) && defined(i386)
68 " pushfl # Get original EFLAGS \n"
70 " movl %%eax,%%ecx \n"
71 " xorl $0x200000,%%eax # Flip ID bit in EFLAGS \n"
72 " pushl %%eax # Save new EFLAGS value on stack \n"
73 " popfl # Replace current EFLAGS value \n"
74 " pushfl # Get new EFLAGS \n"
75 " popl %%eax # Store new EFLAGS in EAX \n"
76 " xorl %%ecx,%%eax # Can not toggle ID bit, \n"
77 " jz 1f # Processor=80486 \n"
78 " movl $1,%0 # We have CPUID support \n"
84 #elif defined(__GNUC__) && defined(__x86_64__)
85 /* Technically, if this is being compiled under __x86_64__ then it has
86 CPUid by definition. But it's nice to be able to prove it. :) */
88 " pushfq # Get original EFLAGS \n"
90 " movq %%rax,%%rcx \n"
91 " xorl $0x200000,%%eax # Flip ID bit in EFLAGS \n"
92 " pushq %%rax # Save new EFLAGS value on stack \n"
93 " popfq # Replace current EFLAGS value \n"
94 " pushfq # Get new EFLAGS \n"
95 " popq %%rax # Store new EFLAGS in EAX \n"
96 " xorl %%ecx,%%eax # Can not toggle ID bit, \n"
97 " jz 1f # Processor=80486 \n"
98 " movl $1,%0 # We have CPUID support \n"
104 #elif defined(_MSC_VER)
106 pushfd ; Get original EFLAGS
109 xor eax, 200000h ; Flip ID bit in EFLAGS
110 push eax ; Save new EFLAGS value on stack
111 popfd ; Replace current EFLAGS value
112 pushfd ; Get new EFLAGS
113 pop eax ; Store new EFLAGS in EAX
114 xor eax, ecx ; Can not toggle ID bit,
115 jz done ; Processor=80486
116 mov has_CPUID,1 ; We have CPUID support
123 static __inline__ int CPU_getCPUIDFeatures()
126 #if defined(__GNUC__) && ( defined(i386) || defined(__x86_64__) )
128 " movl %%ebx,%%edi\n"
129 " xorl %%eax,%%eax # Set up for CPUID instruction \n"
130 " cpuid # Get and save vendor ID \n"
131 " cmpl $1,%%eax # Make sure 1 is valid input for CPUID\n"
132 " jl 1f # We dont have the CPUID instruction\n"
133 " xorl %%eax,%%eax \n"
135 " cpuid # Get family/model/stepping/features\n"
138 " movl %%edi,%%ebx\n"
141 : "%eax", "%ebx", "%ecx", "%edx", "%edi"
143 #elif defined(_MSC_VER)
145 xor eax, eax ; Set up for CPUID instruction
146 cpuid ; Get and save vendor ID
147 cmp eax, 1 ; Make sure 1 is valid input for CPUID
148 jl done ; We dont have the CPUID instruction
151 cpuid ; Get family/model/stepping/features
159 static __inline__ int CPU_getCPUIDFeaturesExt()
162 #if defined(__GNUC__) && (defined(i386) || defined (__x86_64__) )
164 " movl %%ebx,%%edi\n"
165 " movl $0x80000000,%%eax # Query for extended functions \n"
166 " cpuid # Get extended function limit \n"
167 " cmpl $0x80000001,%%eax \n"
168 " jl 1f # Nope, we dont have function 800000001h\n"
169 " movl $0x80000001,%%eax # Setup extended function 800000001h\n"
170 " cpuid # and get the information \n"
173 " movl %%edi,%%ebx\n"
176 : "%eax", "%ebx", "%ecx", "%edx", "%edi"
178 #elif defined(_MSC_VER)
180 mov eax,80000000h ; Query for extended functions
181 cpuid ; Get extended function limit
183 jl done ; Nope, we dont have function 800000001h
184 mov eax,80000001h ; Setup extended function 800000001h
185 cpuid ; and get the information
193 static __inline__ int CPU_haveRDTSC()
195 if ( CPU_haveCPUID() ) {
196 return (CPU_getCPUIDFeatures() & 0x00000010);
201 static __inline__ int CPU_haveMMX()
203 if ( CPU_haveCPUID() ) {
204 return (CPU_getCPUIDFeatures() & 0x00800000);
209 static __inline__ int CPU_haveMMXExt()
211 if ( CPU_haveCPUID() ) {
212 return (CPU_getCPUIDFeaturesExt() & 0x00400000);
217 static __inline__ int CPU_have3DNow()
219 if ( CPU_haveCPUID() ) {
220 return (CPU_getCPUIDFeaturesExt() & 0x80000000);
225 static __inline__ int CPU_have3DNowExt()
227 if ( CPU_haveCPUID() ) {
228 return (CPU_getCPUIDFeaturesExt() & 0x40000000);
233 static __inline__ int CPU_haveSSE()
235 if ( CPU_haveCPUID() ) {
236 return (CPU_getCPUIDFeatures() & 0x02000000);
241 static __inline__ int CPU_haveSSE2()
243 if ( CPU_haveCPUID() ) {
244 return (CPU_getCPUIDFeatures() & 0x04000000);
249 static __inline__ int CPU_haveAltiVec()
251 volatile int altivec = 0;
253 int selectors[2] = { CTL_HW, HW_VECTORUNIT };
254 int hasVectorUnit = 0;
255 size_t length = sizeof(hasVectorUnit);
256 int error = sysctl(selectors, 2, &hasVectorUnit, &length, NULL, 0);
258 altivec = (hasVectorUnit != 0);
259 #elif defined(USE_SETJMP) && defined(GCC_ALTIVEC)
260 void (*handler)(int sig);
261 handler = signal(SIGILL, illegal_instruction);
262 if ( setjmp(jmpbuf) == 0 ) {
263 asm volatile ("mtspr 256, %0\n\t"
264 "vand %%v0, %%v0, %%v0"
269 signal(SIGILL, handler);
274 static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
276 static Uint32 SDL_GetCPUFeatures()
278 if ( SDL_CPUFeatures == 0xFFFFFFFF ) {
280 if ( CPU_haveRDTSC() ) {
281 SDL_CPUFeatures |= CPU_HAS_RDTSC;
283 if ( CPU_haveMMX() ) {
284 SDL_CPUFeatures |= CPU_HAS_MMX;
286 if ( CPU_haveMMXExt() ) {
287 SDL_CPUFeatures |= CPU_HAS_MMXEXT;
289 if ( CPU_have3DNow() ) {
290 SDL_CPUFeatures |= CPU_HAS_3DNOW;
292 if ( CPU_have3DNowExt() ) {
293 SDL_CPUFeatures |= CPU_HAS_3DNOWEXT;
295 if ( CPU_haveSSE() ) {
296 SDL_CPUFeatures |= CPU_HAS_SSE;
298 if ( CPU_haveSSE2() ) {
299 SDL_CPUFeatures |= CPU_HAS_SSE2;
301 if ( CPU_haveAltiVec() ) {
302 SDL_CPUFeatures |= CPU_HAS_ALTIVEC;
305 return SDL_CPUFeatures;
308 SDL_bool SDL_HasRDTSC()
310 if ( SDL_GetCPUFeatures() & CPU_HAS_RDTSC ) {
316 SDL_bool SDL_HasMMX()
318 if ( SDL_GetCPUFeatures() & CPU_HAS_MMX ) {
324 SDL_bool SDL_HasMMXExt()
326 if ( SDL_GetCPUFeatures() & CPU_HAS_MMXEXT ) {
332 SDL_bool SDL_Has3DNow()
334 if ( SDL_GetCPUFeatures() & CPU_HAS_3DNOW ) {
340 SDL_bool SDL_Has3DNowExt()
342 if ( SDL_GetCPUFeatures() & CPU_HAS_3DNOWEXT ) {
348 SDL_bool SDL_HasSSE()
350 if ( SDL_GetCPUFeatures() & CPU_HAS_SSE ) {
356 SDL_bool SDL_HasSSE2()
358 if ( SDL_GetCPUFeatures() & CPU_HAS_SSE2 ) {
364 SDL_bool SDL_HasAltiVec()
366 if ( SDL_GetCPUFeatures() & CPU_HAS_ALTIVEC ) {
378 printf("RDTSC: %d\n", SDL_HasRDTSC());
379 printf("MMX: %d\n", SDL_HasMMX());
380 printf("MMXExt: %d\n", SDL_HasMMXExt());
381 printf("3DNow: %d\n", SDL_Has3DNow());
382 printf("3DNowExt: %d\n", SDL_Has3DNowExt());
383 printf("SSE: %d\n", SDL_HasSSE());
384 printf("SSE2: %d\n", SDL_HasSSE2());
385 printf("AltiVec: %d\n", SDL_HasAltiVec());
389 #endif /* TEST_MAIN */