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