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