src/cpuinfo/SDL_cpuinfo.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 21 May 2018 11:35:42 -0400
changeset 11986 e307b74aa643
parent 11985 36aa0bf7312b
child 12242 df7260f149f2
permissions -rw-r--r--
cpuinfo: Added SDL_HasAVX512F().

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