src/cpuinfo/SDL_cpuinfo.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 06 Jan 2004 17:18:38 +0000
changeset 778 8ac3f46f9d09
parent 769 b8d311d90021
child 784 a2dde6aff60e
permissions -rw-r--r--
Date: Tue, 6 Jan 2004 12:42:19 +0100
From: Max Horn
Subject: SDL_HasAltiVec; BUGS file

the attached patch adds SDL_HasAltiVec to SDL CVS. Note that at this
point, this only works on MacOSX (and maybe darwin). I don't know how
to properly add a test for e.g. Linux/PPC at this point. I found an
email which might help in doing so:
http://zebra.fh-weingarten.de/~maxi/html/mplayer-dev-eng/2003-01msg00783.html
However, since I have no way to test on a non-OSX PowerPC system, I am
not comfortable blindly adding such code... I just hope that if
somebody from the Linux/PPC (or FreeBSD/PPC, or whatever) community
notices this, they'll jump up and provide a patch for us ;-)
slouken@739
     1
/*
slouken@739
     2
    SDL - Simple DirectMedia Layer
slouken@769
     3
    Copyright (C) 1997-2004 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@778
    33
#ifdef MACOSX
slouken@778
    34
#include <sys/sysctl.h> /* For AltiVec check */
slouken@778
    35
#endif
slouken@778
    36
slouken@745
    37
#define CPU_HAS_RDTSC	0x00000001
slouken@745
    38
#define CPU_HAS_MMX	0x00000002
slouken@745
    39
#define CPU_HAS_3DNOW	0x00000004
slouken@745
    40
#define CPU_HAS_SSE	0x00000008
slouken@778
    41
#define CPU_HAS_ALTIVEC	0x00000010
slouken@739
    42
slouken@745
    43
static __inline__ int CPU_haveCPUID()
slouken@745
    44
{
slouken@745
    45
	int has_CPUID = 0;
slouken@745
    46
#if defined(__GNUC__) && defined(i386)
slouken@745
    47
	__asm__ (
slouken@745
    48
"push %%ecx\n"
slouken@745
    49
"        pushfl                      # Get original EFLAGS             \n"
slouken@745
    50
"        popl    %%eax                                                 \n"
slouken@745
    51
"        movl    %%eax,%%ecx                                           \n"
slouken@745
    52
"        xorl    $0x200000,%%eax     # Flip ID bit in EFLAGS           \n"
slouken@745
    53
"        pushl   %%eax               # Save new EFLAGS value on stack  \n"
slouken@745
    54
"        popfl                       # Replace current EFLAGS value    \n"
slouken@745
    55
"        pushfl                      # Get new EFLAGS                  \n"
slouken@745
    56
"        popl    %%eax               # Store new EFLAGS in EAX         \n"
slouken@745
    57
"        xorl    %%ecx,%%eax         # Can not toggle ID bit,          \n"
slouken@745
    58
"        jz      1f                  # Processor=80486                 \n"
slouken@745
    59
"        movl    $1,%0               # We have CPUID support           \n"
slouken@745
    60
"1:                                                                    \n"
slouken@745
    61
"pop %%ecx\n"
slouken@745
    62
	: "=r" (has_CPUID)
slouken@745
    63
	:
slouken@745
    64
	: "%eax", "%ecx"
slouken@745
    65
	);
slouken@745
    66
#elif defined(_MSC_VER)
slouken@749
    67
	__asm {
slouken@745
    68
        pushfd                      ; Get original EFLAGS
slouken@745
    69
        pop     eax
slouken@745
    70
        mov     ecx, eax
slouken@745
    71
        xor     eax, 200000h        ; Flip ID bit in EFLAGS
slouken@745
    72
        push    eax                 ; Save new EFLAGS value on stack
slouken@745
    73
        popfd                       ; Replace current EFLAGS value
slouken@745
    74
        pushfd                      ; Get new EFLAGS
slouken@745
    75
        pop     eax                 ; Store new EFLAGS in EAX
slouken@745
    76
        xor     eax, ecx            ; Can not toggle ID bit,
slouken@745
    77
        jz      done                ; Processor=80486
slouken@745
    78
        mov     has_CPUID,1         ; We have CPUID support
slouken@745
    79
done:
slouken@745
    80
	}
slouken@745
    81
#endif
slouken@745
    82
	return has_CPUID;
slouken@745
    83
}
slouken@745
    84
slouken@745
    85
static __inline__ int CPU_getCPUIDFeatures()
slouken@745
    86
{
slouken@745
    87
	int features = 0;
slouken@745
    88
#if defined(__GNUC__) && defined(i386)
slouken@745
    89
	__asm__ (
slouken@745
    90
"push %%ebx\n"
slouken@745
    91
"push %%ecx\n"
slouken@745
    92
"push %%edx\n"
slouken@745
    93
"        xorl    %%eax,%%eax         # Set up for CPUID instruction    \n"
slouken@745
    94
"        cpuid                       # Get and save vendor ID          \n"
slouken@745
    95
"        cmpl    $1,%%eax            # Make sure 1 is valid input for CPUID\n"
slouken@745
    96
"        jl      1f                  # We dont have the CPUID instruction\n"
slouken@745
    97
"        xorl    %%eax,%%eax                                           \n"
slouken@745
    98
"        incl    %%eax                                                 \n"
slouken@745
    99
"        cpuid                       # Get family/model/stepping/features\n"
slouken@745
   100
"        movl    %%edx,%0                                              \n"
slouken@745
   101
"1:                                                                    \n"
slouken@745
   102
"pop %%edx\n"
slouken@745
   103
"pop %%ecx\n"
slouken@745
   104
"pop %%ebx\n"
slouken@745
   105
	: "=r" (features)
slouken@745
   106
	:
slouken@745
   107
	: "%eax", "%ebx", "%ecx", "%edx"
slouken@745
   108
	);
slouken@745
   109
#elif defined(_MSC_VER)
slouken@749
   110
	__asm {
slouken@745
   111
        xor     eax, eax            ; Set up for CPUID instruction
slouken@745
   112
        cpuid                       ; Get and save vendor ID
slouken@745
   113
        cmp     eax, 1              ; Make sure 1 is valid input for CPUID
slouken@745
   114
        jl      done                ; We dont have the CPUID instruction
slouken@745
   115
        xor     eax, eax
slouken@745
   116
        inc     eax
slouken@745
   117
        cpuid                       ; Get family/model/stepping/features
slouken@745
   118
        mov     features, edx
slouken@745
   119
done:
slouken@745
   120
	}
slouken@745
   121
#endif
slouken@745
   122
	return features;
slouken@745
   123
}
slouken@745
   124
slouken@745
   125
static __inline__ int CPU_haveRDTSC()
slouken@745
   126
{
slouken@745
   127
	if ( CPU_haveCPUID() ) {
slouken@745
   128
		return (CPU_getCPUIDFeatures() & 0x00000010);
slouken@745
   129
	}
slouken@745
   130
	return 0;
slouken@745
   131
}
slouken@745
   132
slouken@745
   133
static __inline__ int CPU_haveMMX()
slouken@745
   134
{
slouken@745
   135
	if ( CPU_haveCPUID() ) {
slouken@745
   136
		return (CPU_getCPUIDFeatures() & 0x00800000);
slouken@745
   137
	}
slouken@745
   138
	return 0;
slouken@745
   139
}
slouken@745
   140
slouken@745
   141
static __inline__ int CPU_have3DNow()
slouken@745
   142
{
slouken@745
   143
	int has_3DNow = 0;
slouken@747
   144
	if ( !CPU_haveCPUID() ) {
slouken@747
   145
		return 0;
slouken@747
   146
	}
slouken@745
   147
#if defined(__GNUC__) && defined(i386)
slouken@745
   148
	__asm__ (
slouken@745
   149
"push %%ebx\n"
slouken@745
   150
"push %%ecx\n"
slouken@745
   151
"push %%edx\n"
slouken@745
   152
"        movl    $0x80000000,%%eax   # Query for extended functions    \n"
slouken@745
   153
"        cpuid                       # Get extended function limit     \n"
slouken@745
   154
"        cmpl    $0x80000001,%%eax                                     \n"
slouken@745
   155
"        jbe     1f                  # Nope, we dont have function 800000001h\n"
slouken@745
   156
"        movl    $0x80000001,%%eax   # Setup extended function 800000001h\n"
slouken@745
   157
"        cpuid                       # and get the information         \n"
slouken@745
   158
"        testl   $0x80000000,%%edx   # Bit 31 is set if 3DNow! present \n"
slouken@745
   159
"        jz      1f                  # Nope, we dont have 3DNow support\n"
slouken@745
   160
"        movl    $1,%0               # Yep, we have 3DNow! support!    \n"
slouken@745
   161
"1:                                                                    \n"
slouken@745
   162
"pop %%edx\n"
slouken@745
   163
"pop %%ecx\n"
slouken@745
   164
"pop %%ebx\n"
slouken@745
   165
	: "=r" (has_3DNow)
slouken@745
   166
	:
slouken@745
   167
	: "%eax", "%ebx", "%ecx", "%edx"
slouken@745
   168
	);
slouken@745
   169
#elif defined(_MSC_VER)
slouken@749
   170
	__asm {
slouken@745
   171
        mov     eax,80000000h       ; Query for extended functions
slouken@745
   172
        cpuid                       ; Get extended function limit
slouken@745
   173
        cmp     eax,80000001h
slouken@745
   174
        jbe     done                ; Nope, we dont have function 800000001h
slouken@745
   175
        mov     eax,80000001h       ; Setup extended function 800000001h
slouken@745
   176
        cpuid                       ; and get the information
slouken@745
   177
        test    edx,80000000h       ; Bit 31 is set if 3DNow! present
slouken@745
   178
        jz      done                ; Nope, we dont have 3DNow support
slouken@745
   179
        mov     has_3DNow,1         ; Yep, we have 3DNow! support!
slouken@745
   180
done:
slouken@745
   181
	}
slouken@745
   182
#endif
slouken@745
   183
	return has_3DNow;
slouken@745
   184
}
slouken@745
   185
slouken@745
   186
static __inline__ int CPU_haveSSE()
slouken@745
   187
{
slouken@745
   188
	if ( CPU_haveCPUID() ) {
slouken@745
   189
		return (CPU_getCPUIDFeatures() & 0x02000000);
slouken@745
   190
	}
slouken@745
   191
	return 0;
slouken@745
   192
}
slouken@739
   193
slouken@778
   194
static __inline__ int CPU_haveAltiVec()
slouken@778
   195
{
slouken@778
   196
#ifdef MACOSX
slouken@778
   197
	/* TODO: This check works on OS X. It would be nice to detect AltiVec
slouken@778
   198
	   properly on for example Linux/PPC, too. But I don't know how that
slouken@778
   199
	   is done in Linux (or FreeBSD, or whatever other OS you run PPC :-)
slouken@778
   200
	 */
slouken@778
   201
	int selectors[2] = { CTL_HW, HW_VECTORUNIT }; 
slouken@778
   202
	int hasVectorUnit = 0; 
slouken@778
   203
	size_t length = sizeof(hasVectorUnit); 
slouken@778
   204
	int error = sysctl(selectors, 2, &hasVectorUnit, &length, NULL, 0); 
slouken@778
   205
	if( 0 == error )
slouken@778
   206
		return hasVectorUnit != 0; 
slouken@778
   207
#endif
slouken@778
   208
	return 0; 
slouken@778
   209
}
slouken@778
   210
slouken@739
   211
static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
slouken@739
   212
slouken@739
   213
static Uint32 SDL_GetCPUFeatures()
slouken@739
   214
{
slouken@739
   215
	if ( SDL_CPUFeatures == 0xFFFFFFFF ) {
slouken@739
   216
		SDL_CPUFeatures = 0;
slouken@745
   217
		if ( CPU_haveRDTSC() ) {
slouken@745
   218
			SDL_CPUFeatures |= CPU_HAS_RDTSC;
slouken@745
   219
		}
slouken@739
   220
		if ( CPU_haveMMX() ) {
slouken@739
   221
			SDL_CPUFeatures |= CPU_HAS_MMX;
slouken@739
   222
		}
slouken@739
   223
		if ( CPU_have3DNow() ) {
slouken@739
   224
			SDL_CPUFeatures |= CPU_HAS_3DNOW;
slouken@739
   225
		}
slouken@739
   226
		if ( CPU_haveSSE() ) {
slouken@739
   227
			SDL_CPUFeatures |= CPU_HAS_SSE;
slouken@739
   228
		}
slouken@778
   229
		if ( CPU_haveAltiVec() ) {
slouken@778
   230
			SDL_CPUFeatures |= CPU_HAS_ALTIVEC;
slouken@778
   231
		}
slouken@739
   232
	}
slouken@739
   233
	return SDL_CPUFeatures;
slouken@739
   234
}
slouken@739
   235
slouken@745
   236
SDL_bool SDL_HasRDTSC()
slouken@745
   237
{
slouken@745
   238
	if ( SDL_GetCPUFeatures() & CPU_HAS_RDTSC ) {
slouken@745
   239
		return SDL_TRUE;
slouken@745
   240
	}
slouken@745
   241
	return SDL_FALSE;
slouken@745
   242
}
slouken@745
   243
slouken@739
   244
SDL_bool SDL_HasMMX()
slouken@739
   245
{
slouken@739
   246
	if ( SDL_GetCPUFeatures() & CPU_HAS_MMX ) {
slouken@739
   247
		return SDL_TRUE;
slouken@739
   248
	}
slouken@739
   249
	return SDL_FALSE;
slouken@739
   250
}
slouken@739
   251
slouken@739
   252
SDL_bool SDL_Has3DNow()
slouken@739
   253
{
slouken@739
   254
	if ( SDL_GetCPUFeatures() & CPU_HAS_3DNOW ) {
slouken@739
   255
		return SDL_TRUE;
slouken@739
   256
	}
slouken@739
   257
	return SDL_FALSE;
slouken@739
   258
}
slouken@739
   259
slouken@739
   260
SDL_bool SDL_HasSSE()
slouken@739
   261
{
slouken@739
   262
	if ( SDL_GetCPUFeatures() & CPU_HAS_SSE ) {
slouken@739
   263
		return SDL_TRUE;
slouken@739
   264
	}
slouken@739
   265
	return SDL_FALSE;
slouken@739
   266
}
slouken@739
   267
slouken@778
   268
SDL_bool SDL_HasAltiVec()
slouken@778
   269
{
slouken@778
   270
	if ( SDL_GetCPUFeatures() & CPU_HAS_ALTIVEC ) {
slouken@778
   271
		return SDL_TRUE;
slouken@778
   272
	}
slouken@778
   273
	return SDL_FALSE;
slouken@778
   274
}
slouken@778
   275
slouken@739
   276
#ifdef TEST_MAIN
slouken@739
   277
slouken@739
   278
#include <stdio.h>
slouken@739
   279
slouken@739
   280
int main()
slouken@739
   281
{
slouken@778
   282
	printf("RDTSC: %d\n", SDL_HasRDTSC());
slouken@739
   283
	printf("MMX: %d\n", SDL_HasMMX());
slouken@739
   284
	printf("3DNow: %d\n", SDL_Has3DNow());
slouken@739
   285
	printf("SSE: %d\n", SDL_HasSSE());
slouken@778
   286
	printf("AltiVec: %d\n", SDL_HasAltiVec());
slouken@745
   287
	return 0;
slouken@739
   288
}
slouken@739
   289
slouken@739
   290
#endif /* TEST_MAIN */