src/cpuinfo/SDL_cpuinfo.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 18 Dec 2014 00:19:52 -0500
changeset 9278 8900afb78a19
parent 9047 101109110bf7
child 9300 0293f45e4814
permissions -rw-r--r--
Initial merge of Emscripten port!

With this commit, you can compile SDL2 with Emscripten
( http://emscripten.org/ ), and make your SDL-based C/C++ program
into a web app.

This port was due to the efforts of several people, including: Charlie Birks,
Sathyanarayanan Gunasekaran, Jukka Jylänki, Alon Zakai, Edward Rudd,
Bruce Mitchener, and Martin Gerhardy. (Thanks, everyone!)
slouken@739
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 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@7828
    30
slouken@739
    31
/* CPU feature detection for SDL */
slouken@739
    32
slouken@1361
    33
#include "SDL_cpuinfo.h"
slouken@1361
    34
slouken@3586
    35
#ifdef HAVE_SYSCONF
slouken@3586
    36
#include <unistd.h>
slouken@3586
    37
#endif
slouken@3579
    38
#ifdef HAVE_SYSCTLBYNAME
slouken@3579
    39
#include <sys/types.h>
slouken@3579
    40
#include <sys/sysctl.h>
slouken@3579
    41
#endif
slouken@5389
    42
#if defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))
slouken@5389
    43
#include <sys/sysctl.h>         /* For AltiVec check */
slouken@6363
    44
#elif defined(__OpenBSD__) && defined(__powerpc__)
slouken@6363
    45
#include <sys/param.h>
slouken@6363
    46
#include <sys/sysctl.h> /* For AltiVec check */
slouken@6363
    47
#include <machine/cpu.h>
slouken@5389
    48
#elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
slouken@5389
    49
#include <signal.h>
slouken@5389
    50
#include <setjmp.h>
slouken@5389
    51
#endif
slouken@793
    52
slouken@3579
    53
#define CPU_HAS_RDTSC   0x00000001
slouken@5389
    54
#define CPU_HAS_ALTIVEC 0x00000002
slouken@5389
    55
#define CPU_HAS_MMX     0x00000004
slouken@5389
    56
#define CPU_HAS_3DNOW   0x00000008
slouken@5259
    57
#define CPU_HAS_SSE     0x00000010
slouken@5259
    58
#define CPU_HAS_SSE2    0x00000020
slouken@5259
    59
#define CPU_HAS_SSE3    0x00000040
slouken@5389
    60
#define CPU_HAS_SSE41   0x00000100
slouken@5389
    61
#define CPU_HAS_SSE42   0x00000200
slouken@8148
    62
#define CPU_HAS_AVX     0x00000400
slouken@9003
    63
#define CPU_HAS_AVX2    0x00000800
slouken@739
    64
slouken@6363
    65
#if SDL_ALTIVEC_BLITTERS && HAVE_SETJMP && !__MACOSX__ && !__OpenBSD__
slouken@5389
    66
/* This is the brute force way of detecting instruction sets...
slouken@5389
    67
   the idea is borrowed from the libmpeg2 library - thanks!
slouken@5389
    68
 */
slouken@5389
    69
static jmp_buf jmpbuf;
slouken@5389
    70
static void
slouken@5389
    71
illegal_instruction(int sig)
slouken@5389
    72
{
slouken@5389
    73
    longjmp(jmpbuf, 1);
slouken@5389
    74
}
slouken@5389
    75
#endif /* HAVE_SETJMP */
slouken@793
    76
slouken@9003
    77
static int
slouken@1895
    78
CPU_haveCPUID(void)
slouken@745
    79
{
slouken@1895
    80
    int has_CPUID = 0;
slouken@1895
    81
/* *INDENT-OFF* */
icculus@9278
    82
#ifndef SDL_CPUINFO_DISABLED
slouken@745
    83
#if defined(__GNUC__) && defined(i386)
slouken@3579
    84
    __asm__ (
slouken@745
    85
"        pushfl                      # Get original EFLAGS             \n"
slouken@745
    86
"        popl    %%eax                                                 \n"
slouken@745
    87
"        movl    %%eax,%%ecx                                           \n"
slouken@745
    88
"        xorl    $0x200000,%%eax     # Flip ID bit in EFLAGS           \n"
slouken@745
    89
"        pushl   %%eax               # Save new EFLAGS value on stack  \n"
slouken@745
    90
"        popfl                       # Replace current EFLAGS value    \n"
slouken@745
    91
"        pushfl                      # Get new EFLAGS                  \n"
slouken@745
    92
"        popl    %%eax               # Store new EFLAGS in EAX         \n"
slouken@745
    93
"        xorl    %%ecx,%%eax         # Can not toggle ID bit,          \n"
slouken@745
    94
"        jz      1f                  # Processor=80486                 \n"
slouken@745
    95
"        movl    $1,%0               # We have CPUID support           \n"
slouken@745
    96
"1:                                                                    \n"
slouken@3579
    97
    : "=m" (has_CPUID)
slouken@3579
    98
    :
slouken@3579
    99
    : "%eax", "%ecx"
slouken@3579
   100
    );
slouken@881
   101
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@881
   102
/* Technically, if this is being compiled under __x86_64__ then it has 
slouken@8241
   103
   CPUid by definition.  But it's nice to be able to prove it.  :)      */
slouken@3579
   104
    __asm__ (
slouken@881
   105
"        pushfq                      # Get original EFLAGS             \n"
slouken@881
   106
"        popq    %%rax                                                 \n"
slouken@881
   107
"        movq    %%rax,%%rcx                                           \n"
slouken@881
   108
"        xorl    $0x200000,%%eax     # Flip ID bit in EFLAGS           \n"
slouken@881
   109
"        pushq   %%rax               # Save new EFLAGS value on stack  \n"
slouken@881
   110
"        popfq                       # Replace current EFLAGS value    \n"
slouken@881
   111
"        pushfq                      # Get new EFLAGS                  \n"
slouken@881
   112
"        popq    %%rax               # Store new EFLAGS in EAX         \n"
slouken@881
   113
"        xorl    %%ecx,%%eax         # Can not toggle ID bit,          \n"
slouken@881
   114
"        jz      1f                  # Processor=80486                 \n"
slouken@881
   115
"        movl    $1,%0               # We have CPUID support           \n"
slouken@881
   116
"1:                                                                    \n"
slouken@3579
   117
    : "=m" (has_CPUID)
slouken@3579
   118
    :
slouken@3579
   119
    : "%rax", "%rcx"
slouken@3579
   120
    );
slouken@1442
   121
#elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
slouken@3579
   122
    __asm {
slouken@745
   123
        pushfd                      ; Get original EFLAGS
slouken@745
   124
        pop     eax
slouken@745
   125
        mov     ecx, eax
slouken@745
   126
        xor     eax, 200000h        ; Flip ID bit in EFLAGS
slouken@745
   127
        push    eax                 ; Save new EFLAGS value on stack
slouken@745
   128
        popfd                       ; Replace current EFLAGS value
slouken@745
   129
        pushfd                      ; Get new EFLAGS
slouken@745
   130
        pop     eax                 ; Store new EFLAGS in EAX
slouken@745
   131
        xor     eax, ecx            ; Can not toggle ID bit,
slouken@745
   132
        jz      done                ; Processor=80486
slouken@745
   133
        mov     has_CPUID,1         ; We have CPUID support
slouken@745
   134
done:
slouken@3579
   135
    }
slouken@8241
   136
#elif defined(_MSC_VER) && defined(_M_X64)
slouken@8241
   137
    has_CPUID = 1;
slouken@1864
   138
#elif defined(__sun) && defined(__i386)
slouken@3579
   139
    __asm (
icculus@1229
   140
"       pushfl                 \n"
slouken@3584
   141
"       popl    %eax           \n"
slouken@3584
   142
"       movl    %eax,%ecx      \n"
slouken@3584
   143
"       xorl    $0x200000,%eax \n"
slouken@3584
   144
"       pushl   %eax           \n"
slouken@3584
   145
"       popfl                  \n"
slouken@3584
   146
"       pushfl                 \n"
slouken@3584
   147
"       popl    %eax           \n"
slouken@3584
   148
"       xorl    %ecx,%eax      \n"
slouken@3584
   149
"       jz      1f             \n"
slouken@3584
   150
"       movl    $1,-8(%ebp)    \n"
icculus@1229
   151
"1:                            \n"
slouken@3579
   152
    );
icculus@1229
   153
#elif defined(__sun) && defined(__amd64)
slouken@3579
   154
    __asm (
icculus@1229
   155
"       pushfq                 \n"
icculus@1229
   156
"       popq    %rax           \n"
icculus@1229
   157
"       movq    %rax,%rcx      \n"
icculus@1229
   158
"       xorl    $0x200000,%eax \n"
icculus@1229
   159
"       pushq   %rax           \n"
icculus@1229
   160
"       popfq                  \n"
icculus@1229
   161
"       pushfq                 \n"
icculus@1229
   162
"       popq    %rax           \n"
icculus@1229
   163
"       xorl    %ecx,%eax      \n"
icculus@1229
   164
"       jz      1f             \n"
icculus@1229
   165
"       movl    $1,-8(%rbp)    \n"
icculus@1229
   166
"1:                            \n"
slouken@3579
   167
    );
slouken@745
   168
#endif
icculus@9278
   169
#endif
slouken@1895
   170
/* *INDENT-ON* */
slouken@1895
   171
    return has_CPUID;
slouken@745
   172
}
slouken@745
   173
slouken@3587
   174
#if defined(__GNUC__) && defined(i386)
slouken@3580
   175
#define cpuid(func, a, b, c, d) \
slouken@3584
   176
    __asm__ __volatile__ ( \
slouken@3584
   177
"        pushl %%ebx        \n" \
slouken@9003
   178
"        xorl %%ecx,%%ecx   \n" \
slouken@3584
   179
"        cpuid              \n" \
slouken@3584
   180
"        movl %%ebx, %%esi  \n" \
slouken@3584
   181
"        popl %%ebx         \n" : \
slouken@3584
   182
            "=a" (a), "=S" (b), "=c" (c), "=d" (d) : "a" (func))
slouken@3587
   183
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@3587
   184
#define cpuid(func, a, b, c, d) \
slouken@3587
   185
    __asm__ __volatile__ ( \
slouken@3587
   186
"        pushq %%rbx        \n" \
slouken@9003
   187
"        xorq %%rcx,%%rcx   \n" \
slouken@3587
   188
"        cpuid              \n" \
slouken@3587
   189
"        movq %%rbx, %%rsi  \n" \
slouken@3587
   190
"        popq %%rbx         \n" : \
slouken@3587
   191
            "=a" (a), "=S" (b), "=c" (c), "=d" (d) : "a" (func))
slouken@3579
   192
#elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
slouken@3580
   193
#define cpuid(func, a, b, c, d) \
slouken@3580
   194
    __asm { \
slouken@3580
   195
        __asm mov eax, func \
slouken@9003
   196
        __asm xor ecx, ecx \
slouken@3580
   197
        __asm cpuid \
slouken@3580
   198
        __asm mov a, eax \
slouken@3580
   199
        __asm mov b, ebx \
slouken@3580
   200
        __asm mov c, ecx \
slouken@3580
   201
        __asm mov d, edx \
slouken@8241
   202
}
slouken@8241
   203
#elif defined(_MSC_VER) && defined(_M_X64)
slouken@8241
   204
#define cpuid(func, a, b, c, d) \
slouken@8241
   205
{ \
slouken@8241
   206
    int CPUInfo[4]; \
slouken@8241
   207
    __cpuid(CPUInfo, func); \
slouken@8241
   208
    a = CPUInfo[0]; \
slouken@8241
   209
    b = CPUInfo[1]; \
slouken@8241
   210
    c = CPUInfo[2]; \
slouken@8241
   211
    d = CPUInfo[3]; \
slouken@8241
   212
}
slouken@3579
   213
#else
slouken@3580
   214
#define cpuid(func, a, b, c, d) \
slouken@3580
   215
    a = b = c = d = 0
slouken@3579
   216
#endif
slouken@3579
   217
slouken@9003
   218
static int
slouken@1895
   219
CPU_getCPUIDFeatures(void)
slouken@745
   220
{
slouken@1895
   221
    int features = 0;
slouken@3580
   222
    int a, b, c, d;
slouken@3579
   223
slouken@3580
   224
    cpuid(0, a, b, c, d);
slouken@3580
   225
    if (a >= 1) {
slouken@3580
   226
        cpuid(1, a, b, c, d);
slouken@3580
   227
        features = d;
slouken@3579
   228
    }
slouken@1895
   229
    return features;
slouken@745
   230
}
slouken@745
   231
slouken@9003
   232
static SDL_bool
slouken@9003
   233
CPU_OSSavesYMM(void)
slouken@9003
   234
{
slouken@9003
   235
    int a, b, c, d;
slouken@9003
   236
slouken@9003
   237
    /* Check to make sure we can call xgetbv */
slouken@9003
   238
    cpuid(0, a, b, c, d);
slouken@9003
   239
    if (a < 1) {
slouken@9003
   240
        return SDL_FALSE;
slouken@9003
   241
    }
slouken@9003
   242
    cpuid(1, a, b, c, d);
slouken@9003
   243
    if (!(c & 0x08000000)) {
slouken@9003
   244
        return SDL_FALSE;
slouken@9003
   245
    }
slouken@9003
   246
slouken@9003
   247
    /* Call xgetbv to see if YMM register state is saved */
slouken@9003
   248
    a = 0;
slouken@9003
   249
#if defined(__GNUC__) && (defined(i386) || defined(__x86_64__))
slouken@9003
   250
    asm(".byte 0x0f, 0x01, 0xd0" : "=a" (a) : "c" (0) : "%edx");
dludwig@9047
   251
#elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64)) && (_MSC_FULL_VER >= 160040219) /* VS2010 SP1 */
slouken@9003
   252
    a = (int)_xgetbv(0);
slouken@9003
   253
#elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
slouken@9003
   254
    __asm
slouken@9003
   255
    {
slouken@9003
   256
        xor ecx, ecx
slouken@9003
   257
        _asm _emit 0x0f _asm _emit 0x01 _asm _emit 0xd0
slouken@9005
   258
        mov a, eax
slouken@9003
   259
    }
slouken@9003
   260
#endif
slouken@9003
   261
    return ((a & 6) == 6) ? SDL_TRUE : SDL_FALSE;
slouken@9003
   262
}
slouken@9003
   263
slouken@9003
   264
static int
slouken@1895
   265
CPU_haveRDTSC(void)
slouken@745
   266
{
slouken@1895
   267
    if (CPU_haveCPUID()) {
slouken@1895
   268
        return (CPU_getCPUIDFeatures() & 0x00000010);
slouken@1895
   269
    }
slouken@1895
   270
    return 0;
slouken@745
   271
}
slouken@745
   272
slouken@9003
   273
static int
slouken@5389
   274
CPU_haveAltiVec(void)
slouken@5389
   275
{
slouken@5389
   276
    volatile int altivec = 0;
icculus@9278
   277
#ifndef SDL_CPUINFO_DISABLED
slouken@6363
   278
#if (defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))) || (defined(__OpenBSD__) && defined(__powerpc__))
slouken@6363
   279
#ifdef __OpenBSD__
slouken@6363
   280
    int selectors[2] = { CTL_MACHDEP, CPU_ALTIVEC };
slouken@6363
   281
#else
slouken@5389
   282
    int selectors[2] = { CTL_HW, HW_VECTORUNIT };
slouken@6363
   283
#endif
slouken@5389
   284
    int hasVectorUnit = 0;
slouken@5389
   285
    size_t length = sizeof(hasVectorUnit);
slouken@5389
   286
    int error = sysctl(selectors, 2, &hasVectorUnit, &length, NULL, 0);
slouken@5389
   287
    if (0 == error)
slouken@5389
   288
        altivec = (hasVectorUnit != 0);
slouken@5389
   289
#elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
slouken@5389
   290
    void (*handler) (int sig);
slouken@5389
   291
    handler = signal(SIGILL, illegal_instruction);
slouken@5389
   292
    if (setjmp(jmpbuf) == 0) {
slouken@5389
   293
        asm volatile ("mtspr 256, %0\n\t" "vand %%v0, %%v0, %%v0"::"r" (-1));
slouken@5389
   294
        altivec = 1;
slouken@5389
   295
    }
slouken@5389
   296
    signal(SIGILL, handler);
slouken@5389
   297
#endif
icculus@9278
   298
#endif
slouken@5389
   299
    return altivec;
slouken@5389
   300
}
slouken@5389
   301
slouken@9003
   302
static int
slouken@1895
   303
CPU_haveMMX(void)
slouken@745
   304
{
slouken@1895
   305
    if (CPU_haveCPUID()) {
slouken@1895
   306
        return (CPU_getCPUIDFeatures() & 0x00800000);
slouken@1895
   307
    }
slouken@1895
   308
    return 0;
slouken@745
   309
}
slouken@745
   310
slouken@9003
   311
static int
slouken@5389
   312
CPU_have3DNow(void)
slouken@5389
   313
{
slouken@5389
   314
    if (CPU_haveCPUID()) {
slouken@5389
   315
        int a, b, c, d;
slouken@5389
   316
slouken@5389
   317
        cpuid(0x80000000, a, b, c, d);
slouken@5389
   318
        if (a >= 0x80000001) {
slouken@5389
   319
            cpuid(0x80000001, a, b, c, d);
slouken@5389
   320
            return (d & 0x80000000);
slouken@5389
   321
        }
slouken@5389
   322
    }
slouken@5389
   323
    return 0;
slouken@5389
   324
}
slouken@5389
   325
slouken@9003
   326
static int
slouken@1895
   327
CPU_haveSSE(void)
slouken@745
   328
{
slouken@1895
   329
    if (CPU_haveCPUID()) {
slouken@1895
   330
        return (CPU_getCPUIDFeatures() & 0x02000000);
slouken@1895
   331
    }
slouken@1895
   332
    return 0;
slouken@745
   333
}
slouken@739
   334
slouken@9003
   335
static int
slouken@1895
   336
CPU_haveSSE2(void)
slouken@785
   337
{
slouken@1895
   338
    if (CPU_haveCPUID()) {
slouken@1895
   339
        return (CPU_getCPUIDFeatures() & 0x04000000);
slouken@1895
   340
    }
slouken@1895
   341
    return 0;
slouken@785
   342
}
slouken@785
   343
slouken@9003
   344
static int
slouken@5259
   345
CPU_haveSSE3(void)
slouken@778
   346
{
slouken@5259
   347
    if (CPU_haveCPUID()) {
slouken@5259
   348
        int a, b, c, d;
slouken@5259
   349
slouken@5259
   350
        cpuid(0, a, b, c, d);
slouken@5259
   351
        if (a >= 1) {
slouken@5259
   352
            cpuid(1, a, b, c, d);
slouken@5259
   353
            return (c & 0x00000001);
slouken@5259
   354
        }
slouken@1895
   355
    }
slouken@5259
   356
    return 0;
slouken@5259
   357
}
slouken@5259
   358
slouken@9003
   359
static int
slouken@5263
   360
CPU_haveSSE41(void)
slouken@5259
   361
{
slouken@5259
   362
    if (CPU_haveCPUID()) {
slouken@5259
   363
        int a, b, c, d;
slouken@5259
   364
slouken@9003
   365
        cpuid(0, a, b, c, d);
slouken@5259
   366
        if (a >= 1) {
slouken@5259
   367
            cpuid(1, a, b, c, d);
slouken@5263
   368
            return (c & 0x00080000);
slouken@5263
   369
        }
slouken@5263
   370
    }
slouken@5263
   371
    return 0;
slouken@5263
   372
}
slouken@5263
   373
slouken@9003
   374
static int
slouken@5263
   375
CPU_haveSSE42(void)
slouken@5263
   376
{
slouken@5263
   377
    if (CPU_haveCPUID()) {
slouken@5263
   378
        int a, b, c, d;
slouken@5263
   379
slouken@9003
   380
        cpuid(0, a, b, c, d);
slouken@5263
   381
        if (a >= 1) {
slouken@5263
   382
            cpuid(1, a, b, c, d);
slouken@5263
   383
            return (c & 0x00100000);
slouken@5259
   384
        }
slouken@5259
   385
    }
slouken@5259
   386
    return 0;
slouken@778
   387
}
slouken@778
   388
slouken@9003
   389
static int
slouken@8148
   390
CPU_haveAVX(void)
slouken@8148
   391
{
slouken@9003
   392
    if (CPU_haveCPUID() && CPU_OSSavesYMM()) {
slouken@8148
   393
        int a, b, c, d;
slouken@8148
   394
slouken@9003
   395
        cpuid(0, a, b, c, d);
slouken@8148
   396
        if (a >= 1) {
slouken@8148
   397
            cpuid(1, a, b, c, d);
slouken@8148
   398
            return (c & 0x10000000);
slouken@8148
   399
        }
slouken@8148
   400
    }
slouken@8148
   401
    return 0;
slouken@8148
   402
}
slouken@8148
   403
slouken@9003
   404
static int
slouken@9003
   405
CPU_haveAVX2(void)
slouken@9003
   406
{
slouken@9003
   407
    if (CPU_haveCPUID() && CPU_OSSavesYMM()) {
slouken@9003
   408
        int a, b, c, d;
slouken@9003
   409
slouken@9003
   410
        cpuid(0, a, b, c, d);
slouken@9003
   411
        if (a >= 7) {
slouken@9003
   412
            cpuid(7, a, b, c, d);
slouken@9003
   413
            return (b & 0x00000020);
slouken@9003
   414
        }
slouken@9003
   415
    }
slouken@9003
   416
    return 0;
slouken@9003
   417
}
slouken@9003
   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
icculus@9278
   444
#endif
slouken@3579
   445
        /* There has to be at least 1, right? :) */
slouken@3586
   446
        if (SDL_CPUCount <= 0) {
slouken@3579
   447
            SDL_CPUCount = 1;
slouken@3579
   448
        }
slouken@3579
   449
    }
slouken@3579
   450
    return SDL_CPUCount;
slouken@3579
   451
}
slouken@3579
   452
slouken@3579
   453
/* Oh, such a sweet sweet trick, just not very useful. :) */
slouken@4472
   454
static const char *
slouken@5120
   455
SDL_GetCPUType(void)
slouken@3579
   456
{
slouken@5115
   457
    static char SDL_CPUType[13];
slouken@3579
   458
slouken@3579
   459
    if (!SDL_CPUType[0]) {
slouken@3579
   460
        int i = 0;
slouken@3580
   461
        int a, b, c, d;
slouken@3579
   462
slouken@3579
   463
        if (CPU_haveCPUID()) {
slouken@5115
   464
            cpuid(0x00000000, a, b, c, d);
slouken@5115
   465
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   466
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   467
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
icculus@8642
   468
            SDL_CPUType[i++] = (char)(b & 0xff);
icculus@8642
   469
slouken@5115
   470
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   471
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   472
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
icculus@8642
   473
            SDL_CPUType[i++] = (char)(d & 0xff);
icculus@8642
   474
slouken@5115
   475
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   476
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   477
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
icculus@8642
   478
            SDL_CPUType[i++] = (char)(c & 0xff);
slouken@5115
   479
        }
slouken@5115
   480
        if (!SDL_CPUType[0]) {
slouken@5115
   481
            SDL_strlcpy(SDL_CPUType, "Unknown", sizeof(SDL_CPUType));
slouken@5115
   482
        }
slouken@5115
   483
    }
slouken@5115
   484
    return SDL_CPUType;
slouken@5115
   485
}
slouken@5115
   486
icculus@5981
   487
icculus@5981
   488
#ifdef TEST_MAIN  /* !!! FIXME: only used for test at the moment. */
slouken@5115
   489
static const char *
slouken@5120
   490
SDL_GetCPUName(void)
slouken@5115
   491
{
slouken@5115
   492
    static char SDL_CPUName[48];
slouken@5115
   493
slouken@5115
   494
    if (!SDL_CPUName[0]) {
slouken@5115
   495
        int i = 0;
slouken@5115
   496
        int a, b, c, d;
slouken@5115
   497
slouken@5115
   498
        if (CPU_haveCPUID()) {
slouken@3580
   499
            cpuid(0x80000000, a, b, c, d);
slouken@3580
   500
            if (a >= 0x80000004) {
slouken@3580
   501
                cpuid(0x80000002, a, b, c, d);
slouken@5115
   502
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   503
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   504
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   505
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   506
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   507
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   508
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   509
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   510
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   511
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   512
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   513
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   514
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   515
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   516
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   517
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@3580
   518
                cpuid(0x80000003, a, b, c, d);
slouken@5115
   519
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   520
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   521
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   522
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   523
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   524
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   525
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   526
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   527
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   528
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   529
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   530
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   531
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   532
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   533
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   534
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@3580
   535
                cpuid(0x80000004, a, b, c, d);
slouken@5115
   536
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   537
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   538
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   539
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   540
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   541
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   542
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   543
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   544
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   545
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   546
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   547
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   548
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   549
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   550
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   551
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@3579
   552
            }
slouken@3579
   553
        }
slouken@5115
   554
        if (!SDL_CPUName[0]) {
slouken@5115
   555
            SDL_strlcpy(SDL_CPUName, "Unknown", sizeof(SDL_CPUName));
slouken@3579
   556
        }
slouken@3579
   557
    }
slouken@5115
   558
    return SDL_CPUName;
slouken@5115
   559
}
icculus@5981
   560
#endif
slouken@5115
   561
slouken@5120
   562
int
slouken@5120
   563
SDL_GetCPUCacheLineSize(void)
slouken@5115
   564
{
slouken@5115
   565
    const char *cpuType = SDL_GetCPUType();
slouken@5115
   566
slouken@5115
   567
    if (SDL_strcmp(cpuType, "GenuineIntel") == 0) {
slouken@5115
   568
        int a, b, c, d;
slouken@5115
   569
slouken@5115
   570
        cpuid(0x00000001, a, b, c, d);
slouken@5115
   571
        return (((b >> 8) & 0xff) * 8);
slouken@5115
   572
    } else if (SDL_strcmp(cpuType, "AuthenticAMD") == 0) {
slouken@5115
   573
        int a, b, c, d;
slouken@5115
   574
slouken@5115
   575
        cpuid(0x80000005, a, b, c, d);
slouken@5115
   576
        return (c & 0xff);
slouken@5115
   577
    } else {
slouken@5115
   578
        /* Just make a guess here... */
slouken@5115
   579
        return SDL_CACHELINE_SIZE;
slouken@5115
   580
    }
slouken@3579
   581
}
slouken@3579
   582
slouken@739
   583
static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
slouken@739
   584
slouken@1895
   585
static Uint32
slouken@1895
   586
SDL_GetCPUFeatures(void)
slouken@739
   587
{
slouken@1895
   588
    if (SDL_CPUFeatures == 0xFFFFFFFF) {
slouken@1895
   589
        SDL_CPUFeatures = 0;
slouken@1895
   590
        if (CPU_haveRDTSC()) {
slouken@1895
   591
            SDL_CPUFeatures |= CPU_HAS_RDTSC;
slouken@1895
   592
        }
slouken@5389
   593
        if (CPU_haveAltiVec()) {
slouken@5389
   594
            SDL_CPUFeatures |= CPU_HAS_ALTIVEC;
slouken@5389
   595
        }
slouken@1895
   596
        if (CPU_haveMMX()) {
slouken@1895
   597
            SDL_CPUFeatures |= CPU_HAS_MMX;
slouken@1895
   598
        }
slouken@5389
   599
        if (CPU_have3DNow()) {
slouken@5389
   600
            SDL_CPUFeatures |= CPU_HAS_3DNOW;
slouken@5389
   601
        }
slouken@1895
   602
        if (CPU_haveSSE()) {
slouken@1895
   603
            SDL_CPUFeatures |= CPU_HAS_SSE;
slouken@1895
   604
        }
slouken@1895
   605
        if (CPU_haveSSE2()) {
slouken@1895
   606
            SDL_CPUFeatures |= CPU_HAS_SSE2;
slouken@1895
   607
        }
slouken@5259
   608
        if (CPU_haveSSE3()) {
slouken@5259
   609
            SDL_CPUFeatures |= CPU_HAS_SSE3;
slouken@5259
   610
        }
slouken@5263
   611
        if (CPU_haveSSE41()) {
slouken@5263
   612
            SDL_CPUFeatures |= CPU_HAS_SSE41;
slouken@5263
   613
        }
slouken@5263
   614
        if (CPU_haveSSE42()) {
slouken@5263
   615
            SDL_CPUFeatures |= CPU_HAS_SSE42;
slouken@1895
   616
        }
slouken@8148
   617
        if (CPU_haveAVX()) {
slouken@8148
   618
            SDL_CPUFeatures |= CPU_HAS_AVX;
slouken@8148
   619
        }
slouken@9003
   620
        if (CPU_haveAVX2()) {
slouken@9003
   621
            SDL_CPUFeatures |= CPU_HAS_AVX2;
slouken@9003
   622
        }
slouken@1895
   623
    }
slouken@1895
   624
    return SDL_CPUFeatures;
slouken@739
   625
}
slouken@739
   626
slouken@1895
   627
SDL_bool
slouken@1895
   628
SDL_HasRDTSC(void)
slouken@745
   629
{
slouken@1895
   630
    if (SDL_GetCPUFeatures() & CPU_HAS_RDTSC) {
slouken@1895
   631
        return SDL_TRUE;
slouken@1895
   632
    }
slouken@1895
   633
    return SDL_FALSE;
slouken@745
   634
}
slouken@745
   635
slouken@1895
   636
SDL_bool
slouken@5389
   637
SDL_HasAltiVec(void)
slouken@5389
   638
{
slouken@5389
   639
    if (SDL_GetCPUFeatures() & CPU_HAS_ALTIVEC) {
slouken@5389
   640
        return SDL_TRUE;
slouken@5389
   641
    }
slouken@5389
   642
    return SDL_FALSE;
slouken@5389
   643
}
slouken@5389
   644
slouken@5389
   645
SDL_bool
slouken@1895
   646
SDL_HasMMX(void)
slouken@739
   647
{
slouken@1895
   648
    if (SDL_GetCPUFeatures() & CPU_HAS_MMX) {
slouken@1895
   649
        return SDL_TRUE;
slouken@1895
   650
    }
slouken@1895
   651
    return SDL_FALSE;
slouken@739
   652
}
slouken@739
   653
slouken@1895
   654
SDL_bool
slouken@5389
   655
SDL_Has3DNow(void)
slouken@5389
   656
{
slouken@5389
   657
    if (SDL_GetCPUFeatures() & CPU_HAS_3DNOW) {
slouken@5389
   658
        return SDL_TRUE;
slouken@5389
   659
    }
slouken@5389
   660
    return SDL_FALSE;
slouken@5389
   661
}
slouken@5389
   662
slouken@5389
   663
SDL_bool
slouken@1895
   664
SDL_HasSSE(void)
slouken@739
   665
{
slouken@1895
   666
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE) {
slouken@1895
   667
        return SDL_TRUE;
slouken@1895
   668
    }
slouken@1895
   669
    return SDL_FALSE;
slouken@739
   670
}
slouken@739
   671
slouken@1895
   672
SDL_bool
slouken@1895
   673
SDL_HasSSE2(void)
slouken@804
   674
{
slouken@1895
   675
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE2) {
slouken@1895
   676
        return SDL_TRUE;
slouken@1895
   677
    }
slouken@1895
   678
    return SDL_FALSE;
slouken@804
   679
}
slouken@804
   680
slouken@1895
   681
SDL_bool
slouken@5259
   682
SDL_HasSSE3(void)
slouken@778
   683
{
slouken@5259
   684
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE3) {
slouken@5259
   685
        return SDL_TRUE;
slouken@5259
   686
    }
slouken@5259
   687
    return SDL_FALSE;
slouken@5259
   688
}
slouken@5259
   689
slouken@5259
   690
SDL_bool
slouken@5263
   691
SDL_HasSSE41(void)
slouken@5259
   692
{
slouken@5263
   693
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE41) {
slouken@5263
   694
        return SDL_TRUE;
slouken@5263
   695
    }
slouken@5263
   696
    return SDL_FALSE;
slouken@5263
   697
}
slouken@5263
   698
slouken@5263
   699
SDL_bool
slouken@5263
   700
SDL_HasSSE42(void)
slouken@5263
   701
{
slouken@5263
   702
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE42) {
slouken@1895
   703
        return SDL_TRUE;
slouken@1895
   704
    }
slouken@1895
   705
    return SDL_FALSE;
slouken@778
   706
}
slouken@778
   707
slouken@8148
   708
SDL_bool
slouken@8148
   709
SDL_HasAVX(void)
slouken@8148
   710
{
slouken@8148
   711
    if (SDL_GetCPUFeatures() & CPU_HAS_AVX) {
slouken@8148
   712
        return SDL_TRUE;
slouken@8148
   713
    }
slouken@8148
   714
    return SDL_FALSE;
slouken@8148
   715
}
slouken@8148
   716
slouken@9003
   717
SDL_bool
slouken@9003
   718
SDL_HasAVX2(void)
slouken@9003
   719
{
slouken@9003
   720
    if (SDL_GetCPUFeatures() & CPU_HAS_AVX2) {
slouken@9003
   721
        return SDL_TRUE;
slouken@9003
   722
    }
slouken@9003
   723
    return SDL_FALSE;
slouken@9003
   724
}
slouken@9003
   725
slouken@7821
   726
static int SDL_SystemRAM = 0;
slouken@7821
   727
slouken@7821
   728
int
slouken@7821
   729
SDL_GetSystemRAM(void)
slouken@7821
   730
{
slouken@7821
   731
    if (!SDL_SystemRAM) {
icculus@9278
   732
#ifndef SDL_CPUINFO_DISABLED
slouken@7821
   733
#if defined(HAVE_SYSCONF) && defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
slouken@7821
   734
        if (SDL_SystemRAM <= 0) {
slouken@7821
   735
            SDL_SystemRAM = (int)((Sint64)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE) / (1024*1024));
slouken@7821
   736
        }
slouken@7821
   737
#endif
slouken@7821
   738
#ifdef HAVE_SYSCTLBYNAME
slouken@7821
   739
        if (SDL_SystemRAM <= 0) {
slouken@8240
   740
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
slouken@7826
   741
#ifdef HW_REALMEM
slouken@7826
   742
            int mib[2] = {CTL_HW, HW_REALMEM};
slouken@7826
   743
#else
slouken@7826
   744
            /* might only report up to 2 GiB */
slouken@7826
   745
            int mib[2] = {CTL_HW, HW_PHYSMEM};
slouken@7826
   746
#endif /* HW_REALMEM */
slouken@7826
   747
#else
slouken@7821
   748
            int mib[2] = {CTL_HW, HW_MEMSIZE};
slouken@8240
   749
#endif /* __FreeBSD__ || __FreeBSD_kernel__ */
slouken@7823
   750
            Uint64 memsize = 0;
slouken@7821
   751
            size_t len = sizeof(memsize);
slouken@7821
   752
            
slouken@7821
   753
            if (sysctl(mib, 2, &memsize, &len, NULL, 0) == 0) {
slouken@7821
   754
                SDL_SystemRAM = (int)(memsize / (1024*1024));
slouken@7821
   755
            }
slouken@7821
   756
        }
slouken@7821
   757
#endif
slouken@7821
   758
#ifdef __WIN32__
slouken@7821
   759
        if (SDL_SystemRAM <= 0) {
slouken@7821
   760
            MEMORYSTATUSEX stat;
icculus@7889
   761
            stat.dwLength = sizeof(stat);
slouken@7821
   762
            if (GlobalMemoryStatusEx(&stat)) {
slouken@7821
   763
                SDL_SystemRAM = (int)(stat.ullTotalPhys / (1024 * 1024));
slouken@7821
   764
            }
slouken@7821
   765
        }
slouken@7821
   766
#endif
icculus@9278
   767
#endif
slouken@7821
   768
    }
slouken@7821
   769
    return SDL_SystemRAM;
slouken@7821
   770
}
slouken@7821
   771
slouken@7821
   772
slouken@739
   773
#ifdef TEST_MAIN
slouken@739
   774
slouken@739
   775
#include <stdio.h>
slouken@739
   776
slouken@1895
   777
int
slouken@1895
   778
main()
slouken@739
   779
{
slouken@3579
   780
    printf("CPU count: %d\n", SDL_GetCPUCount());
slouken@5115
   781
    printf("CPU type: %s\n", SDL_GetCPUType());
slouken@5115
   782
    printf("CPU name: %s\n", SDL_GetCPUName());
slouken@5115
   783
    printf("CacheLine size: %d\n", SDL_GetCPUCacheLineSize());
slouken@1895
   784
    printf("RDTSC: %d\n", SDL_HasRDTSC());
slouken@5389
   785
    printf("Altivec: %d\n", SDL_HasAltiVec());
slouken@1895
   786
    printf("MMX: %d\n", SDL_HasMMX());
slouken@5389
   787
    printf("3DNow: %d\n", SDL_Has3DNow());
slouken@1895
   788
    printf("SSE: %d\n", SDL_HasSSE());
slouken@1895
   789
    printf("SSE2: %d\n", SDL_HasSSE2());
slouken@5259
   790
    printf("SSE3: %d\n", SDL_HasSSE3());
slouken@5263
   791
    printf("SSE4.1: %d\n", SDL_HasSSE41());
slouken@5263
   792
    printf("SSE4.2: %d\n", SDL_HasSSE42());
slouken@8148
   793
    printf("AVX: %d\n", SDL_HasAVX());
slouken@9003
   794
    printf("AVX2: %d\n", SDL_HasAVX2());
slouken@7821
   795
    printf("RAM: %d MB\n", SDL_GetSystemRAM());
slouken@1895
   796
    return 0;
slouken@739
   797
}
slouken@739
   798
slouken@739
   799
#endif /* TEST_MAIN */
slouken@1895
   800
slouken@1895
   801
/* vi: set ts=4 sw=4 expandtab: */