comparison src/cpuinfo/SDL_cpuinfo.c @ 2915:deb377097225

Fixed CPU feature detection on x86_64 platform (registers were being corrupted, causing crashes)
author Sam Lantinga <slouken@libsdl.org>
date Wed, 24 Dec 2008 13:13:38 +0000
parents 99210400e8b9
children 0b160c970b7e
comparison
equal deleted inserted replaced
2914:fa109e90be36 2915:deb377097225
150 static __inline__ int 150 static __inline__ int
151 CPU_getCPUIDFeatures(void) 151 CPU_getCPUIDFeatures(void)
152 { 152 {
153 int features = 0; 153 int features = 0;
154 /* *INDENT-OFF* */ 154 /* *INDENT-OFF* */
155 #if defined(__GNUC__) && ( defined(i386) || defined(__x86_64__) ) 155 #if defined(__GNUC__) && defined(i386)
156 __asm__ ( 156 __asm__ (
157 " movl %%ebx,%%edi\n" 157 " movl %%ebx,%%edi\n"
158 " xorl %%eax,%%eax # Set up for CPUID instruction \n" 158 " xorl %%eax,%%eax # Set up for CPUID instruction \n"
159 " cpuid # Get and save vendor ID \n" 159 " cpuid # Get and save vendor ID \n"
160 " cmpl $1,%%eax # Make sure 1 is valid input for CPUID\n" 160 " cmpl $1,%%eax # Make sure 1 is valid input for CPUID\n"
166 "1: \n" 166 "1: \n"
167 " movl %%edi,%%ebx\n" 167 " movl %%edi,%%ebx\n"
168 : "=m" (features) 168 : "=m" (features)
169 : 169 :
170 : "%eax", "%ecx", "%edx", "%edi" 170 : "%eax", "%ecx", "%edx", "%edi"
171 );
172 #elif defined(__GNUC__) && defined(__x86_64__)
173 __asm__ (
174 " movq %%rbx,%%rdi\n"
175 " xorl %%eax,%%eax # Set up for CPUID instruction \n"
176 " cpuid # Get and save vendor ID \n"
177 " cmpl $1,%%eax # Make sure 1 is valid input for CPUID\n"
178 " jl 1f # We dont have the CPUID instruction\n"
179 " xorl %%eax,%%eax \n"
180 " incl %%eax \n"
181 " cpuid # Get family/model/stepping/features\n"
182 " movl %%edx,%0 \n"
183 "1: \n"
184 " movq %%rdi,%%rbx\n"
185 : "=m" (features)
186 :
187 : "%rax", "%rcx", "%rdx", "%rdi"
171 ); 188 );
172 #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__) 189 #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
173 __asm { 190 __asm {
174 xor eax, eax ; Set up for CPUID instruction 191 xor eax, eax ; Set up for CPUID instruction
175 cpuid ; Get and save vendor ID 192 cpuid ; Get and save vendor ID
206 static __inline__ int 223 static __inline__ int
207 CPU_getCPUIDFeaturesExt(void) 224 CPU_getCPUIDFeaturesExt(void)
208 { 225 {
209 int features = 0; 226 int features = 0;
210 /* *INDENT-OFF* */ 227 /* *INDENT-OFF* */
211 #if defined(__GNUC__) && (defined(i386) || defined (__x86_64__) ) 228 #if defined(__GNUC__) && defined(i386)
212 __asm__ ( 229 __asm__ (
213 " movl %%ebx,%%edi\n" 230 " movl %%ebx,%%edi\n"
214 " movl $0x80000000,%%eax # Query for extended functions \n" 231 " movl $0x80000000,%%eax # Query for extended functions \n"
215 " cpuid # Get extended function limit \n" 232 " cpuid # Get extended function limit \n"
216 " cmpl $0x80000001,%%eax \n" 233 " cmpl $0x80000001,%%eax \n"
221 "1: \n" 238 "1: \n"
222 " movl %%edi,%%ebx\n" 239 " movl %%edi,%%ebx\n"
223 : "=m" (features) 240 : "=m" (features)
224 : 241 :
225 : "%eax", "%ecx", "%edx", "%edi" 242 : "%eax", "%ecx", "%edx", "%edi"
243 );
244 #elif defined(__GNUC__) && defined (__x86_64__)
245 __asm__ (
246 " movq %%rbx,%%rdi\n"
247 " movl $0x80000000,%%eax # Query for extended functions \n"
248 " cpuid # Get extended function limit \n"
249 " cmpl $0x80000001,%%eax \n"
250 " jl 1f # Nope, we dont have function 800000001h\n"
251 " movl $0x80000001,%%eax # Setup extended function 800000001h\n"
252 " cpuid # and get the information \n"
253 " movl %%edx,%0 \n"
254 "1: \n"
255 " movq %%rdi,%%rbx\n"
256 : "=m" (features)
257 :
258 : "%rax", "%rcx", "%rdx", "%rdi"
226 ); 259 );
227 #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__) 260 #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
228 __asm { 261 __asm {
229 mov eax,80000000h ; Query for extended functions 262 mov eax,80000000h ; Query for extended functions
230 cpuid ; Get extended function limit 263 cpuid ; Get extended function limit