src/cpuinfo/SDL_cpuinfo.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 08 Jun 2019 14:54:37 -0700
changeset 12794 a2dc7ba484fd
parent 12665 4743da9c3eea
child 13082 f77988be3607
permissions -rw-r--r--
Fixed bug 4557 - SDL_SIMDAlloc and *Free should be in the public interface

Martin Gerhardy

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