src/cpuinfo/SDL_cpuinfo.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 21 May 2018 11:34:57 -0400
changeset 11984 987c5dc71309
parent 11811 5d94cb6b24d3
child 11985 36aa0bf7312b
permissions -rw-r--r--
cpuinfo: Added some internal SIMD-aligned allocation functions.

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