src/cpuinfo/SDL_cpuinfo.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 24 Nov 2003 09:16:52 +0000
changeset 745 71ee03909f42
parent 739 22dbf364c017
child 747 da36f59485da
permissions -rw-r--r--
Greatly simplified the SDL CPU info code
slouken@739
     1
/*
slouken@739
     2
    SDL - Simple DirectMedia Layer
slouken@739
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
slouken@739
     4
slouken@739
     5
    This library is free software; you can redistribute it and/or
slouken@739
     6
    modify it under the terms of the GNU Library General Public
slouken@739
     7
    License as published by the Free Software Foundation; either
slouken@739
     8
    version 2 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@739
    13
    Library General Public License for more details.
slouken@739
    14
slouken@739
    15
    You should have received a copy of the GNU Library General Public
slouken@739
    16
    License along with this library; if not, write to the Free
slouken@739
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@739
    18
slouken@739
    19
    Sam Lantinga
slouken@739
    20
    slouken@libsdl.org
slouken@739
    21
*/
slouken@739
    22
slouken@739
    23
#ifdef SAVE_RCSID
slouken@739
    24
static char rcsid =
slouken@739
    25
 "@(#) $Id$";
slouken@739
    26
#endif
slouken@739
    27
slouken@739
    28
/* CPU feature detection for SDL */
slouken@739
    29
slouken@739
    30
#include "SDL.h"
slouken@745
    31
#include "SDL_cpuinfo.h"
slouken@739
    32
slouken@745
    33
#define CPU_HAS_RDTSC	0x00000001
slouken@745
    34
#define CPU_HAS_MMX	0x00000002
slouken@745
    35
#define CPU_HAS_3DNOW	0x00000004
slouken@745
    36
#define CPU_HAS_SSE	0x00000008
slouken@739
    37
slouken@745
    38
static __inline__ int CPU_haveCPUID()
slouken@745
    39
{
slouken@745
    40
	int has_CPUID = 0;
slouken@745
    41
#if defined(__GNUC__) && defined(i386)
slouken@745
    42
	__asm__ (
slouken@745
    43
"push %%ecx\n"
slouken@745
    44
"        pushfl                      # Get original EFLAGS             \n"
slouken@745
    45
"        popl    %%eax                                                 \n"
slouken@745
    46
"        movl    %%eax,%%ecx                                           \n"
slouken@745
    47
"        xorl    $0x200000,%%eax     # Flip ID bit in EFLAGS           \n"
slouken@745
    48
"        pushl   %%eax               # Save new EFLAGS value on stack  \n"
slouken@745
    49
"        popfl                       # Replace current EFLAGS value    \n"
slouken@745
    50
"        pushfl                      # Get new EFLAGS                  \n"
slouken@745
    51
"        popl    %%eax               # Store new EFLAGS in EAX         \n"
slouken@745
    52
"        xorl    %%ecx,%%eax         # Can not toggle ID bit,          \n"
slouken@745
    53
"        jz      1f                  # Processor=80486                 \n"
slouken@745
    54
"        movl    $1,%0               # We have CPUID support           \n"
slouken@745
    55
"1:                                                                    \n"
slouken@745
    56
"pop %%ecx\n"
slouken@745
    57
	: "=r" (has_CPUID)
slouken@745
    58
	:
slouken@745
    59
	: "%eax", "%ecx"
slouken@745
    60
	);
slouken@745
    61
#elif defined(_MSC_VER)
slouken@745
    62
	__asm__ {
slouken@745
    63
        pushfd                      ; Get original EFLAGS
slouken@745
    64
        pop     eax
slouken@745
    65
        mov     ecx, eax
slouken@745
    66
        xor     eax, 200000h        ; Flip ID bit in EFLAGS
slouken@745
    67
        push    eax                 ; Save new EFLAGS value on stack
slouken@745
    68
        popfd                       ; Replace current EFLAGS value
slouken@745
    69
        pushfd                      ; Get new EFLAGS
slouken@745
    70
        pop     eax                 ; Store new EFLAGS in EAX
slouken@745
    71
        xor     eax, ecx            ; Can not toggle ID bit,
slouken@745
    72
        jz      done                ; Processor=80486
slouken@745
    73
        mov     has_CPUID,1         ; We have CPUID support
slouken@745
    74
done:
slouken@745
    75
	}
slouken@745
    76
#endif
slouken@745
    77
	return has_CPUID;
slouken@745
    78
}
slouken@745
    79
slouken@745
    80
static __inline__ int CPU_getCPUIDFeatures()
slouken@745
    81
{
slouken@745
    82
	int features = 0;
slouken@745
    83
#if defined(__GNUC__) && defined(i386)
slouken@745
    84
	__asm__ (
slouken@745
    85
"push %%ebx\n"
slouken@745
    86
"push %%ecx\n"
slouken@745
    87
"push %%edx\n"
slouken@745
    88
"        xorl    %%eax,%%eax         # Set up for CPUID instruction    \n"
slouken@745
    89
"        cpuid                       # Get and save vendor ID          \n"
slouken@745
    90
"        cmpl    $1,%%eax            # Make sure 1 is valid input for CPUID\n"
slouken@745
    91
"        jl      1f                  # We dont have the CPUID instruction\n"
slouken@745
    92
"        xorl    %%eax,%%eax                                           \n"
slouken@745
    93
"        incl    %%eax                                                 \n"
slouken@745
    94
"        cpuid                       # Get family/model/stepping/features\n"
slouken@745
    95
"        movl    %%edx,%0                                              \n"
slouken@745
    96
"1:                                                                    \n"
slouken@745
    97
"pop %%edx\n"
slouken@745
    98
"pop %%ecx\n"
slouken@745
    99
"pop %%ebx\n"
slouken@745
   100
	: "=r" (features)
slouken@745
   101
	:
slouken@745
   102
	: "%eax", "%ebx", "%ecx", "%edx"
slouken@745
   103
	);
slouken@745
   104
#elif defined(_MSC_VER)
slouken@745
   105
	__asm__ {
slouken@745
   106
        xor     eax, eax            ; Set up for CPUID instruction
slouken@745
   107
        cpuid                       ; Get and save vendor ID
slouken@745
   108
        cmp     eax, 1              ; Make sure 1 is valid input for CPUID
slouken@745
   109
        jl      done                ; We dont have the CPUID instruction
slouken@745
   110
        xor     eax, eax
slouken@745
   111
        inc     eax
slouken@745
   112
        cpuid                       ; Get family/model/stepping/features
slouken@745
   113
        mov     features, edx
slouken@745
   114
done:
slouken@745
   115
	}
slouken@745
   116
#endif
slouken@745
   117
	return features;
slouken@745
   118
}
slouken@745
   119
slouken@745
   120
static __inline__ int CPU_haveRDTSC()
slouken@745
   121
{
slouken@745
   122
	if ( CPU_haveCPUID() ) {
slouken@745
   123
		return (CPU_getCPUIDFeatures() & 0x00000010);
slouken@745
   124
	}
slouken@745
   125
	return 0;
slouken@745
   126
}
slouken@745
   127
slouken@745
   128
static __inline__ int CPU_haveMMX()
slouken@745
   129
{
slouken@745
   130
	if ( CPU_haveCPUID() ) {
slouken@745
   131
		return (CPU_getCPUIDFeatures() & 0x00800000);
slouken@745
   132
	}
slouken@745
   133
	return 0;
slouken@745
   134
}
slouken@745
   135
slouken@745
   136
static __inline__ int CPU_have3DNow()
slouken@745
   137
{
slouken@745
   138
	int has_3DNow = 0;
slouken@745
   139
#if defined(__GNUC__) && defined(i386)
slouken@745
   140
	__asm__ (
slouken@745
   141
"push %%ebx\n"
slouken@745
   142
"push %%ecx\n"
slouken@745
   143
"push %%edx\n"
slouken@745
   144
"        movl    $0x80000000,%%eax   # Query for extended functions    \n"
slouken@745
   145
"        cpuid                       # Get extended function limit     \n"
slouken@745
   146
"        cmpl    $0x80000001,%%eax                                     \n"
slouken@745
   147
"        jbe     1f                  # Nope, we dont have function 800000001h\n"
slouken@745
   148
"        movl    $0x80000001,%%eax   # Setup extended function 800000001h\n"
slouken@745
   149
"        cpuid                       # and get the information         \n"
slouken@745
   150
"        testl   $0x80000000,%%edx   # Bit 31 is set if 3DNow! present \n"
slouken@745
   151
"        jz      1f                  # Nope, we dont have 3DNow support\n"
slouken@745
   152
"        movl    $1,%0               # Yep, we have 3DNow! support!    \n"
slouken@745
   153
"1:                                                                    \n"
slouken@745
   154
"pop %%edx\n"
slouken@745
   155
"pop %%ecx\n"
slouken@745
   156
"pop %%ebx\n"
slouken@745
   157
	: "=r" (has_3DNow)
slouken@745
   158
	:
slouken@745
   159
	: "%eax", "%ebx", "%ecx", "%edx"
slouken@745
   160
	);
slouken@745
   161
#elif defined(_MSC_VER)
slouken@745
   162
	__asm__ {
slouken@745
   163
        mov     eax,80000000h       ; Query for extended functions
slouken@745
   164
        cpuid                       ; Get extended function limit
slouken@745
   165
        cmp     eax,80000001h
slouken@745
   166
        jbe     done                ; Nope, we dont have function 800000001h
slouken@745
   167
        mov     eax,80000001h       ; Setup extended function 800000001h
slouken@745
   168
        cpuid                       ; and get the information
slouken@745
   169
        test    edx,80000000h       ; Bit 31 is set if 3DNow! present
slouken@745
   170
        jz      done                ; Nope, we dont have 3DNow support
slouken@745
   171
        mov     has_3DNow,1         ; Yep, we have 3DNow! support!
slouken@745
   172
done:
slouken@745
   173
	}
slouken@745
   174
#endif
slouken@745
   175
	return has_3DNow;
slouken@745
   176
}
slouken@745
   177
slouken@745
   178
static __inline__ int CPU_haveSSE()
slouken@745
   179
{
slouken@745
   180
	if ( CPU_haveCPUID() ) {
slouken@745
   181
		return (CPU_getCPUIDFeatures() & 0x02000000);
slouken@745
   182
	}
slouken@745
   183
	return 0;
slouken@745
   184
}
slouken@739
   185
slouken@739
   186
static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
slouken@739
   187
slouken@739
   188
static Uint32 SDL_GetCPUFeatures()
slouken@739
   189
{
slouken@739
   190
	if ( SDL_CPUFeatures == 0xFFFFFFFF ) {
slouken@739
   191
		SDL_CPUFeatures = 0;
slouken@745
   192
		if ( CPU_haveRDTSC() ) {
slouken@745
   193
			SDL_CPUFeatures |= CPU_HAS_RDTSC;
slouken@745
   194
		}
slouken@739
   195
		if ( CPU_haveMMX() ) {
slouken@739
   196
			SDL_CPUFeatures |= CPU_HAS_MMX;
slouken@739
   197
		}
slouken@739
   198
		if ( CPU_have3DNow() ) {
slouken@739
   199
			SDL_CPUFeatures |= CPU_HAS_3DNOW;
slouken@739
   200
		}
slouken@739
   201
		if ( CPU_haveSSE() ) {
slouken@739
   202
			SDL_CPUFeatures |= CPU_HAS_SSE;
slouken@739
   203
		}
slouken@739
   204
	}
slouken@739
   205
	return SDL_CPUFeatures;
slouken@739
   206
}
slouken@739
   207
slouken@745
   208
SDL_bool SDL_HasRDTSC()
slouken@745
   209
{
slouken@745
   210
	if ( SDL_GetCPUFeatures() & CPU_HAS_RDTSC ) {
slouken@745
   211
		return SDL_TRUE;
slouken@745
   212
	}
slouken@745
   213
	return SDL_FALSE;
slouken@745
   214
}
slouken@745
   215
slouken@739
   216
SDL_bool SDL_HasMMX()
slouken@739
   217
{
slouken@739
   218
	if ( SDL_GetCPUFeatures() & CPU_HAS_MMX ) {
slouken@739
   219
		return SDL_TRUE;
slouken@739
   220
	}
slouken@739
   221
	return SDL_FALSE;
slouken@739
   222
}
slouken@739
   223
slouken@739
   224
SDL_bool SDL_Has3DNow()
slouken@739
   225
{
slouken@739
   226
	if ( SDL_GetCPUFeatures() & CPU_HAS_3DNOW ) {
slouken@739
   227
		return SDL_TRUE;
slouken@739
   228
	}
slouken@739
   229
	return SDL_FALSE;
slouken@739
   230
}
slouken@739
   231
slouken@739
   232
SDL_bool SDL_HasSSE()
slouken@739
   233
{
slouken@739
   234
	if ( SDL_GetCPUFeatures() & CPU_HAS_SSE ) {
slouken@739
   235
		return SDL_TRUE;
slouken@739
   236
	}
slouken@739
   237
	return SDL_FALSE;
slouken@739
   238
}
slouken@739
   239
slouken@739
   240
#ifdef TEST_MAIN
slouken@739
   241
slouken@739
   242
#include <stdio.h>
slouken@739
   243
slouken@739
   244
int main()
slouken@739
   245
{
slouken@739
   246
	printf("MMX: %d\n", SDL_HasMMX());
slouken@739
   247
	printf("3DNow: %d\n", SDL_Has3DNow());
slouken@739
   248
	printf("SSE: %d\n", SDL_HasSSE());
slouken@745
   249
	return 0;
slouken@739
   250
}
slouken@739
   251
slouken@739
   252
#endif /* TEST_MAIN */