src/cpuinfo/SDL_cpuinfo.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 24 Nov 2013 23:56:17 -0500
changeset 8093 b43765095a6f
parent 7889 9ec71e56071c
child 8148 56ba41ac64fe
permissions -rw-r--r--
Make internal SDL sources include SDL_internal.h instead of SDL_config.h

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