src/cpuinfo/SDL_cpuinfo.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 13 Oct 2011 01:08:30 -0400
changeset 5981 75caa8a7d559
parent 5535 96594ac5fd1a
child 6138 4c64952a58fb
permissions -rw-r--r--
Fixed a whole slew of compiler warnings that -Wall exposed.
slouken@739
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 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@5389
    36
#elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
slouken@5389
    37
#include <signal.h>
slouken@5389
    38
#include <setjmp.h>
slouken@5389
    39
#endif
slouken@5086
    40
#ifdef __WIN32__
slouken@5090
    41
#include "../core/windows/SDL_windows.h"
slouken@3580
    42
#endif
slouken@793
    43
slouken@3579
    44
#define CPU_HAS_RDTSC   0x00000001
slouken@5389
    45
#define CPU_HAS_ALTIVEC 0x00000002
slouken@5389
    46
#define CPU_HAS_MMX     0x00000004
slouken@5389
    47
#define CPU_HAS_3DNOW   0x00000008
slouken@5259
    48
#define CPU_HAS_SSE     0x00000010
slouken@5259
    49
#define CPU_HAS_SSE2    0x00000020
slouken@5259
    50
#define CPU_HAS_SSE3    0x00000040
slouken@5389
    51
#define CPU_HAS_SSE41   0x00000100
slouken@5389
    52
#define CPU_HAS_SSE42   0x00000200
slouken@739
    53
slouken@5389
    54
#if SDL_ALTIVEC_BLITTERS && HAVE_SETJMP && !__MACOSX__
slouken@5389
    55
/* This is the brute force way of detecting instruction sets...
slouken@5389
    56
   the idea is borrowed from the libmpeg2 library - thanks!
slouken@5389
    57
 */
slouken@5389
    58
static jmp_buf jmpbuf;
slouken@5389
    59
static void
slouken@5389
    60
illegal_instruction(int sig)
slouken@5389
    61
{
slouken@5389
    62
    longjmp(jmpbuf, 1);
slouken@5389
    63
}
slouken@5389
    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_haveRDTSC(void)
slouken@745
   206
{
slouken@1895
   207
    if (CPU_haveCPUID()) {
slouken@1895
   208
        return (CPU_getCPUIDFeatures() & 0x00000010);
slouken@1895
   209
    }
slouken@1895
   210
    return 0;
slouken@745
   211
}
slouken@745
   212
slouken@1895
   213
static __inline__ int
slouken@5389
   214
CPU_haveAltiVec(void)
slouken@5389
   215
{
slouken@5389
   216
    volatile int altivec = 0;
slouken@5389
   217
#if defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))
slouken@5389
   218
    int selectors[2] = { CTL_HW, HW_VECTORUNIT };
slouken@5389
   219
    int hasVectorUnit = 0;
slouken@5389
   220
    size_t length = sizeof(hasVectorUnit);
slouken@5389
   221
    int error = sysctl(selectors, 2, &hasVectorUnit, &length, NULL, 0);
slouken@5389
   222
    if (0 == error)
slouken@5389
   223
        altivec = (hasVectorUnit != 0);
slouken@5389
   224
#elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
slouken@5389
   225
    void (*handler) (int sig);
slouken@5389
   226
    handler = signal(SIGILL, illegal_instruction);
slouken@5389
   227
    if (setjmp(jmpbuf) == 0) {
slouken@5389
   228
        asm volatile ("mtspr 256, %0\n\t" "vand %%v0, %%v0, %%v0"::"r" (-1));
slouken@5389
   229
        altivec = 1;
slouken@5389
   230
    }
slouken@5389
   231
    signal(SIGILL, handler);
slouken@5389
   232
#endif
slouken@5389
   233
    return altivec;
slouken@5389
   234
}
slouken@5389
   235
slouken@5389
   236
static __inline__ int
slouken@1895
   237
CPU_haveMMX(void)
slouken@745
   238
{
slouken@1895
   239
    if (CPU_haveCPUID()) {
slouken@1895
   240
        return (CPU_getCPUIDFeatures() & 0x00800000);
slouken@1895
   241
    }
slouken@1895
   242
    return 0;
slouken@745
   243
}
slouken@745
   244
slouken@1895
   245
static __inline__ int
slouken@5389
   246
CPU_have3DNow(void)
slouken@5389
   247
{
slouken@5389
   248
    if (CPU_haveCPUID()) {
slouken@5389
   249
        int a, b, c, d;
slouken@5389
   250
slouken@5389
   251
        cpuid(0x80000000, a, b, c, d);
slouken@5389
   252
        if (a >= 0x80000001) {
slouken@5389
   253
            cpuid(0x80000001, a, b, c, d);
slouken@5389
   254
            return (d & 0x80000000);
slouken@5389
   255
        }
slouken@5389
   256
    }
slouken@5389
   257
    return 0;
slouken@5389
   258
}
slouken@5389
   259
slouken@5389
   260
static __inline__ int
slouken@1895
   261
CPU_haveSSE(void)
slouken@745
   262
{
slouken@1895
   263
    if (CPU_haveCPUID()) {
slouken@1895
   264
        return (CPU_getCPUIDFeatures() & 0x02000000);
slouken@1895
   265
    }
slouken@1895
   266
    return 0;
slouken@745
   267
}
slouken@739
   268
slouken@1895
   269
static __inline__ int
slouken@1895
   270
CPU_haveSSE2(void)
slouken@785
   271
{
slouken@1895
   272
    if (CPU_haveCPUID()) {
slouken@1895
   273
        return (CPU_getCPUIDFeatures() & 0x04000000);
slouken@1895
   274
    }
slouken@1895
   275
    return 0;
slouken@785
   276
}
slouken@785
   277
slouken@1895
   278
static __inline__ int
slouken@5259
   279
CPU_haveSSE3(void)
slouken@778
   280
{
slouken@5259
   281
    if (CPU_haveCPUID()) {
slouken@5259
   282
        int a, b, c, d;
slouken@5259
   283
slouken@5259
   284
        cpuid(0, a, b, c, d);
slouken@5259
   285
        if (a >= 1) {
slouken@5259
   286
            cpuid(1, a, b, c, d);
slouken@5259
   287
            return (c & 0x00000001);
slouken@5259
   288
        }
slouken@1895
   289
    }
slouken@5259
   290
    return 0;
slouken@5259
   291
}
slouken@5259
   292
slouken@5259
   293
static __inline__ int
slouken@5263
   294
CPU_haveSSE41(void)
slouken@5259
   295
{
slouken@5259
   296
    if (CPU_haveCPUID()) {
slouken@5259
   297
        int a, b, c, d;
slouken@5259
   298
slouken@5263
   299
        cpuid(1, a, b, c, d);
slouken@5259
   300
        if (a >= 1) {
slouken@5259
   301
            cpuid(1, a, b, c, d);
slouken@5263
   302
            return (c & 0x00080000);
slouken@5263
   303
        }
slouken@5263
   304
    }
slouken@5263
   305
    return 0;
slouken@5263
   306
}
slouken@5263
   307
slouken@5263
   308
static __inline__ int
slouken@5263
   309
CPU_haveSSE42(void)
slouken@5263
   310
{
slouken@5263
   311
    if (CPU_haveCPUID()) {
slouken@5263
   312
        int a, b, c, d;
slouken@5263
   313
slouken@5263
   314
        cpuid(1, a, b, c, d);
slouken@5263
   315
        if (a >= 1) {
slouken@5263
   316
            cpuid(1, a, b, c, d);
slouken@5263
   317
            return (c & 0x00100000);
slouken@5259
   318
        }
slouken@5259
   319
    }
slouken@5259
   320
    return 0;
slouken@778
   321
}
slouken@778
   322
slouken@3579
   323
static int SDL_CPUCount = 0;
slouken@3579
   324
slouken@3579
   325
int
slouken@5120
   326
SDL_GetCPUCount(void)
slouken@3579
   327
{
slouken@3579
   328
    if (!SDL_CPUCount) {
slouken@3606
   329
#if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
slouken@3586
   330
        if (SDL_CPUCount <= 0) {
slouken@3586
   331
            SDL_CPUCount = (int)sysconf(_SC_NPROCESSORS_ONLN);
slouken@3586
   332
        }
slouken@3586
   333
#endif
slouken@3579
   334
#ifdef HAVE_SYSCTLBYNAME
slouken@3586
   335
        if (SDL_CPUCount <= 0) {
slouken@3580
   336
            size_t size = sizeof(SDL_CPUCount);
slouken@3580
   337
            sysctlbyname("hw.ncpu", &SDL_CPUCount, &size, NULL, 0);
slouken@3580
   338
        }
slouken@3580
   339
#endif
slouken@5086
   340
#ifdef __WIN32__
slouken@3586
   341
        if (SDL_CPUCount <= 0) {
slouken@3580
   342
            SYSTEM_INFO info;
slouken@3580
   343
            GetSystemInfo(&info);
slouken@3580
   344
            SDL_CPUCount = info.dwNumberOfProcessors;
slouken@3580
   345
        }
slouken@3579
   346
#endif
slouken@3579
   347
        /* There has to be at least 1, right? :) */
slouken@3586
   348
        if (SDL_CPUCount <= 0) {
slouken@3579
   349
            SDL_CPUCount = 1;
slouken@3579
   350
        }
slouken@3579
   351
    }
slouken@3579
   352
    return SDL_CPUCount;
slouken@3579
   353
}
slouken@3579
   354
slouken@3579
   355
/* Oh, such a sweet sweet trick, just not very useful. :) */
slouken@4472
   356
static const char *
slouken@5120
   357
SDL_GetCPUType(void)
slouken@3579
   358
{
slouken@5115
   359
    static char SDL_CPUType[13];
slouken@3579
   360
slouken@3579
   361
    if (!SDL_CPUType[0]) {
slouken@3579
   362
        int i = 0;
slouken@3580
   363
        int a, b, c, d;
slouken@3579
   364
slouken@3579
   365
        if (CPU_haveCPUID()) {
slouken@5115
   366
            cpuid(0x00000000, a, b, c, d);
slouken@5115
   367
            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
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)(d & 0xff); d >>= 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)(c & 0xff); c >>= 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
        }
slouken@5115
   380
        if (!SDL_CPUType[0]) {
slouken@5115
   381
            SDL_strlcpy(SDL_CPUType, "Unknown", sizeof(SDL_CPUType));
slouken@5115
   382
        }
slouken@5115
   383
    }
slouken@5115
   384
    return SDL_CPUType;
slouken@5115
   385
}
slouken@5115
   386
icculus@5981
   387
icculus@5981
   388
#ifdef TEST_MAIN  /* !!! FIXME: only used for test at the moment. */
slouken@5115
   389
static const char *
slouken@5120
   390
SDL_GetCPUName(void)
slouken@5115
   391
{
slouken@5115
   392
    static char SDL_CPUName[48];
slouken@5115
   393
slouken@5115
   394
    if (!SDL_CPUName[0]) {
slouken@5115
   395
        int i = 0;
slouken@5115
   396
        int a, b, c, d;
slouken@5115
   397
slouken@5115
   398
        if (CPU_haveCPUID()) {
slouken@3580
   399
            cpuid(0x80000000, a, b, c, d);
slouken@3580
   400
            if (a >= 0x80000004) {
slouken@3580
   401
                cpuid(0x80000002, a, b, c, d);
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)(a & 0xff); a >>= 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)(b & 0xff); b >>= 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)(c & 0xff); c >>= 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@5115
   417
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@3580
   418
                cpuid(0x80000003, a, b, c, d);
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)(a & 0xff); a >>= 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)(b & 0xff); b >>= 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)(c & 0xff); c >>= 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@5115
   434
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@3580
   435
                cpuid(0x80000004, a, b, c, d);
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)(a & 0xff); a >>= 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)(b & 0xff); b >>= 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)(c & 0xff); c >>= 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@5115
   451
                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
slouken@3579
   452
            }
slouken@3579
   453
        }
slouken@5115
   454
        if (!SDL_CPUName[0]) {
slouken@5115
   455
            SDL_strlcpy(SDL_CPUName, "Unknown", sizeof(SDL_CPUName));
slouken@3579
   456
        }
slouken@3579
   457
    }
slouken@5115
   458
    return SDL_CPUName;
slouken@5115
   459
}
icculus@5981
   460
#endif
slouken@5115
   461
slouken@5120
   462
int
slouken@5120
   463
SDL_GetCPUCacheLineSize(void)
slouken@5115
   464
{
slouken@5115
   465
    const char *cpuType = SDL_GetCPUType();
slouken@5115
   466
slouken@5115
   467
    if (SDL_strcmp(cpuType, "GenuineIntel") == 0) {
slouken@5115
   468
        int a, b, c, d;
slouken@5115
   469
slouken@5115
   470
        cpuid(0x00000001, a, b, c, d);
slouken@5115
   471
        return (((b >> 8) & 0xff) * 8);
slouken@5115
   472
    } else if (SDL_strcmp(cpuType, "AuthenticAMD") == 0) {
slouken@5115
   473
        int a, b, c, d;
slouken@5115
   474
slouken@5115
   475
        cpuid(0x80000005, a, b, c, d);
slouken@5115
   476
        return (c & 0xff);
slouken@5115
   477
    } else {
slouken@5115
   478
        /* Just make a guess here... */
slouken@5115
   479
        return SDL_CACHELINE_SIZE;
slouken@5115
   480
    }
slouken@3579
   481
}
slouken@3579
   482
slouken@739
   483
static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
slouken@739
   484
slouken@1895
   485
static Uint32
slouken@1895
   486
SDL_GetCPUFeatures(void)
slouken@739
   487
{
slouken@1895
   488
    if (SDL_CPUFeatures == 0xFFFFFFFF) {
slouken@1895
   489
        SDL_CPUFeatures = 0;
slouken@1895
   490
        if (CPU_haveRDTSC()) {
slouken@1895
   491
            SDL_CPUFeatures |= CPU_HAS_RDTSC;
slouken@1895
   492
        }
slouken@5389
   493
        if (CPU_haveAltiVec()) {
slouken@5389
   494
            SDL_CPUFeatures |= CPU_HAS_ALTIVEC;
slouken@5389
   495
        }
slouken@1895
   496
        if (CPU_haveMMX()) {
slouken@1895
   497
            SDL_CPUFeatures |= CPU_HAS_MMX;
slouken@1895
   498
        }
slouken@5389
   499
        if (CPU_have3DNow()) {
slouken@5389
   500
            SDL_CPUFeatures |= CPU_HAS_3DNOW;
slouken@5389
   501
        }
slouken@1895
   502
        if (CPU_haveSSE()) {
slouken@1895
   503
            SDL_CPUFeatures |= CPU_HAS_SSE;
slouken@1895
   504
        }
slouken@1895
   505
        if (CPU_haveSSE2()) {
slouken@1895
   506
            SDL_CPUFeatures |= CPU_HAS_SSE2;
slouken@1895
   507
        }
slouken@5259
   508
        if (CPU_haveSSE3()) {
slouken@5259
   509
            SDL_CPUFeatures |= CPU_HAS_SSE3;
slouken@5259
   510
        }
slouken@5263
   511
        if (CPU_haveSSE41()) {
slouken@5263
   512
            SDL_CPUFeatures |= CPU_HAS_SSE41;
slouken@5263
   513
        }
slouken@5263
   514
        if (CPU_haveSSE42()) {
slouken@5263
   515
            SDL_CPUFeatures |= CPU_HAS_SSE42;
slouken@1895
   516
        }
slouken@1895
   517
    }
slouken@1895
   518
    return SDL_CPUFeatures;
slouken@739
   519
}
slouken@739
   520
slouken@1895
   521
SDL_bool
slouken@1895
   522
SDL_HasRDTSC(void)
slouken@745
   523
{
slouken@1895
   524
    if (SDL_GetCPUFeatures() & CPU_HAS_RDTSC) {
slouken@1895
   525
        return SDL_TRUE;
slouken@1895
   526
    }
slouken@1895
   527
    return SDL_FALSE;
slouken@745
   528
}
slouken@745
   529
slouken@1895
   530
SDL_bool
slouken@5389
   531
SDL_HasAltiVec(void)
slouken@5389
   532
{
slouken@5389
   533
    if (SDL_GetCPUFeatures() & CPU_HAS_ALTIVEC) {
slouken@5389
   534
        return SDL_TRUE;
slouken@5389
   535
    }
slouken@5389
   536
    return SDL_FALSE;
slouken@5389
   537
}
slouken@5389
   538
slouken@5389
   539
SDL_bool
slouken@1895
   540
SDL_HasMMX(void)
slouken@739
   541
{
slouken@1895
   542
    if (SDL_GetCPUFeatures() & CPU_HAS_MMX) {
slouken@1895
   543
        return SDL_TRUE;
slouken@1895
   544
    }
slouken@1895
   545
    return SDL_FALSE;
slouken@739
   546
}
slouken@739
   547
slouken@1895
   548
SDL_bool
slouken@5389
   549
SDL_Has3DNow(void)
slouken@5389
   550
{
slouken@5389
   551
    if (SDL_GetCPUFeatures() & CPU_HAS_3DNOW) {
slouken@5389
   552
        return SDL_TRUE;
slouken@5389
   553
    }
slouken@5389
   554
    return SDL_FALSE;
slouken@5389
   555
}
slouken@5389
   556
slouken@5389
   557
SDL_bool
slouken@1895
   558
SDL_HasSSE(void)
slouken@739
   559
{
slouken@1895
   560
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE) {
slouken@1895
   561
        return SDL_TRUE;
slouken@1895
   562
    }
slouken@1895
   563
    return SDL_FALSE;
slouken@739
   564
}
slouken@739
   565
slouken@1895
   566
SDL_bool
slouken@1895
   567
SDL_HasSSE2(void)
slouken@804
   568
{
slouken@1895
   569
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE2) {
slouken@1895
   570
        return SDL_TRUE;
slouken@1895
   571
    }
slouken@1895
   572
    return SDL_FALSE;
slouken@804
   573
}
slouken@804
   574
slouken@1895
   575
SDL_bool
slouken@5259
   576
SDL_HasSSE3(void)
slouken@778
   577
{
slouken@5259
   578
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE3) {
slouken@5259
   579
        return SDL_TRUE;
slouken@5259
   580
    }
slouken@5259
   581
    return SDL_FALSE;
slouken@5259
   582
}
slouken@5259
   583
slouken@5259
   584
SDL_bool
slouken@5263
   585
SDL_HasSSE41(void)
slouken@5259
   586
{
slouken@5263
   587
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE41) {
slouken@5263
   588
        return SDL_TRUE;
slouken@5263
   589
    }
slouken@5263
   590
    return SDL_FALSE;
slouken@5263
   591
}
slouken@5263
   592
slouken@5263
   593
SDL_bool
slouken@5263
   594
SDL_HasSSE42(void)
slouken@5263
   595
{
slouken@5263
   596
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE42) {
slouken@1895
   597
        return SDL_TRUE;
slouken@1895
   598
    }
slouken@1895
   599
    return SDL_FALSE;
slouken@778
   600
}
slouken@778
   601
slouken@739
   602
#ifdef TEST_MAIN
slouken@739
   603
slouken@739
   604
#include <stdio.h>
slouken@739
   605
slouken@1895
   606
int
slouken@1895
   607
main()
slouken@739
   608
{
slouken@3579
   609
    printf("CPU count: %d\n", SDL_GetCPUCount());
slouken@5115
   610
    printf("CPU type: %s\n", SDL_GetCPUType());
slouken@5115
   611
    printf("CPU name: %s\n", SDL_GetCPUName());
slouken@5115
   612
    printf("CacheLine size: %d\n", SDL_GetCPUCacheLineSize());
slouken@1895
   613
    printf("RDTSC: %d\n", SDL_HasRDTSC());
slouken@5389
   614
    printf("Altivec: %d\n", SDL_HasAltiVec());
slouken@1895
   615
    printf("MMX: %d\n", SDL_HasMMX());
slouken@5389
   616
    printf("3DNow: %d\n", SDL_Has3DNow());
slouken@1895
   617
    printf("SSE: %d\n", SDL_HasSSE());
slouken@1895
   618
    printf("SSE2: %d\n", SDL_HasSSE2());
slouken@5259
   619
    printf("SSE3: %d\n", SDL_HasSSE3());
slouken@5263
   620
    printf("SSE4.1: %d\n", SDL_HasSSE41());
slouken@5263
   621
    printf("SSE4.2: %d\n", SDL_HasSSE42());
slouken@1895
   622
    return 0;
slouken@739
   623
}
slouken@739
   624
slouken@739
   625
#endif /* TEST_MAIN */
slouken@1895
   626
slouken@1895
   627
/* vi: set ts=4 sw=4 expandtab: */