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