src/cpuinfo/SDL_cpuinfo.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 27 Sep 2018 14:56:29 -0700
changeset 12242 df7260f149f2
parent 11986 e307b74aa643
child 12439 3a0793e5e134
permissions -rw-r--r--
Fixed bug 4277 - warnings patch

Sylvain

Patch a few warnings when using:
-Wmissing-prototypes -Wdocumentation -Wdocumentation-unknown-command

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