src/cpuinfo/SDL_cpuinfo.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 24 Jan 2011 21:20:30 -0800
changeset 5090 327f181542f1
parent 5086 c2539ff054c8
child 5115 427998ff3bcf
permissions -rw-r--r--
Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.

I think this also fixes the bug relating to non-latin characters in filenames, since UNICODE wasn't defined in SDL_rwops.c
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@3579
   307
SDL_GetCPUCount()
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@3579
   338
SDL_GetCPUType()
slouken@3579
   339
{
slouken@3579
   340
    static char SDL_CPUType[48];
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@3580
   347
            cpuid(0x80000000, a, b, c, d);
slouken@3580
   348
            if (a >= 0x80000004) {
slouken@3580
   349
                cpuid(0x80000002, a, b, c, d);
slouken@3580
   350
                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
slouken@3580
   351
                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
slouken@3580
   352
                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
slouken@3580
   353
                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
slouken@3580
   354
                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@3580
   355
                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@3580
   356
                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@3580
   357
                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@3580
   358
                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@3580
   359
                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@3580
   360
                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@3580
   361
                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@3580
   362
                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@3580
   363
                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@3580
   364
                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@3580
   365
                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@3580
   366
                cpuid(0x80000003, a, b, c, d);
slouken@3580
   367
                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
slouken@3580
   368
                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
slouken@3580
   369
                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
slouken@3580
   370
                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
slouken@3580
   371
                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@3580
   372
                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@3580
   373
                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@3580
   374
                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@3580
   375
                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@3580
   376
                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@3580
   377
                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@3580
   378
                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@3580
   379
                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@3580
   380
                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@3580
   381
                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@3580
   382
                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@3580
   383
                cpuid(0x80000004, a, b, c, d);
slouken@3580
   384
                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
slouken@3580
   385
                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
slouken@3580
   386
                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
slouken@3580
   387
                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
slouken@3580
   388
                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@3580
   389
                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@3580
   390
                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@3580
   391
                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
slouken@3580
   392
                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@3580
   393
                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@3580
   394
                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@3580
   395
                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
slouken@3580
   396
                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@3580
   397
                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@3580
   398
                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@3580
   399
                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
slouken@3579
   400
            }
slouken@3579
   401
        }
slouken@3579
   402
        if (!SDL_CPUType[0]) {
slouken@3579
   403
            SDL_strlcpy(SDL_CPUType, "Unknown", sizeof(SDL_CPUType));
slouken@3579
   404
        }
slouken@3579
   405
    }
slouken@3579
   406
    return SDL_CPUType;
slouken@3579
   407
}
slouken@3579
   408
slouken@739
   409
static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
slouken@739
   410
slouken@1895
   411
static Uint32
slouken@1895
   412
SDL_GetCPUFeatures(void)
slouken@739
   413
{
slouken@1895
   414
    if (SDL_CPUFeatures == 0xFFFFFFFF) {
slouken@1895
   415
        SDL_CPUFeatures = 0;
slouken@1895
   416
        if (CPU_haveRDTSC()) {
slouken@1895
   417
            SDL_CPUFeatures |= CPU_HAS_RDTSC;
slouken@1895
   418
        }
slouken@1895
   419
        if (CPU_haveMMX()) {
slouken@1895
   420
            SDL_CPUFeatures |= CPU_HAS_MMX;
slouken@1895
   421
        }
slouken@1895
   422
        if (CPU_haveMMXExt()) {
slouken@1895
   423
            SDL_CPUFeatures |= CPU_HAS_MMXEXT;
slouken@1895
   424
        }
slouken@1895
   425
        if (CPU_have3DNow()) {
slouken@1895
   426
            SDL_CPUFeatures |= CPU_HAS_3DNOW;
slouken@1895
   427
        }
slouken@1895
   428
        if (CPU_have3DNowExt()) {
slouken@1895
   429
            SDL_CPUFeatures |= CPU_HAS_3DNOWEXT;
slouken@1895
   430
        }
slouken@1895
   431
        if (CPU_haveSSE()) {
slouken@1895
   432
            SDL_CPUFeatures |= CPU_HAS_SSE;
slouken@1895
   433
        }
slouken@1895
   434
        if (CPU_haveSSE2()) {
slouken@1895
   435
            SDL_CPUFeatures |= CPU_HAS_SSE2;
slouken@1895
   436
        }
slouken@1895
   437
        if (CPU_haveAltiVec()) {
slouken@1895
   438
            SDL_CPUFeatures |= CPU_HAS_ALTIVEC;
slouken@1895
   439
        }
slouken@1895
   440
    }
slouken@1895
   441
    return SDL_CPUFeatures;
slouken@739
   442
}
slouken@739
   443
slouken@1895
   444
SDL_bool
slouken@1895
   445
SDL_HasRDTSC(void)
slouken@745
   446
{
slouken@1895
   447
    if (SDL_GetCPUFeatures() & CPU_HAS_RDTSC) {
slouken@1895
   448
        return SDL_TRUE;
slouken@1895
   449
    }
slouken@1895
   450
    return SDL_FALSE;
slouken@745
   451
}
slouken@745
   452
slouken@1895
   453
SDL_bool
slouken@1895
   454
SDL_HasMMX(void)
slouken@739
   455
{
slouken@1895
   456
    if (SDL_GetCPUFeatures() & CPU_HAS_MMX) {
slouken@1895
   457
        return SDL_TRUE;
slouken@1895
   458
    }
slouken@1895
   459
    return SDL_FALSE;
slouken@739
   460
}
slouken@739
   461
slouken@1895
   462
SDL_bool
slouken@1895
   463
SDL_HasMMXExt(void)
slouken@804
   464
{
slouken@1895
   465
    if (SDL_GetCPUFeatures() & CPU_HAS_MMXEXT) {
slouken@1895
   466
        return SDL_TRUE;
slouken@1895
   467
    }
slouken@1895
   468
    return SDL_FALSE;
slouken@804
   469
}
slouken@804
   470
slouken@1895
   471
SDL_bool
slouken@1895
   472
SDL_Has3DNow(void)
slouken@739
   473
{
slouken@1895
   474
    if (SDL_GetCPUFeatures() & CPU_HAS_3DNOW) {
slouken@1895
   475
        return SDL_TRUE;
slouken@1895
   476
    }
slouken@1895
   477
    return SDL_FALSE;
slouken@739
   478
}
slouken@739
   479
slouken@1895
   480
SDL_bool
slouken@1895
   481
SDL_Has3DNowExt(void)
slouken@804
   482
{
slouken@1895
   483
    if (SDL_GetCPUFeatures() & CPU_HAS_3DNOWEXT) {
slouken@1895
   484
        return SDL_TRUE;
slouken@1895
   485
    }
slouken@1895
   486
    return SDL_FALSE;
slouken@804
   487
}
slouken@804
   488
slouken@1895
   489
SDL_bool
slouken@1895
   490
SDL_HasSSE(void)
slouken@739
   491
{
slouken@1895
   492
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE) {
slouken@1895
   493
        return SDL_TRUE;
slouken@1895
   494
    }
slouken@1895
   495
    return SDL_FALSE;
slouken@739
   496
}
slouken@739
   497
slouken@1895
   498
SDL_bool
slouken@1895
   499
SDL_HasSSE2(void)
slouken@804
   500
{
slouken@1895
   501
    if (SDL_GetCPUFeatures() & CPU_HAS_SSE2) {
slouken@1895
   502
        return SDL_TRUE;
slouken@1895
   503
    }
slouken@1895
   504
    return SDL_FALSE;
slouken@804
   505
}
slouken@804
   506
slouken@1895
   507
SDL_bool
slouken@1895
   508
SDL_HasAltiVec(void)
slouken@778
   509
{
slouken@1895
   510
    if (SDL_GetCPUFeatures() & CPU_HAS_ALTIVEC) {
slouken@1895
   511
        return SDL_TRUE;
slouken@1895
   512
    }
slouken@1895
   513
    return SDL_FALSE;
slouken@778
   514
}
slouken@778
   515
slouken@739
   516
#ifdef TEST_MAIN
slouken@739
   517
slouken@739
   518
#include <stdio.h>
slouken@739
   519
slouken@1895
   520
int
slouken@1895
   521
main()
slouken@739
   522
{
slouken@3579
   523
    printf("CPU count: %d\n", SDL_GetCPUCount());
slouken@3579
   524
    printf("CPU name: %s\n", SDL_GetCPUType());
slouken@1895
   525
    printf("RDTSC: %d\n", SDL_HasRDTSC());
slouken@1895
   526
    printf("MMX: %d\n", SDL_HasMMX());
slouken@1895
   527
    printf("MMXExt: %d\n", SDL_HasMMXExt());
slouken@1895
   528
    printf("3DNow: %d\n", SDL_Has3DNow());
slouken@1895
   529
    printf("3DNowExt: %d\n", SDL_Has3DNowExt());
slouken@1895
   530
    printf("SSE: %d\n", SDL_HasSSE());
slouken@1895
   531
    printf("SSE2: %d\n", SDL_HasSSE2());
slouken@1895
   532
    printf("AltiVec: %d\n", SDL_HasAltiVec());
slouken@1895
   533
    return 0;
slouken@739
   534
}
slouken@739
   535
slouken@739
   536
#endif /* TEST_MAIN */
slouken@1895
   537
slouken@1895
   538
/* vi: set ts=4 sw=4 expandtab: */