src/cpuinfo/SDL_cpuinfo.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Jul 2014 22:15:34 -0700
changeset 9005 ef3ef3c8da98
parent 9004 51bc44d49052
child 9047 101109110bf7
permissions -rw-r--r--
Fixed Visual Studio 2010 build
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* */
slouken@745
    82
#if defined(__GNUC__) && defined(i386)
slouken@3579
    83
    __asm__ (
slouken@745
    84
"        pushfl                      # Get original EFLAGS             \n"
slouken@745
    85
"        popl    %%eax                                                 \n"
slouken@745
    86
"        movl    %%eax,%%ecx                                           \n"
slouken@745
    87
"        xorl    $0x200000,%%eax     # Flip ID bit in EFLAGS           \n"
slouken@745
    88
"        pushl   %%eax               # Save new EFLAGS value on stack  \n"
slouken@745
    89
"        popfl                       # Replace current EFLAGS value    \n"
slouken@745
    90
"        pushfl                      # Get new EFLAGS                  \n"
slouken@745
    91
"        popl    %%eax               # Store new EFLAGS in EAX         \n"
slouken@745
    92
"        xorl    %%ecx,%%eax         # Can not toggle ID bit,          \n"
slouken@745
    93
"        jz      1f                  # Processor=80486                 \n"
slouken@745
    94
"        movl    $1,%0               # We have CPUID support           \n"
slouken@745
    95
"1:                                                                    \n"
slouken@3579
    96
    : "=m" (has_CPUID)
slouken@3579
    97
    :
slouken@3579
    98
    : "%eax", "%ecx"
slouken@3579
    99
    );
slouken@881
   100
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@881
   101
/* Technically, if this is being compiled under __x86_64__ then it has 
slouken@8241
   102
   CPUid by definition.  But it's nice to be able to prove it.  :)      */
slouken@3579
   103
    __asm__ (
slouken@881
   104
"        pushfq                      # Get original EFLAGS             \n"
slouken@881
   105
"        popq    %%rax                                                 \n"
slouken@881
   106
"        movq    %%rax,%%rcx                                           \n"
slouken@881
   107
"        xorl    $0x200000,%%eax     # Flip ID bit in EFLAGS           \n"
slouken@881
   108
"        pushq   %%rax               # Save new EFLAGS value on stack  \n"
slouken@881
   109
"        popfq                       # Replace current EFLAGS value    \n"
slouken@881
   110
"        pushfq                      # Get new EFLAGS                  \n"
slouken@881
   111
"        popq    %%rax               # Store new EFLAGS in EAX         \n"
slouken@881
   112
"        xorl    %%ecx,%%eax         # Can not toggle ID bit,          \n"
slouken@881
   113
"        jz      1f                  # Processor=80486                 \n"
slouken@881
   114
"        movl    $1,%0               # We have CPUID support           \n"
slouken@881
   115
"1:                                                                    \n"
slouken@3579
   116
    : "=m" (has_CPUID)
slouken@3579
   117
    :
slouken@3579
   118
    : "%rax", "%rcx"
slouken@3579
   119
    );
slouken@1442
   120
#elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
slouken@3579
   121
    __asm {
slouken@745
   122
        pushfd                      ; Get original EFLAGS
slouken@745
   123
        pop     eax
slouken@745
   124
        mov     ecx, eax
slouken@745
   125
        xor     eax, 200000h        ; Flip ID bit in EFLAGS
slouken@745
   126
        push    eax                 ; Save new EFLAGS value on stack
slouken@745
   127
        popfd                       ; Replace current EFLAGS value
slouken@745
   128
        pushfd                      ; Get new EFLAGS
slouken@745
   129
        pop     eax                 ; Store new EFLAGS in EAX
slouken@745
   130
        xor     eax, ecx            ; Can not toggle ID bit,
slouken@745
   131
        jz      done                ; Processor=80486
slouken@745
   132
        mov     has_CPUID,1         ; We have CPUID support
slouken@745
   133
done:
slouken@3579
   134
    }
slouken@8241
   135
#elif defined(_MSC_VER) && defined(_M_X64)
slouken@8241
   136
    has_CPUID = 1;
slouken@1864
   137
#elif defined(__sun) && defined(__i386)
slouken@3579
   138
    __asm (
icculus@1229
   139
"       pushfl                 \n"
slouken@3584
   140
"       popl    %eax           \n"
slouken@3584
   141
"       movl    %eax,%ecx      \n"
slouken@3584
   142
"       xorl    $0x200000,%eax \n"
slouken@3584
   143
"       pushl   %eax           \n"
slouken@3584
   144
"       popfl                  \n"
slouken@3584
   145
"       pushfl                 \n"
slouken@3584
   146
"       popl    %eax           \n"
slouken@3584
   147
"       xorl    %ecx,%eax      \n"
slouken@3584
   148
"       jz      1f             \n"
slouken@3584
   149
"       movl    $1,-8(%ebp)    \n"
icculus@1229
   150
"1:                            \n"
slouken@3579
   151
    );
icculus@1229
   152
#elif defined(__sun) && defined(__amd64)
slouken@3579
   153
    __asm (
icculus@1229
   154
"       pushfq                 \n"
icculus@1229
   155
"       popq    %rax           \n"
icculus@1229
   156
"       movq    %rax,%rcx      \n"
icculus@1229
   157
"       xorl    $0x200000,%eax \n"
icculus@1229
   158
"       pushq   %rax           \n"
icculus@1229
   159
"       popfq                  \n"
icculus@1229
   160
"       pushfq                 \n"
icculus@1229
   161
"       popq    %rax           \n"
icculus@1229
   162
"       xorl    %ecx,%eax      \n"
icculus@1229
   163
"       jz      1f             \n"
icculus@1229
   164
"       movl    $1,-8(%rbp)    \n"
icculus@1229
   165
"1:                            \n"
slouken@3579
   166
    );
slouken@745
   167
#endif
slouken@1895
   168
/* *INDENT-ON* */
slouken@1895
   169
    return has_CPUID;
slouken@745
   170
}
slouken@745
   171
slouken@3587
   172
#if defined(__GNUC__) && defined(i386)
slouken@3580
   173
#define cpuid(func, a, b, c, d) \
slouken@3584
   174
    __asm__ __volatile__ ( \
slouken@3584
   175
"        pushl %%ebx        \n" \
slouken@9003
   176
"        xorl %%ecx,%%ecx   \n" \
slouken@3584
   177
"        cpuid              \n" \
slouken@3584
   178
"        movl %%ebx, %%esi  \n" \
slouken@3584
   179
"        popl %%ebx         \n" : \
slouken@3584
   180
            "=a" (a), "=S" (b), "=c" (c), "=d" (d) : "a" (func))
slouken@3587
   181
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@3587
   182
#define cpuid(func, a, b, c, d) \
slouken@3587
   183
    __asm__ __volatile__ ( \
slouken@3587
   184
"        pushq %%rbx        \n" \
slouken@9003
   185
"        xorq %%rcx,%%rcx   \n" \
slouken@3587
   186
"        cpuid              \n" \
slouken@3587
   187
"        movq %%rbx, %%rsi  \n" \
slouken@3587
   188
"        popq %%rbx         \n" : \
slouken@3587
   189
            "=a" (a), "=S" (b), "=c" (c), "=d" (d) : "a" (func))
slouken@3579
   190
#elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
slouken@3580
   191
#define cpuid(func, a, b, c, d) \
slouken@3580
   192
    __asm { \
slouken@3580
   193
        __asm mov eax, func \
slouken@9003
   194
        __asm xor ecx, ecx \
slouken@3580
   195
        __asm cpuid \
slouken@3580
   196
        __asm mov a, eax \
slouken@3580
   197
        __asm mov b, ebx \
slouken@3580
   198
        __asm mov c, ecx \
slouken@3580
   199
        __asm mov d, edx \
slouken@8241
   200
}
slouken@8241
   201
#elif defined(_MSC_VER) && defined(_M_X64)
slouken@8241
   202
#define cpuid(func, a, b, c, d) \
slouken@8241
   203
{ \
slouken@8241
   204
    int CPUInfo[4]; \
slouken@8241
   205
    __cpuid(CPUInfo, func); \
slouken@8241
   206
    a = CPUInfo[0]; \
slouken@8241
   207
    b = CPUInfo[1]; \
slouken@8241
   208
    c = CPUInfo[2]; \
slouken@8241
   209
    d = CPUInfo[3]; \
slouken@8241
   210
}
slouken@3579
   211
#else
slouken@3580
   212
#define cpuid(func, a, b, c, d) \
slouken@3580
   213
    a = b = c = d = 0
slouken@3579
   214
#endif
slouken@3579
   215
slouken@9003
   216
static int
slouken@1895
   217
CPU_getCPUIDFeatures(void)
slouken@745
   218
{
slouken@1895
   219
    int features = 0;
slouken@3580
   220
    int a, b, c, d;
slouken@3579
   221
slouken@3580
   222
    cpuid(0, a, b, c, d);
slouken@3580
   223
    if (a >= 1) {
slouken@3580
   224
        cpuid(1, a, b, c, d);
slouken@3580
   225
        features = d;
slouken@3579
   226
    }
slouken@1895
   227
    return features;
slouken@745
   228
}
slouken@745
   229
slouken@9003
   230
static SDL_bool
slouken@9003
   231
CPU_OSSavesYMM(void)
slouken@9003
   232
{
slouken@9003
   233
    int a, b, c, d;
slouken@9003
   234
slouken@9003
   235
    /* Check to make sure we can call xgetbv */
slouken@9003
   236
    cpuid(0, a, b, c, d);
slouken@9003
   237
    if (a < 1) {
slouken@9003
   238
        return SDL_FALSE;
slouken@9003
   239
    }
slouken@9003
   240
    cpuid(1, a, b, c, d);
slouken@9003
   241
    if (!(c & 0x08000000)) {
slouken@9003
   242
        return SDL_FALSE;
slouken@9003
   243
    }
slouken@9003
   244
slouken@9003
   245
    /* Call xgetbv to see if YMM register state is saved */
slouken@9003
   246
    a = 0;
slouken@9003
   247
#if defined(__GNUC__) && (defined(i386) || defined(__x86_64__))
slouken@9003
   248
    asm(".byte 0x0f, 0x01, 0xd0" : "=a" (a) : "c" (0) : "%edx");
slouken@9003
   249
#elif defined(_MSC_VER) && (_MSC_FULL_VER >= 160040219) /* VS2010 SP1 */
slouken@9003
   250
    a = (int)_xgetbv(0);
slouken@9003
   251
#elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
slouken@9003
   252
    __asm
slouken@9003
   253
    {
slouken@9003
   254
        xor ecx, ecx
slouken@9003
   255
        _asm _emit 0x0f _asm _emit 0x01 _asm _emit 0xd0
slouken@9005
   256
        mov a, eax
slouken@9003
   257
    }
slouken@9003
   258
#endif
slouken@9003
   259
    return ((a & 6) == 6) ? SDL_TRUE : SDL_FALSE;
slouken@9003
   260
}
slouken@9003
   261
slouken@9003
   262
static int
slouken@1895
   263
CPU_haveRDTSC(void)
slouken@745
   264
{
slouken@1895
   265
    if (CPU_haveCPUID()) {
slouken@1895
   266
        return (CPU_getCPUIDFeatures() & 0x00000010);
slouken@1895
   267
    }
slouken@1895
   268
    return 0;
slouken@745
   269
}
slouken@745
   270
slouken@9003
   271
static int
slouken@5389
   272
CPU_haveAltiVec(void)
slouken@5389
   273
{
slouken@5389
   274
    volatile int altivec = 0;
slouken@6363
   275
#if (defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))) || (defined(__OpenBSD__) && defined(__powerpc__))
slouken@6363
   276
#ifdef __OpenBSD__
slouken@6363
   277
    int selectors[2] = { CTL_MACHDEP, CPU_ALTIVEC };
slouken@6363
   278
#else
slouken@5389
   279
    int selectors[2] = { CTL_HW, HW_VECTORUNIT };
slouken@6363
   280
#endif
slouken@5389
   281
    int hasVectorUnit = 0;
slouken@5389
   282
    size_t length = sizeof(hasVectorUnit);
slouken@5389
   283
    int error = sysctl(selectors, 2, &hasVectorUnit, &length, NULL, 0);
slouken@5389
   284
    if (0 == error)
slouken@5389
   285
        altivec = (hasVectorUnit != 0);
slouken@5389
   286
#elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
slouken@5389
   287
    void (*handler) (int sig);
slouken@5389
   288
    handler = signal(SIGILL, illegal_instruction);
slouken@5389
   289
    if (setjmp(jmpbuf) == 0) {
slouken@5389
   290
        asm volatile ("mtspr 256, %0\n\t" "vand %%v0, %%v0, %%v0"::"r" (-1));
slouken@5389
   291
        altivec = 1;
slouken@5389
   292
    }
slouken@5389
   293
    signal(SIGILL, handler);
slouken@5389
   294
#endif
slouken@5389
   295
    return altivec;
slouken@5389
   296
}
slouken@5389
   297
slouken@9003
   298
static int
slouken@1895
   299
CPU_haveMMX(void)
slouken@745
   300
{
slouken@1895
   301
    if (CPU_haveCPUID()) {
slouken@1895
   302
        return (CPU_getCPUIDFeatures() & 0x00800000);
slouken@1895
   303
    }
slouken@1895
   304
    return 0;
slouken@745
   305
}
slouken@745
   306
slouken@9003
   307
static int
slouken@5389
   308
CPU_have3DNow(void)
slouken@5389
   309
{
slouken@5389
   310
    if (CPU_haveCPUID()) {
slouken@5389
   311
        int a, b, c, d;
slouken@5389
   312
slouken@5389
   313
        cpuid(0x80000000, a, b, c, d);
slouken@5389
   314
        if (a >= 0x80000001) {
slouken@5389
   315
            cpuid(0x80000001, a, b, c, d);
slouken@5389
   316
            return (d & 0x80000000);
slouken@5389
   317
        }
slouken@5389
   318
    }
slouken@5389
   319
    return 0;
slouken@5389
   320
}
slouken@5389
   321
slouken@9003
   322
static int
slouken@1895
   323
CPU_haveSSE(void)
slouken@745
   324
{
slouken@1895
   325
    if (CPU_haveCPUID()) {
slouken@1895
   326
        return (CPU_getCPUIDFeatures() & 0x02000000);
slouken@1895
   327
    }
slouken@1895
   328
    return 0;
slouken@745
   329
}
slouken@739
   330
slouken@9003
   331
static int
slouken@1895
   332
CPU_haveSSE2(void)
slouken@785
   333
{
slouken@1895
   334
    if (CPU_haveCPUID()) {
slouken@1895
   335
        return (CPU_getCPUIDFeatures() & 0x04000000);
slouken@1895
   336
    }
slouken@1895
   337
    return 0;
slouken@785
   338
}
slouken@785
   339
slouken@9003
   340
static int
slouken@5259
   341
CPU_haveSSE3(void)
slouken@778
   342
{
slouken@5259
   343
    if (CPU_haveCPUID()) {
slouken@5259
   344
        int a, b, c, d;
slouken@5259
   345
slouken@5259
   346
        cpuid(0, a, b, c, d);
slouken@5259
   347
        if (a >= 1) {
slouken@5259
   348
            cpuid(1, a, b, c, d);
slouken@5259
   349
            return (c & 0x00000001);
slouken@5259
   350
        }
slouken@1895
   351
    }
slouken@5259
   352
    return 0;
slouken@5259
   353
}
slouken@5259
   354
slouken@9003
   355
static int
slouken@5263
   356
CPU_haveSSE41(void)
slouken@5259
   357
{
slouken@5259
   358
    if (CPU_haveCPUID()) {
slouken@5259
   359
        int a, b, c, d;
slouken@5259
   360
slouken@9003
   361
        cpuid(0, a, b, c, d);
slouken@5259
   362
        if (a >= 1) {
slouken@5259
   363
            cpuid(1, a, b, c, d);
slouken@5263
   364
            return (c & 0x00080000);
slouken@5263
   365
        }
slouken@5263
   366
    }
slouken@5263
   367
    return 0;
slouken@5263
   368
}
slouken@5263
   369
slouken@9003
   370
static int
slouken@5263
   371
CPU_haveSSE42(void)
slouken@5263
   372
{
slouken@5263
   373
    if (CPU_haveCPUID()) {
slouken@5263
   374
        int a, b, c, d;
slouken@5263
   375
slouken@9003
   376
        cpuid(0, a, b, c, d);
slouken@5263
   377
        if (a >= 1) {
slouken@5263
   378
            cpuid(1, a, b, c, d);
slouken@5263
   379
            return (c & 0x00100000);
slouken@5259
   380
        }
slouken@5259
   381
    }
slouken@5259
   382
    return 0;
slouken@778
   383
}
slouken@778
   384
slouken@9003
   385
static int
slouken@8148
   386
CPU_haveAVX(void)
slouken@8148
   387
{
slouken@9003
   388
    if (CPU_haveCPUID() && CPU_OSSavesYMM()) {
slouken@8148
   389
        int a, b, c, d;
slouken@8148
   390
slouken@9003
   391
        cpuid(0, a, b, c, d);
slouken@8148
   392
        if (a >= 1) {
slouken@8148
   393
            cpuid(1, a, b, c, d);
slouken@8148
   394
            return (c & 0x10000000);
slouken@8148
   395
        }
slouken@8148
   396
    }
slouken@8148
   397
    return 0;
slouken@8148
   398
}
slouken@8148
   399
slouken@9003
   400
static int
slouken@9003
   401
CPU_haveAVX2(void)
slouken@9003
   402
{
slouken@9003
   403
    if (CPU_haveCPUID() && CPU_OSSavesYMM()) {
slouken@9003
   404
        int a, b, c, d;
slouken@9003
   405
slouken@9003
   406
        cpuid(0, a, b, c, d);
slouken@9003
   407
        if (a >= 7) {
slouken@9003
   408
            cpuid(7, a, b, c, d);
slouken@9003
   409
            return (b & 0x00000020);
slouken@9003
   410
        }
slouken@9003
   411
    }
slouken@9003
   412
    return 0;
slouken@9003
   413
}
slouken@9003
   414
slouken@3579
   415
static int SDL_CPUCount = 0;
slouken@3579
   416
slouken@3579
   417
int
slouken@5120
   418
SDL_GetCPUCount(void)
slouken@3579
   419
{
slouken@3579
   420
    if (!SDL_CPUCount) {
slouken@3606
   421
#if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
slouken@3586
   422
        if (SDL_CPUCount <= 0) {
slouken@3586
   423
            SDL_CPUCount = (int)sysconf(_SC_NPROCESSORS_ONLN);
slouken@3586
   424
        }
slouken@3586
   425
#endif
slouken@3579
   426
#ifdef HAVE_SYSCTLBYNAME
slouken@3586
   427
        if (SDL_CPUCount <= 0) {
slouken@3580
   428
            size_t size = sizeof(SDL_CPUCount);
slouken@3580
   429
            sysctlbyname("hw.ncpu", &SDL_CPUCount, &size, NULL, 0);
slouken@3580
   430
        }
slouken@3580
   431
#endif
slouken@5086
   432
#ifdef __WIN32__
slouken@3586
   433
        if (SDL_CPUCount <= 0) {
slouken@3580
   434
            SYSTEM_INFO info;
slouken@3580
   435
            GetSystemInfo(&info);
slouken@3580
   436
            SDL_CPUCount = info.dwNumberOfProcessors;
slouken@3580
   437
        }
slouken@3579
   438
#endif
slouken@3579
   439
        /* There has to be at least 1, right? :) */
slouken@3586
   440
        if (SDL_CPUCount <= 0) {
slouken@3579
   441
            SDL_CPUCount = 1;
slouken@3579
   442
        }
slouken@3579
   443
    }
slouken@3579
   444
    return SDL_CPUCount;
slouken@3579
   445
}
slouken@3579
   446
slouken@3579
   447
/* Oh, such a sweet sweet trick, just not very useful. :) */
slouken@4472
   448
static const char *
slouken@5120
   449
SDL_GetCPUType(void)
slouken@3579
   450
{
slouken@5115
   451
    static char SDL_CPUType[13];
slouken@3579
   452
slouken@3579
   453
    if (!SDL_CPUType[0]) {
slouken@3579
   454
        int i = 0;
slouken@3580
   455
        int a, b, c, d;
slouken@3579
   456
slouken@3579
   457
        if (CPU_haveCPUID()) {
slouken@5115
   458
            cpuid(0x00000000, a, b, c, d);
slouken@5115
   459
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   460
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   461
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
icculus@8642
   462
            SDL_CPUType[i++] = (char)(b & 0xff);
icculus@8642
   463
slouken@5115
   464
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   465
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   466
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
icculus@8642
   467
            SDL_CPUType[i++] = (char)(d & 0xff);
icculus@8642
   468
slouken@5115
   469
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   470
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   471
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
icculus@8642
   472
            SDL_CPUType[i++] = (char)(c & 0xff);
slouken@5115
   473
        }
slouken@5115
   474
        if (!SDL_CPUType[0]) {
slouken@5115
   475
            SDL_strlcpy(SDL_CPUType, "Unknown", sizeof(SDL_CPUType));
slouken@5115
   476
        }
slouken@5115
   477
    }
slouken@5115
   478
    return SDL_CPUType;
slouken@5115
   479
}
slouken@5115
   480
icculus@5981
   481
icculus@5981
   482
#ifdef TEST_MAIN  /* !!! FIXME: only used for test at the moment. */
slouken@5115
   483
static const char *
slouken@5120
   484
SDL_GetCPUName(void)
slouken@5115
   485
{
slouken@5115
   486
    static char SDL_CPUName[48];
slouken@5115
   487
slouken@5115
   488
    if (!SDL_CPUName[0]) {
slouken@5115
   489
        int i = 0;
slouken@5115
   490
        int a, b, c, d;
slouken@5115
   491
slouken@5115
   492
        if (CPU_haveCPUID()) {
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();
slouken@5115
   560
slouken@5115
   561
    if (SDL_strcmp(cpuType, "GenuineIntel") == 0) {
slouken@5115
   562
        int a, b, c, d;
slouken@5115
   563
slouken@5115
   564
        cpuid(0x00000001, a, b, c, d);
slouken@5115
   565
        return (((b >> 8) & 0xff) * 8);
slouken@5115
   566
    } else if (SDL_strcmp(cpuType, "AuthenticAMD") == 0) {
slouken@5115
   567
        int a, b, c, d;
slouken@5115
   568
slouken@5115
   569
        cpuid(0x80000005, a, b, c, d);
slouken@5115
   570
        return (c & 0xff);
slouken@5115
   571
    } else {
slouken@5115
   572
        /* Just make a guess here... */
slouken@5115
   573
        return SDL_CACHELINE_SIZE;
slouken@5115
   574
    }
slouken@3579
   575
}
slouken@3579
   576
slouken@739
   577
static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
slouken@739
   578
slouken@1895
   579
static Uint32
slouken@1895
   580
SDL_GetCPUFeatures(void)
slouken@739
   581
{
slouken@1895
   582
    if (SDL_CPUFeatures == 0xFFFFFFFF) {
slouken@1895
   583
        SDL_CPUFeatures = 0;
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;
slouken@5389
   589
        }
slouken@1895
   590
        if (CPU_haveMMX()) {
slouken@1895
   591
            SDL_CPUFeatures |= CPU_HAS_MMX;
slouken@1895
   592
        }
slouken@5389
   593
        if (CPU_have3DNow()) {
slouken@5389
   594
            SDL_CPUFeatures |= CPU_HAS_3DNOW;
slouken@5389
   595
        }
slouken@1895
   596
        if (CPU_haveSSE()) {
slouken@1895
   597
            SDL_CPUFeatures |= CPU_HAS_SSE;
slouken@1895
   598
        }
slouken@1895
   599
        if (CPU_haveSSE2()) {
slouken@1895
   600
            SDL_CPUFeatures |= CPU_HAS_SSE2;
slouken@1895
   601
        }
slouken@5259
   602
        if (CPU_haveSSE3()) {
slouken@5259
   603
            SDL_CPUFeatures |= CPU_HAS_SSE3;
slouken@5259
   604
        }
slouken@5263
   605
        if (CPU_haveSSE41()) {
slouken@5263
   606
            SDL_CPUFeatures |= CPU_HAS_SSE41;
slouken@5263
   607
        }
slouken@5263
   608
        if (CPU_haveSSE42()) {
slouken@5263
   609
            SDL_CPUFeatures |= CPU_HAS_SSE42;
slouken@1895
   610
        }
slouken@8148
   611
        if (CPU_haveAVX()) {
slouken@8148
   612
            SDL_CPUFeatures |= CPU_HAS_AVX;
slouken@8148
   613
        }
slouken@9003
   614
        if (CPU_haveAVX2()) {
slouken@9003
   615
            SDL_CPUFeatures |= CPU_HAS_AVX2;
slouken@9003
   616
        }
slouken@1895
   617
    }
slouken@1895
   618
    return SDL_CPUFeatures;
slouken@739
   619
}
slouken@739
   620
slouken@1895
   621
SDL_bool
slouken@1895
   622
SDL_HasRDTSC(void)
slouken@745
   623
{
slouken@1895
   624
    if (SDL_GetCPUFeatures() & CPU_HAS_RDTSC) {
slouken@1895
   625
        return SDL_TRUE;
slouken@1895
   626
    }
slouken@1895
   627
    return SDL_FALSE;
slouken@745
   628
}
slouken@745
   629
slouken@1895
   630
SDL_bool
slouken@5389
   631
SDL_HasAltiVec(void)
slouken@5389
   632
{
slouken@5389
   633
    if (SDL_GetCPUFeatures() & CPU_HAS_ALTIVEC) {
slouken@5389
   634
        return SDL_TRUE;
slouken@5389
   635
    }
slouken@5389
   636
    return SDL_FALSE;
slouken@5389
   637
}
slouken@5389
   638
slouken@5389
   639
SDL_bool
slouken@1895
   640
SDL_HasMMX(void)
slouken@739
   641
{
slouken@1895
   642
    if (SDL_GetCPUFeatures() & CPU_HAS_MMX) {
slouken@1895
   643
        return SDL_TRUE;
slouken@1895
   644
    }
slouken@1895
   645
    return SDL_FALSE;
slouken@739
   646
}
slouken@739
   647
slouken@1895
   648
SDL_bool
slouken@5389
   649
SDL_Has3DNow(void)
slouken@5389
   650
{
slouken@5389
   651
    if (SDL_GetCPUFeatures() & CPU_HAS_3DNOW) {
slouken@5389
   652
        return SDL_TRUE;
slouken@5389
   653
    }
slouken@5389
   654
    return SDL_FALSE;
slouken@5389
   655
}
slouken@5389
   656
slouken@5389
   657
SDL_bool
slouken@1895
   658
SDL_HasSSE(void)
slouken@739
   659
{
slouken@1895
   660
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE) {
slouken@1895
   661
        return SDL_TRUE;
slouken@1895
   662
    }
slouken@1895
   663
    return SDL_FALSE;
slouken@739
   664
}
slouken@739
   665
slouken@1895
   666
SDL_bool
slouken@1895
   667
SDL_HasSSE2(void)
slouken@804
   668
{
slouken@1895
   669
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE2) {
slouken@1895
   670
        return SDL_TRUE;
slouken@1895
   671
    }
slouken@1895
   672
    return SDL_FALSE;
slouken@804
   673
}
slouken@804
   674
slouken@1895
   675
SDL_bool
slouken@5259
   676
SDL_HasSSE3(void)
slouken@778
   677
{
slouken@5259
   678
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE3) {
slouken@5259
   679
        return SDL_TRUE;
slouken@5259
   680
    }
slouken@5259
   681
    return SDL_FALSE;
slouken@5259
   682
}
slouken@5259
   683
slouken@5259
   684
SDL_bool
slouken@5263
   685
SDL_HasSSE41(void)
slouken@5259
   686
{
slouken@5263
   687
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE41) {
slouken@5263
   688
        return SDL_TRUE;
slouken@5263
   689
    }
slouken@5263
   690
    return SDL_FALSE;
slouken@5263
   691
}
slouken@5263
   692
slouken@5263
   693
SDL_bool
slouken@5263
   694
SDL_HasSSE42(void)
slouken@5263
   695
{
slouken@5263
   696
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE42) {
slouken@1895
   697
        return SDL_TRUE;
slouken@1895
   698
    }
slouken@1895
   699
    return SDL_FALSE;
slouken@778
   700
}
slouken@778
   701
slouken@8148
   702
SDL_bool
slouken@8148
   703
SDL_HasAVX(void)
slouken@8148
   704
{
slouken@8148
   705
    if (SDL_GetCPUFeatures() & CPU_HAS_AVX) {
slouken@8148
   706
        return SDL_TRUE;
slouken@8148
   707
    }
slouken@8148
   708
    return SDL_FALSE;
slouken@8148
   709
}
slouken@8148
   710
slouken@9003
   711
SDL_bool
slouken@9003
   712
SDL_HasAVX2(void)
slouken@9003
   713
{
slouken@9003
   714
    if (SDL_GetCPUFeatures() & CPU_HAS_AVX2) {
slouken@9003
   715
        return SDL_TRUE;
slouken@9003
   716
    }
slouken@9003
   717
    return SDL_FALSE;
slouken@9003
   718
}
slouken@9003
   719
slouken@7821
   720
static int SDL_SystemRAM = 0;
slouken@7821
   721
slouken@7821
   722
int
slouken@7821
   723
SDL_GetSystemRAM(void)
slouken@7821
   724
{
slouken@7821
   725
    if (!SDL_SystemRAM) {
slouken@7821
   726
#if defined(HAVE_SYSCONF) && defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
slouken@7821
   727
        if (SDL_SystemRAM <= 0) {
slouken@7821
   728
            SDL_SystemRAM = (int)((Sint64)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE) / (1024*1024));
slouken@7821
   729
        }
slouken@7821
   730
#endif
slouken@7821
   731
#ifdef HAVE_SYSCTLBYNAME
slouken@7821
   732
        if (SDL_SystemRAM <= 0) {
slouken@8240
   733
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
slouken@7826
   734
#ifdef HW_REALMEM
slouken@7826
   735
            int mib[2] = {CTL_HW, HW_REALMEM};
slouken@7826
   736
#else
slouken@7826
   737
            /* might only report up to 2 GiB */
slouken@7826
   738
            int mib[2] = {CTL_HW, HW_PHYSMEM};
slouken@7826
   739
#endif /* HW_REALMEM */
slouken@7826
   740
#else
slouken@7821
   741
            int mib[2] = {CTL_HW, HW_MEMSIZE};
slouken@8240
   742
#endif /* __FreeBSD__ || __FreeBSD_kernel__ */
slouken@7823
   743
            Uint64 memsize = 0;
slouken@7821
   744
            size_t len = sizeof(memsize);
slouken@7821
   745
            
slouken@7821
   746
            if (sysctl(mib, 2, &memsize, &len, NULL, 0) == 0) {
slouken@7821
   747
                SDL_SystemRAM = (int)(memsize / (1024*1024));
slouken@7821
   748
            }
slouken@7821
   749
        }
slouken@7821
   750
#endif
slouken@7821
   751
#ifdef __WIN32__
slouken@7821
   752
        if (SDL_SystemRAM <= 0) {
slouken@7821
   753
            MEMORYSTATUSEX stat;
icculus@7889
   754
            stat.dwLength = sizeof(stat);
slouken@7821
   755
            if (GlobalMemoryStatusEx(&stat)) {
slouken@7821
   756
                SDL_SystemRAM = (int)(stat.ullTotalPhys / (1024 * 1024));
slouken@7821
   757
            }
slouken@7821
   758
        }
slouken@7821
   759
#endif
slouken@7821
   760
    }
slouken@7821
   761
    return SDL_SystemRAM;
slouken@7821
   762
}
slouken@7821
   763
slouken@7821
   764
slouken@739
   765
#ifdef TEST_MAIN
slouken@739
   766
slouken@739
   767
#include <stdio.h>
slouken@739
   768
slouken@1895
   769
int
slouken@1895
   770
main()
slouken@739
   771
{
slouken@3579
   772
    printf("CPU count: %d\n", SDL_GetCPUCount());
slouken@5115
   773
    printf("CPU type: %s\n", SDL_GetCPUType());
slouken@5115
   774
    printf("CPU name: %s\n", SDL_GetCPUName());
slouken@5115
   775
    printf("CacheLine size: %d\n", SDL_GetCPUCacheLineSize());
slouken@1895
   776
    printf("RDTSC: %d\n", SDL_HasRDTSC());
slouken@5389
   777
    printf("Altivec: %d\n", SDL_HasAltiVec());
slouken@1895
   778
    printf("MMX: %d\n", SDL_HasMMX());
slouken@5389
   779
    printf("3DNow: %d\n", SDL_Has3DNow());
slouken@1895
   780
    printf("SSE: %d\n", SDL_HasSSE());
slouken@1895
   781
    printf("SSE2: %d\n", SDL_HasSSE2());
slouken@5259
   782
    printf("SSE3: %d\n", SDL_HasSSE3());
slouken@5263
   783
    printf("SSE4.1: %d\n", SDL_HasSSE41());
slouken@5263
   784
    printf("SSE4.2: %d\n", SDL_HasSSE42());
slouken@8148
   785
    printf("AVX: %d\n", SDL_HasAVX());
slouken@9003
   786
    printf("AVX2: %d\n", SDL_HasAVX2());
slouken@7821
   787
    printf("RAM: %d MB\n", SDL_GetSystemRAM());
slouken@1895
   788
    return 0;
slouken@739
   789
}
slouken@739
   790
slouken@739
   791
#endif /* TEST_MAIN */
slouken@1895
   792
slouken@1895
   793
/* vi: set ts=4 sw=4 expandtab: */