src/cpuinfo/SDL_cpuinfo.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 24 Jan 2011 15:46:11 -0800
changeset 5086 c2539ff054c8
parent 5062 e8916fe9cfc8
child 5090 327f181542f1
permissions -rw-r--r--
Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2010 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_cpuinfo.h"
    27 
    28 #ifdef HAVE_SYSCONF
    29 #include <unistd.h>
    30 #endif
    31 #ifdef HAVE_SYSCTLBYNAME
    32 #include <sys/types.h>
    33 #include <sys/sysctl.h>
    34 #endif
    35 #if defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))
    36 #include <sys/sysctl.h>         /* For AltiVec check */
    37 #elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
    38 #include <signal.h>
    39 #include <setjmp.h>
    40 #endif
    41 #ifdef __WIN32__
    42 #define WIN32_LEAN_AND_MEAN
    43 #include <windows.h>
    44 #endif
    45 
    46 #define CPU_HAS_RDTSC   0x00000001
    47 #define CPU_HAS_MMX     0x00000002
    48 #define CPU_HAS_MMXEXT  0x00000004
    49 #define CPU_HAS_3DNOW   0x00000010
    50 #define CPU_HAS_3DNOWEXT 0x00000020
    51 #define CPU_HAS_SSE     0x00000040
    52 #define CPU_HAS_SSE2    0x00000080
    53 #define CPU_HAS_ALTIVEC 0x00000100
    54 
    55 #if SDL_ALTIVEC_BLITTERS && HAVE_SETJMP && !__MACOSX__
    56 /* This is the brute force way of detecting instruction sets...
    57    the idea is borrowed from the libmpeg2 library - thanks!
    58  */
    59 static jmp_buf jmpbuf;
    60 static void
    61 illegal_instruction(int sig)
    62 {
    63     longjmp(jmpbuf, 1);
    64 }
    65 #endif /* HAVE_SETJMP */
    66 
    67 static __inline__ int
    68 CPU_haveCPUID(void)
    69 {
    70     int has_CPUID = 0;
    71 /* *INDENT-OFF* */
    72 #if defined(__GNUC__) && defined(i386)
    73     __asm__ (
    74 "        pushfl                      # Get original EFLAGS             \n"
    75 "        popl    %%eax                                                 \n"
    76 "        movl    %%eax,%%ecx                                           \n"
    77 "        xorl    $0x200000,%%eax     # Flip ID bit in EFLAGS           \n"
    78 "        pushl   %%eax               # Save new EFLAGS value on stack  \n"
    79 "        popfl                       # Replace current EFLAGS value    \n"
    80 "        pushfl                      # Get new EFLAGS                  \n"
    81 "        popl    %%eax               # Store new EFLAGS in EAX         \n"
    82 "        xorl    %%ecx,%%eax         # Can not toggle ID bit,          \n"
    83 "        jz      1f                  # Processor=80486                 \n"
    84 "        movl    $1,%0               # We have CPUID support           \n"
    85 "1:                                                                    \n"
    86     : "=m" (has_CPUID)
    87     :
    88     : "%eax", "%ecx"
    89     );
    90 #elif defined(__GNUC__) && defined(__x86_64__)
    91 /* Technically, if this is being compiled under __x86_64__ then it has 
    92 CPUid by definition.  But it's nice to be able to prove it.  :)      */
    93     __asm__ (
    94 "        pushfq                      # Get original EFLAGS             \n"
    95 "        popq    %%rax                                                 \n"
    96 "        movq    %%rax,%%rcx                                           \n"
    97 "        xorl    $0x200000,%%eax     # Flip ID bit in EFLAGS           \n"
    98 "        pushq   %%rax               # Save new EFLAGS value on stack  \n"
    99 "        popfq                       # Replace current EFLAGS value    \n"
   100 "        pushfq                      # Get new EFLAGS                  \n"
   101 "        popq    %%rax               # Store new EFLAGS in EAX         \n"
   102 "        xorl    %%ecx,%%eax         # Can not toggle ID bit,          \n"
   103 "        jz      1f                  # Processor=80486                 \n"
   104 "        movl    $1,%0               # We have CPUID support           \n"
   105 "1:                                                                    \n"
   106     : "=m" (has_CPUID)
   107     :
   108     : "%rax", "%rcx"
   109     );
   110 #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
   111     __asm {
   112         pushfd                      ; Get original EFLAGS
   113         pop     eax
   114         mov     ecx, eax
   115         xor     eax, 200000h        ; Flip ID bit in EFLAGS
   116         push    eax                 ; Save new EFLAGS value on stack
   117         popfd                       ; Replace current EFLAGS value
   118         pushfd                      ; Get new EFLAGS
   119         pop     eax                 ; Store new EFLAGS in EAX
   120         xor     eax, ecx            ; Can not toggle ID bit,
   121         jz      done                ; Processor=80486
   122         mov     has_CPUID,1         ; We have CPUID support
   123 done:
   124     }
   125 #elif defined(__sun) && defined(__i386)
   126     __asm (
   127 "       pushfl                 \n"
   128 "       popl    %eax           \n"
   129 "       movl    %eax,%ecx      \n"
   130 "       xorl    $0x200000,%eax \n"
   131 "       pushl   %eax           \n"
   132 "       popfl                  \n"
   133 "       pushfl                 \n"
   134 "       popl    %eax           \n"
   135 "       xorl    %ecx,%eax      \n"
   136 "       jz      1f             \n"
   137 "       movl    $1,-8(%ebp)    \n"
   138 "1:                            \n"
   139     );
   140 #elif defined(__sun) && defined(__amd64)
   141     __asm (
   142 "       pushfq                 \n"
   143 "       popq    %rax           \n"
   144 "       movq    %rax,%rcx      \n"
   145 "       xorl    $0x200000,%eax \n"
   146 "       pushq   %rax           \n"
   147 "       popfq                  \n"
   148 "       pushfq                 \n"
   149 "       popq    %rax           \n"
   150 "       xorl    %ecx,%eax      \n"
   151 "       jz      1f             \n"
   152 "       movl    $1,-8(%rbp)    \n"
   153 "1:                            \n"
   154     );
   155 #endif
   156 /* *INDENT-ON* */
   157     return has_CPUID;
   158 }
   159 
   160 #if defined(__GNUC__) && defined(i386)
   161 #define cpuid(func, a, b, c, d) \
   162     __asm__ __volatile__ ( \
   163 "        pushl %%ebx        \n" \
   164 "        cpuid              \n" \
   165 "        movl %%ebx, %%esi  \n" \
   166 "        popl %%ebx         \n" : \
   167             "=a" (a), "=S" (b), "=c" (c), "=d" (d) : "a" (func))
   168 #elif defined(__GNUC__) && defined(__x86_64__)
   169 #define cpuid(func, a, b, c, d) \
   170     __asm__ __volatile__ ( \
   171 "        pushq %%rbx        \n" \
   172 "        cpuid              \n" \
   173 "        movq %%rbx, %%rsi  \n" \
   174 "        popq %%rbx         \n" : \
   175             "=a" (a), "=S" (b), "=c" (c), "=d" (d) : "a" (func))
   176 #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
   177 #define cpuid(func, a, b, c, d) \
   178     __asm { \
   179         __asm mov eax, func \
   180         __asm cpuid \
   181         __asm mov a, eax \
   182         __asm mov b, ebx \
   183         __asm mov c, ecx \
   184         __asm mov d, edx \
   185     }
   186 #else
   187 #define cpuid(func, a, b, c, d) \
   188     a = b = c = d = 0
   189 #endif
   190 
   191 static __inline__ int
   192 CPU_getCPUIDFeatures(void)
   193 {
   194     int features = 0;
   195     int a, b, c, d;
   196 
   197     cpuid(0, a, b, c, d);
   198     if (a >= 1) {
   199         cpuid(1, a, b, c, d);
   200         features = d;
   201     }
   202     return features;
   203 }
   204 
   205 static __inline__ int
   206 CPU_getCPUIDFeaturesExt(void)
   207 {
   208     int features = 0;
   209     int a, b, c, d;
   210 
   211     cpuid(0x80000000, a, b, c, d);
   212     if (a >= 0x80000001) {
   213         cpuid(0x80000001, a, b, c, d);
   214         features = d;
   215     }
   216     return features;
   217 }
   218 
   219 static __inline__ int
   220 CPU_haveRDTSC(void)
   221 {
   222     if (CPU_haveCPUID()) {
   223         return (CPU_getCPUIDFeatures() & 0x00000010);
   224     }
   225     return 0;
   226 }
   227 
   228 static __inline__ int
   229 CPU_haveMMX(void)
   230 {
   231     if (CPU_haveCPUID()) {
   232         return (CPU_getCPUIDFeatures() & 0x00800000);
   233     }
   234     return 0;
   235 }
   236 
   237 static __inline__ int
   238 CPU_haveMMXExt(void)
   239 {
   240     if (CPU_haveCPUID()) {
   241         return (CPU_getCPUIDFeaturesExt() & 0x00400000);
   242     }
   243     return 0;
   244 }
   245 
   246 static __inline__ int
   247 CPU_have3DNow(void)
   248 {
   249     if (CPU_haveCPUID()) {
   250         return (CPU_getCPUIDFeaturesExt() & 0x80000000);
   251     }
   252     return 0;
   253 }
   254 
   255 static __inline__ int
   256 CPU_have3DNowExt(void)
   257 {
   258     if (CPU_haveCPUID()) {
   259         return (CPU_getCPUIDFeaturesExt() & 0x40000000);
   260     }
   261     return 0;
   262 }
   263 
   264 static __inline__ int
   265 CPU_haveSSE(void)
   266 {
   267     if (CPU_haveCPUID()) {
   268         return (CPU_getCPUIDFeatures() & 0x02000000);
   269     }
   270     return 0;
   271 }
   272 
   273 static __inline__ int
   274 CPU_haveSSE2(void)
   275 {
   276     if (CPU_haveCPUID()) {
   277         return (CPU_getCPUIDFeatures() & 0x04000000);
   278     }
   279     return 0;
   280 }
   281 
   282 static __inline__ int
   283 CPU_haveAltiVec(void)
   284 {
   285     volatile int altivec = 0;
   286 #if defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))
   287     int selectors[2] = { CTL_HW, HW_VECTORUNIT };
   288     int hasVectorUnit = 0;
   289     size_t length = sizeof(hasVectorUnit);
   290     int error = sysctl(selectors, 2, &hasVectorUnit, &length, NULL, 0);
   291     if (0 == error)
   292         altivec = (hasVectorUnit != 0);
   293 #elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
   294     void (*handler) (int sig);
   295     handler = signal(SIGILL, illegal_instruction);
   296     if (setjmp(jmpbuf) == 0) {
   297         asm volatile ("mtspr 256, %0\n\t" "vand %%v0, %%v0, %%v0"::"r" (-1));
   298         altivec = 1;
   299     }
   300     signal(SIGILL, handler);
   301 #endif
   302     return altivec;
   303 }
   304 
   305 static int SDL_CPUCount = 0;
   306 
   307 int
   308 SDL_GetCPUCount()
   309 {
   310     if (!SDL_CPUCount) {
   311 #if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
   312         if (SDL_CPUCount <= 0) {
   313             SDL_CPUCount = (int)sysconf(_SC_NPROCESSORS_ONLN);
   314         }
   315 #endif
   316 #ifdef HAVE_SYSCTLBYNAME
   317         if (SDL_CPUCount <= 0) {
   318             size_t size = sizeof(SDL_CPUCount);
   319             sysctlbyname("hw.ncpu", &SDL_CPUCount, &size, NULL, 0);
   320         }
   321 #endif
   322 #ifdef __WIN32__
   323         if (SDL_CPUCount <= 0) {
   324             SYSTEM_INFO info;
   325             GetSystemInfo(&info);
   326             SDL_CPUCount = info.dwNumberOfProcessors;
   327         }
   328 #endif
   329         /* There has to be at least 1, right? :) */
   330         if (SDL_CPUCount <= 0) {
   331             SDL_CPUCount = 1;
   332         }
   333     }
   334     return SDL_CPUCount;
   335 }
   336 
   337 /* Oh, such a sweet sweet trick, just not very useful. :) */
   338 static const char *
   339 SDL_GetCPUType()
   340 {
   341     static char SDL_CPUType[48];
   342 
   343     if (!SDL_CPUType[0]) {
   344         int i = 0;
   345         int a, b, c, d;
   346 
   347         if (CPU_haveCPUID()) {
   348             cpuid(0x80000000, a, b, c, d);
   349             if (a >= 0x80000004) {
   350                 cpuid(0x80000002, a, b, c, d);
   351                 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
   352                 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
   353                 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
   354                 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
   355                 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
   356                 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
   357                 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
   358                 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
   359                 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
   360                 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
   361                 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
   362                 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
   363                 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
   364                 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
   365                 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
   366                 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
   367                 cpuid(0x80000003, a, b, c, d);
   368                 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
   369                 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
   370                 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
   371                 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
   372                 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
   373                 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
   374                 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
   375                 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
   376                 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
   377                 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
   378                 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
   379                 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
   380                 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
   381                 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
   382                 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
   383                 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
   384                 cpuid(0x80000004, a, b, c, d);
   385                 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
   386                 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
   387                 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
   388                 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
   389                 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
   390                 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
   391                 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
   392                 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
   393                 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
   394                 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
   395                 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
   396                 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
   397                 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
   398                 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
   399                 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
   400                 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
   401             }
   402         }
   403         if (!SDL_CPUType[0]) {
   404             SDL_strlcpy(SDL_CPUType, "Unknown", sizeof(SDL_CPUType));
   405         }
   406     }
   407     return SDL_CPUType;
   408 }
   409 
   410 static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
   411 
   412 static Uint32
   413 SDL_GetCPUFeatures(void)
   414 {
   415     if (SDL_CPUFeatures == 0xFFFFFFFF) {
   416         SDL_CPUFeatures = 0;
   417         if (CPU_haveRDTSC()) {
   418             SDL_CPUFeatures |= CPU_HAS_RDTSC;
   419         }
   420         if (CPU_haveMMX()) {
   421             SDL_CPUFeatures |= CPU_HAS_MMX;
   422         }
   423         if (CPU_haveMMXExt()) {
   424             SDL_CPUFeatures |= CPU_HAS_MMXEXT;
   425         }
   426         if (CPU_have3DNow()) {
   427             SDL_CPUFeatures |= CPU_HAS_3DNOW;
   428         }
   429         if (CPU_have3DNowExt()) {
   430             SDL_CPUFeatures |= CPU_HAS_3DNOWEXT;
   431         }
   432         if (CPU_haveSSE()) {
   433             SDL_CPUFeatures |= CPU_HAS_SSE;
   434         }
   435         if (CPU_haveSSE2()) {
   436             SDL_CPUFeatures |= CPU_HAS_SSE2;
   437         }
   438         if (CPU_haveAltiVec()) {
   439             SDL_CPUFeatures |= CPU_HAS_ALTIVEC;
   440         }
   441     }
   442     return SDL_CPUFeatures;
   443 }
   444 
   445 SDL_bool
   446 SDL_HasRDTSC(void)
   447 {
   448     if (SDL_GetCPUFeatures() & CPU_HAS_RDTSC) {
   449         return SDL_TRUE;
   450     }
   451     return SDL_FALSE;
   452 }
   453 
   454 SDL_bool
   455 SDL_HasMMX(void)
   456 {
   457     if (SDL_GetCPUFeatures() & CPU_HAS_MMX) {
   458         return SDL_TRUE;
   459     }
   460     return SDL_FALSE;
   461 }
   462 
   463 SDL_bool
   464 SDL_HasMMXExt(void)
   465 {
   466     if (SDL_GetCPUFeatures() & CPU_HAS_MMXEXT) {
   467         return SDL_TRUE;
   468     }
   469     return SDL_FALSE;
   470 }
   471 
   472 SDL_bool
   473 SDL_Has3DNow(void)
   474 {
   475     if (SDL_GetCPUFeatures() & CPU_HAS_3DNOW) {
   476         return SDL_TRUE;
   477     }
   478     return SDL_FALSE;
   479 }
   480 
   481 SDL_bool
   482 SDL_Has3DNowExt(void)
   483 {
   484     if (SDL_GetCPUFeatures() & CPU_HAS_3DNOWEXT) {
   485         return SDL_TRUE;
   486     }
   487     return SDL_FALSE;
   488 }
   489 
   490 SDL_bool
   491 SDL_HasSSE(void)
   492 {
   493     if (SDL_GetCPUFeatures() & CPU_HAS_SSE) {
   494         return SDL_TRUE;
   495     }
   496     return SDL_FALSE;
   497 }
   498 
   499 SDL_bool
   500 SDL_HasSSE2(void)
   501 {
   502     if (SDL_GetCPUFeatures() & CPU_HAS_SSE2) {
   503         return SDL_TRUE;
   504     }
   505     return SDL_FALSE;
   506 }
   507 
   508 SDL_bool
   509 SDL_HasAltiVec(void)
   510 {
   511     if (SDL_GetCPUFeatures() & CPU_HAS_ALTIVEC) {
   512         return SDL_TRUE;
   513     }
   514     return SDL_FALSE;
   515 }
   516 
   517 #ifdef TEST_MAIN
   518 
   519 #include <stdio.h>
   520 
   521 int
   522 main()
   523 {
   524     printf("CPU count: %d\n", SDL_GetCPUCount());
   525     printf("CPU name: %s\n", SDL_GetCPUType());
   526     printf("RDTSC: %d\n", SDL_HasRDTSC());
   527     printf("MMX: %d\n", SDL_HasMMX());
   528     printf("MMXExt: %d\n", SDL_HasMMXExt());
   529     printf("3DNow: %d\n", SDL_Has3DNow());
   530     printf("3DNowExt: %d\n", SDL_Has3DNowExt());
   531     printf("SSE: %d\n", SDL_HasSSE());
   532     printf("SSE2: %d\n", SDL_HasSSE2());
   533     printf("AltiVec: %d\n", SDL_HasAltiVec());
   534     return 0;
   535 }
   536 
   537 #endif /* TEST_MAIN */
   538 
   539 /* vi: set ts=4 sw=4 expandtab: */