src/cpuinfo/SDL_cpuinfo.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Jul 2014 22:02:50 -0700
changeset 9003 446ed0fe0fc3
parent 8642 3506de54b829
child 9004 51bc44d49052
permissions -rw-r--r--
Fixed bug in AVX detection and added AVX2 detection
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@9003
   256
        mov a, xcr0
slouken@9003
   257
    }
slouken@9003
   258
#else
slouken@9003
   259
#error Need xgetbv implementation!
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;
slouken@6363
   277
#if (defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))) || (defined(__OpenBSD__) && defined(__powerpc__))
slouken@6363
   278
#ifdef __OpenBSD__
slouken@6363
   279
    int selectors[2] = { CTL_MACHDEP, CPU_ALTIVEC };
slouken@6363
   280
#else
slouken@5389
   281
    int selectors[2] = { CTL_HW, HW_VECTORUNIT };
slouken@6363
   282
#endif
slouken@5389
   283
    int hasVectorUnit = 0;
slouken@5389
   284
    size_t length = sizeof(hasVectorUnit);
slouken@5389
   285
    int error = sysctl(selectors, 2, &hasVectorUnit, &length, NULL, 0);
slouken@5389
   286
    if (0 == error)
slouken@5389
   287
        altivec = (hasVectorUnit != 0);
slouken@5389
   288
#elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
slouken@5389
   289
    void (*handler) (int sig);
slouken@5389
   290
    handler = signal(SIGILL, illegal_instruction);
slouken@5389
   291
    if (setjmp(jmpbuf) == 0) {
slouken@5389
   292
        asm volatile ("mtspr 256, %0\n\t" "vand %%v0, %%v0, %%v0"::"r" (-1));
slouken@5389
   293
        altivec = 1;
slouken@5389
   294
    }
slouken@5389
   295
    signal(SIGILL, handler);
slouken@5389
   296
#endif
slouken@5389
   297
    return altivec;
slouken@5389
   298
}
slouken@5389
   299
slouken@9003
   300
static int
slouken@1895
   301
CPU_haveMMX(void)
slouken@745
   302
{
slouken@1895
   303
    if (CPU_haveCPUID()) {
slouken@1895
   304
        return (CPU_getCPUIDFeatures() & 0x00800000);
slouken@1895
   305
    }
slouken@1895
   306
    return 0;
slouken@745
   307
}
slouken@745
   308
slouken@9003
   309
static int
slouken@5389
   310
CPU_have3DNow(void)
slouken@5389
   311
{
slouken@5389
   312
    if (CPU_haveCPUID()) {
slouken@5389
   313
        int a, b, c, d;
slouken@5389
   314
slouken@5389
   315
        cpuid(0x80000000, a, b, c, d);
slouken@5389
   316
        if (a >= 0x80000001) {
slouken@5389
   317
            cpuid(0x80000001, a, b, c, d);
slouken@5389
   318
            return (d & 0x80000000);
slouken@5389
   319
        }
slouken@5389
   320
    }
slouken@5389
   321
    return 0;
slouken@5389
   322
}
slouken@5389
   323
slouken@9003
   324
static int
slouken@1895
   325
CPU_haveSSE(void)
slouken@745
   326
{
slouken@1895
   327
    if (CPU_haveCPUID()) {
slouken@1895
   328
        return (CPU_getCPUIDFeatures() & 0x02000000);
slouken@1895
   329
    }
slouken@1895
   330
    return 0;
slouken@745
   331
}
slouken@739
   332
slouken@9003
   333
static int
slouken@1895
   334
CPU_haveSSE2(void)
slouken@785
   335
{
slouken@1895
   336
    if (CPU_haveCPUID()) {
slouken@1895
   337
        return (CPU_getCPUIDFeatures() & 0x04000000);
slouken@1895
   338
    }
slouken@1895
   339
    return 0;
slouken@785
   340
}
slouken@785
   341
slouken@9003
   342
static int
slouken@5259
   343
CPU_haveSSE3(void)
slouken@778
   344
{
slouken@5259
   345
    if (CPU_haveCPUID()) {
slouken@5259
   346
        int a, b, c, d;
slouken@5259
   347
slouken@5259
   348
        cpuid(0, a, b, c, d);
slouken@5259
   349
        if (a >= 1) {
slouken@5259
   350
            cpuid(1, a, b, c, d);
slouken@5259
   351
            return (c & 0x00000001);
slouken@5259
   352
        }
slouken@1895
   353
    }
slouken@5259
   354
    return 0;
slouken@5259
   355
}
slouken@5259
   356
slouken@9003
   357
static int
slouken@5263
   358
CPU_haveSSE41(void)
slouken@5259
   359
{
slouken@5259
   360
    if (CPU_haveCPUID()) {
slouken@5259
   361
        int a, b, c, d;
slouken@5259
   362
slouken@9003
   363
        cpuid(0, a, b, c, d);
slouken@5259
   364
        if (a >= 1) {
slouken@5259
   365
            cpuid(1, a, b, c, d);
slouken@5263
   366
            return (c & 0x00080000);
slouken@5263
   367
        }
slouken@5263
   368
    }
slouken@5263
   369
    return 0;
slouken@5263
   370
}
slouken@5263
   371
slouken@9003
   372
static int
slouken@5263
   373
CPU_haveSSE42(void)
slouken@5263
   374
{
slouken@5263
   375
    if (CPU_haveCPUID()) {
slouken@5263
   376
        int a, b, c, d;
slouken@5263
   377
slouken@9003
   378
        cpuid(0, a, b, c, d);
slouken@5263
   379
        if (a >= 1) {
slouken@5263
   380
            cpuid(1, a, b, c, d);
slouken@5263
   381
            return (c & 0x00100000);
slouken@5259
   382
        }
slouken@5259
   383
    }
slouken@5259
   384
    return 0;
slouken@778
   385
}
slouken@778
   386
slouken@9003
   387
static int
slouken@8148
   388
CPU_haveAVX(void)
slouken@8148
   389
{
slouken@9003
   390
    if (CPU_haveCPUID() && CPU_OSSavesYMM()) {
slouken@8148
   391
        int a, b, c, d;
slouken@8148
   392
slouken@9003
   393
        cpuid(0, a, b, c, d);
slouken@8148
   394
        if (a >= 1) {
slouken@8148
   395
            cpuid(1, a, b, c, d);
slouken@8148
   396
            return (c & 0x10000000);
slouken@8148
   397
        }
slouken@8148
   398
    }
slouken@8148
   399
    return 0;
slouken@8148
   400
}
slouken@8148
   401
slouken@9003
   402
static int
slouken@9003
   403
CPU_haveAVX2(void)
slouken@9003
   404
{
slouken@9003
   405
    if (CPU_haveCPUID() && CPU_OSSavesYMM()) {
slouken@9003
   406
        int a, b, c, d;
slouken@9003
   407
slouken@9003
   408
        cpuid(0, a, b, c, d);
slouken@9003
   409
        if (a >= 7) {
slouken@9003
   410
            cpuid(7, a, b, c, d);
slouken@9003
   411
            return (b & 0x00000020);
slouken@9003
   412
        }
slouken@9003
   413
    }
slouken@9003
   414
    return 0;
slouken@9003
   415
}
slouken@9003
   416
slouken@3579
   417
static int SDL_CPUCount = 0;
slouken@3579
   418
slouken@3579
   419
int
slouken@5120
   420
SDL_GetCPUCount(void)
slouken@3579
   421
{
slouken@3579
   422
    if (!SDL_CPUCount) {
slouken@3606
   423
#if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
slouken@3586
   424
        if (SDL_CPUCount <= 0) {
slouken@3586
   425
            SDL_CPUCount = (int)sysconf(_SC_NPROCESSORS_ONLN);
slouken@3586
   426
        }
slouken@3586
   427
#endif
slouken@3579
   428
#ifdef HAVE_SYSCTLBYNAME
slouken@3586
   429
        if (SDL_CPUCount <= 0) {
slouken@3580
   430
            size_t size = sizeof(SDL_CPUCount);
slouken@3580
   431
            sysctlbyname("hw.ncpu", &SDL_CPUCount, &size, NULL, 0);
slouken@3580
   432
        }
slouken@3580
   433
#endif
slouken@5086
   434
#ifdef __WIN32__
slouken@3586
   435
        if (SDL_CPUCount <= 0) {
slouken@3580
   436
            SYSTEM_INFO info;
slouken@3580
   437
            GetSystemInfo(&info);
slouken@3580
   438
            SDL_CPUCount = info.dwNumberOfProcessors;
slouken@3580
   439
        }
slouken@3579
   440
#endif
slouken@3579
   441
        /* There has to be at least 1, right? :) */
slouken@3586
   442
        if (SDL_CPUCount <= 0) {
slouken@3579
   443
            SDL_CPUCount = 1;
slouken@3579
   444
        }
slouken@3579
   445
    }
slouken@3579
   446
    return SDL_CPUCount;
slouken@3579
   447
}
slouken@3579
   448
slouken@3579
   449
/* Oh, such a sweet sweet trick, just not very useful. :) */
slouken@4472
   450
static const char *
slouken@5120
   451
SDL_GetCPUType(void)
slouken@3579
   452
{
slouken@5115
   453
    static char SDL_CPUType[13];
slouken@3579
   454
slouken@3579
   455
    if (!SDL_CPUType[0]) {
slouken@3579
   456
        int i = 0;
slouken@3580
   457
        int a, b, c, d;
slouken@3579
   458
slouken@3579
   459
        if (CPU_haveCPUID()) {
slouken@5115
   460
            cpuid(0x00000000, a, b, c, d);
slouken@5115
   461
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   462
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   463
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
icculus@8642
   464
            SDL_CPUType[i++] = (char)(b & 0xff);
icculus@8642
   465
slouken@5115
   466
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   467
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   468
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
icculus@8642
   469
            SDL_CPUType[i++] = (char)(d & 0xff);
icculus@8642
   470
slouken@5115
   471
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   472
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   473
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
icculus@8642
   474
            SDL_CPUType[i++] = (char)(c & 0xff);
slouken@5115
   475
        }
slouken@5115
   476
        if (!SDL_CPUType[0]) {
slouken@5115
   477
            SDL_strlcpy(SDL_CPUType, "Unknown", sizeof(SDL_CPUType));
slouken@5115
   478
        }
slouken@5115
   479
    }
slouken@5115
   480
    return SDL_CPUType;
slouken@5115
   481
}
slouken@5115
   482
icculus@5981
   483
icculus@5981
   484
#ifdef TEST_MAIN  /* !!! FIXME: only used for test at the moment. */
slouken@5115
   485
static const char *
slouken@5120
   486
SDL_GetCPUName(void)
slouken@5115
   487
{
slouken@5115
   488
    static char SDL_CPUName[48];
slouken@5115
   489
slouken@5115
   490
    if (!SDL_CPUName[0]) {
slouken@5115
   491
        int i = 0;
slouken@5115
   492
        int a, b, c, d;
slouken@5115
   493
slouken@5115
   494
        if (CPU_haveCPUID()) {
slouken@3580
   495
            cpuid(0x80000000, a, b, c, d);
slouken@3580
   496
            if (a >= 0x80000004) {
slouken@3580
   497
                cpuid(0x80000002, a, b, c, d);
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)(a & 0xff); a >>= 8;
slouken@5115
   501
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 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)(b & 0xff); b >>= 8;
slouken@5115
   505
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 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)(c & 0xff); c >>= 8;
slouken@5115
   509
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   510
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   511
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   512
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   513
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@3580
   514
                cpuid(0x80000003, a, b, c, d);
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)(a & 0xff); a >>= 8;
slouken@5115
   518
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 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)(b & 0xff); b >>= 8;
slouken@5115
   522
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 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)(c & 0xff); c >>= 8;
slouken@5115
   526
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   527
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   528
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   529
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   530
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@3580
   531
                cpuid(0x80000004, a, b, c, d);
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)(a & 0xff); a >>= 8;
slouken@5115
   535
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 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)(b & 0xff); b >>= 8;
slouken@5115
   539
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 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)(c & 0xff); c >>= 8;
slouken@5115
   543
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   544
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   545
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   546
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   547
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@3579
   548
            }
slouken@3579
   549
        }
slouken@5115
   550
        if (!SDL_CPUName[0]) {
slouken@5115
   551
            SDL_strlcpy(SDL_CPUName, "Unknown", sizeof(SDL_CPUName));
slouken@3579
   552
        }
slouken@3579
   553
    }
slouken@5115
   554
    return SDL_CPUName;
slouken@5115
   555
}
icculus@5981
   556
#endif
slouken@5115
   557
slouken@5120
   558
int
slouken@5120
   559
SDL_GetCPUCacheLineSize(void)
slouken@5115
   560
{
slouken@5115
   561
    const char *cpuType = SDL_GetCPUType();
slouken@5115
   562
slouken@5115
   563
    if (SDL_strcmp(cpuType, "GenuineIntel") == 0) {
slouken@5115
   564
        int a, b, c, d;
slouken@5115
   565
slouken@5115
   566
        cpuid(0x00000001, a, b, c, d);
slouken@5115
   567
        return (((b >> 8) & 0xff) * 8);
slouken@5115
   568
    } else if (SDL_strcmp(cpuType, "AuthenticAMD") == 0) {
slouken@5115
   569
        int a, b, c, d;
slouken@5115
   570
slouken@5115
   571
        cpuid(0x80000005, a, b, c, d);
slouken@5115
   572
        return (c & 0xff);
slouken@5115
   573
    } else {
slouken@5115
   574
        /* Just make a guess here... */
slouken@5115
   575
        return SDL_CACHELINE_SIZE;
slouken@5115
   576
    }
slouken@3579
   577
}
slouken@3579
   578
slouken@739
   579
static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
slouken@739
   580
slouken@1895
   581
static Uint32
slouken@1895
   582
SDL_GetCPUFeatures(void)
slouken@739
   583
{
slouken@1895
   584
    if (SDL_CPUFeatures == 0xFFFFFFFF) {
slouken@1895
   585
        SDL_CPUFeatures = 0;
slouken@1895
   586
        if (CPU_haveRDTSC()) {
slouken@1895
   587
            SDL_CPUFeatures |= CPU_HAS_RDTSC;
slouken@1895
   588
        }
slouken@5389
   589
        if (CPU_haveAltiVec()) {
slouken@5389
   590
            SDL_CPUFeatures |= CPU_HAS_ALTIVEC;
slouken@5389
   591
        }
slouken@1895
   592
        if (CPU_haveMMX()) {
slouken@1895
   593
            SDL_CPUFeatures |= CPU_HAS_MMX;
slouken@1895
   594
        }
slouken@5389
   595
        if (CPU_have3DNow()) {
slouken@5389
   596
            SDL_CPUFeatures |= CPU_HAS_3DNOW;
slouken@5389
   597
        }
slouken@1895
   598
        if (CPU_haveSSE()) {
slouken@1895
   599
            SDL_CPUFeatures |= CPU_HAS_SSE;
slouken@1895
   600
        }
slouken@1895
   601
        if (CPU_haveSSE2()) {
slouken@1895
   602
            SDL_CPUFeatures |= CPU_HAS_SSE2;
slouken@1895
   603
        }
slouken@5259
   604
        if (CPU_haveSSE3()) {
slouken@5259
   605
            SDL_CPUFeatures |= CPU_HAS_SSE3;
slouken@5259
   606
        }
slouken@5263
   607
        if (CPU_haveSSE41()) {
slouken@5263
   608
            SDL_CPUFeatures |= CPU_HAS_SSE41;
slouken@5263
   609
        }
slouken@5263
   610
        if (CPU_haveSSE42()) {
slouken@5263
   611
            SDL_CPUFeatures |= CPU_HAS_SSE42;
slouken@1895
   612
        }
slouken@8148
   613
        if (CPU_haveAVX()) {
slouken@8148
   614
            SDL_CPUFeatures |= CPU_HAS_AVX;
slouken@8148
   615
        }
slouken@9003
   616
        if (CPU_haveAVX2()) {
slouken@9003
   617
            SDL_CPUFeatures |= CPU_HAS_AVX2;
slouken@9003
   618
        }
slouken@1895
   619
    }
slouken@1895
   620
    return SDL_CPUFeatures;
slouken@739
   621
}
slouken@739
   622
slouken@1895
   623
SDL_bool
slouken@1895
   624
SDL_HasRDTSC(void)
slouken@745
   625
{
slouken@1895
   626
    if (SDL_GetCPUFeatures() & CPU_HAS_RDTSC) {
slouken@1895
   627
        return SDL_TRUE;
slouken@1895
   628
    }
slouken@1895
   629
    return SDL_FALSE;
slouken@745
   630
}
slouken@745
   631
slouken@1895
   632
SDL_bool
slouken@5389
   633
SDL_HasAltiVec(void)
slouken@5389
   634
{
slouken@5389
   635
    if (SDL_GetCPUFeatures() & CPU_HAS_ALTIVEC) {
slouken@5389
   636
        return SDL_TRUE;
slouken@5389
   637
    }
slouken@5389
   638
    return SDL_FALSE;
slouken@5389
   639
}
slouken@5389
   640
slouken@5389
   641
SDL_bool
slouken@1895
   642
SDL_HasMMX(void)
slouken@739
   643
{
slouken@1895
   644
    if (SDL_GetCPUFeatures() & CPU_HAS_MMX) {
slouken@1895
   645
        return SDL_TRUE;
slouken@1895
   646
    }
slouken@1895
   647
    return SDL_FALSE;
slouken@739
   648
}
slouken@739
   649
slouken@1895
   650
SDL_bool
slouken@5389
   651
SDL_Has3DNow(void)
slouken@5389
   652
{
slouken@5389
   653
    if (SDL_GetCPUFeatures() & CPU_HAS_3DNOW) {
slouken@5389
   654
        return SDL_TRUE;
slouken@5389
   655
    }
slouken@5389
   656
    return SDL_FALSE;
slouken@5389
   657
}
slouken@5389
   658
slouken@5389
   659
SDL_bool
slouken@1895
   660
SDL_HasSSE(void)
slouken@739
   661
{
slouken@1895
   662
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE) {
slouken@1895
   663
        return SDL_TRUE;
slouken@1895
   664
    }
slouken@1895
   665
    return SDL_FALSE;
slouken@739
   666
}
slouken@739
   667
slouken@1895
   668
SDL_bool
slouken@1895
   669
SDL_HasSSE2(void)
slouken@804
   670
{
slouken@1895
   671
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE2) {
slouken@1895
   672
        return SDL_TRUE;
slouken@1895
   673
    }
slouken@1895
   674
    return SDL_FALSE;
slouken@804
   675
}
slouken@804
   676
slouken@1895
   677
SDL_bool
slouken@5259
   678
SDL_HasSSE3(void)
slouken@778
   679
{
slouken@5259
   680
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE3) {
slouken@5259
   681
        return SDL_TRUE;
slouken@5259
   682
    }
slouken@5259
   683
    return SDL_FALSE;
slouken@5259
   684
}
slouken@5259
   685
slouken@5259
   686
SDL_bool
slouken@5263
   687
SDL_HasSSE41(void)
slouken@5259
   688
{
slouken@5263
   689
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE41) {
slouken@5263
   690
        return SDL_TRUE;
slouken@5263
   691
    }
slouken@5263
   692
    return SDL_FALSE;
slouken@5263
   693
}
slouken@5263
   694
slouken@5263
   695
SDL_bool
slouken@5263
   696
SDL_HasSSE42(void)
slouken@5263
   697
{
slouken@5263
   698
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE42) {
slouken@1895
   699
        return SDL_TRUE;
slouken@1895
   700
    }
slouken@1895
   701
    return SDL_FALSE;
slouken@778
   702
}
slouken@778
   703
slouken@8148
   704
SDL_bool
slouken@8148
   705
SDL_HasAVX(void)
slouken@8148
   706
{
slouken@8148
   707
    if (SDL_GetCPUFeatures() & CPU_HAS_AVX) {
slouken@8148
   708
        return SDL_TRUE;
slouken@8148
   709
    }
slouken@8148
   710
    return SDL_FALSE;
slouken@8148
   711
}
slouken@8148
   712
slouken@9003
   713
SDL_bool
slouken@9003
   714
SDL_HasAVX2(void)
slouken@9003
   715
{
slouken@9003
   716
    if (SDL_GetCPUFeatures() & CPU_HAS_AVX2) {
slouken@9003
   717
        return SDL_TRUE;
slouken@9003
   718
    }
slouken@9003
   719
    return SDL_FALSE;
slouken@9003
   720
}
slouken@9003
   721
slouken@7821
   722
static int SDL_SystemRAM = 0;
slouken@7821
   723
slouken@7821
   724
int
slouken@7821
   725
SDL_GetSystemRAM(void)
slouken@7821
   726
{
slouken@7821
   727
    if (!SDL_SystemRAM) {
slouken@7821
   728
#if defined(HAVE_SYSCONF) && defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
slouken@7821
   729
        if (SDL_SystemRAM <= 0) {
slouken@7821
   730
            SDL_SystemRAM = (int)((Sint64)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE) / (1024*1024));
slouken@7821
   731
        }
slouken@7821
   732
#endif
slouken@7821
   733
#ifdef HAVE_SYSCTLBYNAME
slouken@7821
   734
        if (SDL_SystemRAM <= 0) {
slouken@8240
   735
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
slouken@7826
   736
#ifdef HW_REALMEM
slouken@7826
   737
            int mib[2] = {CTL_HW, HW_REALMEM};
slouken@7826
   738
#else
slouken@7826
   739
            /* might only report up to 2 GiB */
slouken@7826
   740
            int mib[2] = {CTL_HW, HW_PHYSMEM};
slouken@7826
   741
#endif /* HW_REALMEM */
slouken@7826
   742
#else
slouken@7821
   743
            int mib[2] = {CTL_HW, HW_MEMSIZE};
slouken@8240
   744
#endif /* __FreeBSD__ || __FreeBSD_kernel__ */
slouken@7823
   745
            Uint64 memsize = 0;
slouken@7821
   746
            size_t len = sizeof(memsize);
slouken@7821
   747
            
slouken@7821
   748
            if (sysctl(mib, 2, &memsize, &len, NULL, 0) == 0) {
slouken@7821
   749
                SDL_SystemRAM = (int)(memsize / (1024*1024));
slouken@7821
   750
            }
slouken@7821
   751
        }
slouken@7821
   752
#endif
slouken@7821
   753
#ifdef __WIN32__
slouken@7821
   754
        if (SDL_SystemRAM <= 0) {
slouken@7821
   755
            MEMORYSTATUSEX stat;
icculus@7889
   756
            stat.dwLength = sizeof(stat);
slouken@7821
   757
            if (GlobalMemoryStatusEx(&stat)) {
slouken@7821
   758
                SDL_SystemRAM = (int)(stat.ullTotalPhys / (1024 * 1024));
slouken@7821
   759
            }
slouken@7821
   760
        }
slouken@7821
   761
#endif
slouken@7821
   762
    }
slouken@7821
   763
    return SDL_SystemRAM;
slouken@7821
   764
}
slouken@7821
   765
slouken@7821
   766
slouken@739
   767
#ifdef TEST_MAIN
slouken@739
   768
slouken@739
   769
#include <stdio.h>
slouken@739
   770
slouken@1895
   771
int
slouken@1895
   772
main()
slouken@739
   773
{
slouken@3579
   774
    printf("CPU count: %d\n", SDL_GetCPUCount());
slouken@5115
   775
    printf("CPU type: %s\n", SDL_GetCPUType());
slouken@5115
   776
    printf("CPU name: %s\n", SDL_GetCPUName());
slouken@5115
   777
    printf("CacheLine size: %d\n", SDL_GetCPUCacheLineSize());
slouken@1895
   778
    printf("RDTSC: %d\n", SDL_HasRDTSC());
slouken@5389
   779
    printf("Altivec: %d\n", SDL_HasAltiVec());
slouken@1895
   780
    printf("MMX: %d\n", SDL_HasMMX());
slouken@5389
   781
    printf("3DNow: %d\n", SDL_Has3DNow());
slouken@1895
   782
    printf("SSE: %d\n", SDL_HasSSE());
slouken@1895
   783
    printf("SSE2: %d\n", SDL_HasSSE2());
slouken@5259
   784
    printf("SSE3: %d\n", SDL_HasSSE3());
slouken@5263
   785
    printf("SSE4.1: %d\n", SDL_HasSSE41());
slouken@5263
   786
    printf("SSE4.2: %d\n", SDL_HasSSE42());
slouken@8148
   787
    printf("AVX: %d\n", SDL_HasAVX());
slouken@9003
   788
    printf("AVX2: %d\n", SDL_HasAVX2());
slouken@7821
   789
    printf("RAM: %d MB\n", SDL_GetSystemRAM());
slouken@1895
   790
    return 0;
slouken@739
   791
}
slouken@739
   792
slouken@739
   793
#endif /* TEST_MAIN */
slouken@1895
   794
slouken@1895
   795
/* vi: set ts=4 sw=4 expandtab: */