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 */
31 #include "SDL_cpuinfo.h"
33 #define CPU_HAS_RDTSC 0x00000001
34 #define CPU_HAS_MMX 0x00000002
35 #define CPU_HAS_3DNOW 0x00000004
36 #define CPU_HAS_SSE 0x00000008
38 static __inline__ int CPU_haveCPUID()
41 #if defined(__GNUC__) && defined(i386)
44 " pushfl # Get original EFLAGS \n"
46 " movl %%eax,%%ecx \n"
47 " xorl $0x200000,%%eax # Flip ID bit in EFLAGS \n"
48 " pushl %%eax # Save new EFLAGS value on stack \n"
49 " popfl # Replace current EFLAGS value \n"
50 " pushfl # Get new EFLAGS \n"
51 " popl %%eax # Store new EFLAGS in EAX \n"
52 " xorl %%ecx,%%eax # Can not toggle ID bit, \n"
53 " jz 1f # Processor=80486 \n"
54 " movl $1,%0 # We have CPUID support \n"
61 #elif defined(_MSC_VER)
63 pushfd ; Get original EFLAGS
66 xor eax, 200000h ; Flip ID bit in EFLAGS
67 push eax ; Save new EFLAGS value on stack
68 popfd ; Replace current EFLAGS value
69 pushfd ; Get new EFLAGS
70 pop eax ; Store new EFLAGS in EAX
71 xor eax, ecx ; Can not toggle ID bit,
72 jz done ; Processor=80486
73 mov has_CPUID,1 ; We have CPUID support
80 static __inline__ int CPU_getCPUIDFeatures()
83 #if defined(__GNUC__) && defined(i386)
88 " xorl %%eax,%%eax # Set up for CPUID instruction \n"
89 " cpuid # Get and save vendor ID \n"
90 " cmpl $1,%%eax # Make sure 1 is valid input for CPUID\n"
91 " jl 1f # We dont have the CPUID instruction\n"
92 " xorl %%eax,%%eax \n"
94 " cpuid # Get family/model/stepping/features\n"
102 : "%eax", "%ebx", "%ecx", "%edx"
104 #elif defined(_MSC_VER)
106 xor eax, eax ; Set up for CPUID instruction
107 cpuid ; Get and save vendor ID
108 cmp eax, 1 ; Make sure 1 is valid input for CPUID
109 jl done ; We dont have the CPUID instruction
112 cpuid ; Get family/model/stepping/features
120 static __inline__ int CPU_haveRDTSC()
122 if ( CPU_haveCPUID() ) {
123 return (CPU_getCPUIDFeatures() & 0x00000010);
128 static __inline__ int CPU_haveMMX()
130 if ( CPU_haveCPUID() ) {
131 return (CPU_getCPUIDFeatures() & 0x00800000);
136 static __inline__ int CPU_have3DNow()
139 if ( !CPU_haveCPUID() ) {
142 #if defined(__GNUC__) && defined(i386)
147 " movl $0x80000000,%%eax # Query for extended functions \n"
148 " cpuid # Get extended function limit \n"
149 " cmpl $0x80000001,%%eax \n"
150 " jbe 1f # Nope, we dont have function 800000001h\n"
151 " movl $0x80000001,%%eax # Setup extended function 800000001h\n"
152 " cpuid # and get the information \n"
153 " testl $0x80000000,%%edx # Bit 31 is set if 3DNow! present \n"
154 " jz 1f # Nope, we dont have 3DNow support\n"
155 " movl $1,%0 # Yep, we have 3DNow! support! \n"
162 : "%eax", "%ebx", "%ecx", "%edx"
164 #elif defined(_MSC_VER)
166 mov eax,80000000h ; Query for extended functions
167 cpuid ; Get extended function limit
169 jbe done ; Nope, we dont have function 800000001h
170 mov eax,80000001h ; Setup extended function 800000001h
171 cpuid ; and get the information
172 test edx,80000000h ; Bit 31 is set if 3DNow! present
173 jz done ; Nope, we dont have 3DNow support
174 mov has_3DNow,1 ; Yep, we have 3DNow! support!
181 static __inline__ int CPU_haveSSE()
183 if ( CPU_haveCPUID() ) {
184 return (CPU_getCPUIDFeatures() & 0x02000000);
189 static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
191 static Uint32 SDL_GetCPUFeatures()
193 if ( SDL_CPUFeatures == 0xFFFFFFFF ) {
195 if ( CPU_haveRDTSC() ) {
196 SDL_CPUFeatures |= CPU_HAS_RDTSC;
198 if ( CPU_haveMMX() ) {
199 SDL_CPUFeatures |= CPU_HAS_MMX;
201 if ( CPU_have3DNow() ) {
202 SDL_CPUFeatures |= CPU_HAS_3DNOW;
204 if ( CPU_haveSSE() ) {
205 SDL_CPUFeatures |= CPU_HAS_SSE;
208 return SDL_CPUFeatures;
211 SDL_bool SDL_HasRDTSC()
213 if ( SDL_GetCPUFeatures() & CPU_HAS_RDTSC ) {
219 SDL_bool SDL_HasMMX()
221 if ( SDL_GetCPUFeatures() & CPU_HAS_MMX ) {
227 SDL_bool SDL_Has3DNow()
229 if ( SDL_GetCPUFeatures() & CPU_HAS_3DNOW ) {
235 SDL_bool SDL_HasSSE()
237 if ( SDL_GetCPUFeatures() & CPU_HAS_SSE ) {
249 printf("MMX: %d\n", SDL_HasMMX());
250 printf("3DNow: %d\n", SDL_Has3DNow());
251 printf("SSE: %d\n", SDL_HasSSE());
255 #endif /* TEST_MAIN */