src/cpuinfo/SDL_cpuinfo.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 22 Feb 2014 19:10:45 -0800
changeset 8241 b6d5a589ce3f
parent 8240 334257cd613d
child 8642 3506de54b829
permissions -rw-r--r--
Fixed bug 2404 - CPU detection not working with MSVC on x64

Tiemo Jung

All CPU detection functions SDL_Has* will return false, even if it is supported by the CPU, if SDL is compiled with MSVC and the target is x64.

The reason for this is that 'CPU_haveCPUID' will return 0 and macro 'cpuid' in SDL_cpuinfo.c is the fallback implementation, which sets all params to zero.

It is safe to assume that cpuid is supported on a CPU that runs windows x64, so CPU_haveCPUID can just return 1, and the empty macro can be replaced with a small wrap around the __cpuid intrinsic.
slouken@739
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
slouken@739
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@739
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@739
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@739
    20
*/
slouken@8148
    21
#ifdef TEST_MAIN
slouken@8148
    22
#include "SDL_config.h"
slouken@8148
    23
#else
icculus@8093
    24
#include "../SDL_internal.h"
slouken@8148
    25
#endif
slouken@739
    26
slouken@7828
    27
#if defined(__WIN32__)
slouken@7828
    28
#include "../core/windows/SDL_windows.h"
slouken@7828
    29
#endif
slouken@7828
    30
slouken@739
    31
/* CPU feature detection for SDL */
slouken@739
    32
slouken@1361
    33
#include "SDL_cpuinfo.h"
slouken@1361
    34
slouken@3586
    35
#ifdef HAVE_SYSCONF
slouken@3586
    36
#include <unistd.h>
slouken@3586
    37
#endif
slouken@3579
    38
#ifdef HAVE_SYSCTLBYNAME
slouken@3579
    39
#include <sys/types.h>
slouken@3579
    40
#include <sys/sysctl.h>
slouken@3579
    41
#endif
slouken@5389
    42
#if defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))
slouken@5389
    43
#include <sys/sysctl.h>         /* For AltiVec check */
slouken@6363
    44
#elif defined(__OpenBSD__) && defined(__powerpc__)
slouken@6363
    45
#include <sys/param.h>
slouken@6363
    46
#include <sys/sysctl.h> /* For AltiVec check */
slouken@6363
    47
#include <machine/cpu.h>
slouken@5389
    48
#elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
slouken@5389
    49
#include <signal.h>
slouken@5389
    50
#include <setjmp.h>
slouken@5389
    51
#endif
slouken@793
    52
slouken@3579
    53
#define CPU_HAS_RDTSC   0x00000001
slouken@5389
    54
#define CPU_HAS_ALTIVEC 0x00000002
slouken@5389
    55
#define CPU_HAS_MMX     0x00000004
slouken@5389
    56
#define CPU_HAS_3DNOW   0x00000008
slouken@5259
    57
#define CPU_HAS_SSE     0x00000010
slouken@5259
    58
#define CPU_HAS_SSE2    0x00000020
slouken@5259
    59
#define CPU_HAS_SSE3    0x00000040
slouken@5389
    60
#define CPU_HAS_SSE41   0x00000100
slouken@5389
    61
#define CPU_HAS_SSE42   0x00000200
slouken@8148
    62
#define CPU_HAS_AVX     0x00000400
slouken@739
    63
slouken@6363
    64
#if SDL_ALTIVEC_BLITTERS && HAVE_SETJMP && !__MACOSX__ && !__OpenBSD__
slouken@5389
    65
/* This is the brute force way of detecting instruction sets...
slouken@5389
    66
   the idea is borrowed from the libmpeg2 library - thanks!
slouken@5389
    67
 */
slouken@5389
    68
static jmp_buf jmpbuf;
slouken@5389
    69
static void
slouken@5389
    70
illegal_instruction(int sig)
slouken@5389
    71
{
slouken@5389
    72
    longjmp(jmpbuf, 1);
slouken@5389
    73
}
slouken@5389
    74
#endif /* HAVE_SETJMP */
slouken@793
    75
slouken@7860
    76
static SDL_INLINE int
slouken@1895
    77
CPU_haveCPUID(void)
slouken@745
    78
{
slouken@1895
    79
    int has_CPUID = 0;
slouken@1895
    80
/* *INDENT-OFF* */
slouken@745
    81
#if defined(__GNUC__) && defined(i386)
slouken@3579
    82
    __asm__ (
slouken@745
    83
"        pushfl                      # Get original EFLAGS             \n"
slouken@745
    84
"        popl    %%eax                                                 \n"
slouken@745
    85
"        movl    %%eax,%%ecx                                           \n"
slouken@745
    86
"        xorl    $0x200000,%%eax     # Flip ID bit in EFLAGS           \n"
slouken@745
    87
"        pushl   %%eax               # Save new EFLAGS value on stack  \n"
slouken@745
    88
"        popfl                       # Replace current EFLAGS value    \n"
slouken@745
    89
"        pushfl                      # Get new EFLAGS                  \n"
slouken@745
    90
"        popl    %%eax               # Store new EFLAGS in EAX         \n"
slouken@745
    91
"        xorl    %%ecx,%%eax         # Can not toggle ID bit,          \n"
slouken@745
    92
"        jz      1f                  # Processor=80486                 \n"
slouken@745
    93
"        movl    $1,%0               # We have CPUID support           \n"
slouken@745
    94
"1:                                                                    \n"
slouken@3579
    95
    : "=m" (has_CPUID)
slouken@3579
    96
    :
slouken@3579
    97
    : "%eax", "%ecx"
slouken@3579
    98
    );
slouken@881
    99
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@881
   100
/* Technically, if this is being compiled under __x86_64__ then it has 
slouken@8241
   101
   CPUid by definition.  But it's nice to be able to prove it.  :)      */
slouken@3579
   102
    __asm__ (
slouken@881
   103
"        pushfq                      # Get original EFLAGS             \n"
slouken@881
   104
"        popq    %%rax                                                 \n"
slouken@881
   105
"        movq    %%rax,%%rcx                                           \n"
slouken@881
   106
"        xorl    $0x200000,%%eax     # Flip ID bit in EFLAGS           \n"
slouken@881
   107
"        pushq   %%rax               # Save new EFLAGS value on stack  \n"
slouken@881
   108
"        popfq                       # Replace current EFLAGS value    \n"
slouken@881
   109
"        pushfq                      # Get new EFLAGS                  \n"
slouken@881
   110
"        popq    %%rax               # Store new EFLAGS in EAX         \n"
slouken@881
   111
"        xorl    %%ecx,%%eax         # Can not toggle ID bit,          \n"
slouken@881
   112
"        jz      1f                  # Processor=80486                 \n"
slouken@881
   113
"        movl    $1,%0               # We have CPUID support           \n"
slouken@881
   114
"1:                                                                    \n"
slouken@3579
   115
    : "=m" (has_CPUID)
slouken@3579
   116
    :
slouken@3579
   117
    : "%rax", "%rcx"
slouken@3579
   118
    );
slouken@1442
   119
#elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
slouken@3579
   120
    __asm {
slouken@745
   121
        pushfd                      ; Get original EFLAGS
slouken@745
   122
        pop     eax
slouken@745
   123
        mov     ecx, eax
slouken@745
   124
        xor     eax, 200000h        ; Flip ID bit in EFLAGS
slouken@745
   125
        push    eax                 ; Save new EFLAGS value on stack
slouken@745
   126
        popfd                       ; Replace current EFLAGS value
slouken@745
   127
        pushfd                      ; Get new EFLAGS
slouken@745
   128
        pop     eax                 ; Store new EFLAGS in EAX
slouken@745
   129
        xor     eax, ecx            ; Can not toggle ID bit,
slouken@745
   130
        jz      done                ; Processor=80486
slouken@745
   131
        mov     has_CPUID,1         ; We have CPUID support
slouken@745
   132
done:
slouken@3579
   133
    }
slouken@8241
   134
#elif defined(_MSC_VER) && defined(_M_X64)
slouken@8241
   135
    has_CPUID = 1;
slouken@1864
   136
#elif defined(__sun) && defined(__i386)
slouken@3579
   137
    __asm (
icculus@1229
   138
"       pushfl                 \n"
slouken@3584
   139
"       popl    %eax           \n"
slouken@3584
   140
"       movl    %eax,%ecx      \n"
slouken@3584
   141
"       xorl    $0x200000,%eax \n"
slouken@3584
   142
"       pushl   %eax           \n"
slouken@3584
   143
"       popfl                  \n"
slouken@3584
   144
"       pushfl                 \n"
slouken@3584
   145
"       popl    %eax           \n"
slouken@3584
   146
"       xorl    %ecx,%eax      \n"
slouken@3584
   147
"       jz      1f             \n"
slouken@3584
   148
"       movl    $1,-8(%ebp)    \n"
icculus@1229
   149
"1:                            \n"
slouken@3579
   150
    );
icculus@1229
   151
#elif defined(__sun) && defined(__amd64)
slouken@3579
   152
    __asm (
icculus@1229
   153
"       pushfq                 \n"
icculus@1229
   154
"       popq    %rax           \n"
icculus@1229
   155
"       movq    %rax,%rcx      \n"
icculus@1229
   156
"       xorl    $0x200000,%eax \n"
icculus@1229
   157
"       pushq   %rax           \n"
icculus@1229
   158
"       popfq                  \n"
icculus@1229
   159
"       pushfq                 \n"
icculus@1229
   160
"       popq    %rax           \n"
icculus@1229
   161
"       xorl    %ecx,%eax      \n"
icculus@1229
   162
"       jz      1f             \n"
icculus@1229
   163
"       movl    $1,-8(%rbp)    \n"
icculus@1229
   164
"1:                            \n"
slouken@3579
   165
    );
slouken@745
   166
#endif
slouken@1895
   167
/* *INDENT-ON* */
slouken@1895
   168
    return has_CPUID;
slouken@745
   169
}
slouken@745
   170
slouken@3587
   171
#if defined(__GNUC__) && defined(i386)
slouken@3580
   172
#define cpuid(func, a, b, c, d) \
slouken@3584
   173
    __asm__ __volatile__ ( \
slouken@3584
   174
"        pushl %%ebx        \n" \
slouken@3584
   175
"        cpuid              \n" \
slouken@3584
   176
"        movl %%ebx, %%esi  \n" \
slouken@3584
   177
"        popl %%ebx         \n" : \
slouken@3584
   178
            "=a" (a), "=S" (b), "=c" (c), "=d" (d) : "a" (func))
slouken@3587
   179
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@3587
   180
#define cpuid(func, a, b, c, d) \
slouken@3587
   181
    __asm__ __volatile__ ( \
slouken@3587
   182
"        pushq %%rbx        \n" \
slouken@3587
   183
"        cpuid              \n" \
slouken@3587
   184
"        movq %%rbx, %%rsi  \n" \
slouken@3587
   185
"        popq %%rbx         \n" : \
slouken@3587
   186
            "=a" (a), "=S" (b), "=c" (c), "=d" (d) : "a" (func))
slouken@3579
   187
#elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
slouken@3580
   188
#define cpuid(func, a, b, c, d) \
slouken@3580
   189
    __asm { \
slouken@3580
   190
        __asm mov eax, func \
slouken@3580
   191
        __asm cpuid \
slouken@3580
   192
        __asm mov a, eax \
slouken@3580
   193
        __asm mov b, ebx \
slouken@3580
   194
        __asm mov c, ecx \
slouken@3580
   195
        __asm mov d, edx \
slouken@8241
   196
}
slouken@8241
   197
#elif defined(_MSC_VER) && defined(_M_X64)
slouken@8241
   198
#define cpuid(func, a, b, c, d) \
slouken@8241
   199
{ \
slouken@8241
   200
    int CPUInfo[4]; \
slouken@8241
   201
    __cpuid(CPUInfo, func); \
slouken@8241
   202
    a = CPUInfo[0]; \
slouken@8241
   203
    b = CPUInfo[1]; \
slouken@8241
   204
    c = CPUInfo[2]; \
slouken@8241
   205
    d = CPUInfo[3]; \
slouken@8241
   206
}
slouken@3579
   207
#else
slouken@3580
   208
#define cpuid(func, a, b, c, d) \
slouken@3580
   209
    a = b = c = d = 0
slouken@3579
   210
#endif
slouken@3579
   211
slouken@7860
   212
static SDL_INLINE int
slouken@1895
   213
CPU_getCPUIDFeatures(void)
slouken@745
   214
{
slouken@1895
   215
    int features = 0;
slouken@3580
   216
    int a, b, c, d;
slouken@3579
   217
slouken@3580
   218
    cpuid(0, a, b, c, d);
slouken@3580
   219
    if (a >= 1) {
slouken@3580
   220
        cpuid(1, a, b, c, d);
slouken@3580
   221
        features = d;
slouken@3579
   222
    }
slouken@1895
   223
    return features;
slouken@745
   224
}
slouken@745
   225
slouken@7860
   226
static SDL_INLINE int
slouken@1895
   227
CPU_haveRDTSC(void)
slouken@745
   228
{
slouken@1895
   229
    if (CPU_haveCPUID()) {
slouken@1895
   230
        return (CPU_getCPUIDFeatures() & 0x00000010);
slouken@1895
   231
    }
slouken@1895
   232
    return 0;
slouken@745
   233
}
slouken@745
   234
slouken@7860
   235
static SDL_INLINE int
slouken@5389
   236
CPU_haveAltiVec(void)
slouken@5389
   237
{
slouken@5389
   238
    volatile int altivec = 0;
slouken@6363
   239
#if (defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))) || (defined(__OpenBSD__) && defined(__powerpc__))
slouken@6363
   240
#ifdef __OpenBSD__
slouken@6363
   241
    int selectors[2] = { CTL_MACHDEP, CPU_ALTIVEC };
slouken@6363
   242
#else
slouken@5389
   243
    int selectors[2] = { CTL_HW, HW_VECTORUNIT };
slouken@6363
   244
#endif
slouken@5389
   245
    int hasVectorUnit = 0;
slouken@5389
   246
    size_t length = sizeof(hasVectorUnit);
slouken@5389
   247
    int error = sysctl(selectors, 2, &hasVectorUnit, &length, NULL, 0);
slouken@5389
   248
    if (0 == error)
slouken@5389
   249
        altivec = (hasVectorUnit != 0);
slouken@5389
   250
#elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
slouken@5389
   251
    void (*handler) (int sig);
slouken@5389
   252
    handler = signal(SIGILL, illegal_instruction);
slouken@5389
   253
    if (setjmp(jmpbuf) == 0) {
slouken@5389
   254
        asm volatile ("mtspr 256, %0\n\t" "vand %%v0, %%v0, %%v0"::"r" (-1));
slouken@5389
   255
        altivec = 1;
slouken@5389
   256
    }
slouken@5389
   257
    signal(SIGILL, handler);
slouken@5389
   258
#endif
slouken@5389
   259
    return altivec;
slouken@5389
   260
}
slouken@5389
   261
slouken@7860
   262
static SDL_INLINE int
slouken@1895
   263
CPU_haveMMX(void)
slouken@745
   264
{
slouken@1895
   265
    if (CPU_haveCPUID()) {
slouken@1895
   266
        return (CPU_getCPUIDFeatures() & 0x00800000);
slouken@1895
   267
    }
slouken@1895
   268
    return 0;
slouken@745
   269
}
slouken@745
   270
slouken@7860
   271
static SDL_INLINE int
slouken@5389
   272
CPU_have3DNow(void)
slouken@5389
   273
{
slouken@5389
   274
    if (CPU_haveCPUID()) {
slouken@5389
   275
        int a, b, c, d;
slouken@5389
   276
slouken@5389
   277
        cpuid(0x80000000, a, b, c, d);
slouken@5389
   278
        if (a >= 0x80000001) {
slouken@5389
   279
            cpuid(0x80000001, a, b, c, d);
slouken@5389
   280
            return (d & 0x80000000);
slouken@5389
   281
        }
slouken@5389
   282
    }
slouken@5389
   283
    return 0;
slouken@5389
   284
}
slouken@5389
   285
slouken@7860
   286
static SDL_INLINE int
slouken@1895
   287
CPU_haveSSE(void)
slouken@745
   288
{
slouken@1895
   289
    if (CPU_haveCPUID()) {
slouken@1895
   290
        return (CPU_getCPUIDFeatures() & 0x02000000);
slouken@1895
   291
    }
slouken@1895
   292
    return 0;
slouken@745
   293
}
slouken@739
   294
slouken@7860
   295
static SDL_INLINE int
slouken@1895
   296
CPU_haveSSE2(void)
slouken@785
   297
{
slouken@1895
   298
    if (CPU_haveCPUID()) {
slouken@1895
   299
        return (CPU_getCPUIDFeatures() & 0x04000000);
slouken@1895
   300
    }
slouken@1895
   301
    return 0;
slouken@785
   302
}
slouken@785
   303
slouken@7860
   304
static SDL_INLINE int
slouken@5259
   305
CPU_haveSSE3(void)
slouken@778
   306
{
slouken@5259
   307
    if (CPU_haveCPUID()) {
slouken@5259
   308
        int a, b, c, d;
slouken@5259
   309
slouken@5259
   310
        cpuid(0, a, b, c, d);
slouken@5259
   311
        if (a >= 1) {
slouken@5259
   312
            cpuid(1, a, b, c, d);
slouken@5259
   313
            return (c & 0x00000001);
slouken@5259
   314
        }
slouken@1895
   315
    }
slouken@5259
   316
    return 0;
slouken@5259
   317
}
slouken@5259
   318
slouken@7860
   319
static SDL_INLINE int
slouken@5263
   320
CPU_haveSSE41(void)
slouken@5259
   321
{
slouken@5259
   322
    if (CPU_haveCPUID()) {
slouken@5259
   323
        int a, b, c, d;
slouken@5259
   324
slouken@5263
   325
        cpuid(1, a, b, c, d);
slouken@5259
   326
        if (a >= 1) {
slouken@5259
   327
            cpuid(1, a, b, c, d);
slouken@5263
   328
            return (c & 0x00080000);
slouken@5263
   329
        }
slouken@5263
   330
    }
slouken@5263
   331
    return 0;
slouken@5263
   332
}
slouken@5263
   333
slouken@7860
   334
static SDL_INLINE int
slouken@5263
   335
CPU_haveSSE42(void)
slouken@5263
   336
{
slouken@5263
   337
    if (CPU_haveCPUID()) {
slouken@5263
   338
        int a, b, c, d;
slouken@5263
   339
slouken@5263
   340
        cpuid(1, a, b, c, d);
slouken@5263
   341
        if (a >= 1) {
slouken@5263
   342
            cpuid(1, a, b, c, d);
slouken@5263
   343
            return (c & 0x00100000);
slouken@5259
   344
        }
slouken@5259
   345
    }
slouken@5259
   346
    return 0;
slouken@778
   347
}
slouken@778
   348
slouken@8148
   349
static SDL_INLINE int
slouken@8148
   350
CPU_haveAVX(void)
slouken@8148
   351
{
slouken@8148
   352
    if (CPU_haveCPUID()) {
slouken@8148
   353
        int a, b, c, d;
slouken@8148
   354
slouken@8148
   355
        cpuid(1, a, b, c, d);
slouken@8148
   356
        if (a >= 1) {
slouken@8148
   357
            cpuid(1, a, b, c, d);
slouken@8148
   358
            return (c & 0x10000000);
slouken@8148
   359
        }
slouken@8148
   360
    }
slouken@8148
   361
    return 0;
slouken@8148
   362
}
slouken@8148
   363
slouken@3579
   364
static int SDL_CPUCount = 0;
slouken@3579
   365
slouken@3579
   366
int
slouken@5120
   367
SDL_GetCPUCount(void)
slouken@3579
   368
{
slouken@3579
   369
    if (!SDL_CPUCount) {
slouken@3606
   370
#if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
slouken@3586
   371
        if (SDL_CPUCount <= 0) {
slouken@3586
   372
            SDL_CPUCount = (int)sysconf(_SC_NPROCESSORS_ONLN);
slouken@3586
   373
        }
slouken@3586
   374
#endif
slouken@3579
   375
#ifdef HAVE_SYSCTLBYNAME
slouken@3586
   376
        if (SDL_CPUCount <= 0) {
slouken@3580
   377
            size_t size = sizeof(SDL_CPUCount);
slouken@3580
   378
            sysctlbyname("hw.ncpu", &SDL_CPUCount, &size, NULL, 0);
slouken@3580
   379
        }
slouken@3580
   380
#endif
slouken@5086
   381
#ifdef __WIN32__
slouken@3586
   382
        if (SDL_CPUCount <= 0) {
slouken@3580
   383
            SYSTEM_INFO info;
slouken@3580
   384
            GetSystemInfo(&info);
slouken@3580
   385
            SDL_CPUCount = info.dwNumberOfProcessors;
slouken@3580
   386
        }
slouken@3579
   387
#endif
slouken@3579
   388
        /* There has to be at least 1, right? :) */
slouken@3586
   389
        if (SDL_CPUCount <= 0) {
slouken@3579
   390
            SDL_CPUCount = 1;
slouken@3579
   391
        }
slouken@3579
   392
    }
slouken@3579
   393
    return SDL_CPUCount;
slouken@3579
   394
}
slouken@3579
   395
slouken@3579
   396
/* Oh, such a sweet sweet trick, just not very useful. :) */
slouken@4472
   397
static const char *
slouken@5120
   398
SDL_GetCPUType(void)
slouken@3579
   399
{
slouken@5115
   400
    static char SDL_CPUType[13];
slouken@3579
   401
slouken@3579
   402
    if (!SDL_CPUType[0]) {
slouken@3579
   403
        int i = 0;
slouken@3580
   404
        int a, b, c, d;
slouken@3579
   405
slouken@3579
   406
        if (CPU_haveCPUID()) {
slouken@5115
   407
            cpuid(0x00000000, a, b, c, d);
slouken@5115
   408
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   409
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   410
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   411
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   412
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   413
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   414
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   415
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   416
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   417
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   418
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   419
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   420
        }
slouken@5115
   421
        if (!SDL_CPUType[0]) {
slouken@5115
   422
            SDL_strlcpy(SDL_CPUType, "Unknown", sizeof(SDL_CPUType));
slouken@5115
   423
        }
slouken@5115
   424
    }
slouken@5115
   425
    return SDL_CPUType;
slouken@5115
   426
}
slouken@5115
   427
icculus@5981
   428
icculus@5981
   429
#ifdef TEST_MAIN  /* !!! FIXME: only used for test at the moment. */
slouken@5115
   430
static const char *
slouken@5120
   431
SDL_GetCPUName(void)
slouken@5115
   432
{
slouken@5115
   433
    static char SDL_CPUName[48];
slouken@5115
   434
slouken@5115
   435
    if (!SDL_CPUName[0]) {
slouken@5115
   436
        int i = 0;
slouken@5115
   437
        int a, b, c, d;
slouken@5115
   438
slouken@5115
   439
        if (CPU_haveCPUID()) {
slouken@3580
   440
            cpuid(0x80000000, a, b, c, d);
slouken@3580
   441
            if (a >= 0x80000004) {
slouken@3580
   442
                cpuid(0x80000002, a, b, c, d);
slouken@5115
   443
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   444
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   445
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   446
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   447
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   448
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   449
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   450
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   451
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   452
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   453
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   454
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   455
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   456
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   457
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   458
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@3580
   459
                cpuid(0x80000003, a, b, c, d);
slouken@5115
   460
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   461
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   462
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   463
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   464
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   465
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   466
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   467
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   468
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   469
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   470
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   471
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   472
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   473
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   474
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   475
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@3580
   476
                cpuid(0x80000004, a, b, c, d);
slouken@5115
   477
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   478
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   479
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   480
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   481
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   482
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   483
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   484
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   485
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   486
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   487
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   488
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   489
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   490
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   491
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   492
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@3579
   493
            }
slouken@3579
   494
        }
slouken@5115
   495
        if (!SDL_CPUName[0]) {
slouken@5115
   496
            SDL_strlcpy(SDL_CPUName, "Unknown", sizeof(SDL_CPUName));
slouken@3579
   497
        }
slouken@3579
   498
    }
slouken@5115
   499
    return SDL_CPUName;
slouken@5115
   500
}
icculus@5981
   501
#endif
slouken@5115
   502
slouken@5120
   503
int
slouken@5120
   504
SDL_GetCPUCacheLineSize(void)
slouken@5115
   505
{
slouken@5115
   506
    const char *cpuType = SDL_GetCPUType();
slouken@5115
   507
slouken@5115
   508
    if (SDL_strcmp(cpuType, "GenuineIntel") == 0) {
slouken@5115
   509
        int a, b, c, d;
slouken@5115
   510
slouken@5115
   511
        cpuid(0x00000001, a, b, c, d);
slouken@5115
   512
        return (((b >> 8) & 0xff) * 8);
slouken@5115
   513
    } else if (SDL_strcmp(cpuType, "AuthenticAMD") == 0) {
slouken@5115
   514
        int a, b, c, d;
slouken@5115
   515
slouken@5115
   516
        cpuid(0x80000005, a, b, c, d);
slouken@5115
   517
        return (c & 0xff);
slouken@5115
   518
    } else {
slouken@5115
   519
        /* Just make a guess here... */
slouken@5115
   520
        return SDL_CACHELINE_SIZE;
slouken@5115
   521
    }
slouken@3579
   522
}
slouken@3579
   523
slouken@739
   524
static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
slouken@739
   525
slouken@1895
   526
static Uint32
slouken@1895
   527
SDL_GetCPUFeatures(void)
slouken@739
   528
{
slouken@1895
   529
    if (SDL_CPUFeatures == 0xFFFFFFFF) {
slouken@1895
   530
        SDL_CPUFeatures = 0;
slouken@1895
   531
        if (CPU_haveRDTSC()) {
slouken@1895
   532
            SDL_CPUFeatures |= CPU_HAS_RDTSC;
slouken@1895
   533
        }
slouken@5389
   534
        if (CPU_haveAltiVec()) {
slouken@5389
   535
            SDL_CPUFeatures |= CPU_HAS_ALTIVEC;
slouken@5389
   536
        }
slouken@1895
   537
        if (CPU_haveMMX()) {
slouken@1895
   538
            SDL_CPUFeatures |= CPU_HAS_MMX;
slouken@1895
   539
        }
slouken@5389
   540
        if (CPU_have3DNow()) {
slouken@5389
   541
            SDL_CPUFeatures |= CPU_HAS_3DNOW;
slouken@5389
   542
        }
slouken@1895
   543
        if (CPU_haveSSE()) {
slouken@1895
   544
            SDL_CPUFeatures |= CPU_HAS_SSE;
slouken@1895
   545
        }
slouken@1895
   546
        if (CPU_haveSSE2()) {
slouken@1895
   547
            SDL_CPUFeatures |= CPU_HAS_SSE2;
slouken@1895
   548
        }
slouken@5259
   549
        if (CPU_haveSSE3()) {
slouken@5259
   550
            SDL_CPUFeatures |= CPU_HAS_SSE3;
slouken@5259
   551
        }
slouken@5263
   552
        if (CPU_haveSSE41()) {
slouken@5263
   553
            SDL_CPUFeatures |= CPU_HAS_SSE41;
slouken@5263
   554
        }
slouken@5263
   555
        if (CPU_haveSSE42()) {
slouken@5263
   556
            SDL_CPUFeatures |= CPU_HAS_SSE42;
slouken@1895
   557
        }
slouken@8148
   558
        if (CPU_haveAVX()) {
slouken@8148
   559
            SDL_CPUFeatures |= CPU_HAS_AVX;
slouken@8148
   560
        }
slouken@1895
   561
    }
slouken@1895
   562
    return SDL_CPUFeatures;
slouken@739
   563
}
slouken@739
   564
slouken@1895
   565
SDL_bool
slouken@1895
   566
SDL_HasRDTSC(void)
slouken@745
   567
{
slouken@1895
   568
    if (SDL_GetCPUFeatures() & CPU_HAS_RDTSC) {
slouken@1895
   569
        return SDL_TRUE;
slouken@1895
   570
    }
slouken@1895
   571
    return SDL_FALSE;
slouken@745
   572
}
slouken@745
   573
slouken@1895
   574
SDL_bool
slouken@5389
   575
SDL_HasAltiVec(void)
slouken@5389
   576
{
slouken@5389
   577
    if (SDL_GetCPUFeatures() & CPU_HAS_ALTIVEC) {
slouken@5389
   578
        return SDL_TRUE;
slouken@5389
   579
    }
slouken@5389
   580
    return SDL_FALSE;
slouken@5389
   581
}
slouken@5389
   582
slouken@5389
   583
SDL_bool
slouken@1895
   584
SDL_HasMMX(void)
slouken@739
   585
{
slouken@1895
   586
    if (SDL_GetCPUFeatures() & CPU_HAS_MMX) {
slouken@1895
   587
        return SDL_TRUE;
slouken@1895
   588
    }
slouken@1895
   589
    return SDL_FALSE;
slouken@739
   590
}
slouken@739
   591
slouken@1895
   592
SDL_bool
slouken@5389
   593
SDL_Has3DNow(void)
slouken@5389
   594
{
slouken@5389
   595
    if (SDL_GetCPUFeatures() & CPU_HAS_3DNOW) {
slouken@5389
   596
        return SDL_TRUE;
slouken@5389
   597
    }
slouken@5389
   598
    return SDL_FALSE;
slouken@5389
   599
}
slouken@5389
   600
slouken@5389
   601
SDL_bool
slouken@1895
   602
SDL_HasSSE(void)
slouken@739
   603
{
slouken@1895
   604
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE) {
slouken@1895
   605
        return SDL_TRUE;
slouken@1895
   606
    }
slouken@1895
   607
    return SDL_FALSE;
slouken@739
   608
}
slouken@739
   609
slouken@1895
   610
SDL_bool
slouken@1895
   611
SDL_HasSSE2(void)
slouken@804
   612
{
slouken@1895
   613
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE2) {
slouken@1895
   614
        return SDL_TRUE;
slouken@1895
   615
    }
slouken@1895
   616
    return SDL_FALSE;
slouken@804
   617
}
slouken@804
   618
slouken@1895
   619
SDL_bool
slouken@5259
   620
SDL_HasSSE3(void)
slouken@778
   621
{
slouken@5259
   622
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE3) {
slouken@5259
   623
        return SDL_TRUE;
slouken@5259
   624
    }
slouken@5259
   625
    return SDL_FALSE;
slouken@5259
   626
}
slouken@5259
   627
slouken@5259
   628
SDL_bool
slouken@5263
   629
SDL_HasSSE41(void)
slouken@5259
   630
{
slouken@5263
   631
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE41) {
slouken@5263
   632
        return SDL_TRUE;
slouken@5263
   633
    }
slouken@5263
   634
    return SDL_FALSE;
slouken@5263
   635
}
slouken@5263
   636
slouken@5263
   637
SDL_bool
slouken@5263
   638
SDL_HasSSE42(void)
slouken@5263
   639
{
slouken@5263
   640
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE42) {
slouken@1895
   641
        return SDL_TRUE;
slouken@1895
   642
    }
slouken@1895
   643
    return SDL_FALSE;
slouken@778
   644
}
slouken@778
   645
slouken@8148
   646
SDL_bool
slouken@8148
   647
SDL_HasAVX(void)
slouken@8148
   648
{
slouken@8148
   649
    if (SDL_GetCPUFeatures() & CPU_HAS_AVX) {
slouken@8148
   650
        return SDL_TRUE;
slouken@8148
   651
    }
slouken@8148
   652
    return SDL_FALSE;
slouken@8148
   653
}
slouken@8148
   654
slouken@7821
   655
static int SDL_SystemRAM = 0;
slouken@7821
   656
slouken@7821
   657
int
slouken@7821
   658
SDL_GetSystemRAM(void)
slouken@7821
   659
{
slouken@7821
   660
    if (!SDL_SystemRAM) {
slouken@7821
   661
#if defined(HAVE_SYSCONF) && defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
slouken@7821
   662
        if (SDL_SystemRAM <= 0) {
slouken@7821
   663
            SDL_SystemRAM = (int)((Sint64)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE) / (1024*1024));
slouken@7821
   664
        }
slouken@7821
   665
#endif
slouken@7821
   666
#ifdef HAVE_SYSCTLBYNAME
slouken@7821
   667
        if (SDL_SystemRAM <= 0) {
slouken@8240
   668
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
slouken@7826
   669
#ifdef HW_REALMEM
slouken@7826
   670
            int mib[2] = {CTL_HW, HW_REALMEM};
slouken@7826
   671
#else
slouken@7826
   672
            /* might only report up to 2 GiB */
slouken@7826
   673
            int mib[2] = {CTL_HW, HW_PHYSMEM};
slouken@7826
   674
#endif /* HW_REALMEM */
slouken@7826
   675
#else
slouken@7821
   676
            int mib[2] = {CTL_HW, HW_MEMSIZE};
slouken@8240
   677
#endif /* __FreeBSD__ || __FreeBSD_kernel__ */
slouken@7823
   678
            Uint64 memsize = 0;
slouken@7821
   679
            size_t len = sizeof(memsize);
slouken@7821
   680
            
slouken@7821
   681
            if (sysctl(mib, 2, &memsize, &len, NULL, 0) == 0) {
slouken@7821
   682
                SDL_SystemRAM = (int)(memsize / (1024*1024));
slouken@7821
   683
            }
slouken@7821
   684
        }
slouken@7821
   685
#endif
slouken@7821
   686
#ifdef __WIN32__
slouken@7821
   687
        if (SDL_SystemRAM <= 0) {
slouken@7821
   688
            MEMORYSTATUSEX stat;
icculus@7889
   689
            stat.dwLength = sizeof(stat);
slouken@7821
   690
            if (GlobalMemoryStatusEx(&stat)) {
slouken@7821
   691
                SDL_SystemRAM = (int)(stat.ullTotalPhys / (1024 * 1024));
slouken@7821
   692
            }
slouken@7821
   693
        }
slouken@7821
   694
#endif
slouken@7821
   695
    }
slouken@7821
   696
    return SDL_SystemRAM;
slouken@7821
   697
}
slouken@7821
   698
slouken@7821
   699
slouken@739
   700
#ifdef TEST_MAIN
slouken@739
   701
slouken@739
   702
#include <stdio.h>
slouken@739
   703
slouken@1895
   704
int
slouken@1895
   705
main()
slouken@739
   706
{
slouken@3579
   707
    printf("CPU count: %d\n", SDL_GetCPUCount());
slouken@5115
   708
    printf("CPU type: %s\n", SDL_GetCPUType());
slouken@5115
   709
    printf("CPU name: %s\n", SDL_GetCPUName());
slouken@5115
   710
    printf("CacheLine size: %d\n", SDL_GetCPUCacheLineSize());
slouken@1895
   711
    printf("RDTSC: %d\n", SDL_HasRDTSC());
slouken@5389
   712
    printf("Altivec: %d\n", SDL_HasAltiVec());
slouken@1895
   713
    printf("MMX: %d\n", SDL_HasMMX());
slouken@5389
   714
    printf("3DNow: %d\n", SDL_Has3DNow());
slouken@1895
   715
    printf("SSE: %d\n", SDL_HasSSE());
slouken@1895
   716
    printf("SSE2: %d\n", SDL_HasSSE2());
slouken@5259
   717
    printf("SSE3: %d\n", SDL_HasSSE3());
slouken@5263
   718
    printf("SSE4.1: %d\n", SDL_HasSSE41());
slouken@5263
   719
    printf("SSE4.2: %d\n", SDL_HasSSE42());
slouken@8148
   720
    printf("AVX: %d\n", SDL_HasAVX());
slouken@7821
   721
    printf("RAM: %d MB\n", SDL_GetSystemRAM());
slouken@1895
   722
    return 0;
slouken@739
   723
}
slouken@739
   724
slouken@739
   725
#endif /* TEST_MAIN */
slouken@1895
   726
slouken@1895
   727
/* vi: set ts=4 sw=4 expandtab: */