src/cpuinfo/SDL_cpuinfo.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 04 Jan 2019 22:01:14 -0800
changeset 12503 806492103856
parent 12450 1055156e99f3
child 12665 4743da9c3eea
permissions -rw-r--r--
Updated copyright for 2019
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@12242
    25
#include "SDL_simd.h"
slouken@8148
    26
#endif
slouken@739
    27
slouken@7828
    28
#if defined(__WIN32__)
slouken@7828
    29
#include "../core/windows/SDL_windows.h"
slouken@7828
    30
#endif
slouken@11338
    31
#if defined(__OS2__)
slouken@11338
    32
#define INCL_DOS
slouken@11338
    33
#include <os2.h>
slouken@11338
    34
#ifndef QSV_NUMPROCESSORS
slouken@11338
    35
#define QSV_NUMPROCESSORS 26
slouken@11338
    36
#endif
slouken@11338
    37
#endif
slouken@7828
    38
slouken@739
    39
/* CPU feature detection for SDL */
slouken@739
    40
slouken@1361
    41
#include "SDL_cpuinfo.h"
icculus@11984
    42
#include "SDL_assert.h"
slouken@1361
    43
slouken@3586
    44
#ifdef HAVE_SYSCONF
slouken@3586
    45
#include <unistd.h>
slouken@3586
    46
#endif
slouken@3579
    47
#ifdef HAVE_SYSCTLBYNAME
slouken@3579
    48
#include <sys/types.h>
slouken@3579
    49
#include <sys/sysctl.h>
slouken@3579
    50
#endif
slouken@5389
    51
#if defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))
slouken@5389
    52
#include <sys/sysctl.h>         /* For AltiVec check */
slouken@6363
    53
#elif defined(__OpenBSD__) && defined(__powerpc__)
slouken@6363
    54
#include <sys/param.h>
slouken@6363
    55
#include <sys/sysctl.h> /* For AltiVec check */
slouken@6363
    56
#include <machine/cpu.h>
slouken@5389
    57
#elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
slouken@5389
    58
#include <signal.h>
slouken@5389
    59
#include <setjmp.h>
slouken@5389
    60
#endif
slouken@793
    61
icculus@11308
    62
#if defined(__QNXNTO__)
icculus@11308
    63
#include <sys/syspage.h>
icculus@11308
    64
#endif
icculus@11308
    65
icculus@10626
    66
#if (defined(__LINUX__) || defined(__ANDROID__)) && defined(__ARM_ARCH)
icculus@10627
    67
/*#include <asm/hwcap.h>*/
icculus@10627
    68
#ifndef AT_HWCAP
icculus@10627
    69
#define AT_HWCAP 16
icculus@10627
    70
#endif
icculus@10627
    71
#ifndef HWCAP_NEON
icculus@10627
    72
#define HWCAP_NEON (1 << 12)
icculus@10627
    73
#endif
icculus@10626
    74
#if defined HAVE_GETAUXVAL
icculus@10620
    75
#include <sys/auxv.h>
icculus@10626
    76
#else
icculus@10625
    77
#include <fcntl.h>
icculus@10625
    78
#endif
icculus@10620
    79
#endif
icculus@10620
    80
icculus@12439
    81
#if defined(__ANDROID__) && defined(__ARM_ARCH) && !defined(HAVE_GETAUXVAL)
icculus@12439
    82
#if __ARM_ARCH < 8
icculus@12439
    83
#include <cpu-features.h>
icculus@12439
    84
#endif
icculus@12439
    85
#endif
icculus@12439
    86
icculus@11985
    87
#define CPU_HAS_RDTSC   (1 << 0)
icculus@11985
    88
#define CPU_HAS_ALTIVEC (1 << 1)
icculus@11985
    89
#define CPU_HAS_MMX     (1 << 2)
icculus@11985
    90
#define CPU_HAS_3DNOW   (1 << 3)
icculus@11985
    91
#define CPU_HAS_SSE     (1 << 4)
icculus@11985
    92
#define CPU_HAS_SSE2    (1 << 5)
icculus@11985
    93
#define CPU_HAS_SSE3    (1 << 6)
icculus@11985
    94
#define CPU_HAS_SSE41   (1 << 7)
icculus@11985
    95
#define CPU_HAS_SSE42   (1 << 8)
icculus@11985
    96
#define CPU_HAS_AVX     (1 << 9)
icculus@11985
    97
#define CPU_HAS_AVX2    (1 << 10)
icculus@11985
    98
#define CPU_HAS_NEON    (1 << 11)
icculus@11986
    99
#define CPU_HAS_AVX512F (1 << 12)
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@12439
   329
#if defined(__LINUX__) && defined(__ARM_ARCH) && !defined(HAVE_GETAUXVAL)
icculus@10624
   330
static int
icculus@10624
   331
readProcAuxvForNeon(void)
icculus@10624
   332
{
icculus@10624
   333
    int neon = 0;
icculus@10624
   334
    int kv[2];
icculus@10624
   335
    const int fd = open("/proc/self/auxv", O_RDONLY);
icculus@10626
   336
    if (fd != -1) {
icculus@10626
   337
        while (read(fd, kv, sizeof (kv)) == sizeof (kv)) {
icculus@10626
   338
            if (kv[0] == AT_HWCAP) {
icculus@10626
   339
                neon = ((kv[1] & HWCAP_NEON) == HWCAP_NEON);
icculus@10626
   340
                break;
icculus@10626
   341
            }
icculus@10626
   342
        }
icculus@10626
   343
        close(fd);
icculus@10624
   344
    }
icculus@10624
   345
    return neon;
icculus@10624
   346
}
icculus@10624
   347
#endif
icculus@10624
   348
icculus@10624
   349
slouken@9003
   350
static int
icculus@10620
   351
CPU_haveNEON(void)
icculus@10620
   352
{
icculus@10620
   353
/* The way you detect NEON is a privileged instruction on ARM, so you have
icculus@10620
   354
   query the OS kernel in a platform-specific way. :/ */
sylvain@12450
   355
#if defined(SDL_CPUINFO_DISABLED)
sylvain@12450
   356
   return 0; /* disabled */
sylvain@12450
   357
#elif (defined(__WINDOWS__) || defined(__WINRT__)) && (defined(_M_ARM) || defined(_M_ARM64))
sylvain@12450
   358
/* Visual Studio, for ARM, doesn't define __ARM_ARCH. Handle this first. */
sylvain@12450
   359
/* Seems to have been removed */
sylvain@12450
   360
#  if !defined(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE)
sylvain@12450
   361
#    define PF_ARM_NEON_INSTRUCTIONS_AVAILABLE 19
sylvain@12450
   362
#  endif
sylvain@12450
   363
/* All WinRT ARM devices are required to support NEON, but just in case. */
sylvain@12450
   364
    return IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE) != 0;
sylvain@12450
   365
#elif !defined(__ARM_ARCH)
sylvain@12450
   366
    return 0;  /* not an ARM CPU at all. */
icculus@10624
   367
#elif __ARM_ARCH >= 8
icculus@10639
   368
    return 1;  /* ARMv8 always has non-optional NEON support. */
icculus@10639
   369
#elif defined(__APPLE__) && (__ARM_ARCH >= 7)
icculus@10639
   370
    /* (note that sysctlbyname("hw.optional.neon") doesn't work!) */
icculus@10639
   371
    return 1;  /* all Apple ARMv7 chips and later have NEON. */
icculus@10624
   372
#elif defined(__APPLE__)
slouken@11700
   373
    return 0;  /* assume anything else from Apple doesn't have NEON. */
icculus@11308
   374
#elif defined(__QNXNTO__)
icculus@11308
   375
    return SYSPAGE_ENTRY(cpuinfo)->flags & ARM_CPU_FLAG_NEON;
icculus@10625
   376
#elif (defined(__LINUX__) || defined(__ANDROID__)) && defined(HAVE_GETAUXVAL)
icculus@10628
   377
    return ((getauxval(AT_HWCAP) & HWCAP_NEON) == HWCAP_NEON);
icculus@12439
   378
#elif defined(__LINUX__)
icculus@12439
   379
    return readProcAuxvForNeon();
icculus@12439
   380
#elif defined(__ANDROID__)
icculus@12439
   381
    /* Use NDK cpufeatures to read either /proc/self/auxv or /proc/cpuinfo */
icculus@12439
   382
    {
icculus@12439
   383
        AndroidCpuFamily cpu_family = android_getCpuFamily();
icculus@12439
   384
        if (cpu_family == ANDROID_CPU_FAMILY_ARM) {
icculus@12439
   385
            uint64_t cpu_features = android_getCpuFeatures();
icculus@12439
   386
            if ((cpu_features & ANDROID_CPU_ARM_FEATURE_NEON) != 0) {
icculus@12439
   387
                return 1;
icculus@12439
   388
            }
icculus@12439
   389
        }
icculus@12439
   390
        return 0;
icculus@12439
   391
    }
icculus@10624
   392
#else
icculus@10624
   393
#warning SDL_HasNEON is not implemented for this ARM platform. Write me.
icculus@10625
   394
    return 0;
icculus@10620
   395
#endif
icculus@10620
   396
}
icculus@10620
   397
icculus@10620
   398
static int
slouken@5389
   399
CPU_have3DNow(void)
slouken@5389
   400
{
icculus@10619
   401
    if (CPU_CPUIDMaxFunction > 0) {  /* that is, do we have CPUID at all? */
slouken@5389
   402
        int a, b, c, d;
slouken@5389
   403
        cpuid(0x80000000, a, b, c, d);
slouken@5389
   404
        if (a >= 0x80000001) {
slouken@5389
   405
            cpuid(0x80000001, a, b, c, d);
slouken@5389
   406
            return (d & 0x80000000);
slouken@5389
   407
        }
slouken@5389
   408
    }
slouken@5389
   409
    return 0;
slouken@5389
   410
}
slouken@5389
   411
icculus@10619
   412
#define CPU_haveRDTSC() (CPU_CPUIDFeatures[3] & 0x00000010)
icculus@10619
   413
#define CPU_haveMMX() (CPU_CPUIDFeatures[3] & 0x00800000)
icculus@10619
   414
#define CPU_haveSSE() (CPU_CPUIDFeatures[3] & 0x02000000)
icculus@10619
   415
#define CPU_haveSSE2() (CPU_CPUIDFeatures[3] & 0x04000000)
icculus@10619
   416
#define CPU_haveSSE3() (CPU_CPUIDFeatures[2] & 0x00000001)
icculus@10619
   417
#define CPU_haveSSE41() (CPU_CPUIDFeatures[2] & 0x00080000)
icculus@10619
   418
#define CPU_haveSSE42() (CPU_CPUIDFeatures[2] & 0x00100000)
icculus@10619
   419
#define CPU_haveAVX() (CPU_OSSavesYMM && (CPU_CPUIDFeatures[2] & 0x10000000))
slouken@8148
   420
slouken@9003
   421
static int
slouken@9003
   422
CPU_haveAVX2(void)
slouken@9003
   423
{
icculus@10619
   424
    if (CPU_OSSavesYMM && (CPU_CPUIDMaxFunction >= 7)) {
slouken@9003
   425
        int a, b, c, d;
icculus@10623
   426
        (void) a; (void) b; (void) c; (void) d;  /* compiler warnings... */
icculus@10619
   427
        cpuid(7, a, b, c, d);
icculus@10619
   428
        return (b & 0x00000020);
slouken@9003
   429
    }
slouken@9003
   430
    return 0;
slouken@9003
   431
}
slouken@9003
   432
icculus@11986
   433
static int
icculus@11986
   434
CPU_haveAVX512F(void)
icculus@11986
   435
{
icculus@11986
   436
    if (CPU_OSSavesZMM && (CPU_CPUIDMaxFunction >= 7)) {
icculus@11986
   437
        int a, b, c, d;
icculus@11986
   438
        (void) a; (void) b; (void) c; (void) d;  /* compiler warnings... */
icculus@11986
   439
        cpuid(7, a, b, c, d);
icculus@11986
   440
        return (b & 0x00010000);
icculus@11986
   441
    }
icculus@11986
   442
    return 0;
icculus@11986
   443
}
icculus@11986
   444
slouken@3579
   445
static int SDL_CPUCount = 0;
slouken@3579
   446
slouken@3579
   447
int
slouken@5120
   448
SDL_GetCPUCount(void)
slouken@3579
   449
{
slouken@3579
   450
    if (!SDL_CPUCount) {
icculus@9278
   451
#ifndef SDL_CPUINFO_DISABLED
slouken@3606
   452
#if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
slouken@3586
   453
        if (SDL_CPUCount <= 0) {
slouken@3586
   454
            SDL_CPUCount = (int)sysconf(_SC_NPROCESSORS_ONLN);
slouken@3586
   455
        }
slouken@3586
   456
#endif
slouken@3579
   457
#ifdef HAVE_SYSCTLBYNAME
slouken@3586
   458
        if (SDL_CPUCount <= 0) {
slouken@3580
   459
            size_t size = sizeof(SDL_CPUCount);
slouken@3580
   460
            sysctlbyname("hw.ncpu", &SDL_CPUCount, &size, NULL, 0);
slouken@3580
   461
        }
slouken@3580
   462
#endif
slouken@5086
   463
#ifdef __WIN32__
slouken@3586
   464
        if (SDL_CPUCount <= 0) {
slouken@3580
   465
            SYSTEM_INFO info;
slouken@3580
   466
            GetSystemInfo(&info);
slouken@3580
   467
            SDL_CPUCount = info.dwNumberOfProcessors;
slouken@3580
   468
        }
slouken@3579
   469
#endif
slouken@11338
   470
#ifdef __OS2__
slouken@11338
   471
        if (SDL_CPUCount <= 0) {
slouken@11338
   472
            DosQuerySysInfo(QSV_NUMPROCESSORS, QSV_NUMPROCESSORS,
slouken@11338
   473
                            &SDL_CPUCount, sizeof(SDL_CPUCount) );
slouken@11338
   474
        }
slouken@11338
   475
#endif
icculus@9278
   476
#endif
slouken@3579
   477
        /* There has to be at least 1, right? :) */
slouken@3586
   478
        if (SDL_CPUCount <= 0) {
slouken@3579
   479
            SDL_CPUCount = 1;
slouken@3579
   480
        }
slouken@3579
   481
    }
slouken@3579
   482
    return SDL_CPUCount;
slouken@3579
   483
}
slouken@3579
   484
slouken@3579
   485
/* Oh, such a sweet sweet trick, just not very useful. :) */
slouken@4472
   486
static const char *
slouken@5120
   487
SDL_GetCPUType(void)
slouken@3579
   488
{
slouken@5115
   489
    static char SDL_CPUType[13];
slouken@3579
   490
slouken@3579
   491
    if (!SDL_CPUType[0]) {
slouken@3579
   492
        int i = 0;
slouken@3579
   493
icculus@10619
   494
        CPU_calcCPUIDFeatures();
icculus@10619
   495
        if (CPU_CPUIDMaxFunction > 0) {  /* do we have CPUID at all? */
icculus@9300
   496
            int a, b, c, d;
slouken@5115
   497
            cpuid(0x00000000, a, b, c, d);
icculus@9300
   498
            (void) a;
slouken@5115
   499
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   500
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   501
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
icculus@8642
   502
            SDL_CPUType[i++] = (char)(b & 0xff);
icculus@8642
   503
slouken@5115
   504
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   505
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   506
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
icculus@8642
   507
            SDL_CPUType[i++] = (char)(d & 0xff);
icculus@8642
   508
slouken@5115
   509
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   510
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   511
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
icculus@8642
   512
            SDL_CPUType[i++] = (char)(c & 0xff);
slouken@5115
   513
        }
slouken@5115
   514
        if (!SDL_CPUType[0]) {
slouken@5115
   515
            SDL_strlcpy(SDL_CPUType, "Unknown", sizeof(SDL_CPUType));
slouken@5115
   516
        }
slouken@5115
   517
    }
slouken@5115
   518
    return SDL_CPUType;
slouken@5115
   519
}
slouken@5115
   520
icculus@5981
   521
icculus@5981
   522
#ifdef TEST_MAIN  /* !!! FIXME: only used for test at the moment. */
slouken@5115
   523
static const char *
slouken@5120
   524
SDL_GetCPUName(void)
slouken@5115
   525
{
slouken@5115
   526
    static char SDL_CPUName[48];
slouken@5115
   527
slouken@5115
   528
    if (!SDL_CPUName[0]) {
slouken@5115
   529
        int i = 0;
slouken@5115
   530
        int a, b, c, d;
slouken@5115
   531
icculus@10619
   532
        CPU_calcCPUIDFeatures();
icculus@10619
   533
        if (CPU_CPUIDMaxFunction > 0) {  /* do we have CPUID at all? */
slouken@3580
   534
            cpuid(0x80000000, a, b, c, d);
slouken@3580
   535
            if (a >= 0x80000004) {
slouken@3580
   536
                cpuid(0x80000002, a, b, c, d);
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)(a & 0xff); a >>= 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)(b & 0xff); b >>= 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)(c & 0xff); c >>= 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@5115
   552
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@3580
   553
                cpuid(0x80000003, a, b, c, d);
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)(a & 0xff); a >>= 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)(b & 0xff); b >>= 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)(c & 0xff); c >>= 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@5115
   569
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@3580
   570
                cpuid(0x80000004, a, b, c, d);
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)(a & 0xff); a >>= 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)(b & 0xff); b >>= 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)(c & 0xff); c >>= 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@5115
   586
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@3579
   587
            }
slouken@3579
   588
        }
slouken@5115
   589
        if (!SDL_CPUName[0]) {
slouken@5115
   590
            SDL_strlcpy(SDL_CPUName, "Unknown", sizeof(SDL_CPUName));
slouken@3579
   591
        }
slouken@3579
   592
    }
slouken@5115
   593
    return SDL_CPUName;
slouken@5115
   594
}
icculus@5981
   595
#endif
slouken@5115
   596
slouken@5120
   597
int
slouken@5120
   598
SDL_GetCPUCacheLineSize(void)
slouken@5115
   599
{
slouken@5115
   600
    const char *cpuType = SDL_GetCPUType();
icculus@9300
   601
    int a, b, c, d;
icculus@9300
   602
    (void) a; (void) b; (void) c; (void) d;
slouken@5115
   603
    if (SDL_strcmp(cpuType, "GenuineIntel") == 0) {
slouken@5115
   604
        cpuid(0x00000001, a, b, c, d);
slouken@5115
   605
        return (((b >> 8) & 0xff) * 8);
slouken@5115
   606
    } else if (SDL_strcmp(cpuType, "AuthenticAMD") == 0) {
slouken@5115
   607
        cpuid(0x80000005, a, b, c, d);
slouken@5115
   608
        return (c & 0xff);
slouken@5115
   609
    } else {
slouken@5115
   610
        /* Just make a guess here... */
slouken@5115
   611
        return SDL_CACHELINE_SIZE;
slouken@5115
   612
    }
slouken@3579
   613
}
slouken@3579
   614
slouken@739
   615
static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
icculus@11984
   616
static Uint32 SDL_SIMDAlignment = 0xFFFFFFFF;
slouken@739
   617
slouken@1895
   618
static Uint32
slouken@1895
   619
SDL_GetCPUFeatures(void)
slouken@739
   620
{
slouken@1895
   621
    if (SDL_CPUFeatures == 0xFFFFFFFF) {
icculus@10619
   622
        CPU_calcCPUIDFeatures();
slouken@1895
   623
        SDL_CPUFeatures = 0;
icculus@11984
   624
        SDL_SIMDAlignment = 4;  /* a good safe base value */
slouken@1895
   625
        if (CPU_haveRDTSC()) {
slouken@1895
   626
            SDL_CPUFeatures |= CPU_HAS_RDTSC;
slouken@1895
   627
        }
slouken@5389
   628
        if (CPU_haveAltiVec()) {
slouken@5389
   629
            SDL_CPUFeatures |= CPU_HAS_ALTIVEC;
icculus@11984
   630
            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
slouken@5389
   631
        }
slouken@1895
   632
        if (CPU_haveMMX()) {
slouken@1895
   633
            SDL_CPUFeatures |= CPU_HAS_MMX;
icculus@11984
   634
            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 8);
slouken@1895
   635
        }
slouken@5389
   636
        if (CPU_have3DNow()) {
slouken@5389
   637
            SDL_CPUFeatures |= CPU_HAS_3DNOW;
icculus@11984
   638
            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 8);
slouken@5389
   639
        }
slouken@1895
   640
        if (CPU_haveSSE()) {
slouken@1895
   641
            SDL_CPUFeatures |= CPU_HAS_SSE;
icculus@11984
   642
            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
slouken@1895
   643
        }
slouken@1895
   644
        if (CPU_haveSSE2()) {
slouken@1895
   645
            SDL_CPUFeatures |= CPU_HAS_SSE2;
icculus@11984
   646
            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
slouken@1895
   647
        }
slouken@5259
   648
        if (CPU_haveSSE3()) {
slouken@5259
   649
            SDL_CPUFeatures |= CPU_HAS_SSE3;
icculus@11984
   650
            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
slouken@5259
   651
        }
slouken@5263
   652
        if (CPU_haveSSE41()) {
slouken@5263
   653
            SDL_CPUFeatures |= CPU_HAS_SSE41;
icculus@11984
   654
            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
slouken@5263
   655
        }
slouken@5263
   656
        if (CPU_haveSSE42()) {
slouken@5263
   657
            SDL_CPUFeatures |= CPU_HAS_SSE42;
icculus@11984
   658
            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
slouken@1895
   659
        }
slouken@8148
   660
        if (CPU_haveAVX()) {
slouken@8148
   661
            SDL_CPUFeatures |= CPU_HAS_AVX;
icculus@11984
   662
            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 32);
slouken@8148
   663
        }
slouken@9003
   664
        if (CPU_haveAVX2()) {
slouken@9003
   665
            SDL_CPUFeatures |= CPU_HAS_AVX2;
icculus@11984
   666
            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 32);
slouken@9003
   667
        }
icculus@11986
   668
        if (CPU_haveAVX512F()) {
icculus@11986
   669
            SDL_CPUFeatures |= CPU_HAS_AVX512F;
icculus@11986
   670
            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 64);
icculus@11986
   671
        }
icculus@10620
   672
        if (CPU_haveNEON()) {
icculus@10620
   673
            SDL_CPUFeatures |= CPU_HAS_NEON;
icculus@11984
   674
            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
icculus@10620
   675
        }
slouken@1895
   676
    }
slouken@1895
   677
    return SDL_CPUFeatures;
slouken@739
   678
}
slouken@739
   679
icculus@10619
   680
#define CPU_FEATURE_AVAILABLE(f) ((SDL_GetCPUFeatures() & f) ? SDL_TRUE : SDL_FALSE)
icculus@10619
   681
icculus@10619
   682
SDL_bool SDL_HasRDTSC(void)
slouken@745
   683
{
icculus@10619
   684
    return CPU_FEATURE_AVAILABLE(CPU_HAS_RDTSC);
slouken@745
   685
}
slouken@745
   686
slouken@1895
   687
SDL_bool
slouken@5389
   688
SDL_HasAltiVec(void)
slouken@5389
   689
{
icculus@10619
   690
    return CPU_FEATURE_AVAILABLE(CPU_HAS_ALTIVEC);
slouken@5389
   691
}
slouken@5389
   692
slouken@5389
   693
SDL_bool
slouken@1895
   694
SDL_HasMMX(void)
slouken@739
   695
{
icculus@10619
   696
    return CPU_FEATURE_AVAILABLE(CPU_HAS_MMX);
slouken@739
   697
}
slouken@739
   698
slouken@1895
   699
SDL_bool
slouken@5389
   700
SDL_Has3DNow(void)
slouken@5389
   701
{
icculus@10619
   702
    return CPU_FEATURE_AVAILABLE(CPU_HAS_3DNOW);
slouken@5389
   703
}
slouken@5389
   704
slouken@5389
   705
SDL_bool
slouken@1895
   706
SDL_HasSSE(void)
slouken@739
   707
{
icculus@10619
   708
    return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE);
slouken@739
   709
}
slouken@739
   710
slouken@1895
   711
SDL_bool
slouken@1895
   712
SDL_HasSSE2(void)
slouken@804
   713
{
icculus@10619
   714
    return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE2);
slouken@804
   715
}
slouken@804
   716
slouken@1895
   717
SDL_bool
slouken@5259
   718
SDL_HasSSE3(void)
slouken@778
   719
{
icculus@10619
   720
    return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE3);
slouken@5259
   721
}
slouken@5259
   722
slouken@5259
   723
SDL_bool
slouken@5263
   724
SDL_HasSSE41(void)
slouken@5259
   725
{
icculus@10619
   726
    return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE41);
slouken@5263
   727
}
slouken@5263
   728
slouken@5263
   729
SDL_bool
slouken@5263
   730
SDL_HasSSE42(void)
slouken@5263
   731
{
icculus@10619
   732
    return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE42);
slouken@778
   733
}
slouken@778
   734
slouken@8148
   735
SDL_bool
slouken@8148
   736
SDL_HasAVX(void)
slouken@8148
   737
{
icculus@10619
   738
    return CPU_FEATURE_AVAILABLE(CPU_HAS_AVX);
slouken@8148
   739
}
slouken@8148
   740
slouken@9003
   741
SDL_bool
slouken@9003
   742
SDL_HasAVX2(void)
slouken@9003
   743
{
icculus@10619
   744
    return CPU_FEATURE_AVAILABLE(CPU_HAS_AVX2);
slouken@9003
   745
}
slouken@9003
   746
icculus@10620
   747
SDL_bool
icculus@11986
   748
SDL_HasAVX512F(void)
icculus@11986
   749
{
icculus@11986
   750
    return CPU_FEATURE_AVAILABLE(CPU_HAS_AVX512F);
icculus@11986
   751
}
icculus@11986
   752
icculus@11986
   753
SDL_bool
icculus@10620
   754
SDL_HasNEON(void)
icculus@10620
   755
{
icculus@10620
   756
    return CPU_FEATURE_AVAILABLE(CPU_HAS_NEON);
icculus@10620
   757
}
icculus@10620
   758
slouken@7821
   759
static int SDL_SystemRAM = 0;
slouken@7821
   760
slouken@7821
   761
int
slouken@7821
   762
SDL_GetSystemRAM(void)
slouken@7821
   763
{
slouken@7821
   764
    if (!SDL_SystemRAM) {
icculus@9278
   765
#ifndef SDL_CPUINFO_DISABLED
slouken@7821
   766
#if defined(HAVE_SYSCONF) && defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
slouken@7821
   767
        if (SDL_SystemRAM <= 0) {
slouken@7821
   768
            SDL_SystemRAM = (int)((Sint64)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE) / (1024*1024));
slouken@7821
   769
        }
slouken@7821
   770
#endif
slouken@7821
   771
#ifdef HAVE_SYSCTLBYNAME
slouken@7821
   772
        if (SDL_SystemRAM <= 0) {
icculus@9987
   773
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
slouken@7826
   774
#ifdef HW_REALMEM
slouken@7826
   775
            int mib[2] = {CTL_HW, HW_REALMEM};
slouken@7826
   776
#else
slouken@7826
   777
            /* might only report up to 2 GiB */
slouken@7826
   778
            int mib[2] = {CTL_HW, HW_PHYSMEM};
slouken@7826
   779
#endif /* HW_REALMEM */
slouken@7826
   780
#else
slouken@7821
   781
            int mib[2] = {CTL_HW, HW_MEMSIZE};
slouken@8240
   782
#endif /* __FreeBSD__ || __FreeBSD_kernel__ */
slouken@7823
   783
            Uint64 memsize = 0;
slouken@7821
   784
            size_t len = sizeof(memsize);
slouken@7821
   785
            
slouken@7821
   786
            if (sysctl(mib, 2, &memsize, &len, NULL, 0) == 0) {
slouken@7821
   787
                SDL_SystemRAM = (int)(memsize / (1024*1024));
slouken@7821
   788
            }
slouken@7821
   789
        }
slouken@7821
   790
#endif
slouken@7821
   791
#ifdef __WIN32__
slouken@7821
   792
        if (SDL_SystemRAM <= 0) {
slouken@7821
   793
            MEMORYSTATUSEX stat;
icculus@7889
   794
            stat.dwLength = sizeof(stat);
slouken@7821
   795
            if (GlobalMemoryStatusEx(&stat)) {
slouken@7821
   796
                SDL_SystemRAM = (int)(stat.ullTotalPhys / (1024 * 1024));
slouken@7821
   797
            }
slouken@7821
   798
        }
slouken@7821
   799
#endif
slouken@11338
   800
#ifdef __OS2__
slouken@11338
   801
        if (SDL_SystemRAM <= 0) {
slouken@11338
   802
            Uint32 sysram = 0;
slouken@11338
   803
            DosQuerySysInfo(QSV_TOTPHYSMEM, QSV_TOTPHYSMEM, &sysram, 4);
slouken@11338
   804
            SDL_SystemRAM = (int) (sysram / 0x100000U);
slouken@11338
   805
        }
slouken@11338
   806
#endif
icculus@9278
   807
#endif
slouken@7821
   808
    }
slouken@7821
   809
    return SDL_SystemRAM;
slouken@7821
   810
}
slouken@7821
   811
slouken@7821
   812
icculus@11984
   813
size_t
icculus@11984
   814
SDL_SIMDGetAlignment(void)
icculus@11984
   815
{
icculus@11984
   816
    if (SDL_SIMDAlignment == 0xFFFFFFFF) {
icculus@11984
   817
        SDL_GetCPUFeatures();  /* make sure this has been calculated */
icculus@11984
   818
    }
icculus@11984
   819
    SDL_assert(SDL_SIMDAlignment != 0);
icculus@11984
   820
    return SDL_SIMDAlignment;
icculus@11984
   821
}
icculus@11984
   822
icculus@11984
   823
void *
icculus@11984
   824
SDL_SIMDAlloc(const size_t len)
icculus@11984
   825
{
icculus@11984
   826
    const size_t alignment = SDL_SIMDGetAlignment();
icculus@11984
   827
    const size_t padding = alignment - (len % alignment);
icculus@11984
   828
    const size_t padded = (padding != alignment) ? (len + padding) : len;
icculus@11984
   829
    Uint8 *retval = NULL;
icculus@11984
   830
    Uint8 *ptr = (Uint8 *) SDL_malloc(padded + alignment + sizeof (void *));
icculus@11984
   831
    if (ptr) {
icculus@11984
   832
        /* store the actual malloc pointer right before our aligned pointer. */
icculus@11984
   833
        retval = ptr + sizeof (void *);
icculus@11984
   834
        retval += alignment - (((size_t) retval) % alignment);
icculus@11984
   835
        *(((void **) retval) - 1) = ptr;
icculus@11984
   836
    }
icculus@11984
   837
    return retval;
icculus@11984
   838
}
icculus@11984
   839
icculus@11984
   840
void
icculus@11984
   841
SDL_SIMDFree(void *ptr)
icculus@11984
   842
{
icculus@11984
   843
    if (ptr) {
icculus@11984
   844
        void **realptr = (void **) ptr;
icculus@11984
   845
        realptr--;
icculus@11984
   846
        SDL_free(*(((void **) ptr) - 1));
icculus@11984
   847
    }
icculus@11984
   848
}
icculus@11984
   849
icculus@11984
   850
slouken@739
   851
#ifdef TEST_MAIN
slouken@739
   852
slouken@739
   853
#include <stdio.h>
slouken@739
   854
slouken@1895
   855
int
slouken@1895
   856
main()
slouken@739
   857
{
slouken@3579
   858
    printf("CPU count: %d\n", SDL_GetCPUCount());
slouken@5115
   859
    printf("CPU type: %s\n", SDL_GetCPUType());
slouken@5115
   860
    printf("CPU name: %s\n", SDL_GetCPUName());
slouken@5115
   861
    printf("CacheLine size: %d\n", SDL_GetCPUCacheLineSize());
slouken@1895
   862
    printf("RDTSC: %d\n", SDL_HasRDTSC());
slouken@5389
   863
    printf("Altivec: %d\n", SDL_HasAltiVec());
slouken@1895
   864
    printf("MMX: %d\n", SDL_HasMMX());
slouken@5389
   865
    printf("3DNow: %d\n", SDL_Has3DNow());
slouken@1895
   866
    printf("SSE: %d\n", SDL_HasSSE());
slouken@1895
   867
    printf("SSE2: %d\n", SDL_HasSSE2());
slouken@5259
   868
    printf("SSE3: %d\n", SDL_HasSSE3());
slouken@5263
   869
    printf("SSE4.1: %d\n", SDL_HasSSE41());
slouken@5263
   870
    printf("SSE4.2: %d\n", SDL_HasSSE42());
slouken@8148
   871
    printf("AVX: %d\n", SDL_HasAVX());
slouken@9003
   872
    printf("AVX2: %d\n", SDL_HasAVX2());
icculus@11986
   873
    printf("AVX-512F: %d\n", SDL_HasAVX512F());
icculus@10620
   874
    printf("NEON: %d\n", SDL_HasNEON());
slouken@7821
   875
    printf("RAM: %d MB\n", SDL_GetSystemRAM());
slouken@1895
   876
    return 0;
slouken@739
   877
}
slouken@739
   878
slouken@739
   879
#endif /* TEST_MAIN */
slouken@1895
   880
slouken@1895
   881
/* vi: set ts=4 sw=4 expandtab: */