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