src/cpuinfo/SDL_cpuinfo.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 24 Nov 2003 19:58:29 +0000
changeset 749 06cdd106d61c
parent 747 da36f59485da
child 769 b8d311d90021
permissions -rw-r--r--
Updated Visual C++ support
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@749
    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@749
   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@747
   139
	if ( !CPU_haveCPUID() ) {
slouken@747
   140
		return 0;
slouken@747
   141
	}
slouken@745
   142
#if defined(__GNUC__) && defined(i386)
slouken@745
   143
	__asm__ (
slouken@745
   144
"push %%ebx\n"
slouken@745
   145
"push %%ecx\n"
slouken@745
   146
"push %%edx\n"
slouken@745
   147
"        movl    $0x80000000,%%eax   # Query for extended functions    \n"
slouken@745
   148
"        cpuid                       # Get extended function limit     \n"
slouken@745
   149
"        cmpl    $0x80000001,%%eax                                     \n"
slouken@745
   150
"        jbe     1f                  # Nope, we dont have function 800000001h\n"
slouken@745
   151
"        movl    $0x80000001,%%eax   # Setup extended function 800000001h\n"
slouken@745
   152
"        cpuid                       # and get the information         \n"
slouken@745
   153
"        testl   $0x80000000,%%edx   # Bit 31 is set if 3DNow! present \n"
slouken@745
   154
"        jz      1f                  # Nope, we dont have 3DNow support\n"
slouken@745
   155
"        movl    $1,%0               # Yep, we have 3DNow! support!    \n"
slouken@745
   156
"1:                                                                    \n"
slouken@745
   157
"pop %%edx\n"
slouken@745
   158
"pop %%ecx\n"
slouken@745
   159
"pop %%ebx\n"
slouken@745
   160
	: "=r" (has_3DNow)
slouken@745
   161
	:
slouken@745
   162
	: "%eax", "%ebx", "%ecx", "%edx"
slouken@745
   163
	);
slouken@745
   164
#elif defined(_MSC_VER)
slouken@749
   165
	__asm {
slouken@745
   166
        mov     eax,80000000h       ; Query for extended functions
slouken@745
   167
        cpuid                       ; Get extended function limit
slouken@745
   168
        cmp     eax,80000001h
slouken@745
   169
        jbe     done                ; Nope, we dont have function 800000001h
slouken@745
   170
        mov     eax,80000001h       ; Setup extended function 800000001h
slouken@745
   171
        cpuid                       ; and get the information
slouken@745
   172
        test    edx,80000000h       ; Bit 31 is set if 3DNow! present
slouken@745
   173
        jz      done                ; Nope, we dont have 3DNow support
slouken@745
   174
        mov     has_3DNow,1         ; Yep, we have 3DNow! support!
slouken@745
   175
done:
slouken@745
   176
	}
slouken@745
   177
#endif
slouken@745
   178
	return has_3DNow;
slouken@745
   179
}
slouken@745
   180
slouken@745
   181
static __inline__ int CPU_haveSSE()
slouken@745
   182
{
slouken@745
   183
	if ( CPU_haveCPUID() ) {
slouken@745
   184
		return (CPU_getCPUIDFeatures() & 0x02000000);
slouken@745
   185
	}
slouken@745
   186
	return 0;
slouken@745
   187
}
slouken@739
   188
slouken@739
   189
static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
slouken@739
   190
slouken@739
   191
static Uint32 SDL_GetCPUFeatures()
slouken@739
   192
{
slouken@739
   193
	if ( SDL_CPUFeatures == 0xFFFFFFFF ) {
slouken@739
   194
		SDL_CPUFeatures = 0;
slouken@745
   195
		if ( CPU_haveRDTSC() ) {
slouken@745
   196
			SDL_CPUFeatures |= CPU_HAS_RDTSC;
slouken@745
   197
		}
slouken@739
   198
		if ( CPU_haveMMX() ) {
slouken@739
   199
			SDL_CPUFeatures |= CPU_HAS_MMX;
slouken@739
   200
		}
slouken@739
   201
		if ( CPU_have3DNow() ) {
slouken@739
   202
			SDL_CPUFeatures |= CPU_HAS_3DNOW;
slouken@739
   203
		}
slouken@739
   204
		if ( CPU_haveSSE() ) {
slouken@739
   205
			SDL_CPUFeatures |= CPU_HAS_SSE;
slouken@739
   206
		}
slouken@739
   207
	}
slouken@739
   208
	return SDL_CPUFeatures;
slouken@739
   209
}
slouken@739
   210
slouken@745
   211
SDL_bool SDL_HasRDTSC()
slouken@745
   212
{
slouken@745
   213
	if ( SDL_GetCPUFeatures() & CPU_HAS_RDTSC ) {
slouken@745
   214
		return SDL_TRUE;
slouken@745
   215
	}
slouken@745
   216
	return SDL_FALSE;
slouken@745
   217
}
slouken@745
   218
slouken@739
   219
SDL_bool SDL_HasMMX()
slouken@739
   220
{
slouken@739
   221
	if ( SDL_GetCPUFeatures() & CPU_HAS_MMX ) {
slouken@739
   222
		return SDL_TRUE;
slouken@739
   223
	}
slouken@739
   224
	return SDL_FALSE;
slouken@739
   225
}
slouken@739
   226
slouken@739
   227
SDL_bool SDL_Has3DNow()
slouken@739
   228
{
slouken@739
   229
	if ( SDL_GetCPUFeatures() & CPU_HAS_3DNOW ) {
slouken@739
   230
		return SDL_TRUE;
slouken@739
   231
	}
slouken@739
   232
	return SDL_FALSE;
slouken@739
   233
}
slouken@739
   234
slouken@739
   235
SDL_bool SDL_HasSSE()
slouken@739
   236
{
slouken@739
   237
	if ( SDL_GetCPUFeatures() & CPU_HAS_SSE ) {
slouken@739
   238
		return SDL_TRUE;
slouken@739
   239
	}
slouken@739
   240
	return SDL_FALSE;
slouken@739
   241
}
slouken@739
   242
slouken@739
   243
#ifdef TEST_MAIN
slouken@739
   244
slouken@739
   245
#include <stdio.h>
slouken@739
   246
slouken@739
   247
int main()
slouken@739
   248
{
slouken@739
   249
	printf("MMX: %d\n", SDL_HasMMX());
slouken@739
   250
	printf("3DNow: %d\n", SDL_Has3DNow());
slouken@739
   251
	printf("SSE: %d\n", SDL_HasSSE());
slouken@745
   252
	return 0;
slouken@739
   253
}
slouken@739
   254
slouken@739
   255
#endif /* TEST_MAIN */