src/cpuinfo/SDL_cpuinfo.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 24 Jan 2011 21:20:30 -0800
changeset 5090 327f181542f1
parent 5086 c2539ff054c8
child 5115 427998ff3bcf
permissions -rw-r--r--
Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.

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