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