src/cpuinfo/SDL_cpuinfo.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 18 Jul 2012 13:08:19 -0700
branchSDL-1.2
changeset 6364 4a08ade5b4c9
parent 6137 4720145f848b
child 6908 8b9fcdd925d8
permissions -rw-r--r--
Fixed bug 1434 - Add AltiVec detection for OpenBSD/powerpc

Brad Smith 2012-02-29 19:31:46 PST

The attached patch adds AltiVec detection for OpenBSD/powerpc. Please apply the
patch to both 2.0 and 1.2.
     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 #endif
   227 	return features;
   228 }
   229 
   230 static __inline__ int CPU_getCPUIDFeaturesExt(void)
   231 {
   232 	int features = 0;
   233 #if defined(__GNUC__) && defined(i386)
   234 	__asm__ (
   235 "        movl    $0x80000000,%%eax   # Query for extended functions    \n"
   236 "        pushl   %%ebx                                                 \n"
   237 "        cpuid                       # Get extended function limit     \n"
   238 "        popl    %%ebx                                                 \n"
   239 "        cmpl    $0x80000001,%%eax                                     \n"
   240 "        jl      1f                  # Nope, we dont have function 800000001h\n"
   241 "        movl    $0x80000001,%%eax   # Setup extended function 800000001h\n"
   242 "        pushl   %%ebx                                                 \n"
   243 "        cpuid                       # and get the information         \n"
   244 "        popl    %%ebx                                                 \n"
   245 "        movl    %%edx,%0                                              \n"
   246 "1:                                                                    \n"
   247 	: "=m" (features)
   248 	:
   249 	: "%eax", "%ecx", "%edx"
   250 	);
   251 #elif defined(__GNUC__) && defined (__x86_64__)
   252 	__asm__ (
   253 "        movl    $0x80000000,%%eax   # Query for extended functions    \n"
   254 "        pushq   %%rbx                                                 \n"
   255 "        cpuid                       # Get extended function limit     \n"
   256 "        popq    %%rbx                                                 \n"
   257 "        cmpl    $0x80000001,%%eax                                     \n"
   258 "        jl      1f                  # Nope, we dont have function 800000001h\n"
   259 "        movl    $0x80000001,%%eax   # Setup extended function 800000001h\n"
   260 "        pushq   %%rbx                                                 \n"
   261 "        cpuid                       # and get the information         \n"
   262 "        popq    %%rbx                                                 \n"
   263 "        movl    %%edx,%0                                              \n"
   264 "1:                                                                    \n"
   265 	: "=m" (features)
   266 	:
   267 	: "%rax", "%rcx", "%rdx"
   268 	);
   269 #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
   270 	__asm {
   271         mov     eax,80000000h       ; Query for extended functions
   272         push    ebx
   273         cpuid                       ; Get extended function limit
   274         pop     ebx
   275         cmp     eax,80000001h
   276         jl      done                ; Nope, we dont have function 800000001h
   277         mov     eax,80000001h       ; Setup extended function 800000001h
   278         push    ebx
   279         cpuid                       ; and get the information
   280         pop     ebx
   281         mov     features,edx
   282 done:
   283 	}
   284 #elif defined(__sun) && ( defined(__i386) || defined(__amd64) )
   285 	    __asm (
   286 "        movl    $0x80000000,%eax \n"
   287 "        pushl   %ebx             \n"
   288 "        cpuid                    \n"
   289 "        popl    %ebx             \n"
   290 "        cmpl    $0x80000001,%eax \n"
   291 "        jl      1f               \n"
   292 "        movl    $0x80000001,%eax \n"
   293 "        pushl   %ebx             \n"
   294 "        cpuid                    \n"
   295 "        popl    %ebx             \n"
   296 #ifdef __i386
   297 "        movl    %edx,-8(%ebp)   \n"
   298 #else
   299 "        movl    %edx,-8(%rbp)   \n"
   300 #endif
   301 "1:                               \n"
   302 	    );
   303 #endif
   304 	return features;
   305 }
   306 
   307 static __inline__ int CPU_haveRDTSC(void)
   308 {
   309 	if ( CPU_haveCPUID() ) {
   310 		return (CPU_getCPUIDFeatures() & 0x00000010);
   311 	}
   312 	return 0;
   313 }
   314 
   315 static __inline__ int CPU_haveMMX(void)
   316 {
   317 	if ( CPU_haveCPUID() ) {
   318 		return (CPU_getCPUIDFeatures() & 0x00800000);
   319 	}
   320 	return 0;
   321 }
   322 
   323 static __inline__ int CPU_haveMMXExt(void)
   324 {
   325 	if ( CPU_haveCPUID() ) {
   326 		return (CPU_getCPUIDFeaturesExt() & 0x00400000);
   327 	}
   328 	return 0;
   329 }
   330 
   331 static __inline__ int CPU_have3DNow(void)
   332 {
   333 	if ( CPU_haveCPUID() ) {
   334 		return (CPU_getCPUIDFeaturesExt() & 0x80000000);
   335 	}
   336 	return 0;
   337 }
   338 
   339 static __inline__ int CPU_have3DNowExt(void)
   340 {
   341 	if ( CPU_haveCPUID() ) {
   342 		return (CPU_getCPUIDFeaturesExt() & 0x40000000);
   343 	}
   344 	return 0;
   345 }
   346 
   347 static __inline__ int CPU_haveSSE(void)
   348 {
   349 	if ( CPU_haveCPUID() ) {
   350 		return (CPU_getCPUIDFeatures() & 0x02000000);
   351 	}
   352 	return 0;
   353 }
   354 
   355 static __inline__ int CPU_haveSSE2(void)
   356 {
   357 	if ( CPU_haveCPUID() ) {
   358 		return (CPU_getCPUIDFeatures() & 0x04000000);
   359 	}
   360 	return 0;
   361 }
   362 
   363 static __inline__ int CPU_haveAltiVec(void)
   364 {
   365 	volatile int altivec = 0;
   366 #if (defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))) || (defined(__OpenBSD__) && defined(__powerpc__))
   367 # ifdef __OpenBSD__
   368 	int selectors[2] = { CTL_MACHDEP, CPU_ALTIVEC };
   369 # else
   370 	int selectors[2] = { CTL_HW, HW_VECTORUNIT };
   371 # endif
   372 	int hasVectorUnit = 0; 
   373 	size_t length = sizeof(hasVectorUnit); 
   374 	int error = sysctl(selectors, 2, &hasVectorUnit, &length, NULL, 0); 
   375 	if( 0 == error )
   376 		altivec = (hasVectorUnit != 0); 
   377 #elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
   378 	void (*handler)(int sig);
   379 	handler = signal(SIGILL, illegal_instruction);
   380 	if ( setjmp(jmpbuf) == 0 ) {
   381 		asm volatile ("mtspr 256, %0\n\t"
   382 			      "vand %%v0, %%v0, %%v0"
   383 			      :
   384 			      : "r" (-1));
   385 		altivec = 1;
   386 	}
   387 	signal(SIGILL, handler);
   388 #endif
   389 	return altivec; 
   390 }
   391 
   392 static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
   393 
   394 static Uint32 SDL_GetCPUFeatures(void)
   395 {
   396 	if ( SDL_CPUFeatures == 0xFFFFFFFF ) {
   397 		SDL_CPUFeatures = 0;
   398 		if ( CPU_haveRDTSC() ) {
   399 			SDL_CPUFeatures |= CPU_HAS_RDTSC;
   400 		}
   401 		if ( CPU_haveMMX() ) {
   402 			SDL_CPUFeatures |= CPU_HAS_MMX;
   403 		}
   404 		if ( CPU_haveMMXExt() ) {
   405 			SDL_CPUFeatures |= CPU_HAS_MMXEXT;
   406 		}
   407 		if ( CPU_have3DNow() ) {
   408 			SDL_CPUFeatures |= CPU_HAS_3DNOW;
   409 		}
   410 		if ( CPU_have3DNowExt() ) {
   411 			SDL_CPUFeatures |= CPU_HAS_3DNOWEXT;
   412 		}
   413 		if ( CPU_haveSSE() ) {
   414 			SDL_CPUFeatures |= CPU_HAS_SSE;
   415 		}
   416 		if ( CPU_haveSSE2() ) {
   417 			SDL_CPUFeatures |= CPU_HAS_SSE2;
   418 		}
   419 		if ( CPU_haveAltiVec() ) {
   420 			SDL_CPUFeatures |= CPU_HAS_ALTIVEC;
   421 		}
   422 	}
   423 	return SDL_CPUFeatures;
   424 }
   425 
   426 SDL_bool SDL_HasRDTSC(void)
   427 {
   428 	if ( SDL_GetCPUFeatures() & CPU_HAS_RDTSC ) {
   429 		return SDL_TRUE;
   430 	}
   431 	return SDL_FALSE;
   432 }
   433 
   434 SDL_bool SDL_HasMMX(void)
   435 {
   436 	if ( SDL_GetCPUFeatures() & CPU_HAS_MMX ) {
   437 		return SDL_TRUE;
   438 	}
   439 	return SDL_FALSE;
   440 }
   441 
   442 SDL_bool SDL_HasMMXExt(void)
   443 {
   444 	if ( SDL_GetCPUFeatures() & CPU_HAS_MMXEXT ) {
   445 		return SDL_TRUE;
   446 	}
   447 	return SDL_FALSE;
   448 }
   449 
   450 SDL_bool SDL_Has3DNow(void)
   451 {
   452 	if ( SDL_GetCPUFeatures() & CPU_HAS_3DNOW ) {
   453 		return SDL_TRUE;
   454 	}
   455 	return SDL_FALSE;
   456 }
   457 
   458 SDL_bool SDL_Has3DNowExt(void)
   459 {
   460 	if ( SDL_GetCPUFeatures() & CPU_HAS_3DNOWEXT ) {
   461 		return SDL_TRUE;
   462 	}
   463 	return SDL_FALSE;
   464 }
   465 
   466 SDL_bool SDL_HasSSE(void)
   467 {
   468 	if ( SDL_GetCPUFeatures() & CPU_HAS_SSE ) {
   469 		return SDL_TRUE;
   470 	}
   471 	return SDL_FALSE;
   472 }
   473 
   474 SDL_bool SDL_HasSSE2(void)
   475 {
   476 	if ( SDL_GetCPUFeatures() & CPU_HAS_SSE2 ) {
   477 		return SDL_TRUE;
   478 	}
   479 	return SDL_FALSE;
   480 }
   481 
   482 SDL_bool SDL_HasAltiVec(void)
   483 {
   484 	if ( SDL_GetCPUFeatures() & CPU_HAS_ALTIVEC ) {
   485 		return SDL_TRUE;
   486 	}
   487 	return SDL_FALSE;
   488 }
   489 
   490 #ifdef TEST_MAIN
   491 
   492 #include <stdio.h>
   493 
   494 int main()
   495 {
   496 	printf("RDTSC: %d\n", SDL_HasRDTSC());
   497 	printf("MMX: %d\n", SDL_HasMMX());
   498 	printf("MMXExt: %d\n", SDL_HasMMXExt());
   499 	printf("3DNow: %d\n", SDL_Has3DNow());
   500 	printf("3DNowExt: %d\n", SDL_Has3DNowExt());
   501 	printf("SSE: %d\n", SDL_HasSSE());
   502 	printf("SSE2: %d\n", SDL_HasSSE2());
   503 	printf("AltiVec: %d\n", SDL_HasAltiVec());
   504 	return 0;
   505 }
   506 
   507 #endif /* TEST_MAIN */