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