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