src/cpuinfo/SDL_cpuinfo.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Feb 2011 22:37:15 -0800
changeset 5262 b530ef003506
parent 5259 6a65c1fc07af
child 5263 e1122f31fec5
permissions -rw-r--r--
Happy 2011! :)
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@5086
    35
#ifdef __WIN32__
slouken@5090
    36
#include "../core/windows/SDL_windows.h"
slouken@3580
    37
#endif
slouken@793
    38
slouken@3579
    39
#define CPU_HAS_RDTSC   0x00000001
slouken@3579
    40
#define CPU_HAS_MMX     0x00000002
slouken@5259
    41
#define CPU_HAS_SSE     0x00000010
slouken@5259
    42
#define CPU_HAS_SSE2    0x00000020
slouken@5259
    43
#define CPU_HAS_SSE3    0x00000040
slouken@5259
    44
#define CPU_HAS_SSE4    0x00000080
slouken@739
    45
slouken@793
    46
slouken@1895
    47
static __inline__ int
slouken@1895
    48
CPU_haveCPUID(void)
slouken@745
    49
{
slouken@1895
    50
    int has_CPUID = 0;
slouken@1895
    51
/* *INDENT-OFF* */
slouken@745
    52
#if defined(__GNUC__) && defined(i386)
slouken@3579
    53
    __asm__ (
slouken@745
    54
"        pushfl                      # Get original EFLAGS             \n"
slouken@745
    55
"        popl    %%eax                                                 \n"
slouken@745
    56
"        movl    %%eax,%%ecx                                           \n"
slouken@745
    57
"        xorl    $0x200000,%%eax     # Flip ID bit in EFLAGS           \n"
slouken@745
    58
"        pushl   %%eax               # Save new EFLAGS value on stack  \n"
slouken@745
    59
"        popfl                       # Replace current EFLAGS value    \n"
slouken@745
    60
"        pushfl                      # Get new EFLAGS                  \n"
slouken@745
    61
"        popl    %%eax               # Store new EFLAGS in EAX         \n"
slouken@745
    62
"        xorl    %%ecx,%%eax         # Can not toggle ID bit,          \n"
slouken@745
    63
"        jz      1f                  # Processor=80486                 \n"
slouken@745
    64
"        movl    $1,%0               # We have CPUID support           \n"
slouken@745
    65
"1:                                                                    \n"
slouken@3579
    66
    : "=m" (has_CPUID)
slouken@3579
    67
    :
slouken@3579
    68
    : "%eax", "%ecx"
slouken@3579
    69
    );
slouken@881
    70
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@881
    71
/* Technically, if this is being compiled under __x86_64__ then it has 
slouken@881
    72
CPUid by definition.  But it's nice to be able to prove it.  :)      */
slouken@3579
    73
    __asm__ (
slouken@881
    74
"        pushfq                      # Get original EFLAGS             \n"
slouken@881
    75
"        popq    %%rax                                                 \n"
slouken@881
    76
"        movq    %%rax,%%rcx                                           \n"
slouken@881
    77
"        xorl    $0x200000,%%eax     # Flip ID bit in EFLAGS           \n"
slouken@881
    78
"        pushq   %%rax               # Save new EFLAGS value on stack  \n"
slouken@881
    79
"        popfq                       # Replace current EFLAGS value    \n"
slouken@881
    80
"        pushfq                      # Get new EFLAGS                  \n"
slouken@881
    81
"        popq    %%rax               # Store new EFLAGS in EAX         \n"
slouken@881
    82
"        xorl    %%ecx,%%eax         # Can not toggle ID bit,          \n"
slouken@881
    83
"        jz      1f                  # Processor=80486                 \n"
slouken@881
    84
"        movl    $1,%0               # We have CPUID support           \n"
slouken@881
    85
"1:                                                                    \n"
slouken@3579
    86
    : "=m" (has_CPUID)
slouken@3579
    87
    :
slouken@3579
    88
    : "%rax", "%rcx"
slouken@3579
    89
    );
slouken@1442
    90
#elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
slouken@3579
    91
    __asm {
slouken@745
    92
        pushfd                      ; Get original EFLAGS
slouken@745
    93
        pop     eax
slouken@745
    94
        mov     ecx, eax
slouken@745
    95
        xor     eax, 200000h        ; Flip ID bit in EFLAGS
slouken@745
    96
        push    eax                 ; Save new EFLAGS value on stack
slouken@745
    97
        popfd                       ; Replace current EFLAGS value
slouken@745
    98
        pushfd                      ; Get new EFLAGS
slouken@745
    99
        pop     eax                 ; Store new EFLAGS in EAX
slouken@745
   100
        xor     eax, ecx            ; Can not toggle ID bit,
slouken@745
   101
        jz      done                ; Processor=80486
slouken@745
   102
        mov     has_CPUID,1         ; We have CPUID support
slouken@745
   103
done:
slouken@3579
   104
    }
slouken@1864
   105
#elif defined(__sun) && defined(__i386)
slouken@3579
   106
    __asm (
icculus@1229
   107
"       pushfl                 \n"
slouken@3584
   108
"       popl    %eax           \n"
slouken@3584
   109
"       movl    %eax,%ecx      \n"
slouken@3584
   110
"       xorl    $0x200000,%eax \n"
slouken@3584
   111
"       pushl   %eax           \n"
slouken@3584
   112
"       popfl                  \n"
slouken@3584
   113
"       pushfl                 \n"
slouken@3584
   114
"       popl    %eax           \n"
slouken@3584
   115
"       xorl    %ecx,%eax      \n"
slouken@3584
   116
"       jz      1f             \n"
slouken@3584
   117
"       movl    $1,-8(%ebp)    \n"
icculus@1229
   118
"1:                            \n"
slouken@3579
   119
    );
icculus@1229
   120
#elif defined(__sun) && defined(__amd64)
slouken@3579
   121
    __asm (
icculus@1229
   122
"       pushfq                 \n"
icculus@1229
   123
"       popq    %rax           \n"
icculus@1229
   124
"       movq    %rax,%rcx      \n"
icculus@1229
   125
"       xorl    $0x200000,%eax \n"
icculus@1229
   126
"       pushq   %rax           \n"
icculus@1229
   127
"       popfq                  \n"
icculus@1229
   128
"       pushfq                 \n"
icculus@1229
   129
"       popq    %rax           \n"
icculus@1229
   130
"       xorl    %ecx,%eax      \n"
icculus@1229
   131
"       jz      1f             \n"
icculus@1229
   132
"       movl    $1,-8(%rbp)    \n"
icculus@1229
   133
"1:                            \n"
slouken@3579
   134
    );
slouken@745
   135
#endif
slouken@1895
   136
/* *INDENT-ON* */
slouken@1895
   137
    return has_CPUID;
slouken@745
   138
}
slouken@745
   139
slouken@3587
   140
#if defined(__GNUC__) && defined(i386)
slouken@3580
   141
#define cpuid(func, a, b, c, d) \
slouken@3584
   142
    __asm__ __volatile__ ( \
slouken@3584
   143
"        pushl %%ebx        \n" \
slouken@3584
   144
"        cpuid              \n" \
slouken@3584
   145
"        movl %%ebx, %%esi  \n" \
slouken@3584
   146
"        popl %%ebx         \n" : \
slouken@3584
   147
            "=a" (a), "=S" (b), "=c" (c), "=d" (d) : "a" (func))
slouken@3587
   148
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@3587
   149
#define cpuid(func, a, b, c, d) \
slouken@3587
   150
    __asm__ __volatile__ ( \
slouken@3587
   151
"        pushq %%rbx        \n" \
slouken@3587
   152
"        cpuid              \n" \
slouken@3587
   153
"        movq %%rbx, %%rsi  \n" \
slouken@3587
   154
"        popq %%rbx         \n" : \
slouken@3587
   155
            "=a" (a), "=S" (b), "=c" (c), "=d" (d) : "a" (func))
slouken@3579
   156
#elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
slouken@3580
   157
#define cpuid(func, a, b, c, d) \
slouken@3580
   158
    __asm { \
slouken@3580
   159
        __asm mov eax, func \
slouken@3580
   160
        __asm cpuid \
slouken@3580
   161
        __asm mov a, eax \
slouken@3580
   162
        __asm mov b, ebx \
slouken@3580
   163
        __asm mov c, ecx \
slouken@3580
   164
        __asm mov d, edx \
slouken@3579
   165
    }
slouken@3579
   166
#else
slouken@3580
   167
#define cpuid(func, a, b, c, d) \
slouken@3580
   168
    a = b = c = d = 0
slouken@3579
   169
#endif
slouken@3579
   170
slouken@1895
   171
static __inline__ int
slouken@1895
   172
CPU_getCPUIDFeatures(void)
slouken@745
   173
{
slouken@1895
   174
    int features = 0;
slouken@3580
   175
    int a, b, c, d;
slouken@3579
   176
slouken@3580
   177
    cpuid(0, a, b, c, d);
slouken@3580
   178
    if (a >= 1) {
slouken@3580
   179
        cpuid(1, a, b, c, d);
slouken@3580
   180
        features = d;
slouken@3579
   181
    }
slouken@1895
   182
    return features;
slouken@745
   183
}
slouken@745
   184
slouken@1895
   185
static __inline__ int
slouken@1895
   186
CPU_haveRDTSC(void)
slouken@745
   187
{
slouken@1895
   188
    if (CPU_haveCPUID()) {
slouken@1895
   189
        return (CPU_getCPUIDFeatures() & 0x00000010);
slouken@1895
   190
    }
slouken@1895
   191
    return 0;
slouken@745
   192
}
slouken@745
   193
slouken@1895
   194
static __inline__ int
slouken@1895
   195
CPU_haveMMX(void)
slouken@745
   196
{
slouken@1895
   197
    if (CPU_haveCPUID()) {
slouken@1895
   198
        return (CPU_getCPUIDFeatures() & 0x00800000);
slouken@1895
   199
    }
slouken@1895
   200
    return 0;
slouken@745
   201
}
slouken@745
   202
slouken@1895
   203
static __inline__ int
slouken@1895
   204
CPU_haveSSE(void)
slouken@745
   205
{
slouken@1895
   206
    if (CPU_haveCPUID()) {
slouken@1895
   207
        return (CPU_getCPUIDFeatures() & 0x02000000);
slouken@1895
   208
    }
slouken@1895
   209
    return 0;
slouken@745
   210
}
slouken@739
   211
slouken@1895
   212
static __inline__ int
slouken@1895
   213
CPU_haveSSE2(void)
slouken@785
   214
{
slouken@1895
   215
    if (CPU_haveCPUID()) {
slouken@1895
   216
        return (CPU_getCPUIDFeatures() & 0x04000000);
slouken@1895
   217
    }
slouken@1895
   218
    return 0;
slouken@785
   219
}
slouken@785
   220
slouken@1895
   221
static __inline__ int
slouken@5259
   222
CPU_haveSSE3(void)
slouken@778
   223
{
slouken@5259
   224
    if (CPU_haveCPUID()) {
slouken@5259
   225
        int a, b, c, d;
slouken@5259
   226
slouken@5259
   227
        cpuid(0, a, b, c, d);
slouken@5259
   228
        if (a >= 1) {
slouken@5259
   229
            cpuid(1, a, b, c, d);
slouken@5259
   230
            return (c & 0x00000001);
slouken@5259
   231
        }
slouken@1895
   232
    }
slouken@5259
   233
    return 0;
slouken@5259
   234
}
slouken@5259
   235
slouken@5259
   236
static __inline__ int
slouken@5259
   237
CPU_haveSSE4(void)
slouken@5259
   238
{
slouken@5259
   239
    if (CPU_haveCPUID()) {
slouken@5259
   240
        int a, b, c, d;
slouken@5259
   241
slouken@5259
   242
        cpuid(0, a, b, c, d);
slouken@5259
   243
        if (a >= 1) {
slouken@5259
   244
            cpuid(1, a, b, c, d);
slouken@5259
   245
            return (c & 0x00000100);
slouken@5259
   246
        }
slouken@5259
   247
    }
slouken@5259
   248
    return 0;
slouken@778
   249
}
slouken@778
   250
slouken@3579
   251
static int SDL_CPUCount = 0;
slouken@3579
   252
slouken@3579
   253
int
slouken@5120
   254
SDL_GetCPUCount(void)
slouken@3579
   255
{
slouken@3579
   256
    if (!SDL_CPUCount) {
slouken@3606
   257
#if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
slouken@3586
   258
        if (SDL_CPUCount <= 0) {
slouken@3586
   259
            SDL_CPUCount = (int)sysconf(_SC_NPROCESSORS_ONLN);
slouken@3586
   260
        }
slouken@3586
   261
#endif
slouken@3579
   262
#ifdef HAVE_SYSCTLBYNAME
slouken@3586
   263
        if (SDL_CPUCount <= 0) {
slouken@3580
   264
            size_t size = sizeof(SDL_CPUCount);
slouken@3580
   265
            sysctlbyname("hw.ncpu", &SDL_CPUCount, &size, NULL, 0);
slouken@3580
   266
        }
slouken@3580
   267
#endif
slouken@5086
   268
#ifdef __WIN32__
slouken@3586
   269
        if (SDL_CPUCount <= 0) {
slouken@3580
   270
            SYSTEM_INFO info;
slouken@3580
   271
            GetSystemInfo(&info);
slouken@3580
   272
            SDL_CPUCount = info.dwNumberOfProcessors;
slouken@3580
   273
        }
slouken@3579
   274
#endif
slouken@3579
   275
        /* There has to be at least 1, right? :) */
slouken@3586
   276
        if (SDL_CPUCount <= 0) {
slouken@3579
   277
            SDL_CPUCount = 1;
slouken@3579
   278
        }
slouken@3579
   279
    }
slouken@3579
   280
    return SDL_CPUCount;
slouken@3579
   281
}
slouken@3579
   282
slouken@3579
   283
/* Oh, such a sweet sweet trick, just not very useful. :) */
slouken@4472
   284
static const char *
slouken@5120
   285
SDL_GetCPUType(void)
slouken@3579
   286
{
slouken@5115
   287
    static char SDL_CPUType[13];
slouken@3579
   288
slouken@3579
   289
    if (!SDL_CPUType[0]) {
slouken@3579
   290
        int i = 0;
slouken@3580
   291
        int a, b, c, d;
slouken@3579
   292
slouken@3579
   293
        if (CPU_haveCPUID()) {
slouken@5115
   294
            cpuid(0x00000000, a, b, c, d);
slouken@5115
   295
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   296
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   297
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   298
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   299
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   300
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   301
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   302
            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   303
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   304
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   305
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   306
            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   307
        }
slouken@5115
   308
        if (!SDL_CPUType[0]) {
slouken@5115
   309
            SDL_strlcpy(SDL_CPUType, "Unknown", sizeof(SDL_CPUType));
slouken@5115
   310
        }
slouken@5115
   311
    }
slouken@5115
   312
    return SDL_CPUType;
slouken@5115
   313
}
slouken@5115
   314
slouken@5115
   315
static const char *
slouken@5120
   316
SDL_GetCPUName(void)
slouken@5115
   317
{
slouken@5115
   318
    static char SDL_CPUName[48];
slouken@5115
   319
slouken@5115
   320
    if (!SDL_CPUName[0]) {
slouken@5115
   321
        int i = 0;
slouken@5115
   322
        int a, b, c, d;
slouken@5115
   323
slouken@5115
   324
        if (CPU_haveCPUID()) {
slouken@3580
   325
            cpuid(0x80000000, a, b, c, d);
slouken@3580
   326
            if (a >= 0x80000004) {
slouken@3580
   327
                cpuid(0x80000002, a, b, c, d);
slouken@5115
   328
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   329
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   330
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   331
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   332
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   333
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   334
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   335
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   336
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   337
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   338
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   339
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   340
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   341
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   342
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   343
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@3580
   344
                cpuid(0x80000003, a, b, c, d);
slouken@5115
   345
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   346
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   347
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   348
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   349
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   350
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   351
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   352
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   353
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   354
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   355
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   356
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   357
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   358
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   359
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   360
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@3580
   361
                cpuid(0x80000004, a, b, c, d);
slouken@5115
   362
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   363
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   364
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   365
                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
slouken@5115
   366
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   367
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   368
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   369
                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
slouken@5115
   370
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   371
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   372
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   373
                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
slouken@5115
   374
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   375
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   376
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@5115
   377
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@3579
   378
            }
slouken@3579
   379
        }
slouken@5115
   380
        if (!SDL_CPUName[0]) {
slouken@5115
   381
            SDL_strlcpy(SDL_CPUName, "Unknown", sizeof(SDL_CPUName));
slouken@3579
   382
        }
slouken@3579
   383
    }
slouken@5115
   384
    return SDL_CPUName;
slouken@5115
   385
}
slouken@5115
   386
slouken@5120
   387
int
slouken@5120
   388
SDL_GetCPUCacheLineSize(void)
slouken@5115
   389
{
slouken@5115
   390
    const char *cpuType = SDL_GetCPUType();
slouken@5115
   391
slouken@5115
   392
    if (SDL_strcmp(cpuType, "GenuineIntel") == 0) {
slouken@5115
   393
        int a, b, c, d;
slouken@5115
   394
slouken@5115
   395
        cpuid(0x00000001, a, b, c, d);
slouken@5115
   396
        return (((b >> 8) & 0xff) * 8);
slouken@5115
   397
    } else if (SDL_strcmp(cpuType, "AuthenticAMD") == 0) {
slouken@5115
   398
        int a, b, c, d;
slouken@5115
   399
slouken@5115
   400
        cpuid(0x80000005, a, b, c, d);
slouken@5115
   401
        return (c & 0xff);
slouken@5115
   402
    } else {
slouken@5115
   403
        /* Just make a guess here... */
slouken@5115
   404
        return SDL_CACHELINE_SIZE;
slouken@5115
   405
    }
slouken@3579
   406
}
slouken@3579
   407
slouken@739
   408
static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
slouken@739
   409
slouken@1895
   410
static Uint32
slouken@1895
   411
SDL_GetCPUFeatures(void)
slouken@739
   412
{
slouken@1895
   413
    if (SDL_CPUFeatures == 0xFFFFFFFF) {
slouken@1895
   414
        SDL_CPUFeatures = 0;
slouken@1895
   415
        if (CPU_haveRDTSC()) {
slouken@1895
   416
            SDL_CPUFeatures |= CPU_HAS_RDTSC;
slouken@1895
   417
        }
slouken@1895
   418
        if (CPU_haveMMX()) {
slouken@1895
   419
            SDL_CPUFeatures |= CPU_HAS_MMX;
slouken@1895
   420
        }
slouken@1895
   421
        if (CPU_haveSSE()) {
slouken@1895
   422
            SDL_CPUFeatures |= CPU_HAS_SSE;
slouken@1895
   423
        }
slouken@1895
   424
        if (CPU_haveSSE2()) {
slouken@1895
   425
            SDL_CPUFeatures |= CPU_HAS_SSE2;
slouken@1895
   426
        }
slouken@5259
   427
        if (CPU_haveSSE3()) {
slouken@5259
   428
            SDL_CPUFeatures |= CPU_HAS_SSE3;
slouken@5259
   429
        }
slouken@5259
   430
        if (CPU_haveSSE4()) {
slouken@5259
   431
            SDL_CPUFeatures |= CPU_HAS_SSE4;
slouken@1895
   432
        }
slouken@1895
   433
    }
slouken@1895
   434
    return SDL_CPUFeatures;
slouken@739
   435
}
slouken@739
   436
slouken@1895
   437
SDL_bool
slouken@1895
   438
SDL_HasRDTSC(void)
slouken@745
   439
{
slouken@1895
   440
    if (SDL_GetCPUFeatures() & CPU_HAS_RDTSC) {
slouken@1895
   441
        return SDL_TRUE;
slouken@1895
   442
    }
slouken@1895
   443
    return SDL_FALSE;
slouken@745
   444
}
slouken@745
   445
slouken@1895
   446
SDL_bool
slouken@1895
   447
SDL_HasMMX(void)
slouken@739
   448
{
slouken@1895
   449
    if (SDL_GetCPUFeatures() & CPU_HAS_MMX) {
slouken@1895
   450
        return SDL_TRUE;
slouken@1895
   451
    }
slouken@1895
   452
    return SDL_FALSE;
slouken@739
   453
}
slouken@739
   454
slouken@1895
   455
SDL_bool
slouken@1895
   456
SDL_HasSSE(void)
slouken@739
   457
{
slouken@1895
   458
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE) {
slouken@1895
   459
        return SDL_TRUE;
slouken@1895
   460
    }
slouken@1895
   461
    return SDL_FALSE;
slouken@739
   462
}
slouken@739
   463
slouken@1895
   464
SDL_bool
slouken@1895
   465
SDL_HasSSE2(void)
slouken@804
   466
{
slouken@1895
   467
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE2) {
slouken@1895
   468
        return SDL_TRUE;
slouken@1895
   469
    }
slouken@1895
   470
    return SDL_FALSE;
slouken@804
   471
}
slouken@804
   472
slouken@1895
   473
SDL_bool
slouken@5259
   474
SDL_HasSSE3(void)
slouken@778
   475
{
slouken@5259
   476
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE3) {
slouken@5259
   477
        return SDL_TRUE;
slouken@5259
   478
    }
slouken@5259
   479
    return SDL_FALSE;
slouken@5259
   480
}
slouken@5259
   481
slouken@5259
   482
SDL_bool
slouken@5259
   483
SDL_HasSSE4(void)
slouken@5259
   484
{
slouken@5259
   485
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE4) {
slouken@1895
   486
        return SDL_TRUE;
slouken@1895
   487
    }
slouken@1895
   488
    return SDL_FALSE;
slouken@778
   489
}
slouken@778
   490
slouken@739
   491
#ifdef TEST_MAIN
slouken@739
   492
slouken@739
   493
#include <stdio.h>
slouken@739
   494
slouken@1895
   495
int
slouken@1895
   496
main()
slouken@739
   497
{
slouken@3579
   498
    printf("CPU count: %d\n", SDL_GetCPUCount());
slouken@5115
   499
    printf("CPU type: %s\n", SDL_GetCPUType());
slouken@5115
   500
    printf("CPU name: %s\n", SDL_GetCPUName());
slouken@5115
   501
    printf("CacheLine size: %d\n", SDL_GetCPUCacheLineSize());
slouken@1895
   502
    printf("RDTSC: %d\n", SDL_HasRDTSC());
slouken@1895
   503
    printf("MMX: %d\n", SDL_HasMMX());
slouken@1895
   504
    printf("SSE: %d\n", SDL_HasSSE());
slouken@1895
   505
    printf("SSE2: %d\n", SDL_HasSSE2());
slouken@5259
   506
    printf("SSE3: %d\n", SDL_HasSSE3());
slouken@5259
   507
    printf("SSE4: %d\n", SDL_HasSSE4());
slouken@1895
   508
    return 0;
slouken@739
   509
}
slouken@739
   510
slouken@739
   511
#endif /* TEST_MAIN */
slouken@1895
   512
slouken@1895
   513
/* vi: set ts=4 sw=4 expandtab: */