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