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