src/cpuinfo/SDL_cpuinfo.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 16 Nov 2016 22:49:04 -0500
changeset 10619 8786a8d1088e
parent 9998 f67cf37e9cd4
child 10620 c37e02dd1a9b
permissions -rw-r--r--
cpuinfo: Removed code duplication, cached CPUID details.
slouken@739
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@9998
     3
  Copyright (C) 1997-2016 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@9003
    63
#define CPU_HAS_AVX2    0x00000800
slouken@739
    64
slouken@6363
    65
#if SDL_ALTIVEC_BLITTERS && HAVE_SETJMP && !__MACOSX__ && !__OpenBSD__
slouken@5389
    66
/* This is the brute force way of detecting instruction sets...
slouken@5389
    67
   the idea is borrowed from the libmpeg2 library - thanks!
slouken@5389
    68
 */
slouken@5389
    69
static jmp_buf jmpbuf;
slouken@5389
    70
static void
slouken@5389
    71
illegal_instruction(int sig)
slouken@5389
    72
{
slouken@5389
    73
    longjmp(jmpbuf, 1);
slouken@5389
    74
}
slouken@5389
    75
#endif /* HAVE_SETJMP */
slouken@793
    76
slouken@9003
    77
static int
slouken@1895
    78
CPU_haveCPUID(void)
slouken@745
    79
{
slouken@1895
    80
    int has_CPUID = 0;
icculus@10619
    81
slouken@1895
    82
/* *INDENT-OFF* */
icculus@9278
    83
#ifndef SDL_CPUINFO_DISABLED
slouken@745
    84
#if defined(__GNUC__) && defined(i386)
slouken@3579
    85
    __asm__ (
slouken@745
    86
"        pushfl                      # Get original EFLAGS             \n"
slouken@745
    87
"        popl    %%eax                                                 \n"
slouken@745
    88
"        movl    %%eax,%%ecx                                           \n"
slouken@745
    89
"        xorl    $0x200000,%%eax     # Flip ID bit in EFLAGS           \n"
slouken@745
    90
"        pushl   %%eax               # Save new EFLAGS value on stack  \n"
slouken@745
    91
"        popfl                       # Replace current EFLAGS value    \n"
slouken@745
    92
"        pushfl                      # Get new EFLAGS                  \n"
slouken@745
    93
"        popl    %%eax               # Store new EFLAGS in EAX         \n"
slouken@745
    94
"        xorl    %%ecx,%%eax         # Can not toggle ID bit,          \n"
slouken@745
    95
"        jz      1f                  # Processor=80486                 \n"
slouken@745
    96
"        movl    $1,%0               # We have CPUID support           \n"
slouken@745
    97
"1:                                                                    \n"
slouken@3579
    98
    : "=m" (has_CPUID)
slouken@3579
    99
    :
slouken@3579
   100
    : "%eax", "%ecx"
slouken@3579
   101
    );
slouken@881
   102
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@881
   103
/* Technically, if this is being compiled under __x86_64__ then it has 
slouken@8241
   104
   CPUid by definition.  But it's nice to be able to prove it.  :)      */
slouken@3579
   105
    __asm__ (
slouken@881
   106
"        pushfq                      # Get original EFLAGS             \n"
slouken@881
   107
"        popq    %%rax                                                 \n"
slouken@881
   108
"        movq    %%rax,%%rcx                                           \n"
slouken@881
   109
"        xorl    $0x200000,%%eax     # Flip ID bit in EFLAGS           \n"
slouken@881
   110
"        pushq   %%rax               # Save new EFLAGS value on stack  \n"
slouken@881
   111
"        popfq                       # Replace current EFLAGS value    \n"
slouken@881
   112
"        pushfq                      # Get new EFLAGS                  \n"
slouken@881
   113
"        popq    %%rax               # Store new EFLAGS in EAX         \n"
slouken@881
   114
"        xorl    %%ecx,%%eax         # Can not toggle ID bit,          \n"
slouken@881
   115
"        jz      1f                  # Processor=80486                 \n"
slouken@881
   116
"        movl    $1,%0               # We have CPUID support           \n"
slouken@881
   117
"1:                                                                    \n"
slouken@3579
   118
    : "=m" (has_CPUID)
slouken@3579
   119
    :
slouken@3579
   120
    : "%rax", "%rcx"
slouken@3579
   121
    );
slouken@1442
   122
#elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
slouken@3579
   123
    __asm {
slouken@745
   124
        pushfd                      ; Get original EFLAGS
slouken@745
   125
        pop     eax
slouken@745
   126
        mov     ecx, eax
slouken@745
   127
        xor     eax, 200000h        ; Flip ID bit in EFLAGS
slouken@745
   128
        push    eax                 ; Save new EFLAGS value on stack
slouken@745
   129
        popfd                       ; Replace current EFLAGS value
slouken@745
   130
        pushfd                      ; Get new EFLAGS
slouken@745
   131
        pop     eax                 ; Store new EFLAGS in EAX
slouken@745
   132
        xor     eax, ecx            ; Can not toggle ID bit,
slouken@745
   133
        jz      done                ; Processor=80486
slouken@745
   134
        mov     has_CPUID,1         ; We have CPUID support
slouken@745
   135
done:
slouken@3579
   136
    }
slouken@8241
   137
#elif defined(_MSC_VER) && defined(_M_X64)
slouken@8241
   138
    has_CPUID = 1;
slouken@1864
   139
#elif defined(__sun) && defined(__i386)
slouken@3579
   140
    __asm (
icculus@1229
   141
"       pushfl                 \n"
slouken@3584
   142
"       popl    %eax           \n"
slouken@3584
   143
"       movl    %eax,%ecx      \n"
slouken@3584
   144
"       xorl    $0x200000,%eax \n"
slouken@3584
   145
"       pushl   %eax           \n"
slouken@3584
   146
"       popfl                  \n"
slouken@3584
   147
"       pushfl                 \n"
slouken@3584
   148
"       popl    %eax           \n"
slouken@3584
   149
"       xorl    %ecx,%eax      \n"
slouken@3584
   150
"       jz      1f             \n"
slouken@3584
   151
"       movl    $1,-8(%ebp)    \n"
icculus@1229
   152
"1:                            \n"
slouken@3579
   153
    );
icculus@1229
   154
#elif defined(__sun) && defined(__amd64)
slouken@3579
   155
    __asm (
icculus@1229
   156
"       pushfq                 \n"
icculus@1229
   157
"       popq    %rax           \n"
icculus@1229
   158
"       movq    %rax,%rcx      \n"
icculus@1229
   159
"       xorl    $0x200000,%eax \n"
icculus@1229
   160
"       pushq   %rax           \n"
icculus@1229
   161
"       popfq                  \n"
icculus@1229
   162
"       pushfq                 \n"
icculus@1229
   163
"       popq    %rax           \n"
icculus@1229
   164
"       xorl    %ecx,%eax      \n"
icculus@1229
   165
"       jz      1f             \n"
icculus@1229
   166
"       movl    $1,-8(%rbp)    \n"
icculus@1229
   167
"1:                            \n"
slouken@3579
   168
    );
slouken@745
   169
#endif
icculus@9278
   170
#endif
slouken@1895
   171
/* *INDENT-ON* */
slouken@1895
   172
    return has_CPUID;
slouken@745
   173
}
slouken@745
   174
slouken@3587
   175
#if defined(__GNUC__) && defined(i386)
slouken@3580
   176
#define cpuid(func, a, b, c, d) \
slouken@3584
   177
    __asm__ __volatile__ ( \
slouken@3584
   178
"        pushl %%ebx        \n" \
slouken@9003
   179
"        xorl %%ecx,%%ecx   \n" \
slouken@3584
   180
"        cpuid              \n" \
slouken@3584
   181
"        movl %%ebx, %%esi  \n" \
slouken@3584
   182
"        popl %%ebx         \n" : \
slouken@3584
   183
            "=a" (a), "=S" (b), "=c" (c), "=d" (d) : "a" (func))
slouken@3587
   184
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@3587
   185
#define cpuid(func, a, b, c, d) \
slouken@3587
   186
    __asm__ __volatile__ ( \
slouken@3587
   187
"        pushq %%rbx        \n" \
slouken@9003
   188
"        xorq %%rcx,%%rcx   \n" \
slouken@3587
   189
"        cpuid              \n" \
slouken@3587
   190
"        movq %%rbx, %%rsi  \n" \
slouken@3587
   191
"        popq %%rbx         \n" : \
slouken@3587
   192
            "=a" (a), "=S" (b), "=c" (c), "=d" (d) : "a" (func))
slouken@3579
   193
#elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
slouken@3580
   194
#define cpuid(func, a, b, c, d) \
slouken@3580
   195
    __asm { \
slouken@3580
   196
        __asm mov eax, func \
slouken@9003
   197
        __asm xor ecx, ecx \
slouken@3580
   198
        __asm cpuid \
slouken@3580
   199
        __asm mov a, eax \
slouken@3580
   200
        __asm mov b, ebx \
slouken@3580
   201
        __asm mov c, ecx \
slouken@3580
   202
        __asm mov d, edx \
slouken@8241
   203
}
slouken@8241
   204
#elif defined(_MSC_VER) && defined(_M_X64)
slouken@8241
   205
#define cpuid(func, a, b, c, d) \
slouken@8241
   206
{ \
slouken@8241
   207
    int CPUInfo[4]; \
slouken@8241
   208
    __cpuid(CPUInfo, func); \
slouken@8241
   209
    a = CPUInfo[0]; \
slouken@8241
   210
    b = CPUInfo[1]; \
slouken@8241
   211
    c = CPUInfo[2]; \
slouken@8241
   212
    d = CPUInfo[3]; \
slouken@8241
   213
}
slouken@3579
   214
#else
slouken@3580
   215
#define cpuid(func, a, b, c, d) \
slouken@3580
   216
    a = b = c = d = 0
slouken@3579
   217
#endif
slouken@3579
   218
icculus@10619
   219
static int CPU_CPUIDFeatures[4];
icculus@10619
   220
static int CPU_CPUIDMaxFunction = 0;
icculus@10619
   221
static SDL_bool CPU_OSSavesYMM = SDL_FALSE;
icculus@10619
   222
icculus@10619
   223
static void
icculus@10619
   224
CPU_calcCPUIDFeatures(void)
slouken@745
   225
{
icculus@10619
   226
    static SDL_bool checked = SDL_FALSE;
icculus@10619
   227
    if (!checked) {
icculus@10619
   228
        checked = SDL_TRUE;
icculus@10619
   229
        if (CPU_haveCPUID()) {
icculus@10619
   230
            int a, b, c, d;
icculus@10619
   231
            cpuid(0, a, b, c, d);
icculus@10619
   232
            CPU_CPUIDMaxFunction = a;
icculus@10619
   233
            if (CPU_CPUIDMaxFunction >= 1) {
icculus@10619
   234
                cpuid(1, a, b, c, d);
icculus@10619
   235
                CPU_CPUIDFeatures[0] = a;
icculus@10619
   236
                CPU_CPUIDFeatures[1] = b;
icculus@10619
   237
                CPU_CPUIDFeatures[2] = c;
icculus@10619
   238
                CPU_CPUIDFeatures[3] = d;
slouken@3579
   239
icculus@10619
   240
                /* Check to make sure we can call xgetbv */
icculus@10619
   241
                if (c & 0x08000000) {
icculus@10619
   242
                    /* Call xgetbv to see if YMM register state is saved */
icculus@10619
   243
#if defined(__GNUC__) && (defined(i386) || defined(__x86_64__))
icculus@10619
   244
                    asm(".byte 0x0f, 0x01, 0xd0" : "=a" (a) : "c" (0) : "%edx");
icculus@10619
   245
#elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64)) && (_MSC_FULL_VER >= 160040219) /* VS2010 SP1 */
icculus@10619
   246
                    a = (int)_xgetbv(0);
icculus@10619
   247
#elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
icculus@10619
   248
                    __asm
icculus@10619
   249
                    {
icculus@10619
   250
                        xor ecx, ecx
icculus@10619
   251
                        _asm _emit 0x0f _asm _emit 0x01 _asm _emit 0xd0
icculus@10619
   252
                        mov a, eax
icculus@10619
   253
                    }
icculus@10619
   254
#endif
icculus@10619
   255
                    CPU_OSSavesYMM = ((a & 6) == 6) ? SDL_TRUE : SDL_FALSE;
icculus@10619
   256
                }
icculus@10619
   257
            }
icculus@10619
   258
        }
slouken@3579
   259
    }
slouken@745
   260
}
slouken@745
   261
slouken@9003
   262
static int
slouken@5389
   263
CPU_haveAltiVec(void)
slouken@5389
   264
{
slouken@5389
   265
    volatile int altivec = 0;
icculus@9278
   266
#ifndef SDL_CPUINFO_DISABLED
slouken@6363
   267
#if (defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))) || (defined(__OpenBSD__) && defined(__powerpc__))
slouken@6363
   268
#ifdef __OpenBSD__
slouken@6363
   269
    int selectors[2] = { CTL_MACHDEP, CPU_ALTIVEC };
slouken@6363
   270
#else
slouken@5389
   271
    int selectors[2] = { CTL_HW, HW_VECTORUNIT };
slouken@6363
   272
#endif
slouken@5389
   273
    int hasVectorUnit = 0;
slouken@5389
   274
    size_t length = sizeof(hasVectorUnit);
slouken@5389
   275
    int error = sysctl(selectors, 2, &hasVectorUnit, &length, NULL, 0);
slouken@5389
   276
    if (0 == error)
slouken@5389
   277
        altivec = (hasVectorUnit != 0);
slouken@5389
   278
#elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
slouken@5389
   279
    void (*handler) (int sig);
slouken@5389
   280
    handler = signal(SIGILL, illegal_instruction);
slouken@5389
   281
    if (setjmp(jmpbuf) == 0) {
slouken@5389
   282
        asm volatile ("mtspr 256, %0\n\t" "vand %%v0, %%v0, %%v0"::"r" (-1));
slouken@5389
   283
        altivec = 1;
slouken@5389
   284
    }
slouken@5389
   285
    signal(SIGILL, handler);
slouken@5389
   286
#endif
icculus@9278
   287
#endif
slouken@5389
   288
    return altivec;
slouken@5389
   289
}
slouken@5389
   290
slouken@9003
   291
static int
slouken@5389
   292
CPU_have3DNow(void)
slouken@5389
   293
{
icculus@10619
   294
    if (CPU_CPUIDMaxFunction > 0) {  /* that is, do we have CPUID at all? */
slouken@5389
   295
        int a, b, c, d;
slouken@5389
   296
        cpuid(0x80000000, a, b, c, d);
slouken@5389
   297
        if (a >= 0x80000001) {
slouken@5389
   298
            cpuid(0x80000001, a, b, c, d);
slouken@5389
   299
            return (d & 0x80000000);
slouken@5389
   300
        }
slouken@5389
   301
    }
slouken@5389
   302
    return 0;
slouken@5389
   303
}
slouken@5389
   304
icculus@10619
   305
#define CPU_haveRDTSC() (CPU_CPUIDFeatures[3] & 0x00000010)
icculus@10619
   306
#define CPU_haveMMX() (CPU_CPUIDFeatures[3] & 0x00800000)
icculus@10619
   307
#define CPU_haveSSE() (CPU_CPUIDFeatures[3] & 0x02000000)
icculus@10619
   308
#define CPU_haveSSE2() (CPU_CPUIDFeatures[3] & 0x04000000)
icculus@10619
   309
#define CPU_haveSSE3() (CPU_CPUIDFeatures[2] & 0x00000001)
icculus@10619
   310
#define CPU_haveSSE41() (CPU_CPUIDFeatures[2] & 0x00080000)
icculus@10619
   311
#define CPU_haveSSE42() (CPU_CPUIDFeatures[2] & 0x00100000)
icculus@10619
   312
#define CPU_haveAVX() (CPU_OSSavesYMM && (CPU_CPUIDFeatures[2] & 0x10000000))
slouken@8148
   313
slouken@9003
   314
static int
slouken@9003
   315
CPU_haveAVX2(void)
slouken@9003
   316
{
icculus@10619
   317
    if (CPU_OSSavesYMM && (CPU_CPUIDMaxFunction >= 7)) {
slouken@9003
   318
        int a, b, c, d;
icculus@10619
   319
        cpuid(7, a, b, c, d);
icculus@10619
   320
        return (b & 0x00000020);
slouken@9003
   321
    }
slouken@9003
   322
    return 0;
slouken@9003
   323
}
slouken@9003
   324
slouken@3579
   325
static int SDL_CPUCount = 0;
slouken@3579
   326
slouken@3579
   327
int
slouken@5120
   328
SDL_GetCPUCount(void)
slouken@3579
   329
{
slouken@3579
   330
    if (!SDL_CPUCount) {
icculus@9278
   331
#ifndef SDL_CPUINFO_DISABLED
slouken@3606
   332
#if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
slouken@3586
   333
        if (SDL_CPUCount <= 0) {
slouken@3586
   334
            SDL_CPUCount = (int)sysconf(_SC_NPROCESSORS_ONLN);
slouken@3586
   335
        }
slouken@3586
   336
#endif
slouken@3579
   337
#ifdef HAVE_SYSCTLBYNAME
slouken@3586
   338
        if (SDL_CPUCount <= 0) {
slouken@3580
   339
            size_t size = sizeof(SDL_CPUCount);
slouken@3580
   340
            sysctlbyname("hw.ncpu", &SDL_CPUCount, &size, NULL, 0);
slouken@3580
   341
        }
slouken@3580
   342
#endif
slouken@5086
   343
#ifdef __WIN32__
slouken@3586
   344
        if (SDL_CPUCount <= 0) {
slouken@3580
   345
            SYSTEM_INFO info;
slouken@3580
   346
            GetSystemInfo(&info);
slouken@3580
   347
            SDL_CPUCount = info.dwNumberOfProcessors;
slouken@3580
   348
        }
slouken@3579
   349
#endif
icculus@9278
   350
#endif
slouken@3579
   351
        /* There has to be at least 1, right? :) */
slouken@3586
   352
        if (SDL_CPUCount <= 0) {
slouken@3579
   353
            SDL_CPUCount = 1;
slouken@3579
   354
        }
slouken@3579
   355
    }
slouken@3579
   356
    return SDL_CPUCount;
slouken@3579
   357
}
slouken@3579
   358
slouken@3579
   359
/* Oh, such a sweet sweet trick, just not very useful. :) */
slouken@4472
   360
static const char *
slouken@5120
   361
SDL_GetCPUType(void)
slouken@3579
   362
{
slouken@5115
   363
    static char SDL_CPUType[13];
slouken@3579
   364
slouken@3579
   365
    if (!SDL_CPUType[0]) {
slouken@3579
   366
        int i = 0;
slouken@3579
   367
icculus@10619
   368
        CPU_calcCPUIDFeatures();
icculus@10619
   369
        if (CPU_CPUIDMaxFunction > 0) {  /* do we have CPUID at all? */
icculus@9300
   370
            int a, b, c, d;
slouken@5115
   371
            cpuid(0x00000000, a, b, c, d);
icculus@9300
   372
            (void) a;
slouken@5115
   373
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   374
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   375
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
icculus@8642
   376
            SDL_CPUType[i++] = (char)(b & 0xff);
icculus@8642
   377
slouken@5115
   378
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   379
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   380
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
icculus@8642
   381
            SDL_CPUType[i++] = (char)(d & 0xff);
icculus@8642
   382
slouken@5115
   383
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   384
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   385
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
icculus@8642
   386
            SDL_CPUType[i++] = (char)(c & 0xff);
slouken@5115
   387
        }
slouken@5115
   388
        if (!SDL_CPUType[0]) {
slouken@5115
   389
            SDL_strlcpy(SDL_CPUType, "Unknown", sizeof(SDL_CPUType));
slouken@5115
   390
        }
slouken@5115
   391
    }
slouken@5115
   392
    return SDL_CPUType;
slouken@5115
   393
}
slouken@5115
   394
icculus@5981
   395
icculus@5981
   396
#ifdef TEST_MAIN  /* !!! FIXME: only used for test at the moment. */
slouken@5115
   397
static const char *
slouken@5120
   398
SDL_GetCPUName(void)
slouken@5115
   399
{
slouken@5115
   400
    static char SDL_CPUName[48];
slouken@5115
   401
slouken@5115
   402
    if (!SDL_CPUName[0]) {
slouken@5115
   403
        int i = 0;
slouken@5115
   404
        int a, b, c, d;
slouken@5115
   405
icculus@10619
   406
        CPU_calcCPUIDFeatures();
icculus@10619
   407
        if (CPU_CPUIDMaxFunction > 0) {  /* do we have CPUID at all? */
slouken@3580
   408
            cpuid(0x80000000, a, b, c, d);
slouken@3580
   409
            if (a >= 0x80000004) {
slouken@3580
   410
                cpuid(0x80000002, a, b, c, d);
slouken@5115
   411
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   412
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   413
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   414
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   415
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   416
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   417
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   418
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   419
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   420
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   421
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   422
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   423
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   424
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   425
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   426
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@3580
   427
                cpuid(0x80000003, a, b, c, d);
slouken@5115
   428
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   429
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   430
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   431
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   432
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   433
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   434
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   435
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   436
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   437
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   438
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   439
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   440
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   441
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   442
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   443
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@3580
   444
                cpuid(0x80000004, a, b, c, d);
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)(a & 0xff); a >>= 8;
slouken@5115
   448
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 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)(b & 0xff); b >>= 8;
slouken@5115
   452
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 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)(c & 0xff); c >>= 8;
slouken@5115
   456
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   457
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   458
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   459
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   460
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@3579
   461
            }
slouken@3579
   462
        }
slouken@5115
   463
        if (!SDL_CPUName[0]) {
slouken@5115
   464
            SDL_strlcpy(SDL_CPUName, "Unknown", sizeof(SDL_CPUName));
slouken@3579
   465
        }
slouken@3579
   466
    }
slouken@5115
   467
    return SDL_CPUName;
slouken@5115
   468
}
icculus@5981
   469
#endif
slouken@5115
   470
slouken@5120
   471
int
slouken@5120
   472
SDL_GetCPUCacheLineSize(void)
slouken@5115
   473
{
slouken@5115
   474
    const char *cpuType = SDL_GetCPUType();
icculus@9300
   475
    int a, b, c, d;
icculus@9300
   476
    (void) a; (void) b; (void) c; (void) d;
slouken@5115
   477
    if (SDL_strcmp(cpuType, "GenuineIntel") == 0) {
slouken@5115
   478
        cpuid(0x00000001, a, b, c, d);
slouken@5115
   479
        return (((b >> 8) & 0xff) * 8);
slouken@5115
   480
    } else if (SDL_strcmp(cpuType, "AuthenticAMD") == 0) {
slouken@5115
   481
        cpuid(0x80000005, a, b, c, d);
slouken@5115
   482
        return (c & 0xff);
slouken@5115
   483
    } else {
slouken@5115
   484
        /* Just make a guess here... */
slouken@5115
   485
        return SDL_CACHELINE_SIZE;
slouken@5115
   486
    }
slouken@3579
   487
}
slouken@3579
   488
slouken@739
   489
static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
slouken@739
   490
slouken@1895
   491
static Uint32
slouken@1895
   492
SDL_GetCPUFeatures(void)
slouken@739
   493
{
slouken@1895
   494
    if (SDL_CPUFeatures == 0xFFFFFFFF) {
icculus@10619
   495
        CPU_calcCPUIDFeatures();
slouken@1895
   496
        SDL_CPUFeatures = 0;
slouken@1895
   497
        if (CPU_haveRDTSC()) {
slouken@1895
   498
            SDL_CPUFeatures |= CPU_HAS_RDTSC;
slouken@1895
   499
        }
slouken@5389
   500
        if (CPU_haveAltiVec()) {
slouken@5389
   501
            SDL_CPUFeatures |= CPU_HAS_ALTIVEC;
slouken@5389
   502
        }
slouken@1895
   503
        if (CPU_haveMMX()) {
slouken@1895
   504
            SDL_CPUFeatures |= CPU_HAS_MMX;
slouken@1895
   505
        }
slouken@5389
   506
        if (CPU_have3DNow()) {
slouken@5389
   507
            SDL_CPUFeatures |= CPU_HAS_3DNOW;
slouken@5389
   508
        }
slouken@1895
   509
        if (CPU_haveSSE()) {
slouken@1895
   510
            SDL_CPUFeatures |= CPU_HAS_SSE;
slouken@1895
   511
        }
slouken@1895
   512
        if (CPU_haveSSE2()) {
slouken@1895
   513
            SDL_CPUFeatures |= CPU_HAS_SSE2;
slouken@1895
   514
        }
slouken@5259
   515
        if (CPU_haveSSE3()) {
slouken@5259
   516
            SDL_CPUFeatures |= CPU_HAS_SSE3;
slouken@5259
   517
        }
slouken@5263
   518
        if (CPU_haveSSE41()) {
slouken@5263
   519
            SDL_CPUFeatures |= CPU_HAS_SSE41;
slouken@5263
   520
        }
slouken@5263
   521
        if (CPU_haveSSE42()) {
slouken@5263
   522
            SDL_CPUFeatures |= CPU_HAS_SSE42;
slouken@1895
   523
        }
slouken@8148
   524
        if (CPU_haveAVX()) {
slouken@8148
   525
            SDL_CPUFeatures |= CPU_HAS_AVX;
slouken@8148
   526
        }
slouken@9003
   527
        if (CPU_haveAVX2()) {
slouken@9003
   528
            SDL_CPUFeatures |= CPU_HAS_AVX2;
slouken@9003
   529
        }
slouken@1895
   530
    }
slouken@1895
   531
    return SDL_CPUFeatures;
slouken@739
   532
}
slouken@739
   533
icculus@10619
   534
#define CPU_FEATURE_AVAILABLE(f) ((SDL_GetCPUFeatures() & f) ? SDL_TRUE : SDL_FALSE)
icculus@10619
   535
icculus@10619
   536
SDL_bool SDL_HasRDTSC(void)
slouken@745
   537
{
icculus@10619
   538
    return CPU_FEATURE_AVAILABLE(CPU_HAS_RDTSC);
slouken@745
   539
}
slouken@745
   540
slouken@1895
   541
SDL_bool
slouken@5389
   542
SDL_HasAltiVec(void)
slouken@5389
   543
{
icculus@10619
   544
    return CPU_FEATURE_AVAILABLE(CPU_HAS_ALTIVEC);
slouken@5389
   545
}
slouken@5389
   546
slouken@5389
   547
SDL_bool
slouken@1895
   548
SDL_HasMMX(void)
slouken@739
   549
{
icculus@10619
   550
    return CPU_FEATURE_AVAILABLE(CPU_HAS_MMX);
slouken@739
   551
}
slouken@739
   552
slouken@1895
   553
SDL_bool
slouken@5389
   554
SDL_Has3DNow(void)
slouken@5389
   555
{
icculus@10619
   556
    return CPU_FEATURE_AVAILABLE(CPU_HAS_3DNOW);
slouken@5389
   557
}
slouken@5389
   558
slouken@5389
   559
SDL_bool
slouken@1895
   560
SDL_HasSSE(void)
slouken@739
   561
{
icculus@10619
   562
    return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE);
slouken@739
   563
}
slouken@739
   564
slouken@1895
   565
SDL_bool
slouken@1895
   566
SDL_HasSSE2(void)
slouken@804
   567
{
icculus@10619
   568
    return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE2);
slouken@804
   569
}
slouken@804
   570
slouken@1895
   571
SDL_bool
slouken@5259
   572
SDL_HasSSE3(void)
slouken@778
   573
{
icculus@10619
   574
    return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE3);
slouken@5259
   575
}
slouken@5259
   576
slouken@5259
   577
SDL_bool
slouken@5263
   578
SDL_HasSSE41(void)
slouken@5259
   579
{
icculus@10619
   580
    return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE41);
slouken@5263
   581
}
slouken@5263
   582
slouken@5263
   583
SDL_bool
slouken@5263
   584
SDL_HasSSE42(void)
slouken@5263
   585
{
icculus@10619
   586
    return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE42);
slouken@778
   587
}
slouken@778
   588
slouken@8148
   589
SDL_bool
slouken@8148
   590
SDL_HasAVX(void)
slouken@8148
   591
{
icculus@10619
   592
    return CPU_FEATURE_AVAILABLE(CPU_HAS_AVX);
slouken@8148
   593
}
slouken@8148
   594
slouken@9003
   595
SDL_bool
slouken@9003
   596
SDL_HasAVX2(void)
slouken@9003
   597
{
icculus@10619
   598
    return CPU_FEATURE_AVAILABLE(CPU_HAS_AVX2);
slouken@9003
   599
}
slouken@9003
   600
slouken@7821
   601
static int SDL_SystemRAM = 0;
slouken@7821
   602
slouken@7821
   603
int
slouken@7821
   604
SDL_GetSystemRAM(void)
slouken@7821
   605
{
slouken@7821
   606
    if (!SDL_SystemRAM) {
icculus@9278
   607
#ifndef SDL_CPUINFO_DISABLED
slouken@7821
   608
#if defined(HAVE_SYSCONF) && defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
slouken@7821
   609
        if (SDL_SystemRAM <= 0) {
slouken@7821
   610
            SDL_SystemRAM = (int)((Sint64)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE) / (1024*1024));
slouken@7821
   611
        }
slouken@7821
   612
#endif
slouken@7821
   613
#ifdef HAVE_SYSCTLBYNAME
slouken@7821
   614
        if (SDL_SystemRAM <= 0) {
icculus@9987
   615
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
slouken@7826
   616
#ifdef HW_REALMEM
slouken@7826
   617
            int mib[2] = {CTL_HW, HW_REALMEM};
slouken@7826
   618
#else
slouken@7826
   619
            /* might only report up to 2 GiB */
slouken@7826
   620
            int mib[2] = {CTL_HW, HW_PHYSMEM};
slouken@7826
   621
#endif /* HW_REALMEM */
slouken@7826
   622
#else
slouken@7821
   623
            int mib[2] = {CTL_HW, HW_MEMSIZE};
slouken@8240
   624
#endif /* __FreeBSD__ || __FreeBSD_kernel__ */
slouken@7823
   625
            Uint64 memsize = 0;
slouken@7821
   626
            size_t len = sizeof(memsize);
slouken@7821
   627
            
slouken@7821
   628
            if (sysctl(mib, 2, &memsize, &len, NULL, 0) == 0) {
slouken@7821
   629
                SDL_SystemRAM = (int)(memsize / (1024*1024));
slouken@7821
   630
            }
slouken@7821
   631
        }
slouken@7821
   632
#endif
slouken@7821
   633
#ifdef __WIN32__
slouken@7821
   634
        if (SDL_SystemRAM <= 0) {
slouken@7821
   635
            MEMORYSTATUSEX stat;
icculus@7889
   636
            stat.dwLength = sizeof(stat);
slouken@7821
   637
            if (GlobalMemoryStatusEx(&stat)) {
slouken@7821
   638
                SDL_SystemRAM = (int)(stat.ullTotalPhys / (1024 * 1024));
slouken@7821
   639
            }
slouken@7821
   640
        }
slouken@7821
   641
#endif
icculus@9278
   642
#endif
slouken@7821
   643
    }
slouken@7821
   644
    return SDL_SystemRAM;
slouken@7821
   645
}
slouken@7821
   646
slouken@7821
   647
slouken@739
   648
#ifdef TEST_MAIN
slouken@739
   649
slouken@739
   650
#include <stdio.h>
slouken@739
   651
slouken@1895
   652
int
slouken@1895
   653
main()
slouken@739
   654
{
slouken@3579
   655
    printf("CPU count: %d\n", SDL_GetCPUCount());
slouken@5115
   656
    printf("CPU type: %s\n", SDL_GetCPUType());
slouken@5115
   657
    printf("CPU name: %s\n", SDL_GetCPUName());
slouken@5115
   658
    printf("CacheLine size: %d\n", SDL_GetCPUCacheLineSize());
slouken@1895
   659
    printf("RDTSC: %d\n", SDL_HasRDTSC());
slouken@5389
   660
    printf("Altivec: %d\n", SDL_HasAltiVec());
slouken@1895
   661
    printf("MMX: %d\n", SDL_HasMMX());
slouken@5389
   662
    printf("3DNow: %d\n", SDL_Has3DNow());
slouken@1895
   663
    printf("SSE: %d\n", SDL_HasSSE());
slouken@1895
   664
    printf("SSE2: %d\n", SDL_HasSSE2());
slouken@5259
   665
    printf("SSE3: %d\n", SDL_HasSSE3());
slouken@5263
   666
    printf("SSE4.1: %d\n", SDL_HasSSE41());
slouken@5263
   667
    printf("SSE4.2: %d\n", SDL_HasSSE42());
slouken@8148
   668
    printf("AVX: %d\n", SDL_HasAVX());
slouken@9003
   669
    printf("AVX2: %d\n", SDL_HasAVX2());
slouken@7821
   670
    printf("RAM: %d MB\n", SDL_GetSystemRAM());
slouken@1895
   671
    return 0;
slouken@739
   672
}
slouken@739
   673
slouken@739
   674
#endif /* TEST_MAIN */
slouken@1895
   675
slouken@1895
   676
/* vi: set ts=4 sw=4 expandtab: */