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