src/cpuinfo/SDL_cpuinfo.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 17 Nov 2016 01:34:18 -0500
changeset 10622 972f7f6dd9b9
parent 10621 3b74422e881d
child 10623 3b93b7acc1b4
permissions -rw-r--r--
cpuinfo: disable NEON detection on Android for now.

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