src/cpuinfo/SDL_cpuinfo.c
changeset 3580 951dd6a5d1a2
parent 3579 3427271a2d75
child 3584 41d01d70659c
equal deleted inserted replaced
3579:3427271a2d75 3580:951dd6a5d1a2
    32 #if defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))
    32 #if defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))
    33 #include <sys/sysctl.h>         /* For AltiVec check */
    33 #include <sys/sysctl.h>         /* For AltiVec check */
    34 #elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
    34 #elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
    35 #include <signal.h>
    35 #include <signal.h>
    36 #include <setjmp.h>
    36 #include <setjmp.h>
       
    37 #endif
       
    38 #ifdef __WIN32__
       
    39 #define WIN32_LEAN_AND_MEAN
       
    40 #include <windows.h>
    37 #endif
    41 #endif
    38 
    42 
    39 #define CPU_HAS_RDTSC   0x00000001
    43 #define CPU_HAS_RDTSC   0x00000001
    40 #define CPU_HAS_MMX     0x00000002
    44 #define CPU_HAS_MMX     0x00000002
    41 #define CPU_HAS_MMXEXT  0x00000004
    45 #define CPU_HAS_MMXEXT  0x00000004
   149 /* *INDENT-ON* */
   153 /* *INDENT-ON* */
   150     return has_CPUID;
   154     return has_CPUID;
   151 }
   155 }
   152 
   156 
   153 #if defined(__GNUC__) && (defined(i386) || defined(__x86_64__))
   157 #if defined(__GNUC__) && (defined(i386) || defined(__x86_64__))
   154 #define cpuid(func, ax, bx, cx, dx) \
   158 #define cpuid(func, a, b, c, d) \
   155     __asm__ __volatile__ ("cpuid": \
   159     __asm__ __volatile__ ("cpuid": \
   156     "=a" (ax), "=b" (bx), "=c" (cx), "=d" (dx) : "a" (func))
   160     "=a" (a), "=b" (b), "=c" (c), "=d" (d) : "a" (func))
   157 #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
   161 #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
   158 #define cpuid(func, ax, bx, cx, dx) \
   162 #define cpuid(func, a, b, c, d) \
   159     asm { \
   163     __asm { \
   160         mov eax, func \
   164         __asm mov eax, func \
   161         cpuid
   165         __asm cpuid \
   162         mov ax, eax \
   166         __asm mov a, eax \
   163         mov bx, ebx \
   167         __asm mov b, ebx \
   164         mov cx, ecx \
   168         __asm mov c, ecx \
   165         mov dx, edx \
   169         __asm mov d, edx \
   166     }
   170     }
   167 #else
   171 #else
   168 #define cpuid(func, ax, bx, cx, dx) \
   172 #define cpuid(func, a, b, c, d) \
   169     ax = bx = cx = dx = 0
   173     a = b = c = d = 0
   170 #endif
   174 #endif
   171 
   175 
   172 static __inline__ int
   176 static __inline__ int
   173 CPU_getCPUIDFeatures(void)
   177 CPU_getCPUIDFeatures(void)
   174 {
   178 {
   175     int features = 0;
   179     int features = 0;
   176     int ax, bx, cx, dx;
   180     int a, b, c, d;
   177 
   181 
   178     cpuid(0, ax, bx, cx, dx);
   182     cpuid(0, a, b, c, d);
   179     if (ax >= 1) {
   183     if (a >= 1) {
   180         cpuid(1, ax, bx, cx, dx);
   184         cpuid(1, a, b, c, d);
   181         features = dx;
   185         features = d;
   182     }
   186     }
   183     return features;
   187     return features;
   184 }
   188 }
   185 
   189 
   186 static __inline__ int
   190 static __inline__ int
   187 CPU_getCPUIDFeaturesExt(void)
   191 CPU_getCPUIDFeaturesExt(void)
   188 {
   192 {
   189     int features = 0;
   193     int features = 0;
   190     int ax, bx, cx, dx;
   194     int a, b, c, d;
   191 
   195 
   192     cpuid(0x80000000, ax, bx, cx, dx);
   196     cpuid(0x80000000, a, b, c, d);
   193     if (ax >= 0x80000001) {
   197     if (a >= 0x80000001) {
   194         cpuid(0x80000001, ax, bx, cx, dx);
   198         cpuid(0x80000001, a, b, c, d);
   195         features = dx;
   199         features = d;
   196     }
   200     }
   197     return features;
   201     return features;
   198 }
   202 }
   199 
   203 
   200 static __inline__ int
   204 static __inline__ int
   288 int
   292 int
   289 SDL_GetCPUCount()
   293 SDL_GetCPUCount()
   290 {
   294 {
   291     if (!SDL_CPUCount) {
   295     if (!SDL_CPUCount) {
   292 #ifdef HAVE_SYSCTLBYNAME
   296 #ifdef HAVE_SYSCTLBYNAME
   293         size_t size = sizeof(SDL_CPUCount);
   297         {
   294         sysctlbyname("hw.ncpu", &SDL_CPUCount, &size, NULL, 0);
   298             size_t size = sizeof(SDL_CPUCount);
       
   299             sysctlbyname("hw.ncpu", &SDL_CPUCount, &size, NULL, 0);
       
   300         }
       
   301 #endif
       
   302 #ifdef __WIN32__
       
   303         {
       
   304             SYSTEM_INFO info;
       
   305             GetSystemInfo(&info);
       
   306             SDL_CPUCount = info.dwNumberOfProcessors;
       
   307         }
   295 #endif
   308 #endif
   296         /* There has to be at least 1, right? :) */
   309         /* There has to be at least 1, right? :) */
   297         if (!SDL_CPUCount) {
   310         if (!SDL_CPUCount) {
   298             SDL_CPUCount = 1;
   311             SDL_CPUCount = 1;
   299         }
   312         }
   307 {
   320 {
   308     static char SDL_CPUType[48];
   321     static char SDL_CPUType[48];
   309 
   322 
   310     if (!SDL_CPUType[0]) {
   323     if (!SDL_CPUType[0]) {
   311         int i = 0;
   324         int i = 0;
   312         int ax, bx, cx, dx;
   325         int a, b, c, d;
   313 
   326 
   314         if (CPU_haveCPUID()) {
   327         if (CPU_haveCPUID()) {
   315             cpuid(0x80000000, ax, bx, cx, dx);
   328             cpuid(0x80000000, a, b, c, d);
   316             if (ax >= 0x80000004) {
   329             if (a >= 0x80000004) {
   317                 cpuid(0x80000002, ax, bx, cx, dx);
   330                 cpuid(0x80000002, a, b, c, d);
   318                 SDL_CPUType[i++] = (char)(ax & 0xff); ax >>= 8;
   331                 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
   319                 SDL_CPUType[i++] = (char)(ax & 0xff); ax >>= 8;
   332                 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
   320                 SDL_CPUType[i++] = (char)(ax & 0xff); ax >>= 8;
   333                 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
   321                 SDL_CPUType[i++] = (char)(ax & 0xff); ax >>= 8;
   334                 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
   322                 SDL_CPUType[i++] = (char)(bx & 0xff); bx >>= 8;
   335                 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
   323                 SDL_CPUType[i++] = (char)(bx & 0xff); bx >>= 8;
   336                 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
   324                 SDL_CPUType[i++] = (char)(bx & 0xff); bx >>= 8;
   337                 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
   325                 SDL_CPUType[i++] = (char)(bx & 0xff); bx >>= 8;
   338                 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
   326                 SDL_CPUType[i++] = (char)(cx & 0xff); cx >>= 8;
   339                 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
   327                 SDL_CPUType[i++] = (char)(cx & 0xff); cx >>= 8;
   340                 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
   328                 SDL_CPUType[i++] = (char)(cx & 0xff); cx >>= 8;
   341                 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
   329                 SDL_CPUType[i++] = (char)(cx & 0xff); cx >>= 8;
   342                 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
   330                 SDL_CPUType[i++] = (char)(dx & 0xff); dx >>= 8;
   343                 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
   331                 SDL_CPUType[i++] = (char)(dx & 0xff); dx >>= 8;
   344                 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
   332                 SDL_CPUType[i++] = (char)(dx & 0xff); dx >>= 8;
   345                 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
   333                 SDL_CPUType[i++] = (char)(dx & 0xff); dx >>= 8;
   346                 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
   334                 cpuid(0x80000003, ax, bx, cx, dx);
   347                 cpuid(0x80000003, a, b, c, d);
   335                 SDL_CPUType[i++] = (char)(ax & 0xff); ax >>= 8;
   348                 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
   336                 SDL_CPUType[i++] = (char)(ax & 0xff); ax >>= 8;
   349                 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
   337                 SDL_CPUType[i++] = (char)(ax & 0xff); ax >>= 8;
   350                 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
   338                 SDL_CPUType[i++] = (char)(ax & 0xff); ax >>= 8;
   351                 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
   339                 SDL_CPUType[i++] = (char)(bx & 0xff); bx >>= 8;
   352                 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
   340                 SDL_CPUType[i++] = (char)(bx & 0xff); bx >>= 8;
   353                 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
   341                 SDL_CPUType[i++] = (char)(bx & 0xff); bx >>= 8;
   354                 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
   342                 SDL_CPUType[i++] = (char)(bx & 0xff); bx >>= 8;
   355                 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
   343                 SDL_CPUType[i++] = (char)(cx & 0xff); cx >>= 8;
   356                 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
   344                 SDL_CPUType[i++] = (char)(cx & 0xff); cx >>= 8;
   357                 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
   345                 SDL_CPUType[i++] = (char)(cx & 0xff); cx >>= 8;
   358                 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
   346                 SDL_CPUType[i++] = (char)(cx & 0xff); cx >>= 8;
   359                 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
   347                 SDL_CPUType[i++] = (char)(dx & 0xff); dx >>= 8;
   360                 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
   348                 SDL_CPUType[i++] = (char)(dx & 0xff); dx >>= 8;
   361                 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
   349                 SDL_CPUType[i++] = (char)(dx & 0xff); dx >>= 8;
   362                 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
   350                 SDL_CPUType[i++] = (char)(dx & 0xff); dx >>= 8;
   363                 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
   351                 cpuid(0x80000004, ax, bx, cx, dx);
   364                 cpuid(0x80000004, a, b, c, d);
   352                 SDL_CPUType[i++] = (char)(ax & 0xff); ax >>= 8;
   365                 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
   353                 SDL_CPUType[i++] = (char)(ax & 0xff); ax >>= 8;
   366                 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
   354                 SDL_CPUType[i++] = (char)(ax & 0xff); ax >>= 8;
   367                 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
   355                 SDL_CPUType[i++] = (char)(ax & 0xff); ax >>= 8;
   368                 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
   356                 SDL_CPUType[i++] = (char)(bx & 0xff); bx >>= 8;
   369                 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
   357                 SDL_CPUType[i++] = (char)(bx & 0xff); bx >>= 8;
   370                 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
   358                 SDL_CPUType[i++] = (char)(bx & 0xff); bx >>= 8;
   371                 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
   359                 SDL_CPUType[i++] = (char)(bx & 0xff); bx >>= 8;
   372                 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
   360                 SDL_CPUType[i++] = (char)(cx & 0xff); cx >>= 8;
   373                 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
   361                 SDL_CPUType[i++] = (char)(cx & 0xff); cx >>= 8;
   374                 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
   362                 SDL_CPUType[i++] = (char)(cx & 0xff); cx >>= 8;
   375                 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
   363                 SDL_CPUType[i++] = (char)(cx & 0xff); cx >>= 8;
   376                 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
   364                 SDL_CPUType[i++] = (char)(dx & 0xff); dx >>= 8;
   377                 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
   365                 SDL_CPUType[i++] = (char)(dx & 0xff); dx >>= 8;
   378                 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
   366                 SDL_CPUType[i++] = (char)(dx & 0xff); dx >>= 8;
   379                 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
   367                 SDL_CPUType[i++] = (char)(dx & 0xff); dx >>= 8;
   380                 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
   368             }
   381             }
   369         }
   382         }
   370         if (!SDL_CPUType[0]) {
   383         if (!SDL_CPUType[0]) {
   371             SDL_strlcpy(SDL_CPUType, "Unknown", sizeof(SDL_CPUType));
   384             SDL_strlcpy(SDL_CPUType, "Unknown", sizeof(SDL_CPUType));
   372         }
   385         }