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