src/cpuinfo/SDL_cpuinfo.c
author Sylvain Becker <sylvain.becker@gmail.com>
Sun, 27 Oct 2019 14:52:09 +0100
changeset 13192 8974226b4075
parent 13187 20031279f4d4
child 13243 2ec6a2476551
permissions -rw-r--r--
Android: fix compilation error of SDL_cpuinfo.c on android-r13b (bug 4365)
slouken@739
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@12503
     3
  Copyright (C) 1997-2019 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@12665
    27
#if defined(__WIN32__) || defined(__WINRT__)
slouken@7828
    28
#include "../core/windows/SDL_windows.h"
slouken@7828
    29
#endif
slouken@11338
    30
#if defined(__OS2__)
slouken@11338
    31
#define INCL_DOS
slouken@11338
    32
#include <os2.h>
slouken@11338
    33
#ifndef QSV_NUMPROCESSORS
slouken@11338
    34
#define QSV_NUMPROCESSORS 26
slouken@11338
    35
#endif
slouken@11338
    36
#endif
slouken@7828
    37
slouken@739
    38
/* CPU feature detection for SDL */
slouken@739
    39
slouken@1361
    40
#include "SDL_cpuinfo.h"
icculus@11984
    41
#include "SDL_assert.h"
slouken@1361
    42
slouken@3586
    43
#ifdef HAVE_SYSCONF
slouken@3586
    44
#include <unistd.h>
slouken@3586
    45
#endif
slouken@3579
    46
#ifdef HAVE_SYSCTLBYNAME
slouken@3579
    47
#include <sys/types.h>
slouken@3579
    48
#include <sys/sysctl.h>
slouken@3579
    49
#endif
slouken@5389
    50
#if defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))
slouken@5389
    51
#include <sys/sysctl.h>         /* For AltiVec check */
slouken@6363
    52
#elif defined(__OpenBSD__) && defined(__powerpc__)
slouken@6363
    53
#include <sys/param.h>
slouken@6363
    54
#include <sys/sysctl.h> /* For AltiVec check */
slouken@6363
    55
#include <machine/cpu.h>
slouken@5389
    56
#elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
slouken@5389
    57
#include <signal.h>
slouken@5389
    58
#include <setjmp.h>
slouken@5389
    59
#endif
slouken@793
    60
icculus@11308
    61
#if defined(__QNXNTO__)
icculus@11308
    62
#include <sys/syspage.h>
icculus@11308
    63
#endif
icculus@11308
    64
icculus@10626
    65
#if (defined(__LINUX__) || defined(__ANDROID__)) && defined(__ARM_ARCH)
icculus@10627
    66
/*#include <asm/hwcap.h>*/
icculus@10627
    67
#ifndef AT_HWCAP
icculus@10627
    68
#define AT_HWCAP 16
icculus@10627
    69
#endif
sylvain@13192
    70
#ifndef AT_PLATFORM
sylvain@13192
    71
#define AT_PLATFORM 15
sylvain@13192
    72
#endif
sylvain@13192
    73
/* Prevent compilation error when including elf.h would also try to define AT_* as an enum */
sylvain@13192
    74
#ifndef AT_NULL
sylvain@13192
    75
#define AT_NULL 0
sylvain@13192
    76
#endif
icculus@10627
    77
#ifndef HWCAP_NEON
icculus@10627
    78
#define HWCAP_NEON (1 << 12)
icculus@10627
    79
#endif
icculus@10626
    80
#if defined HAVE_GETAUXVAL
icculus@10620
    81
#include <sys/auxv.h>
icculus@10626
    82
#else
icculus@10625
    83
#include <fcntl.h>
icculus@10625
    84
#endif
icculus@10620
    85
#endif
icculus@10620
    86
icculus@12439
    87
#if defined(__ANDROID__) && defined(__ARM_ARCH) && !defined(HAVE_GETAUXVAL)
icculus@12439
    88
#if __ARM_ARCH < 8
icculus@12439
    89
#include <cpu-features.h>
icculus@12439
    90
#endif
icculus@12439
    91
#endif
icculus@12439
    92
icculus@11985
    93
#define CPU_HAS_RDTSC   (1 << 0)
icculus@11985
    94
#define CPU_HAS_ALTIVEC (1 << 1)
icculus@11985
    95
#define CPU_HAS_MMX     (1 << 2)
icculus@11985
    96
#define CPU_HAS_3DNOW   (1 << 3)
icculus@11985
    97
#define CPU_HAS_SSE     (1 << 4)
icculus@11985
    98
#define CPU_HAS_SSE2    (1 << 5)
icculus@11985
    99
#define CPU_HAS_SSE3    (1 << 6)
icculus@11985
   100
#define CPU_HAS_SSE41   (1 << 7)
icculus@11985
   101
#define CPU_HAS_SSE42   (1 << 8)
icculus@11985
   102
#define CPU_HAS_AVX     (1 << 9)
icculus@11985
   103
#define CPU_HAS_AVX2    (1 << 10)
icculus@11985
   104
#define CPU_HAS_NEON    (1 << 11)
icculus@11986
   105
#define CPU_HAS_AVX512F (1 << 12)
bavison@13173
   106
#define CPU_HAS_ARM_SIMD (1 << 13)
slouken@739
   107
slouken@6363
   108
#if SDL_ALTIVEC_BLITTERS && HAVE_SETJMP && !__MACOSX__ && !__OpenBSD__
slouken@5389
   109
/* This is the brute force way of detecting instruction sets...
slouken@5389
   110
   the idea is borrowed from the libmpeg2 library - thanks!
slouken@5389
   111
 */
slouken@5389
   112
static jmp_buf jmpbuf;
slouken@5389
   113
static void
slouken@5389
   114
illegal_instruction(int sig)
slouken@5389
   115
{
slouken@5389
   116
    longjmp(jmpbuf, 1);
slouken@5389
   117
}
slouken@5389
   118
#endif /* HAVE_SETJMP */
slouken@793
   119
slouken@9003
   120
static int
slouken@1895
   121
CPU_haveCPUID(void)
slouken@745
   122
{
slouken@1895
   123
    int has_CPUID = 0;
icculus@10619
   124
slouken@1895
   125
/* *INDENT-OFF* */
icculus@9278
   126
#ifndef SDL_CPUINFO_DISABLED
slouken@745
   127
#if defined(__GNUC__) && defined(i386)
slouken@3579
   128
    __asm__ (
slouken@745
   129
"        pushfl                      # Get original EFLAGS             \n"
slouken@745
   130
"        popl    %%eax                                                 \n"
slouken@745
   131
"        movl    %%eax,%%ecx                                           \n"
slouken@745
   132
"        xorl    $0x200000,%%eax     # Flip ID bit in EFLAGS           \n"
slouken@745
   133
"        pushl   %%eax               # Save new EFLAGS value on stack  \n"
slouken@745
   134
"        popfl                       # Replace current EFLAGS value    \n"
slouken@745
   135
"        pushfl                      # Get new EFLAGS                  \n"
slouken@745
   136
"        popl    %%eax               # Store new EFLAGS in EAX         \n"
slouken@745
   137
"        xorl    %%ecx,%%eax         # Can not toggle ID bit,          \n"
slouken@745
   138
"        jz      1f                  # Processor=80486                 \n"
slouken@745
   139
"        movl    $1,%0               # We have CPUID support           \n"
slouken@745
   140
"1:                                                                    \n"
slouken@3579
   141
    : "=m" (has_CPUID)
slouken@3579
   142
    :
slouken@3579
   143
    : "%eax", "%ecx"
slouken@3579
   144
    );
slouken@881
   145
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@881
   146
/* Technically, if this is being compiled under __x86_64__ then it has 
slouken@8241
   147
   CPUid by definition.  But it's nice to be able to prove it.  :)      */
slouken@3579
   148
    __asm__ (
slouken@881
   149
"        pushfq                      # Get original EFLAGS             \n"
slouken@881
   150
"        popq    %%rax                                                 \n"
slouken@881
   151
"        movq    %%rax,%%rcx                                           \n"
slouken@881
   152
"        xorl    $0x200000,%%eax     # Flip ID bit in EFLAGS           \n"
slouken@881
   153
"        pushq   %%rax               # Save new EFLAGS value on stack  \n"
slouken@881
   154
"        popfq                       # Replace current EFLAGS value    \n"
slouken@881
   155
"        pushfq                      # Get new EFLAGS                  \n"
slouken@881
   156
"        popq    %%rax               # Store new EFLAGS in EAX         \n"
slouken@881
   157
"        xorl    %%ecx,%%eax         # Can not toggle ID bit,          \n"
slouken@881
   158
"        jz      1f                  # Processor=80486                 \n"
slouken@881
   159
"        movl    $1,%0               # We have CPUID support           \n"
slouken@881
   160
"1:                                                                    \n"
slouken@3579
   161
    : "=m" (has_CPUID)
slouken@3579
   162
    :
slouken@3579
   163
    : "%rax", "%rcx"
slouken@3579
   164
    );
slouken@1442
   165
#elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
slouken@3579
   166
    __asm {
slouken@745
   167
        pushfd                      ; Get original EFLAGS
slouken@745
   168
        pop     eax
slouken@745
   169
        mov     ecx, eax
slouken@745
   170
        xor     eax, 200000h        ; Flip ID bit in EFLAGS
slouken@745
   171
        push    eax                 ; Save new EFLAGS value on stack
slouken@745
   172
        popfd                       ; Replace current EFLAGS value
slouken@745
   173
        pushfd                      ; Get new EFLAGS
slouken@745
   174
        pop     eax                 ; Store new EFLAGS in EAX
slouken@745
   175
        xor     eax, ecx            ; Can not toggle ID bit,
slouken@745
   176
        jz      done                ; Processor=80486
slouken@745
   177
        mov     has_CPUID,1         ; We have CPUID support
slouken@745
   178
done:
slouken@3579
   179
    }
slouken@8241
   180
#elif defined(_MSC_VER) && defined(_M_X64)
slouken@8241
   181
    has_CPUID = 1;
slouken@1864
   182
#elif defined(__sun) && defined(__i386)
slouken@3579
   183
    __asm (
icculus@1229
   184
"       pushfl                 \n"
slouken@3584
   185
"       popl    %eax           \n"
slouken@3584
   186
"       movl    %eax,%ecx      \n"
slouken@3584
   187
"       xorl    $0x200000,%eax \n"
slouken@3584
   188
"       pushl   %eax           \n"
slouken@3584
   189
"       popfl                  \n"
slouken@3584
   190
"       pushfl                 \n"
slouken@3584
   191
"       popl    %eax           \n"
slouken@3584
   192
"       xorl    %ecx,%eax      \n"
slouken@3584
   193
"       jz      1f             \n"
slouken@3584
   194
"       movl    $1,-8(%ebp)    \n"
icculus@1229
   195
"1:                            \n"
slouken@3579
   196
    );
icculus@1229
   197
#elif defined(__sun) && defined(__amd64)
slouken@3579
   198
    __asm (
icculus@1229
   199
"       pushfq                 \n"
icculus@1229
   200
"       popq    %rax           \n"
icculus@1229
   201
"       movq    %rax,%rcx      \n"
icculus@1229
   202
"       xorl    $0x200000,%eax \n"
icculus@1229
   203
"       pushq   %rax           \n"
icculus@1229
   204
"       popfq                  \n"
icculus@1229
   205
"       pushfq                 \n"
icculus@1229
   206
"       popq    %rax           \n"
icculus@1229
   207
"       xorl    %ecx,%eax      \n"
icculus@1229
   208
"       jz      1f             \n"
icculus@1229
   209
"       movl    $1,-8(%rbp)    \n"
icculus@1229
   210
"1:                            \n"
slouken@3579
   211
    );
slouken@745
   212
#endif
icculus@9278
   213
#endif
slouken@1895
   214
/* *INDENT-ON* */
slouken@1895
   215
    return has_CPUID;
slouken@745
   216
}
slouken@745
   217
slouken@3587
   218
#if defined(__GNUC__) && defined(i386)
slouken@3580
   219
#define cpuid(func, a, b, c, d) \
slouken@3584
   220
    __asm__ __volatile__ ( \
slouken@3584
   221
"        pushl %%ebx        \n" \
slouken@9003
   222
"        xorl %%ecx,%%ecx   \n" \
slouken@3584
   223
"        cpuid              \n" \
slouken@3584
   224
"        movl %%ebx, %%esi  \n" \
slouken@3584
   225
"        popl %%ebx         \n" : \
slouken@3584
   226
            "=a" (a), "=S" (b), "=c" (c), "=d" (d) : "a" (func))
slouken@3587
   227
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@3587
   228
#define cpuid(func, a, b, c, d) \
slouken@3587
   229
    __asm__ __volatile__ ( \
slouken@3587
   230
"        pushq %%rbx        \n" \
slouken@9003
   231
"        xorq %%rcx,%%rcx   \n" \
slouken@3587
   232
"        cpuid              \n" \
slouken@3587
   233
"        movq %%rbx, %%rsi  \n" \
slouken@3587
   234
"        popq %%rbx         \n" : \
slouken@3587
   235
            "=a" (a), "=S" (b), "=c" (c), "=d" (d) : "a" (func))
slouken@3579
   236
#elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
slouken@3580
   237
#define cpuid(func, a, b, c, d) \
slouken@3580
   238
    __asm { \
slouken@3580
   239
        __asm mov eax, func \
slouken@9003
   240
        __asm xor ecx, ecx \
slouken@3580
   241
        __asm cpuid \
slouken@3580
   242
        __asm mov a, eax \
slouken@3580
   243
        __asm mov b, ebx \
slouken@3580
   244
        __asm mov c, ecx \
slouken@3580
   245
        __asm mov d, edx \
slouken@8241
   246
}
slouken@8241
   247
#elif defined(_MSC_VER) && defined(_M_X64)
slouken@8241
   248
#define cpuid(func, a, b, c, d) \
slouken@8241
   249
{ \
slouken@8241
   250
    int CPUInfo[4]; \
slouken@8241
   251
    __cpuid(CPUInfo, func); \
slouken@8241
   252
    a = CPUInfo[0]; \
slouken@8241
   253
    b = CPUInfo[1]; \
slouken@8241
   254
    c = CPUInfo[2]; \
slouken@8241
   255
    d = CPUInfo[3]; \
slouken@8241
   256
}
slouken@3579
   257
#else
slouken@3580
   258
#define cpuid(func, a, b, c, d) \
icculus@10926
   259
    do { a = b = c = d = 0; (void) a; (void) b; (void) c; (void) d; } while (0)
slouken@3579
   260
#endif
slouken@3579
   261
icculus@10619
   262
static int CPU_CPUIDFeatures[4];
icculus@10619
   263
static int CPU_CPUIDMaxFunction = 0;
icculus@10619
   264
static SDL_bool CPU_OSSavesYMM = SDL_FALSE;
icculus@11986
   265
static SDL_bool CPU_OSSavesZMM = SDL_FALSE;
icculus@10619
   266
icculus@10619
   267
static void
icculus@10619
   268
CPU_calcCPUIDFeatures(void)
slouken@745
   269
{
icculus@10619
   270
    static SDL_bool checked = SDL_FALSE;
icculus@10619
   271
    if (!checked) {
icculus@10619
   272
        checked = SDL_TRUE;
icculus@10619
   273
        if (CPU_haveCPUID()) {
icculus@10619
   274
            int a, b, c, d;
icculus@10619
   275
            cpuid(0, a, b, c, d);
icculus@10619
   276
            CPU_CPUIDMaxFunction = a;
icculus@10619
   277
            if (CPU_CPUIDMaxFunction >= 1) {
icculus@10619
   278
                cpuid(1, a, b, c, d);
icculus@10619
   279
                CPU_CPUIDFeatures[0] = a;
icculus@10619
   280
                CPU_CPUIDFeatures[1] = b;
icculus@10619
   281
                CPU_CPUIDFeatures[2] = c;
icculus@10619
   282
                CPU_CPUIDFeatures[3] = d;
slouken@3579
   283
icculus@10619
   284
                /* Check to make sure we can call xgetbv */
icculus@10619
   285
                if (c & 0x08000000) {
icculus@11986
   286
                    /* Call xgetbv to see if YMM (etc) register state is saved */
icculus@10619
   287
#if defined(__GNUC__) && (defined(i386) || defined(__x86_64__))
slouken@10931
   288
                    __asm__(".byte 0x0f, 0x01, 0xd0" : "=a" (a) : "c" (0) : "%edx");
icculus@10619
   289
#elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64)) && (_MSC_FULL_VER >= 160040219) /* VS2010 SP1 */
icculus@10619
   290
                    a = (int)_xgetbv(0);
icculus@10619
   291
#elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
icculus@10619
   292
                    __asm
icculus@10619
   293
                    {
icculus@10619
   294
                        xor ecx, ecx
icculus@10619
   295
                        _asm _emit 0x0f _asm _emit 0x01 _asm _emit 0xd0
icculus@10619
   296
                        mov a, eax
icculus@10619
   297
                    }
icculus@10619
   298
#endif
icculus@10619
   299
                    CPU_OSSavesYMM = ((a & 6) == 6) ? SDL_TRUE : SDL_FALSE;
icculus@11986
   300
                    CPU_OSSavesZMM = (CPU_OSSavesYMM && ((a & 0xe0) == 0xe0)) ? SDL_TRUE : SDL_FALSE;
icculus@10619
   301
                }
icculus@10619
   302
            }
icculus@10619
   303
        }
slouken@3579
   304
    }
slouken@745
   305
}
slouken@745
   306
slouken@9003
   307
static int
slouken@5389
   308
CPU_haveAltiVec(void)
slouken@5389
   309
{
slouken@5389
   310
    volatile int altivec = 0;
icculus@9278
   311
#ifndef SDL_CPUINFO_DISABLED
slouken@6363
   312
#if (defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))) || (defined(__OpenBSD__) && defined(__powerpc__))
slouken@6363
   313
#ifdef __OpenBSD__
slouken@6363
   314
    int selectors[2] = { CTL_MACHDEP, CPU_ALTIVEC };
slouken@6363
   315
#else
slouken@5389
   316
    int selectors[2] = { CTL_HW, HW_VECTORUNIT };
slouken@6363
   317
#endif
slouken@5389
   318
    int hasVectorUnit = 0;
slouken@5389
   319
    size_t length = sizeof(hasVectorUnit);
slouken@5389
   320
    int error = sysctl(selectors, 2, &hasVectorUnit, &length, NULL, 0);
slouken@5389
   321
    if (0 == error)
slouken@5389
   322
        altivec = (hasVectorUnit != 0);
slouken@5389
   323
#elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
slouken@5389
   324
    void (*handler) (int sig);
slouken@5389
   325
    handler = signal(SIGILL, illegal_instruction);
slouken@5389
   326
    if (setjmp(jmpbuf) == 0) {
slouken@5389
   327
        asm volatile ("mtspr 256, %0\n\t" "vand %%v0, %%v0, %%v0"::"r" (-1));
slouken@5389
   328
        altivec = 1;
slouken@5389
   329
    }
slouken@5389
   330
    signal(SIGILL, handler);
slouken@5389
   331
#endif
icculus@9278
   332
#endif
slouken@5389
   333
    return altivec;
slouken@5389
   334
}
slouken@5389
   335
icculus@13187
   336
#if !defined(__ARM_ARCH)
icculus@13187
   337
static SDL_bool CPU_haveARMSIMD(void) { return 0; }
bavison@13173
   338
icculus@13187
   339
#elif defined(__linux__)
bavison@13173
   340
#include <unistd.h>
bavison@13173
   341
#include <sys/types.h>
bavison@13173
   342
#include <sys/stat.h>
bavison@13173
   343
#include <fcntl.h>
bavison@13173
   344
#include <elf.h>
bavison@13173
   345
bavison@13173
   346
static SDL_bool
bavison@13173
   347
CPU_haveARMSIMD(void)
bavison@13173
   348
{
bavison@13173
   349
    int arm_simd = 0;
bavison@13173
   350
    int fd;
bavison@13173
   351
bavison@13173
   352
    fd = open("/proc/self/auxv", O_RDONLY);
bavison@13173
   353
    if (fd >= 0)
bavison@13173
   354
    {
bavison@13173
   355
        Elf32_auxv_t aux;
bavison@13173
   356
        while (read(fd, &aux, sizeof aux) == sizeof aux)
bavison@13173
   357
        {
bavison@13173
   358
            if (aux.a_type == AT_PLATFORM)
bavison@13173
   359
            {
bavison@13173
   360
                const char *plat = (const char *) aux.a_un.a_val;
bavison@13173
   361
                arm_simd = strncmp(plat, "v6l", 3) == 0 ||
bavison@13173
   362
                           strncmp(plat, "v7l", 3) == 0;
bavison@13173
   363
            }
bavison@13173
   364
        }
bavison@13173
   365
        close(fd);
bavison@13173
   366
    }
bavison@13173
   367
    return arm_simd;
bavison@13173
   368
}
bavison@13173
   369
bavison@13173
   370
#else
bavison@13173
   371
static SDL_bool
bavison@13173
   372
CPU_haveARMSIMD(void)
bavison@13173
   373
{
icculus@13187
   374
    #warning SDL_HasARMSIMD is not implemented for this ARM platform. Write me.
bavison@13173
   375
    return 0;
bavison@13173
   376
}
bavison@13173
   377
#endif
bavison@13173
   378
bavison@13173
   379
#if (defined(__LINUX__) || defined(__ANDROID__)) && defined(__ARM_ARCH) && !defined(HAVE_GETAUXVAL)
icculus@10624
   380
static int
icculus@10624
   381
readProcAuxvForNeon(void)
icculus@10624
   382
{
icculus@10624
   383
    int neon = 0;
icculus@10624
   384
    int kv[2];
icculus@10624
   385
    const int fd = open("/proc/self/auxv", O_RDONLY);
icculus@10626
   386
    if (fd != -1) {
icculus@10626
   387
        while (read(fd, kv, sizeof (kv)) == sizeof (kv)) {
icculus@10626
   388
            if (kv[0] == AT_HWCAP) {
icculus@10626
   389
                neon = ((kv[1] & HWCAP_NEON) == HWCAP_NEON);
icculus@10626
   390
                break;
icculus@10626
   391
            }
icculus@10626
   392
        }
icculus@10626
   393
        close(fd);
icculus@10624
   394
    }
icculus@10624
   395
    return neon;
icculus@10624
   396
}
icculus@10624
   397
#endif
icculus@10624
   398
icculus@10624
   399
slouken@9003
   400
static int
icculus@10620
   401
CPU_haveNEON(void)
icculus@10620
   402
{
icculus@10620
   403
/* The way you detect NEON is a privileged instruction on ARM, so you have
icculus@10620
   404
   query the OS kernel in a platform-specific way. :/ */
sylvain@12450
   405
#if defined(SDL_CPUINFO_DISABLED)
sylvain@12450
   406
   return 0; /* disabled */
sylvain@12450
   407
#elif (defined(__WINDOWS__) || defined(__WINRT__)) && (defined(_M_ARM) || defined(_M_ARM64))
sylvain@12450
   408
/* Visual Studio, for ARM, doesn't define __ARM_ARCH. Handle this first. */
sylvain@12450
   409
/* Seems to have been removed */
sylvain@12450
   410
#  if !defined(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE)
sylvain@12450
   411
#    define PF_ARM_NEON_INSTRUCTIONS_AVAILABLE 19
sylvain@12450
   412
#  endif
sylvain@12450
   413
/* All WinRT ARM devices are required to support NEON, but just in case. */
sylvain@12450
   414
    return IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE) != 0;
sylvain@12450
   415
#elif !defined(__ARM_ARCH)
sylvain@12450
   416
    return 0;  /* not an ARM CPU at all. */
icculus@10624
   417
#elif __ARM_ARCH >= 8
icculus@10639
   418
    return 1;  /* ARMv8 always has non-optional NEON support. */
icculus@10639
   419
#elif defined(__APPLE__) && (__ARM_ARCH >= 7)
icculus@10639
   420
    /* (note that sysctlbyname("hw.optional.neon") doesn't work!) */
icculus@10639
   421
    return 1;  /* all Apple ARMv7 chips and later have NEON. */
icculus@10624
   422
#elif defined(__APPLE__)
slouken@11700
   423
    return 0;  /* assume anything else from Apple doesn't have NEON. */
icculus@11308
   424
#elif defined(__QNXNTO__)
icculus@11308
   425
    return SYSPAGE_ENTRY(cpuinfo)->flags & ARM_CPU_FLAG_NEON;
icculus@10625
   426
#elif (defined(__LINUX__) || defined(__ANDROID__)) && defined(HAVE_GETAUXVAL)
icculus@10628
   427
    return ((getauxval(AT_HWCAP) & HWCAP_NEON) == HWCAP_NEON);
icculus@12439
   428
#elif defined(__LINUX__)
icculus@12439
   429
    return readProcAuxvForNeon();
icculus@12439
   430
#elif defined(__ANDROID__)
icculus@12439
   431
    /* Use NDK cpufeatures to read either /proc/self/auxv or /proc/cpuinfo */
icculus@12439
   432
    {
icculus@12439
   433
        AndroidCpuFamily cpu_family = android_getCpuFamily();
icculus@12439
   434
        if (cpu_family == ANDROID_CPU_FAMILY_ARM) {
icculus@12439
   435
            uint64_t cpu_features = android_getCpuFeatures();
icculus@12439
   436
            if ((cpu_features & ANDROID_CPU_ARM_FEATURE_NEON) != 0) {
icculus@12439
   437
                return 1;
icculus@12439
   438
            }
icculus@12439
   439
        }
icculus@12439
   440
        return 0;
icculus@12439
   441
    }
icculus@10624
   442
#else
icculus@10624
   443
#warning SDL_HasNEON is not implemented for this ARM platform. Write me.
icculus@10625
   444
    return 0;
icculus@10620
   445
#endif
icculus@10620
   446
}
icculus@10620
   447
icculus@10620
   448
static int
slouken@5389
   449
CPU_have3DNow(void)
slouken@5389
   450
{
icculus@10619
   451
    if (CPU_CPUIDMaxFunction > 0) {  /* that is, do we have CPUID at all? */
slouken@5389
   452
        int a, b, c, d;
slouken@5389
   453
        cpuid(0x80000000, a, b, c, d);
slouken@5389
   454
        if (a >= 0x80000001) {
slouken@5389
   455
            cpuid(0x80000001, a, b, c, d);
slouken@5389
   456
            return (d & 0x80000000);
slouken@5389
   457
        }
slouken@5389
   458
    }
slouken@5389
   459
    return 0;
slouken@5389
   460
}
slouken@5389
   461
icculus@10619
   462
#define CPU_haveRDTSC() (CPU_CPUIDFeatures[3] & 0x00000010)
icculus@10619
   463
#define CPU_haveMMX() (CPU_CPUIDFeatures[3] & 0x00800000)
icculus@10619
   464
#define CPU_haveSSE() (CPU_CPUIDFeatures[3] & 0x02000000)
icculus@10619
   465
#define CPU_haveSSE2() (CPU_CPUIDFeatures[3] & 0x04000000)
icculus@10619
   466
#define CPU_haveSSE3() (CPU_CPUIDFeatures[2] & 0x00000001)
icculus@10619
   467
#define CPU_haveSSE41() (CPU_CPUIDFeatures[2] & 0x00080000)
icculus@10619
   468
#define CPU_haveSSE42() (CPU_CPUIDFeatures[2] & 0x00100000)
icculus@10619
   469
#define CPU_haveAVX() (CPU_OSSavesYMM && (CPU_CPUIDFeatures[2] & 0x10000000))
slouken@8148
   470
slouken@9003
   471
static int
slouken@9003
   472
CPU_haveAVX2(void)
slouken@9003
   473
{
icculus@10619
   474
    if (CPU_OSSavesYMM && (CPU_CPUIDMaxFunction >= 7)) {
slouken@9003
   475
        int a, b, c, d;
icculus@10623
   476
        (void) a; (void) b; (void) c; (void) d;  /* compiler warnings... */
icculus@10619
   477
        cpuid(7, a, b, c, d);
icculus@10619
   478
        return (b & 0x00000020);
slouken@9003
   479
    }
slouken@9003
   480
    return 0;
slouken@9003
   481
}
slouken@9003
   482
icculus@11986
   483
static int
icculus@11986
   484
CPU_haveAVX512F(void)
icculus@11986
   485
{
icculus@11986
   486
    if (CPU_OSSavesZMM && (CPU_CPUIDMaxFunction >= 7)) {
icculus@11986
   487
        int a, b, c, d;
icculus@11986
   488
        (void) a; (void) b; (void) c; (void) d;  /* compiler warnings... */
icculus@11986
   489
        cpuid(7, a, b, c, d);
icculus@11986
   490
        return (b & 0x00010000);
icculus@11986
   491
    }
icculus@11986
   492
    return 0;
icculus@11986
   493
}
icculus@11986
   494
slouken@3579
   495
static int SDL_CPUCount = 0;
slouken@3579
   496
slouken@3579
   497
int
slouken@5120
   498
SDL_GetCPUCount(void)
slouken@3579
   499
{
slouken@3579
   500
    if (!SDL_CPUCount) {
icculus@9278
   501
#ifndef SDL_CPUINFO_DISABLED
slouken@3606
   502
#if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
slouken@3586
   503
        if (SDL_CPUCount <= 0) {
slouken@3586
   504
            SDL_CPUCount = (int)sysconf(_SC_NPROCESSORS_ONLN);
slouken@3586
   505
        }
slouken@3586
   506
#endif
slouken@3579
   507
#ifdef HAVE_SYSCTLBYNAME
slouken@3586
   508
        if (SDL_CPUCount <= 0) {
slouken@3580
   509
            size_t size = sizeof(SDL_CPUCount);
slouken@3580
   510
            sysctlbyname("hw.ncpu", &SDL_CPUCount, &size, NULL, 0);
slouken@3580
   511
        }
slouken@3580
   512
#endif
slouken@5086
   513
#ifdef __WIN32__
slouken@3586
   514
        if (SDL_CPUCount <= 0) {
slouken@3580
   515
            SYSTEM_INFO info;
slouken@3580
   516
            GetSystemInfo(&info);
slouken@3580
   517
            SDL_CPUCount = info.dwNumberOfProcessors;
slouken@3580
   518
        }
slouken@3579
   519
#endif
slouken@11338
   520
#ifdef __OS2__
slouken@11338
   521
        if (SDL_CPUCount <= 0) {
slouken@11338
   522
            DosQuerySysInfo(QSV_NUMPROCESSORS, QSV_NUMPROCESSORS,
slouken@11338
   523
                            &SDL_CPUCount, sizeof(SDL_CPUCount) );
slouken@11338
   524
        }
slouken@11338
   525
#endif
icculus@9278
   526
#endif
slouken@3579
   527
        /* There has to be at least 1, right? :) */
slouken@3586
   528
        if (SDL_CPUCount <= 0) {
slouken@3579
   529
            SDL_CPUCount = 1;
slouken@3579
   530
        }
slouken@3579
   531
    }
slouken@3579
   532
    return SDL_CPUCount;
slouken@3579
   533
}
slouken@3579
   534
slouken@3579
   535
/* Oh, such a sweet sweet trick, just not very useful. :) */
slouken@4472
   536
static const char *
slouken@5120
   537
SDL_GetCPUType(void)
slouken@3579
   538
{
slouken@5115
   539
    static char SDL_CPUType[13];
slouken@3579
   540
slouken@3579
   541
    if (!SDL_CPUType[0]) {
slouken@3579
   542
        int i = 0;
slouken@3579
   543
icculus@10619
   544
        CPU_calcCPUIDFeatures();
icculus@10619
   545
        if (CPU_CPUIDMaxFunction > 0) {  /* do we have CPUID at all? */
icculus@9300
   546
            int a, b, c, d;
slouken@5115
   547
            cpuid(0x00000000, a, b, c, d);
icculus@9300
   548
            (void) a;
slouken@5115
   549
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   550
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   551
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
icculus@8642
   552
            SDL_CPUType[i++] = (char)(b & 0xff);
icculus@8642
   553
slouken@5115
   554
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   555
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   556
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
icculus@8642
   557
            SDL_CPUType[i++] = (char)(d & 0xff);
icculus@8642
   558
slouken@5115
   559
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   560
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   561
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
icculus@8642
   562
            SDL_CPUType[i++] = (char)(c & 0xff);
slouken@5115
   563
        }
slouken@5115
   564
        if (!SDL_CPUType[0]) {
slouken@5115
   565
            SDL_strlcpy(SDL_CPUType, "Unknown", sizeof(SDL_CPUType));
slouken@5115
   566
        }
slouken@5115
   567
    }
slouken@5115
   568
    return SDL_CPUType;
slouken@5115
   569
}
slouken@5115
   570
icculus@5981
   571
icculus@5981
   572
#ifdef TEST_MAIN  /* !!! FIXME: only used for test at the moment. */
slouken@5115
   573
static const char *
slouken@5120
   574
SDL_GetCPUName(void)
slouken@5115
   575
{
slouken@5115
   576
    static char SDL_CPUName[48];
slouken@5115
   577
slouken@5115
   578
    if (!SDL_CPUName[0]) {
slouken@5115
   579
        int i = 0;
slouken@5115
   580
        int a, b, c, d;
slouken@5115
   581
icculus@10619
   582
        CPU_calcCPUIDFeatures();
icculus@10619
   583
        if (CPU_CPUIDMaxFunction > 0) {  /* do we have CPUID at all? */
slouken@3580
   584
            cpuid(0x80000000, a, b, c, d);
slouken@3580
   585
            if (a >= 0x80000004) {
slouken@3580
   586
                cpuid(0x80000002, a, b, c, d);
slouken@5115
   587
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   588
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   589
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   590
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   591
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   592
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   593
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   594
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   595
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   596
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   597
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   598
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   599
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   600
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   601
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   602
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@3580
   603
                cpuid(0x80000003, a, b, c, d);
slouken@5115
   604
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   605
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   606
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   607
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   608
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   609
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   610
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   611
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   612
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   613
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   614
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   615
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   616
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   617
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   618
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   619
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@3580
   620
                cpuid(0x80000004, a, b, c, d);
slouken@5115
   621
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   622
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   623
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   624
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   625
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   626
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   627
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   628
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   629
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   630
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   631
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   632
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   633
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   634
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   635
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   636
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@3579
   637
            }
slouken@3579
   638
        }
slouken@5115
   639
        if (!SDL_CPUName[0]) {
slouken@5115
   640
            SDL_strlcpy(SDL_CPUName, "Unknown", sizeof(SDL_CPUName));
slouken@3579
   641
        }
slouken@3579
   642
    }
slouken@5115
   643
    return SDL_CPUName;
slouken@5115
   644
}
icculus@5981
   645
#endif
slouken@5115
   646
slouken@5120
   647
int
slouken@5120
   648
SDL_GetCPUCacheLineSize(void)
slouken@5115
   649
{
slouken@5115
   650
    const char *cpuType = SDL_GetCPUType();
icculus@9300
   651
    int a, b, c, d;
icculus@9300
   652
    (void) a; (void) b; (void) c; (void) d;
slouken@5115
   653
    if (SDL_strcmp(cpuType, "GenuineIntel") == 0) {
slouken@5115
   654
        cpuid(0x00000001, a, b, c, d);
slouken@5115
   655
        return (((b >> 8) & 0xff) * 8);
fanjinke@13082
   656
    } else if (SDL_strcmp(cpuType, "AuthenticAMD") == 0 || SDL_strcmp(cpuType, "HygonGenuine") == 0) {
slouken@5115
   657
        cpuid(0x80000005, a, b, c, d);
slouken@5115
   658
        return (c & 0xff);
slouken@5115
   659
    } else {
slouken@5115
   660
        /* Just make a guess here... */
slouken@5115
   661
        return SDL_CACHELINE_SIZE;
slouken@5115
   662
    }
slouken@3579
   663
}
slouken@3579
   664
slouken@739
   665
static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
icculus@11984
   666
static Uint32 SDL_SIMDAlignment = 0xFFFFFFFF;
slouken@739
   667
slouken@1895
   668
static Uint32
slouken@1895
   669
SDL_GetCPUFeatures(void)
slouken@739
   670
{
slouken@1895
   671
    if (SDL_CPUFeatures == 0xFFFFFFFF) {
icculus@10619
   672
        CPU_calcCPUIDFeatures();
slouken@1895
   673
        SDL_CPUFeatures = 0;
icculus@13151
   674
        SDL_SIMDAlignment = sizeof(void *);  /* a good safe base value */
slouken@1895
   675
        if (CPU_haveRDTSC()) {
slouken@1895
   676
            SDL_CPUFeatures |= CPU_HAS_RDTSC;
slouken@1895
   677
        }
slouken@5389
   678
        if (CPU_haveAltiVec()) {
slouken@5389
   679
            SDL_CPUFeatures |= CPU_HAS_ALTIVEC;
icculus@11984
   680
            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
slouken@5389
   681
        }
slouken@1895
   682
        if (CPU_haveMMX()) {
slouken@1895
   683
            SDL_CPUFeatures |= CPU_HAS_MMX;
icculus@11984
   684
            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 8);
slouken@1895
   685
        }
slouken@5389
   686
        if (CPU_have3DNow()) {
slouken@5389
   687
            SDL_CPUFeatures |= CPU_HAS_3DNOW;
icculus@11984
   688
            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 8);
slouken@5389
   689
        }
slouken@1895
   690
        if (CPU_haveSSE()) {
slouken@1895
   691
            SDL_CPUFeatures |= CPU_HAS_SSE;
icculus@11984
   692
            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
slouken@1895
   693
        }
slouken@1895
   694
        if (CPU_haveSSE2()) {
slouken@1895
   695
            SDL_CPUFeatures |= CPU_HAS_SSE2;
icculus@11984
   696
            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
slouken@1895
   697
        }
slouken@5259
   698
        if (CPU_haveSSE3()) {
slouken@5259
   699
            SDL_CPUFeatures |= CPU_HAS_SSE3;
icculus@11984
   700
            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
slouken@5259
   701
        }
slouken@5263
   702
        if (CPU_haveSSE41()) {
slouken@5263
   703
            SDL_CPUFeatures |= CPU_HAS_SSE41;
icculus@11984
   704
            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
slouken@5263
   705
        }
slouken@5263
   706
        if (CPU_haveSSE42()) {
slouken@5263
   707
            SDL_CPUFeatures |= CPU_HAS_SSE42;
icculus@11984
   708
            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
slouken@1895
   709
        }
slouken@8148
   710
        if (CPU_haveAVX()) {
slouken@8148
   711
            SDL_CPUFeatures |= CPU_HAS_AVX;
icculus@11984
   712
            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 32);
slouken@8148
   713
        }
slouken@9003
   714
        if (CPU_haveAVX2()) {
slouken@9003
   715
            SDL_CPUFeatures |= CPU_HAS_AVX2;
icculus@11984
   716
            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 32);
slouken@9003
   717
        }
icculus@11986
   718
        if (CPU_haveAVX512F()) {
icculus@11986
   719
            SDL_CPUFeatures |= CPU_HAS_AVX512F;
icculus@11986
   720
            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 64);
icculus@11986
   721
        }
bavison@13173
   722
        if (CPU_haveARMSIMD()) {
bavison@13173
   723
            SDL_CPUFeatures |= CPU_HAS_ARM_SIMD;
bavison@13173
   724
            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
bavison@13173
   725
        }
icculus@10620
   726
        if (CPU_haveNEON()) {
icculus@10620
   727
            SDL_CPUFeatures |= CPU_HAS_NEON;
icculus@11984
   728
            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
icculus@10620
   729
        }
slouken@1895
   730
    }
slouken@1895
   731
    return SDL_CPUFeatures;
slouken@739
   732
}
slouken@739
   733
icculus@10619
   734
#define CPU_FEATURE_AVAILABLE(f) ((SDL_GetCPUFeatures() & f) ? SDL_TRUE : SDL_FALSE)
icculus@10619
   735
icculus@10619
   736
SDL_bool SDL_HasRDTSC(void)
slouken@745
   737
{
icculus@10619
   738
    return CPU_FEATURE_AVAILABLE(CPU_HAS_RDTSC);
slouken@745
   739
}
slouken@745
   740
slouken@1895
   741
SDL_bool
slouken@5389
   742
SDL_HasAltiVec(void)
slouken@5389
   743
{
icculus@10619
   744
    return CPU_FEATURE_AVAILABLE(CPU_HAS_ALTIVEC);
slouken@5389
   745
}
slouken@5389
   746
slouken@5389
   747
SDL_bool
slouken@1895
   748
SDL_HasMMX(void)
slouken@739
   749
{
icculus@10619
   750
    return CPU_FEATURE_AVAILABLE(CPU_HAS_MMX);
slouken@739
   751
}
slouken@739
   752
slouken@1895
   753
SDL_bool
slouken@5389
   754
SDL_Has3DNow(void)
slouken@5389
   755
{
icculus@10619
   756
    return CPU_FEATURE_AVAILABLE(CPU_HAS_3DNOW);
slouken@5389
   757
}
slouken@5389
   758
slouken@5389
   759
SDL_bool
slouken@1895
   760
SDL_HasSSE(void)
slouken@739
   761
{
icculus@10619
   762
    return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE);
slouken@739
   763
}
slouken@739
   764
slouken@1895
   765
SDL_bool
slouken@1895
   766
SDL_HasSSE2(void)
slouken@804
   767
{
icculus@10619
   768
    return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE2);
slouken@804
   769
}
slouken@804
   770
slouken@1895
   771
SDL_bool
slouken@5259
   772
SDL_HasSSE3(void)
slouken@778
   773
{
icculus@10619
   774
    return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE3);
slouken@5259
   775
}
slouken@5259
   776
slouken@5259
   777
SDL_bool
slouken@5263
   778
SDL_HasSSE41(void)
slouken@5259
   779
{
icculus@10619
   780
    return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE41);
slouken@5263
   781
}
slouken@5263
   782
slouken@5263
   783
SDL_bool
slouken@5263
   784
SDL_HasSSE42(void)
slouken@5263
   785
{
icculus@10619
   786
    return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE42);
slouken@778
   787
}
slouken@778
   788
slouken@8148
   789
SDL_bool
slouken@8148
   790
SDL_HasAVX(void)
slouken@8148
   791
{
icculus@10619
   792
    return CPU_FEATURE_AVAILABLE(CPU_HAS_AVX);
slouken@8148
   793
}
slouken@8148
   794
slouken@9003
   795
SDL_bool
slouken@9003
   796
SDL_HasAVX2(void)
slouken@9003
   797
{
icculus@10619
   798
    return CPU_FEATURE_AVAILABLE(CPU_HAS_AVX2);
slouken@9003
   799
}
slouken@9003
   800
icculus@10620
   801
SDL_bool
icculus@11986
   802
SDL_HasAVX512F(void)
icculus@11986
   803
{
icculus@11986
   804
    return CPU_FEATURE_AVAILABLE(CPU_HAS_AVX512F);
icculus@11986
   805
}
icculus@11986
   806
icculus@11986
   807
SDL_bool
bavison@13173
   808
SDL_HasARMSIMD(void)
bavison@13173
   809
{
bavison@13173
   810
    return CPU_FEATURE_AVAILABLE(CPU_HAS_ARM_SIMD);
bavison@13173
   811
}
bavison@13173
   812
bavison@13173
   813
SDL_bool
icculus@10620
   814
SDL_HasNEON(void)
icculus@10620
   815
{
icculus@10620
   816
    return CPU_FEATURE_AVAILABLE(CPU_HAS_NEON);
icculus@10620
   817
}
icculus@10620
   818
slouken@7821
   819
static int SDL_SystemRAM = 0;
slouken@7821
   820
slouken@7821
   821
int
slouken@7821
   822
SDL_GetSystemRAM(void)
slouken@7821
   823
{
slouken@7821
   824
    if (!SDL_SystemRAM) {
icculus@9278
   825
#ifndef SDL_CPUINFO_DISABLED
slouken@7821
   826
#if defined(HAVE_SYSCONF) && defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
slouken@7821
   827
        if (SDL_SystemRAM <= 0) {
slouken@7821
   828
            SDL_SystemRAM = (int)((Sint64)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE) / (1024*1024));
slouken@7821
   829
        }
slouken@7821
   830
#endif
slouken@7821
   831
#ifdef HAVE_SYSCTLBYNAME
slouken@7821
   832
        if (SDL_SystemRAM <= 0) {
icculus@9987
   833
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
slouken@7826
   834
#ifdef HW_REALMEM
slouken@7826
   835
            int mib[2] = {CTL_HW, HW_REALMEM};
slouken@7826
   836
#else
slouken@7826
   837
            /* might only report up to 2 GiB */
slouken@7826
   838
            int mib[2] = {CTL_HW, HW_PHYSMEM};
slouken@7826
   839
#endif /* HW_REALMEM */
slouken@7826
   840
#else
slouken@7821
   841
            int mib[2] = {CTL_HW, HW_MEMSIZE};
slouken@8240
   842
#endif /* __FreeBSD__ || __FreeBSD_kernel__ */
slouken@7823
   843
            Uint64 memsize = 0;
slouken@7821
   844
            size_t len = sizeof(memsize);
slouken@7821
   845
            
slouken@7821
   846
            if (sysctl(mib, 2, &memsize, &len, NULL, 0) == 0) {
slouken@7821
   847
                SDL_SystemRAM = (int)(memsize / (1024*1024));
slouken@7821
   848
            }
slouken@7821
   849
        }
slouken@7821
   850
#endif
slouken@7821
   851
#ifdef __WIN32__
slouken@7821
   852
        if (SDL_SystemRAM <= 0) {
slouken@7821
   853
            MEMORYSTATUSEX stat;
icculus@7889
   854
            stat.dwLength = sizeof(stat);
slouken@7821
   855
            if (GlobalMemoryStatusEx(&stat)) {
slouken@7821
   856
                SDL_SystemRAM = (int)(stat.ullTotalPhys / (1024 * 1024));
slouken@7821
   857
            }
slouken@7821
   858
        }
slouken@7821
   859
#endif
slouken@11338
   860
#ifdef __OS2__
slouken@11338
   861
        if (SDL_SystemRAM <= 0) {
slouken@11338
   862
            Uint32 sysram = 0;
slouken@11338
   863
            DosQuerySysInfo(QSV_TOTPHYSMEM, QSV_TOTPHYSMEM, &sysram, 4);
slouken@11338
   864
            SDL_SystemRAM = (int) (sysram / 0x100000U);
slouken@11338
   865
        }
slouken@11338
   866
#endif
icculus@9278
   867
#endif
slouken@7821
   868
    }
slouken@7821
   869
    return SDL_SystemRAM;
slouken@7821
   870
}
slouken@7821
   871
slouken@7821
   872
icculus@11984
   873
size_t
icculus@11984
   874
SDL_SIMDGetAlignment(void)
icculus@11984
   875
{
icculus@11984
   876
    if (SDL_SIMDAlignment == 0xFFFFFFFF) {
icculus@11984
   877
        SDL_GetCPUFeatures();  /* make sure this has been calculated */
icculus@11984
   878
    }
icculus@11984
   879
    SDL_assert(SDL_SIMDAlignment != 0);
icculus@11984
   880
    return SDL_SIMDAlignment;
icculus@11984
   881
}
icculus@11984
   882
icculus@11984
   883
void *
icculus@11984
   884
SDL_SIMDAlloc(const size_t len)
icculus@11984
   885
{
icculus@11984
   886
    const size_t alignment = SDL_SIMDGetAlignment();
icculus@11984
   887
    const size_t padding = alignment - (len % alignment);
icculus@11984
   888
    const size_t padded = (padding != alignment) ? (len + padding) : len;
icculus@11984
   889
    Uint8 *retval = NULL;
icculus@11984
   890
    Uint8 *ptr = (Uint8 *) SDL_malloc(padded + alignment + sizeof (void *));
icculus@11984
   891
    if (ptr) {
icculus@11984
   892
        /* store the actual malloc pointer right before our aligned pointer. */
icculus@11984
   893
        retval = ptr + sizeof (void *);
icculus@11984
   894
        retval += alignment - (((size_t) retval) % alignment);
icculus@11984
   895
        *(((void **) retval) - 1) = ptr;
icculus@11984
   896
    }
icculus@11984
   897
    return retval;
icculus@11984
   898
}
icculus@11984
   899
icculus@11984
   900
void
icculus@11984
   901
SDL_SIMDFree(void *ptr)
icculus@11984
   902
{
icculus@11984
   903
    if (ptr) {
icculus@11984
   904
        void **realptr = (void **) ptr;
icculus@11984
   905
        realptr--;
icculus@11984
   906
        SDL_free(*(((void **) ptr) - 1));
icculus@11984
   907
    }
icculus@11984
   908
}
icculus@11984
   909
icculus@11984
   910
slouken@739
   911
#ifdef TEST_MAIN
slouken@739
   912
slouken@739
   913
#include <stdio.h>
slouken@739
   914
slouken@1895
   915
int
slouken@1895
   916
main()
slouken@739
   917
{
slouken@3579
   918
    printf("CPU count: %d\n", SDL_GetCPUCount());
slouken@5115
   919
    printf("CPU type: %s\n", SDL_GetCPUType());
slouken@5115
   920
    printf("CPU name: %s\n", SDL_GetCPUName());
slouken@5115
   921
    printf("CacheLine size: %d\n", SDL_GetCPUCacheLineSize());
slouken@1895
   922
    printf("RDTSC: %d\n", SDL_HasRDTSC());
slouken@5389
   923
    printf("Altivec: %d\n", SDL_HasAltiVec());
slouken@1895
   924
    printf("MMX: %d\n", SDL_HasMMX());
slouken@5389
   925
    printf("3DNow: %d\n", SDL_Has3DNow());
slouken@1895
   926
    printf("SSE: %d\n", SDL_HasSSE());
slouken@1895
   927
    printf("SSE2: %d\n", SDL_HasSSE2());
slouken@5259
   928
    printf("SSE3: %d\n", SDL_HasSSE3());
slouken@5263
   929
    printf("SSE4.1: %d\n", SDL_HasSSE41());
slouken@5263
   930
    printf("SSE4.2: %d\n", SDL_HasSSE42());
slouken@8148
   931
    printf("AVX: %d\n", SDL_HasAVX());
slouken@9003
   932
    printf("AVX2: %d\n", SDL_HasAVX2());
icculus@11986
   933
    printf("AVX-512F: %d\n", SDL_HasAVX512F());
bavison@13173
   934
    printf("ARM SIMD: %d\n", SDL_HasARMSIMD());
icculus@10620
   935
    printf("NEON: %d\n", SDL_HasNEON());
slouken@7821
   936
    printf("RAM: %d MB\n", SDL_GetSystemRAM());
slouken@1895
   937
    return 0;
slouken@739
   938
}
slouken@739
   939
slouken@739
   940
#endif /* TEST_MAIN */
slouken@1895
   941
slouken@1895
   942
/* vi: set ts=4 sw=4 expandtab: */