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