src/cpuinfo/SDL_cpuinfo.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 24 Feb 2006 10:23:49 +0000
changeset 1426 ef9a9064bff2
parent 1402 d910939febfa
child 1442 e3242177fe4a
permissions -rw-r--r--
*** empty log message ***
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     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.
     9 
    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.
    14 
    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
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 /* CPU feature detection for SDL */
    25 
    26 #include "SDL.h"
    27 #include "SDL_cpuinfo.h"
    28 
    29 #if SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
    30 #include <signal.h>
    31 #include <setjmp.h>
    32 #endif
    33 
    34 #ifdef __MACOSX__
    35 #include <sys/sysctl.h> /* For AltiVec check */
    36 #endif
    37 
    38 #define CPU_HAS_RDTSC	0x00000001
    39 #define CPU_HAS_MMX	0x00000002
    40 #define CPU_HAS_MMXEXT	0x00000004
    41 #define CPU_HAS_3DNOW	0x00000010
    42 #define CPU_HAS_3DNOWEXT 0x00000020
    43 #define CPU_HAS_SSE	0x00000040
    44 #define CPU_HAS_SSE2	0x00000080
    45 #define CPU_HAS_ALTIVEC	0x00000100
    46 
    47 #if SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
    48 /* This is the brute force way of detecting instruction sets...
    49    the idea is borrowed from the libmpeg2 library - thanks!
    50  */
    51 static jmp_buf jmpbuf;
    52 static void illegal_instruction(int sig)
    53 {
    54 	longjmp(jmpbuf, 1);
    55 }
    56 #endif /* HAVE_SETJMP */
    57 
    58 static __inline__ int CPU_haveCPUID(void)
    59 {
    60 	int has_CPUID = 0;
    61 #if defined(__GNUC__) && defined(i386)
    62 	__asm__ (
    63 "        pushfl                      # Get original EFLAGS             \n"
    64 "        popl    %%eax                                                 \n"
    65 "        movl    %%eax,%%ecx                                           \n"
    66 "        xorl    $0x200000,%%eax     # Flip ID bit in EFLAGS           \n"
    67 "        pushl   %%eax               # Save new EFLAGS value on stack  \n"
    68 "        popfl                       # Replace current EFLAGS value    \n"
    69 "        pushfl                      # Get new EFLAGS                  \n"
    70 "        popl    %%eax               # Store new EFLAGS in EAX         \n"
    71 "        xorl    %%ecx,%%eax         # Can not toggle ID bit,          \n"
    72 "        jz      1f                  # Processor=80486                 \n"
    73 "        movl    $1,%0               # We have CPUID support           \n"
    74 "1:                                                                    \n"
    75 	: "=m" (has_CPUID)
    76 	:
    77 	: "%eax", "%ecx"
    78 	);
    79 #elif defined(__GNUC__) && defined(__x86_64__)
    80 /* Technically, if this is being compiled under __x86_64__ then it has 
    81 CPUid by definition.  But it's nice to be able to prove it.  :)      */
    82 	__asm__ (
    83 "        pushfq                      # Get original EFLAGS             \n"
    84 "        popq    %%rax                                                 \n"
    85 "        movq    %%rax,%%rcx                                           \n"
    86 "        xorl    $0x200000,%%eax     # Flip ID bit in EFLAGS           \n"
    87 "        pushq   %%rax               # Save new EFLAGS value on stack  \n"
    88 "        popfq                       # Replace current EFLAGS value    \n"
    89 "        pushfq                      # Get new EFLAGS                  \n"
    90 "        popq    %%rax               # Store new EFLAGS in EAX         \n"
    91 "        xorl    %%ecx,%%eax         # Can not toggle ID bit,          \n"
    92 "        jz      1f                  # Processor=80486                 \n"
    93 "        movl    $1,%0               # We have CPUID support           \n"
    94 "1:                                                                    \n"
    95 	: "=m" (has_CPUID)
    96 	:
    97 	: "%rax", "%rcx"
    98 	);
    99 #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_X86_))
   100 	__asm {
   101         pushfd                      ; Get original EFLAGS
   102         pop     eax
   103         mov     ecx, eax
   104         xor     eax, 200000h        ; Flip ID bit in EFLAGS
   105         push    eax                 ; Save new EFLAGS value on stack
   106         popfd                       ; Replace current EFLAGS value
   107         pushfd                      ; Get new EFLAGS
   108         pop     eax                 ; Store new EFLAGS in EAX
   109         xor     eax, ecx            ; Can not toggle ID bit,
   110         jz      done                ; Processor=80486
   111         mov     has_CPUID,1         ; We have CPUID support
   112 done:
   113 	}
   114 #elif defined(__sun) && defined(__x86)
   115 	__asm (
   116 "       pushfl                 \n"
   117 "	popl    %eax           \n"
   118 "	movl    %eax,%ecx      \n"
   119 "	xorl    $0x200000,%eax \n"
   120 "	pushl   %eax           \n"
   121 "	popfl                  \n"
   122 "	pushfl                 \n"
   123 "	popl    %eax           \n"
   124 "	xorl    %ecx,%eax      \n"
   125 "	jz      1f             \n"
   126 "	movl    $1,-8(%ebp)    \n"
   127 "1:                            \n"
   128 	);
   129 #elif defined(__sun) && defined(__amd64)
   130 	__asm (
   131 "       pushfq                 \n"
   132 "       popq    %rax           \n"
   133 "       movq    %rax,%rcx      \n"
   134 "       xorl    $0x200000,%eax \n"
   135 "       pushq   %rax           \n"
   136 "       popfq                  \n"
   137 "       pushfq                 \n"
   138 "       popq    %rax           \n"
   139 "       xorl    %ecx,%eax      \n"
   140 "       jz      1f             \n"
   141 "       movl    $1,-8(%rbp)    \n"
   142 "1:                            \n"
   143 	);
   144 #endif
   145 	return has_CPUID;
   146 }
   147 
   148 static __inline__ int CPU_getCPUIDFeatures(void)
   149 {
   150 	int features = 0;
   151 #if defined(__GNUC__) && ( defined(i386) || defined(__x86_64__) )
   152 	__asm__ (
   153 "        movl    %%ebx,%%edi\n"
   154 "        xorl    %%eax,%%eax         # Set up for CPUID instruction    \n"
   155 "        cpuid                       # Get and save vendor ID          \n"
   156 "        cmpl    $1,%%eax            # Make sure 1 is valid input for CPUID\n"
   157 "        jl      1f                  # We dont have the CPUID instruction\n"
   158 "        xorl    %%eax,%%eax                                           \n"
   159 "        incl    %%eax                                                 \n"
   160 "        cpuid                       # Get family/model/stepping/features\n"
   161 "        movl    %%edx,%0                                              \n"
   162 "1:                                                                    \n"
   163 "        movl    %%edi,%%ebx\n"
   164 	: "=m" (features)
   165 	:
   166 	: "%eax", "%ecx", "%edx", "%edi"
   167 	);
   168 #elif defined(_MSC_VER)  && (defined(_M_IX86) || defined(_X86_))
   169 	__asm {
   170         xor     eax, eax            ; Set up for CPUID instruction
   171         cpuid                       ; Get and save vendor ID
   172         cmp     eax, 1              ; Make sure 1 is valid input for CPUID
   173         jl      done                ; We dont have the CPUID instruction
   174         xor     eax, eax
   175         inc     eax
   176         cpuid                       ; Get family/model/stepping/features
   177         mov     features, edx
   178 done:
   179 	}
   180 #elif defined(__sun) && (defined(__x86) || defined(__amd64))
   181 	    __asm(
   182 "        movl    %ebx,%edi\n"
   183 "        xorl    %eax,%eax         \n"
   184 "        cpuid                     \n"
   185 "        cmpl    $1,%eax           \n"
   186 "        jl      1f                \n"
   187 "        xorl    %eax,%eax         \n"
   188 "        incl    %eax              \n"
   189 "        cpuid                     \n"
   190 #ifdef __i386
   191 "        movl    %edx,-8(%ebp)     \n"
   192 #else
   193 "        movl    %edx,-8(%rbp)     \n"
   194 #endif
   195 "1:                                \n"
   196 "        movl    %edi,%ebx\n" );
   197 #endif
   198 	return features;
   199 }
   200 
   201 static __inline__ int CPU_getCPUIDFeaturesExt(void)
   202 {
   203 	int features = 0;
   204 #if defined(__GNUC__) && (defined(i386) || defined (__x86_64__) )
   205 	__asm__ (
   206 "        movl    %%ebx,%%edi\n"
   207 "        movl    $0x80000000,%%eax   # Query for extended functions    \n"
   208 "        cpuid                       # Get extended function limit     \n"
   209 "        cmpl    $0x80000001,%%eax                                     \n"
   210 "        jl      1f                  # Nope, we dont have function 800000001h\n"
   211 "        movl    $0x80000001,%%eax   # Setup extended function 800000001h\n"
   212 "        cpuid                       # and get the information         \n"
   213 "        movl    %%edx,%0                                              \n"
   214 "1:                                                                    \n"
   215 "        movl    %%edi,%%ebx\n"
   216 	: "=m" (features)
   217 	:
   218 	: "%eax", "%ecx", "%edx", "%edi"
   219 	);
   220 #elif defined(_MSC_VER)  && (defined(_M_IX86) || defined(_X86_))
   221 	__asm {
   222         mov     eax,80000000h       ; Query for extended functions
   223         cpuid                       ; Get extended function limit
   224         cmp     eax,80000001h
   225         jl      done                ; Nope, we dont have function 800000001h
   226         mov     eax,80000001h       ; Setup extended function 800000001h
   227         cpuid                       ; and get the information
   228         mov     features,edx
   229 done:
   230 	}
   231 #elif defined(__sun) && ( defined(__i386) || defined(__amd64) )
   232 	    __asm (
   233 "        movl    %ebx,%edi\n"
   234 "        movl    $0x80000000,%eax \n"
   235 "        cpuid                    \n"
   236 "        cmpl    $0x80000001,%eax \n"
   237 "        jl      1f               \n"
   238 "        movl    $0x80000001,%eax \n"
   239 "        cpuid                    \n"
   240 #ifdef __i386
   241 "        movl    %edx,-8(%ebp)   \n"
   242 #else
   243 "        movl    %edx,-8(%rbp)   \n"
   244 #endif
   245 "1:                               \n"
   246 "        movl    %edi,%ebx\n"
   247 	    );
   248 #endif
   249 	return features;
   250 }
   251 
   252 static __inline__ int CPU_haveRDTSC(void)
   253 {
   254 	if ( CPU_haveCPUID() ) {
   255 		return (CPU_getCPUIDFeatures() & 0x00000010);
   256 	}
   257 	return 0;
   258 }
   259 
   260 static __inline__ int CPU_haveMMX(void)
   261 {
   262 	if ( CPU_haveCPUID() ) {
   263 		return (CPU_getCPUIDFeatures() & 0x00800000);
   264 	}
   265 	return 0;
   266 }
   267 
   268 static __inline__ int CPU_haveMMXExt(void)
   269 {
   270 	if ( CPU_haveCPUID() ) {
   271 		return (CPU_getCPUIDFeaturesExt() & 0x00400000);
   272 	}
   273 	return 0;
   274 }
   275 
   276 static __inline__ int CPU_have3DNow(void)
   277 {
   278 	if ( CPU_haveCPUID() ) {
   279 		return (CPU_getCPUIDFeaturesExt() & 0x80000000);
   280 	}
   281 	return 0;
   282 }
   283 
   284 static __inline__ int CPU_have3DNowExt(void)
   285 {
   286 	if ( CPU_haveCPUID() ) {
   287 		return (CPU_getCPUIDFeaturesExt() & 0x40000000);
   288 	}
   289 	return 0;
   290 }
   291 
   292 static __inline__ int CPU_haveSSE(void)
   293 {
   294 	if ( CPU_haveCPUID() ) {
   295 		return (CPU_getCPUIDFeatures() & 0x02000000);
   296 	}
   297 	return 0;
   298 }
   299 
   300 static __inline__ int CPU_haveSSE2(void)
   301 {
   302 	if ( CPU_haveCPUID() ) {
   303 		return (CPU_getCPUIDFeatures() & 0x04000000);
   304 	}
   305 	return 0;
   306 }
   307 
   308 static __inline__ int CPU_haveAltiVec(void)
   309 {
   310 	volatile int altivec = 0;
   311 #ifdef __MACOSX__
   312 	int selectors[2] = { CTL_HW, HW_VECTORUNIT }; 
   313 	int hasVectorUnit = 0; 
   314 	size_t length = sizeof(hasVectorUnit); 
   315 	int error = sysctl(selectors, 2, &hasVectorUnit, &length, NULL, 0); 
   316 	if( 0 == error )
   317 		altivec = (hasVectorUnit != 0); 
   318 #elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
   319 	void (*handler)(int sig);
   320 	handler = signal(SIGILL, illegal_instruction);
   321 	if ( setjmp(jmpbuf) == 0 ) {
   322 		asm volatile ("mtspr 256, %0\n\t"
   323 			      "vand %%v0, %%v0, %%v0"
   324 			      :
   325 			      : "r" (-1));
   326 		altivec = 1;
   327 	}
   328 	signal(SIGILL, handler);
   329 #endif
   330 	return altivec; 
   331 }
   332 
   333 static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
   334 
   335 static Uint32 SDL_GetCPUFeatures(void)
   336 {
   337 	if ( SDL_CPUFeatures == 0xFFFFFFFF ) {
   338 		SDL_CPUFeatures = 0;
   339 		if ( CPU_haveRDTSC() ) {
   340 			SDL_CPUFeatures |= CPU_HAS_RDTSC;
   341 		}
   342 		if ( CPU_haveMMX() ) {
   343 			SDL_CPUFeatures |= CPU_HAS_MMX;
   344 		}
   345 		if ( CPU_haveMMXExt() ) {
   346 			SDL_CPUFeatures |= CPU_HAS_MMXEXT;
   347 		}
   348 		if ( CPU_have3DNow() ) {
   349 			SDL_CPUFeatures |= CPU_HAS_3DNOW;
   350 		}
   351 		if ( CPU_have3DNowExt() ) {
   352 			SDL_CPUFeatures |= CPU_HAS_3DNOWEXT;
   353 		}
   354 		if ( CPU_haveSSE() ) {
   355 			SDL_CPUFeatures |= CPU_HAS_SSE;
   356 		}
   357 		if ( CPU_haveSSE2() ) {
   358 			SDL_CPUFeatures |= CPU_HAS_SSE2;
   359 		}
   360 		if ( CPU_haveAltiVec() ) {
   361 			SDL_CPUFeatures |= CPU_HAS_ALTIVEC;
   362 		}
   363 	}
   364 	return SDL_CPUFeatures;
   365 }
   366 
   367 SDL_bool SDL_HasRDTSC(void)
   368 {
   369 	if ( SDL_GetCPUFeatures() & CPU_HAS_RDTSC ) {
   370 		return SDL_TRUE;
   371 	}
   372 	return SDL_FALSE;
   373 }
   374 
   375 SDL_bool SDL_HasMMX(void)
   376 {
   377 	if ( SDL_GetCPUFeatures() & CPU_HAS_MMX ) {
   378 		return SDL_TRUE;
   379 	}
   380 	return SDL_FALSE;
   381 }
   382 
   383 SDL_bool SDL_HasMMXExt(void)
   384 {
   385 	if ( SDL_GetCPUFeatures() & CPU_HAS_MMXEXT ) {
   386 		return SDL_TRUE;
   387 	}
   388 	return SDL_FALSE;
   389 }
   390 
   391 SDL_bool SDL_Has3DNow(void)
   392 {
   393 	if ( SDL_GetCPUFeatures() & CPU_HAS_3DNOW ) {
   394 		return SDL_TRUE;
   395 	}
   396 	return SDL_FALSE;
   397 }
   398 
   399 SDL_bool SDL_Has3DNowExt(void)
   400 {
   401 	if ( SDL_GetCPUFeatures() & CPU_HAS_3DNOWEXT ) {
   402 		return SDL_TRUE;
   403 	}
   404 	return SDL_FALSE;
   405 }
   406 
   407 SDL_bool SDL_HasSSE(void)
   408 {
   409 	if ( SDL_GetCPUFeatures() & CPU_HAS_SSE ) {
   410 		return SDL_TRUE;
   411 	}
   412 	return SDL_FALSE;
   413 }
   414 
   415 SDL_bool SDL_HasSSE2(void)
   416 {
   417 	if ( SDL_GetCPUFeatures() & CPU_HAS_SSE2 ) {
   418 		return SDL_TRUE;
   419 	}
   420 	return SDL_FALSE;
   421 }
   422 
   423 SDL_bool SDL_HasAltiVec(void)
   424 {
   425 	if ( SDL_GetCPUFeatures() & CPU_HAS_ALTIVEC ) {
   426 		return SDL_TRUE;
   427 	}
   428 	return SDL_FALSE;
   429 }
   430 
   431 #ifdef TEST_MAIN
   432 
   433 #include <stdio.h>
   434 
   435 int main()
   436 {
   437 	printf("RDTSC: %d\n", SDL_HasRDTSC());
   438 	printf("MMX: %d\n", SDL_HasMMX());
   439 	printf("MMXExt: %d\n", SDL_HasMMXExt());
   440 	printf("3DNow: %d\n", SDL_Has3DNow());
   441 	printf("3DNowExt: %d\n", SDL_Has3DNowExt());
   442 	printf("SSE: %d\n", SDL_HasSSE());
   443 	printf("SSE2: %d\n", SDL_HasSSE2());
   444 	printf("AltiVec: %d\n", SDL_HasAltiVec());
   445 	return 0;
   446 }
   447 
   448 #endif /* TEST_MAIN */