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