src/cpuinfo/SDL_cpuinfo.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 17 Nov 2016 16:04:00 -0500
changeset 10626 04035f36048e
parent 10625 d40f7ebf34eb
child 10627 35fe16a7960a
permissions -rw-r--r--
cpuinfo: Patched to compile on Android, Linux.
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@10626
    53
#if (defined(__LINUX__) || defined(__ANDROID__)) && defined(__ARM_ARCH)
icculus@10626
    54
#include <asm/hwcap.h>
icculus@10626
    55
#if defined HAVE_GETAUXVAL
icculus@10620
    56
#include <sys/auxv.h>
icculus@10626
    57
#else
icculus@10625
    58
#include <fcntl.h>
icculus@10625
    59
#endif
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
icculus@10626
   301
#if (defined(__LINUX__) || defined(__ANDROID__)) && defined(__ARM_ARCH) && !defined(HAVE_GETAUXVAL)
icculus@10624
   302
static int
icculus@10624
   303
readProcAuxvForNeon(void)
icculus@10624
   304
{
icculus@10624
   305
    int neon = 0;
icculus@10624
   306
    int kv[2];
icculus@10624
   307
    const int fd = open("/proc/self/auxv", O_RDONLY);
icculus@10626
   308
    if (fd != -1) {
icculus@10626
   309
        while (read(fd, kv, sizeof (kv)) == sizeof (kv)) {
icculus@10626
   310
            if (kv[0] == AT_HWCAP) {
icculus@10626
   311
                neon = ((kv[1] & HWCAP_NEON) == HWCAP_NEON);
icculus@10626
   312
                break;
icculus@10626
   313
            }
icculus@10626
   314
        }
icculus@10626
   315
        close(fd);
icculus@10624
   316
    }
icculus@10624
   317
    return neon;
icculus@10624
   318
}
icculus@10624
   319
#endif
icculus@10624
   320
icculus@10624
   321
slouken@9003
   322
static int
icculus@10620
   323
CPU_haveNEON(void)
icculus@10620
   324
{
icculus@10620
   325
/* The way you detect NEON is a privileged instruction on ARM, so you have
icculus@10620
   326
   query the OS kernel in a platform-specific way. :/ */
icculus@10624
   327
#if defined(SDL_CPUINFO_DISABLED) || !defined(__ARM_ARCH)
icculus@10625
   328
    return 0;  /* disabled or not an ARM CPU at all. */
icculus@10624
   329
#elif __ARM_ARCH >= 8
icculus@10624
   330
    return 1;  // ARMv8 always has non-optional NEON support.
icculus@10624
   331
#elif defined(__APPLE__)
icculus@10620
   332
    /* all hardware that runs iOS 5 and later support NEON, but check anyhow */
icculus@10624
   333
    int neon = 0;
icculus@10620
   334
    size_t length = sizeof (neon);
icculus@10620
   335
    const int error = sysctlbyname("hw.optional.neon", &neon, &length, NULL, 0);
icculus@10624
   336
    return (!error) && (neon != 0);
icculus@10625
   337
#elif (defined(__LINUX__) || defined(__ANDROID__)) && defined(HAVE_GETAUXVAL)
icculus@10624
   338
    return ((getauxval(AT_HWCAP) & HWCAP_NEON) == HWCAP_NEON)
icculus@10624
   339
#elif (defined(__LINUX__) || defined(__ANDROID__))
icculus@10626
   340
    return readProcAuxvForNeon();   /* Android offers a static library for this, but it just parses /proc/self/auxv */
icculus@10620
   341
#elif (defined(__WINDOWS__) || defined(__WINRT__)) && defined(_M_ARM)
icculus@10620
   342
    /* All WinRT ARM devices are required to support NEON, but just in case. */
icculus@10625
   343
    return IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE) != 0;
icculus@10624
   344
#else
icculus@10624
   345
#warning SDL_HasNEON is not implemented for this ARM platform. Write me.
icculus@10625
   346
    return 0;
icculus@10620
   347
#endif
icculus@10620
   348
}
icculus@10620
   349
icculus@10620
   350
static int
slouken@5389
   351
CPU_have3DNow(void)
slouken@5389
   352
{
icculus@10619
   353
    if (CPU_CPUIDMaxFunction > 0) {  /* that is, do we have CPUID at all? */
slouken@5389
   354
        int a, b, c, d;
slouken@5389
   355
        cpuid(0x80000000, a, b, c, d);
slouken@5389
   356
        if (a >= 0x80000001) {
slouken@5389
   357
            cpuid(0x80000001, a, b, c, d);
slouken@5389
   358
            return (d & 0x80000000);
slouken@5389
   359
        }
slouken@5389
   360
    }
slouken@5389
   361
    return 0;
slouken@5389
   362
}
slouken@5389
   363
icculus@10619
   364
#define CPU_haveRDTSC() (CPU_CPUIDFeatures[3] & 0x00000010)
icculus@10619
   365
#define CPU_haveMMX() (CPU_CPUIDFeatures[3] & 0x00800000)
icculus@10619
   366
#define CPU_haveSSE() (CPU_CPUIDFeatures[3] & 0x02000000)
icculus@10619
   367
#define CPU_haveSSE2() (CPU_CPUIDFeatures[3] & 0x04000000)
icculus@10619
   368
#define CPU_haveSSE3() (CPU_CPUIDFeatures[2] & 0x00000001)
icculus@10619
   369
#define CPU_haveSSE41() (CPU_CPUIDFeatures[2] & 0x00080000)
icculus@10619
   370
#define CPU_haveSSE42() (CPU_CPUIDFeatures[2] & 0x00100000)
icculus@10619
   371
#define CPU_haveAVX() (CPU_OSSavesYMM && (CPU_CPUIDFeatures[2] & 0x10000000))
slouken@8148
   372
slouken@9003
   373
static int
slouken@9003
   374
CPU_haveAVX2(void)
slouken@9003
   375
{
icculus@10619
   376
    if (CPU_OSSavesYMM && (CPU_CPUIDMaxFunction >= 7)) {
slouken@9003
   377
        int a, b, c, d;
icculus@10623
   378
        (void) a; (void) b; (void) c; (void) d;  /* compiler warnings... */
icculus@10619
   379
        cpuid(7, a, b, c, d);
icculus@10619
   380
        return (b & 0x00000020);
slouken@9003
   381
    }
slouken@9003
   382
    return 0;
slouken@9003
   383
}
slouken@9003
   384
slouken@3579
   385
static int SDL_CPUCount = 0;
slouken@3579
   386
slouken@3579
   387
int
slouken@5120
   388
SDL_GetCPUCount(void)
slouken@3579
   389
{
slouken@3579
   390
    if (!SDL_CPUCount) {
icculus@9278
   391
#ifndef SDL_CPUINFO_DISABLED
slouken@3606
   392
#if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
slouken@3586
   393
        if (SDL_CPUCount <= 0) {
slouken@3586
   394
            SDL_CPUCount = (int)sysconf(_SC_NPROCESSORS_ONLN);
slouken@3586
   395
        }
slouken@3586
   396
#endif
slouken@3579
   397
#ifdef HAVE_SYSCTLBYNAME
slouken@3586
   398
        if (SDL_CPUCount <= 0) {
slouken@3580
   399
            size_t size = sizeof(SDL_CPUCount);
slouken@3580
   400
            sysctlbyname("hw.ncpu", &SDL_CPUCount, &size, NULL, 0);
slouken@3580
   401
        }
slouken@3580
   402
#endif
slouken@5086
   403
#ifdef __WIN32__
slouken@3586
   404
        if (SDL_CPUCount <= 0) {
slouken@3580
   405
            SYSTEM_INFO info;
slouken@3580
   406
            GetSystemInfo(&info);
slouken@3580
   407
            SDL_CPUCount = info.dwNumberOfProcessors;
slouken@3580
   408
        }
slouken@3579
   409
#endif
icculus@9278
   410
#endif
slouken@3579
   411
        /* There has to be at least 1, right? :) */
slouken@3586
   412
        if (SDL_CPUCount <= 0) {
slouken@3579
   413
            SDL_CPUCount = 1;
slouken@3579
   414
        }
slouken@3579
   415
    }
slouken@3579
   416
    return SDL_CPUCount;
slouken@3579
   417
}
slouken@3579
   418
slouken@3579
   419
/* Oh, such a sweet sweet trick, just not very useful. :) */
slouken@4472
   420
static const char *
slouken@5120
   421
SDL_GetCPUType(void)
slouken@3579
   422
{
slouken@5115
   423
    static char SDL_CPUType[13];
slouken@3579
   424
slouken@3579
   425
    if (!SDL_CPUType[0]) {
slouken@3579
   426
        int i = 0;
slouken@3579
   427
icculus@10619
   428
        CPU_calcCPUIDFeatures();
icculus@10619
   429
        if (CPU_CPUIDMaxFunction > 0) {  /* do we have CPUID at all? */
icculus@9300
   430
            int a, b, c, d;
slouken@5115
   431
            cpuid(0x00000000, a, b, c, d);
icculus@9300
   432
            (void) a;
slouken@5115
   433
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   434
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   435
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
icculus@8642
   436
            SDL_CPUType[i++] = (char)(b & 0xff);
icculus@8642
   437
slouken@5115
   438
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   439
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   440
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
icculus@8642
   441
            SDL_CPUType[i++] = (char)(d & 0xff);
icculus@8642
   442
slouken@5115
   443
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   444
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   445
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
icculus@8642
   446
            SDL_CPUType[i++] = (char)(c & 0xff);
slouken@5115
   447
        }
slouken@5115
   448
        if (!SDL_CPUType[0]) {
slouken@5115
   449
            SDL_strlcpy(SDL_CPUType, "Unknown", sizeof(SDL_CPUType));
slouken@5115
   450
        }
slouken@5115
   451
    }
slouken@5115
   452
    return SDL_CPUType;
slouken@5115
   453
}
slouken@5115
   454
icculus@5981
   455
icculus@5981
   456
#ifdef TEST_MAIN  /* !!! FIXME: only used for test at the moment. */
slouken@5115
   457
static const char *
slouken@5120
   458
SDL_GetCPUName(void)
slouken@5115
   459
{
slouken@5115
   460
    static char SDL_CPUName[48];
slouken@5115
   461
slouken@5115
   462
    if (!SDL_CPUName[0]) {
slouken@5115
   463
        int i = 0;
slouken@5115
   464
        int a, b, c, d;
slouken@5115
   465
icculus@10619
   466
        CPU_calcCPUIDFeatures();
icculus@10619
   467
        if (CPU_CPUIDMaxFunction > 0) {  /* do we have CPUID at all? */
slouken@3580
   468
            cpuid(0x80000000, a, b, c, d);
slouken@3580
   469
            if (a >= 0x80000004) {
slouken@3580
   470
                cpuid(0x80000002, a, b, c, d);
slouken@5115
   471
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
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)(b & 0xff); b >>= 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)(c & 0xff); c >>= 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)(d & 0xff); d >>= 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@3580
   487
                cpuid(0x80000003, a, b, c, d);
slouken@5115
   488
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
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)(b & 0xff); b >>= 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)(c & 0xff); c >>= 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)(d & 0xff); d >>= 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@3580
   504
                cpuid(0x80000004, a, b, c, d);
slouken@5115
   505
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   506
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   507
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   508
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   509
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   510
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   511
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   512
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   513
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   514
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   515
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   516
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   517
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   518
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   519
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   520
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@3579
   521
            }
slouken@3579
   522
        }
slouken@5115
   523
        if (!SDL_CPUName[0]) {
slouken@5115
   524
            SDL_strlcpy(SDL_CPUName, "Unknown", sizeof(SDL_CPUName));
slouken@3579
   525
        }
slouken@3579
   526
    }
slouken@5115
   527
    return SDL_CPUName;
slouken@5115
   528
}
icculus@5981
   529
#endif
slouken@5115
   530
slouken@5120
   531
int
slouken@5120
   532
SDL_GetCPUCacheLineSize(void)
slouken@5115
   533
{
slouken@5115
   534
    const char *cpuType = SDL_GetCPUType();
icculus@9300
   535
    int a, b, c, d;
icculus@9300
   536
    (void) a; (void) b; (void) c; (void) d;
slouken@5115
   537
    if (SDL_strcmp(cpuType, "GenuineIntel") == 0) {
slouken@5115
   538
        cpuid(0x00000001, a, b, c, d);
slouken@5115
   539
        return (((b >> 8) & 0xff) * 8);
slouken@5115
   540
    } else if (SDL_strcmp(cpuType, "AuthenticAMD") == 0) {
slouken@5115
   541
        cpuid(0x80000005, a, b, c, d);
slouken@5115
   542
        return (c & 0xff);
slouken@5115
   543
    } else {
slouken@5115
   544
        /* Just make a guess here... */
slouken@5115
   545
        return SDL_CACHELINE_SIZE;
slouken@5115
   546
    }
slouken@3579
   547
}
slouken@3579
   548
slouken@739
   549
static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
slouken@739
   550
slouken@1895
   551
static Uint32
slouken@1895
   552
SDL_GetCPUFeatures(void)
slouken@739
   553
{
slouken@1895
   554
    if (SDL_CPUFeatures == 0xFFFFFFFF) {
icculus@10619
   555
        CPU_calcCPUIDFeatures();
slouken@1895
   556
        SDL_CPUFeatures = 0;
slouken@1895
   557
        if (CPU_haveRDTSC()) {
slouken@1895
   558
            SDL_CPUFeatures |= CPU_HAS_RDTSC;
slouken@1895
   559
        }
slouken@5389
   560
        if (CPU_haveAltiVec()) {
slouken@5389
   561
            SDL_CPUFeatures |= CPU_HAS_ALTIVEC;
slouken@5389
   562
        }
slouken@1895
   563
        if (CPU_haveMMX()) {
slouken@1895
   564
            SDL_CPUFeatures |= CPU_HAS_MMX;
slouken@1895
   565
        }
slouken@5389
   566
        if (CPU_have3DNow()) {
slouken@5389
   567
            SDL_CPUFeatures |= CPU_HAS_3DNOW;
slouken@5389
   568
        }
slouken@1895
   569
        if (CPU_haveSSE()) {
slouken@1895
   570
            SDL_CPUFeatures |= CPU_HAS_SSE;
slouken@1895
   571
        }
slouken@1895
   572
        if (CPU_haveSSE2()) {
slouken@1895
   573
            SDL_CPUFeatures |= CPU_HAS_SSE2;
slouken@1895
   574
        }
slouken@5259
   575
        if (CPU_haveSSE3()) {
slouken@5259
   576
            SDL_CPUFeatures |= CPU_HAS_SSE3;
slouken@5259
   577
        }
slouken@5263
   578
        if (CPU_haveSSE41()) {
slouken@5263
   579
            SDL_CPUFeatures |= CPU_HAS_SSE41;
slouken@5263
   580
        }
slouken@5263
   581
        if (CPU_haveSSE42()) {
slouken@5263
   582
            SDL_CPUFeatures |= CPU_HAS_SSE42;
slouken@1895
   583
        }
slouken@8148
   584
        if (CPU_haveAVX()) {
slouken@8148
   585
            SDL_CPUFeatures |= CPU_HAS_AVX;
slouken@8148
   586
        }
slouken@9003
   587
        if (CPU_haveAVX2()) {
slouken@9003
   588
            SDL_CPUFeatures |= CPU_HAS_AVX2;
slouken@9003
   589
        }
icculus@10620
   590
        if (CPU_haveNEON()) {
icculus@10620
   591
            SDL_CPUFeatures |= CPU_HAS_NEON;
icculus@10620
   592
        }
slouken@1895
   593
    }
slouken@1895
   594
    return SDL_CPUFeatures;
slouken@739
   595
}
slouken@739
   596
icculus@10619
   597
#define CPU_FEATURE_AVAILABLE(f) ((SDL_GetCPUFeatures() & f) ? SDL_TRUE : SDL_FALSE)
icculus@10619
   598
icculus@10619
   599
SDL_bool SDL_HasRDTSC(void)
slouken@745
   600
{
icculus@10619
   601
    return CPU_FEATURE_AVAILABLE(CPU_HAS_RDTSC);
slouken@745
   602
}
slouken@745
   603
slouken@1895
   604
SDL_bool
slouken@5389
   605
SDL_HasAltiVec(void)
slouken@5389
   606
{
icculus@10619
   607
    return CPU_FEATURE_AVAILABLE(CPU_HAS_ALTIVEC);
slouken@5389
   608
}
slouken@5389
   609
slouken@5389
   610
SDL_bool
slouken@1895
   611
SDL_HasMMX(void)
slouken@739
   612
{
icculus@10619
   613
    return CPU_FEATURE_AVAILABLE(CPU_HAS_MMX);
slouken@739
   614
}
slouken@739
   615
slouken@1895
   616
SDL_bool
slouken@5389
   617
SDL_Has3DNow(void)
slouken@5389
   618
{
icculus@10619
   619
    return CPU_FEATURE_AVAILABLE(CPU_HAS_3DNOW);
slouken@5389
   620
}
slouken@5389
   621
slouken@5389
   622
SDL_bool
slouken@1895
   623
SDL_HasSSE(void)
slouken@739
   624
{
icculus@10619
   625
    return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE);
slouken@739
   626
}
slouken@739
   627
slouken@1895
   628
SDL_bool
slouken@1895
   629
SDL_HasSSE2(void)
slouken@804
   630
{
icculus@10619
   631
    return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE2);
slouken@804
   632
}
slouken@804
   633
slouken@1895
   634
SDL_bool
slouken@5259
   635
SDL_HasSSE3(void)
slouken@778
   636
{
icculus@10619
   637
    return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE3);
slouken@5259
   638
}
slouken@5259
   639
slouken@5259
   640
SDL_bool
slouken@5263
   641
SDL_HasSSE41(void)
slouken@5259
   642
{
icculus@10619
   643
    return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE41);
slouken@5263
   644
}
slouken@5263
   645
slouken@5263
   646
SDL_bool
slouken@5263
   647
SDL_HasSSE42(void)
slouken@5263
   648
{
icculus@10619
   649
    return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE42);
slouken@778
   650
}
slouken@778
   651
slouken@8148
   652
SDL_bool
slouken@8148
   653
SDL_HasAVX(void)
slouken@8148
   654
{
icculus@10619
   655
    return CPU_FEATURE_AVAILABLE(CPU_HAS_AVX);
slouken@8148
   656
}
slouken@8148
   657
slouken@9003
   658
SDL_bool
slouken@9003
   659
SDL_HasAVX2(void)
slouken@9003
   660
{
icculus@10619
   661
    return CPU_FEATURE_AVAILABLE(CPU_HAS_AVX2);
slouken@9003
   662
}
slouken@9003
   663
icculus@10620
   664
SDL_bool
icculus@10620
   665
SDL_HasNEON(void)
icculus@10620
   666
{
icculus@10620
   667
    return CPU_FEATURE_AVAILABLE(CPU_HAS_NEON);
icculus@10620
   668
}
icculus@10620
   669
slouken@7821
   670
static int SDL_SystemRAM = 0;
slouken@7821
   671
slouken@7821
   672
int
slouken@7821
   673
SDL_GetSystemRAM(void)
slouken@7821
   674
{
slouken@7821
   675
    if (!SDL_SystemRAM) {
icculus@9278
   676
#ifndef SDL_CPUINFO_DISABLED
slouken@7821
   677
#if defined(HAVE_SYSCONF) && defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
slouken@7821
   678
        if (SDL_SystemRAM <= 0) {
slouken@7821
   679
            SDL_SystemRAM = (int)((Sint64)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE) / (1024*1024));
slouken@7821
   680
        }
slouken@7821
   681
#endif
slouken@7821
   682
#ifdef HAVE_SYSCTLBYNAME
slouken@7821
   683
        if (SDL_SystemRAM <= 0) {
icculus@9987
   684
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
slouken@7826
   685
#ifdef HW_REALMEM
slouken@7826
   686
            int mib[2] = {CTL_HW, HW_REALMEM};
slouken@7826
   687
#else
slouken@7826
   688
            /* might only report up to 2 GiB */
slouken@7826
   689
            int mib[2] = {CTL_HW, HW_PHYSMEM};
slouken@7826
   690
#endif /* HW_REALMEM */
slouken@7826
   691
#else
slouken@7821
   692
            int mib[2] = {CTL_HW, HW_MEMSIZE};
slouken@8240
   693
#endif /* __FreeBSD__ || __FreeBSD_kernel__ */
slouken@7823
   694
            Uint64 memsize = 0;
slouken@7821
   695
            size_t len = sizeof(memsize);
slouken@7821
   696
            
slouken@7821
   697
            if (sysctl(mib, 2, &memsize, &len, NULL, 0) == 0) {
slouken@7821
   698
                SDL_SystemRAM = (int)(memsize / (1024*1024));
slouken@7821
   699
            }
slouken@7821
   700
        }
slouken@7821
   701
#endif
slouken@7821
   702
#ifdef __WIN32__
slouken@7821
   703
        if (SDL_SystemRAM <= 0) {
slouken@7821
   704
            MEMORYSTATUSEX stat;
icculus@7889
   705
            stat.dwLength = sizeof(stat);
slouken@7821
   706
            if (GlobalMemoryStatusEx(&stat)) {
slouken@7821
   707
                SDL_SystemRAM = (int)(stat.ullTotalPhys / (1024 * 1024));
slouken@7821
   708
            }
slouken@7821
   709
        }
slouken@7821
   710
#endif
icculus@9278
   711
#endif
slouken@7821
   712
    }
slouken@7821
   713
    return SDL_SystemRAM;
slouken@7821
   714
}
slouken@7821
   715
slouken@7821
   716
slouken@739
   717
#ifdef TEST_MAIN
slouken@739
   718
slouken@739
   719
#include <stdio.h>
slouken@739
   720
slouken@1895
   721
int
slouken@1895
   722
main()
slouken@739
   723
{
slouken@3579
   724
    printf("CPU count: %d\n", SDL_GetCPUCount());
slouken@5115
   725
    printf("CPU type: %s\n", SDL_GetCPUType());
slouken@5115
   726
    printf("CPU name: %s\n", SDL_GetCPUName());
slouken@5115
   727
    printf("CacheLine size: %d\n", SDL_GetCPUCacheLineSize());
slouken@1895
   728
    printf("RDTSC: %d\n", SDL_HasRDTSC());
slouken@5389
   729
    printf("Altivec: %d\n", SDL_HasAltiVec());
slouken@1895
   730
    printf("MMX: %d\n", SDL_HasMMX());
slouken@5389
   731
    printf("3DNow: %d\n", SDL_Has3DNow());
slouken@1895
   732
    printf("SSE: %d\n", SDL_HasSSE());
slouken@1895
   733
    printf("SSE2: %d\n", SDL_HasSSE2());
slouken@5259
   734
    printf("SSE3: %d\n", SDL_HasSSE3());
slouken@5263
   735
    printf("SSE4.1: %d\n", SDL_HasSSE41());
slouken@5263
   736
    printf("SSE4.2: %d\n", SDL_HasSSE42());
slouken@8148
   737
    printf("AVX: %d\n", SDL_HasAVX());
slouken@9003
   738
    printf("AVX2: %d\n", SDL_HasAVX2());
icculus@10620
   739
    printf("NEON: %d\n", SDL_HasNEON());
slouken@7821
   740
    printf("RAM: %d MB\n", SDL_GetSystemRAM());
slouken@1895
   741
    return 0;
slouken@739
   742
}
slouken@739
   743
slouken@739
   744
#endif /* TEST_MAIN */
slouken@1895
   745
slouken@1895
   746
/* vi: set ts=4 sw=4 expandtab: */