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