src/cpuinfo/SDL_cpuinfo.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 06 Jan 2010 04:33:31 +0000
changeset 3606 1a4456a01995
parent 3587 2080e8d75ac6
child 3697 f7b03b6838cb
permissions -rw-r--r--
Fixed bug #924

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