2003-11-18 01:27:06 +00:00
|
|
|
/*
|
|
|
|
SDL - Simple DirectMedia Layer
|
2004-01-04 16:49:27 +00:00
|
|
|
Copyright (C) 1997-2004 Sam Lantinga
|
2003-11-18 01:27:06 +00:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
Sam Lantinga
|
|
|
|
slouken@libsdl.org
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef SAVE_RCSID
|
|
|
|
static char rcsid =
|
|
|
|
"@(#) $Id$";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* CPU feature detection for SDL */
|
|
|
|
|
2004-01-29 05:22:23 +00:00
|
|
|
#ifdef unix /* FIXME: Better setjmp detection? */
|
|
|
|
#define USE_SETJMP
|
|
|
|
#include <signal.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#endif
|
|
|
|
|
2003-11-18 01:27:06 +00:00
|
|
|
#include "SDL.h"
|
2003-11-24 09:16:52 +00:00
|
|
|
#include "SDL_cpuinfo.h"
|
2003-11-18 01:27:06 +00:00
|
|
|
|
2004-01-06 17:18:38 +00:00
|
|
|
#ifdef MACOSX
|
|
|
|
#include <sys/sysctl.h> /* For AltiVec check */
|
|
|
|
#endif
|
|
|
|
|
2003-11-24 09:16:52 +00:00
|
|
|
#define CPU_HAS_RDTSC 0x00000001
|
|
|
|
#define CPU_HAS_MMX 0x00000002
|
2004-01-24 05:47:19 +00:00
|
|
|
#define CPU_HAS_MMXEXT 0x00000004
|
|
|
|
#define CPU_HAS_3DNOW 0x00000010
|
|
|
|
#define CPU_HAS_3DNOWEXT 0x00000020
|
|
|
|
#define CPU_HAS_SSE 0x00000040
|
|
|
|
#define CPU_HAS_SSE2 0x00000080
|
|
|
|
#define CPU_HAS_ALTIVEC 0x00000100
|
2003-11-18 01:27:06 +00:00
|
|
|
|
2005-12-08 14:48:21 +00:00
|
|
|
#if defined(USE_SETJMP) && defined(GCC_ALTIVEC)
|
2004-01-29 05:22:23 +00:00
|
|
|
/* This is the brute force way of detecting instruction sets...
|
|
|
|
the idea is borrowed from the libmpeg2 library - thanks!
|
|
|
|
*/
|
|
|
|
static jmp_buf jmpbuf;
|
|
|
|
static void illegal_instruction(int sig)
|
|
|
|
{
|
|
|
|
longjmp(jmpbuf, 1);
|
|
|
|
}
|
|
|
|
#endif // USE_SETJMP
|
|
|
|
|
2003-11-24 09:16:52 +00:00
|
|
|
static __inline__ int CPU_haveCPUID()
|
|
|
|
{
|
|
|
|
int has_CPUID = 0;
|
|
|
|
#if defined(__GNUC__) && defined(i386)
|
|
|
|
__asm__ (
|
|
|
|
" pushfl # Get original EFLAGS \n"
|
|
|
|
" popl %%eax \n"
|
|
|
|
" movl %%eax,%%ecx \n"
|
|
|
|
" xorl $0x200000,%%eax # Flip ID bit in EFLAGS \n"
|
|
|
|
" pushl %%eax # Save new EFLAGS value on stack \n"
|
|
|
|
" popfl # Replace current EFLAGS value \n"
|
|
|
|
" pushfl # Get new EFLAGS \n"
|
|
|
|
" popl %%eax # Store new EFLAGS in EAX \n"
|
|
|
|
" xorl %%ecx,%%eax # Can not toggle ID bit, \n"
|
|
|
|
" jz 1f # Processor=80486 \n"
|
|
|
|
" movl $1,%0 # We have CPUID support \n"
|
|
|
|
"1: \n"
|
2004-01-17 20:37:06 +00:00
|
|
|
: "=m" (has_CPUID)
|
2003-11-24 09:16:52 +00:00
|
|
|
:
|
|
|
|
: "%eax", "%ecx"
|
|
|
|
);
|
2004-04-11 19:49:34 +00:00
|
|
|
#elif defined(__GNUC__) && defined(__x86_64__)
|
|
|
|
/* Technically, if this is being compiled under __x86_64__ then it has
|
|
|
|
CPUid by definition. But it's nice to be able to prove it. :) */
|
|
|
|
__asm__ (
|
|
|
|
" pushfq # Get original EFLAGS \n"
|
|
|
|
" popq %%rax \n"
|
|
|
|
" movq %%rax,%%rcx \n"
|
|
|
|
" xorl $0x200000,%%eax # Flip ID bit in EFLAGS \n"
|
|
|
|
" pushq %%rax # Save new EFLAGS value on stack \n"
|
|
|
|
" popfq # Replace current EFLAGS value \n"
|
|
|
|
" pushfq # Get new EFLAGS \n"
|
|
|
|
" popq %%rax # Store new EFLAGS in EAX \n"
|
|
|
|
" xorl %%ecx,%%eax # Can not toggle ID bit, \n"
|
|
|
|
" jz 1f # Processor=80486 \n"
|
|
|
|
" movl $1,%0 # We have CPUID support \n"
|
|
|
|
"1: \n"
|
|
|
|
: "=m" (has_CPUID)
|
|
|
|
:
|
|
|
|
: "%rax", "%rcx"
|
|
|
|
);
|
2005-09-29 09:43:00 +00:00
|
|
|
#elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_X86_))
|
2003-11-24 19:58:29 +00:00
|
|
|
__asm {
|
2003-11-24 09:16:52 +00:00
|
|
|
pushfd ; Get original EFLAGS
|
|
|
|
pop eax
|
|
|
|
mov ecx, eax
|
|
|
|
xor eax, 200000h ; Flip ID bit in EFLAGS
|
|
|
|
push eax ; Save new EFLAGS value on stack
|
|
|
|
popfd ; Replace current EFLAGS value
|
|
|
|
pushfd ; Get new EFLAGS
|
|
|
|
pop eax ; Store new EFLAGS in EAX
|
|
|
|
xor eax, ecx ; Can not toggle ID bit,
|
|
|
|
jz done ; Processor=80486
|
|
|
|
mov has_CPUID,1 ; We have CPUID support
|
|
|
|
done:
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return has_CPUID;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ int CPU_getCPUIDFeatures()
|
|
|
|
{
|
|
|
|
int features = 0;
|
2004-04-11 19:49:34 +00:00
|
|
|
#if defined(__GNUC__) && ( defined(i386) || defined(__x86_64__) )
|
2003-11-24 09:16:52 +00:00
|
|
|
__asm__ (
|
2004-01-24 05:47:19 +00:00
|
|
|
" movl %%ebx,%%edi\n"
|
2003-11-24 09:16:52 +00:00
|
|
|
" xorl %%eax,%%eax # Set up for CPUID instruction \n"
|
|
|
|
" cpuid # Get and save vendor ID \n"
|
|
|
|
" cmpl $1,%%eax # Make sure 1 is valid input for CPUID\n"
|
|
|
|
" jl 1f # We dont have the CPUID instruction\n"
|
|
|
|
" xorl %%eax,%%eax \n"
|
|
|
|
" incl %%eax \n"
|
|
|
|
" cpuid # Get family/model/stepping/features\n"
|
|
|
|
" movl %%edx,%0 \n"
|
|
|
|
"1: \n"
|
2004-01-24 05:47:19 +00:00
|
|
|
" movl %%edi,%%ebx\n"
|
2004-01-17 20:37:06 +00:00
|
|
|
: "=m" (features)
|
2003-11-24 09:16:52 +00:00
|
|
|
:
|
2004-05-16 17:19:48 +00:00
|
|
|
: "%eax", "%ecx", "%edx", "%edi"
|
2003-11-24 09:16:52 +00:00
|
|
|
);
|
2005-09-29 09:43:00 +00:00
|
|
|
#elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_X86_))
|
2003-11-24 19:58:29 +00:00
|
|
|
__asm {
|
2003-11-24 09:16:52 +00:00
|
|
|
xor eax, eax ; Set up for CPUID instruction
|
|
|
|
cpuid ; Get and save vendor ID
|
|
|
|
cmp eax, 1 ; Make sure 1 is valid input for CPUID
|
|
|
|
jl done ; We dont have the CPUID instruction
|
|
|
|
xor eax, eax
|
|
|
|
inc eax
|
|
|
|
cpuid ; Get family/model/stepping/features
|
|
|
|
mov features, edx
|
|
|
|
done:
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return features;
|
|
|
|
}
|
|
|
|
|
2004-01-24 05:47:19 +00:00
|
|
|
static __inline__ int CPU_getCPUIDFeaturesExt()
|
2003-11-24 09:16:52 +00:00
|
|
|
{
|
2004-01-24 05:47:19 +00:00
|
|
|
int features = 0;
|
2004-04-11 19:49:34 +00:00
|
|
|
#if defined(__GNUC__) && (defined(i386) || defined (__x86_64__) )
|
2003-11-24 09:16:52 +00:00
|
|
|
__asm__ (
|
2004-01-24 05:47:19 +00:00
|
|
|
" movl %%ebx,%%edi\n"
|
2003-11-24 09:16:52 +00:00
|
|
|
" movl $0x80000000,%%eax # Query for extended functions \n"
|
|
|
|
" cpuid # Get extended function limit \n"
|
|
|
|
" cmpl $0x80000001,%%eax \n"
|
2004-01-24 15:55:00 +00:00
|
|
|
" jl 1f # Nope, we dont have function 800000001h\n"
|
2003-11-24 09:16:52 +00:00
|
|
|
" movl $0x80000001,%%eax # Setup extended function 800000001h\n"
|
|
|
|
" cpuid # and get the information \n"
|
2004-01-24 05:47:19 +00:00
|
|
|
" movl %%edx,%0 \n"
|
2003-11-24 09:16:52 +00:00
|
|
|
"1: \n"
|
2004-01-24 05:47:19 +00:00
|
|
|
" movl %%edi,%%ebx\n"
|
|
|
|
: "=m" (features)
|
2003-11-24 09:16:52 +00:00
|
|
|
:
|
2004-05-16 17:19:48 +00:00
|
|
|
: "%eax", "%ecx", "%edx", "%edi"
|
2003-11-24 09:16:52 +00:00
|
|
|
);
|
2005-09-29 09:43:00 +00:00
|
|
|
#elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_X86_))
|
2003-11-24 19:58:29 +00:00
|
|
|
__asm {
|
2003-11-24 09:16:52 +00:00
|
|
|
mov eax,80000000h ; Query for extended functions
|
|
|
|
cpuid ; Get extended function limit
|
|
|
|
cmp eax,80000001h
|
2004-01-24 15:55:00 +00:00
|
|
|
jl done ; Nope, we dont have function 800000001h
|
2003-11-24 09:16:52 +00:00
|
|
|
mov eax,80000001h ; Setup extended function 800000001h
|
|
|
|
cpuid ; and get the information
|
2004-01-24 05:47:19 +00:00
|
|
|
mov features,edx
|
2003-11-24 09:16:52 +00:00
|
|
|
done:
|
|
|
|
}
|
|
|
|
#endif
|
2004-01-24 05:47:19 +00:00
|
|
|
return features;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ int CPU_haveRDTSC()
|
|
|
|
{
|
|
|
|
if ( CPU_haveCPUID() ) {
|
|
|
|
return (CPU_getCPUIDFeatures() & 0x00000010);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ int CPU_haveMMX()
|
|
|
|
{
|
|
|
|
if ( CPU_haveCPUID() ) {
|
|
|
|
return (CPU_getCPUIDFeatures() & 0x00800000);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ int CPU_haveMMXExt()
|
|
|
|
{
|
|
|
|
if ( CPU_haveCPUID() ) {
|
|
|
|
return (CPU_getCPUIDFeaturesExt() & 0x00400000);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ int CPU_have3DNow()
|
|
|
|
{
|
|
|
|
if ( CPU_haveCPUID() ) {
|
|
|
|
return (CPU_getCPUIDFeaturesExt() & 0x80000000);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ int CPU_have3DNowExt()
|
|
|
|
{
|
|
|
|
if ( CPU_haveCPUID() ) {
|
|
|
|
return (CPU_getCPUIDFeaturesExt() & 0x40000000);
|
|
|
|
}
|
|
|
|
return 0;
|
2003-11-24 09:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ int CPU_haveSSE()
|
|
|
|
{
|
|
|
|
if ( CPU_haveCPUID() ) {
|
|
|
|
return (CPU_getCPUIDFeatures() & 0x02000000);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2003-11-18 01:27:06 +00:00
|
|
|
|
2004-01-24 05:47:19 +00:00
|
|
|
static __inline__ int CPU_haveSSE2()
|
|
|
|
{
|
|
|
|
if ( CPU_haveCPUID() ) {
|
|
|
|
return (CPU_getCPUIDFeatures() & 0x04000000);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-01-06 17:18:38 +00:00
|
|
|
static __inline__ int CPU_haveAltiVec()
|
|
|
|
{
|
2004-01-31 06:49:18 +00:00
|
|
|
volatile int altivec = 0;
|
2004-01-06 17:18:38 +00:00
|
|
|
#ifdef MACOSX
|
|
|
|
int selectors[2] = { CTL_HW, HW_VECTORUNIT };
|
|
|
|
int hasVectorUnit = 0;
|
|
|
|
size_t length = sizeof(hasVectorUnit);
|
|
|
|
int error = sysctl(selectors, 2, &hasVectorUnit, &length, NULL, 0);
|
|
|
|
if( 0 == error )
|
2004-01-29 05:22:23 +00:00
|
|
|
altivec = (hasVectorUnit != 0);
|
2004-01-29 16:15:12 +00:00
|
|
|
#elif defined(USE_SETJMP) && defined(GCC_ALTIVEC)
|
2004-01-29 05:22:23 +00:00
|
|
|
void (*handler)(int sig);
|
|
|
|
handler = signal(SIGILL, illegal_instruction);
|
|
|
|
if ( setjmp(jmpbuf) == 0 ) {
|
|
|
|
asm volatile ("mtspr 256, %0\n\t"
|
|
|
|
"vand %%v0, %%v0, %%v0"
|
|
|
|
:
|
|
|
|
: "r" (-1));
|
|
|
|
altivec = 1;
|
|
|
|
}
|
|
|
|
signal(SIGILL, handler);
|
2004-01-06 17:18:38 +00:00
|
|
|
#endif
|
2004-01-29 05:22:23 +00:00
|
|
|
return altivec;
|
2004-01-06 17:18:38 +00:00
|
|
|
}
|
|
|
|
|
2003-11-18 01:27:06 +00:00
|
|
|
static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
|
|
|
|
|
|
|
|
static Uint32 SDL_GetCPUFeatures()
|
|
|
|
{
|
|
|
|
if ( SDL_CPUFeatures == 0xFFFFFFFF ) {
|
|
|
|
SDL_CPUFeatures = 0;
|
2003-11-24 09:16:52 +00:00
|
|
|
if ( CPU_haveRDTSC() ) {
|
|
|
|
SDL_CPUFeatures |= CPU_HAS_RDTSC;
|
|
|
|
}
|
2003-11-18 01:27:06 +00:00
|
|
|
if ( CPU_haveMMX() ) {
|
|
|
|
SDL_CPUFeatures |= CPU_HAS_MMX;
|
|
|
|
}
|
2004-01-24 05:57:56 +00:00
|
|
|
if ( CPU_haveMMXExt() ) {
|
|
|
|
SDL_CPUFeatures |= CPU_HAS_MMXEXT;
|
|
|
|
}
|
2003-11-18 01:27:06 +00:00
|
|
|
if ( CPU_have3DNow() ) {
|
|
|
|
SDL_CPUFeatures |= CPU_HAS_3DNOW;
|
|
|
|
}
|
2004-01-24 05:57:56 +00:00
|
|
|
if ( CPU_have3DNowExt() ) {
|
|
|
|
SDL_CPUFeatures |= CPU_HAS_3DNOWEXT;
|
|
|
|
}
|
2003-11-18 01:27:06 +00:00
|
|
|
if ( CPU_haveSSE() ) {
|
|
|
|
SDL_CPUFeatures |= CPU_HAS_SSE;
|
|
|
|
}
|
2004-01-24 05:57:56 +00:00
|
|
|
if ( CPU_haveSSE2() ) {
|
|
|
|
SDL_CPUFeatures |= CPU_HAS_SSE2;
|
|
|
|
}
|
2004-01-06 17:18:38 +00:00
|
|
|
if ( CPU_haveAltiVec() ) {
|
|
|
|
SDL_CPUFeatures |= CPU_HAS_ALTIVEC;
|
|
|
|
}
|
2003-11-18 01:27:06 +00:00
|
|
|
}
|
|
|
|
return SDL_CPUFeatures;
|
|
|
|
}
|
|
|
|
|
2003-11-24 09:16:52 +00:00
|
|
|
SDL_bool SDL_HasRDTSC()
|
|
|
|
{
|
|
|
|
if ( SDL_GetCPUFeatures() & CPU_HAS_RDTSC ) {
|
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
2003-11-18 01:27:06 +00:00
|
|
|
SDL_bool SDL_HasMMX()
|
|
|
|
{
|
|
|
|
if ( SDL_GetCPUFeatures() & CPU_HAS_MMX ) {
|
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
2004-02-10 15:31:35 +00:00
|
|
|
SDL_bool SDL_HasMMXExt()
|
2003-11-18 01:27:06 +00:00
|
|
|
{
|
2004-02-10 15:31:35 +00:00
|
|
|
if ( SDL_GetCPUFeatures() & CPU_HAS_MMXEXT ) {
|
2003-11-18 01:27:06 +00:00
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
2004-02-10 15:31:35 +00:00
|
|
|
SDL_bool SDL_Has3DNow()
|
2003-11-18 01:27:06 +00:00
|
|
|
{
|
2004-02-10 15:31:35 +00:00
|
|
|
if ( SDL_GetCPUFeatures() & CPU_HAS_3DNOW ) {
|
2003-11-18 01:27:06 +00:00
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
2004-02-10 15:31:35 +00:00
|
|
|
SDL_bool SDL_Has3DNowExt()
|
2004-01-06 17:18:38 +00:00
|
|
|
{
|
2004-02-10 15:31:35 +00:00
|
|
|
if ( SDL_GetCPUFeatures() & CPU_HAS_3DNOWEXT ) {
|
2004-01-06 17:18:38 +00:00
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
2004-02-10 15:31:35 +00:00
|
|
|
SDL_bool SDL_HasSSE()
|
2004-01-24 05:47:19 +00:00
|
|
|
{
|
2004-02-10 15:31:35 +00:00
|
|
|
if ( SDL_GetCPUFeatures() & CPU_HAS_SSE ) {
|
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
return SDL_FALSE;
|
2004-01-24 05:47:19 +00:00
|
|
|
}
|
|
|
|
|
2004-02-10 15:31:35 +00:00
|
|
|
SDL_bool SDL_HasSSE2()
|
2004-01-24 05:47:19 +00:00
|
|
|
{
|
2004-02-10 15:31:35 +00:00
|
|
|
if ( SDL_GetCPUFeatures() & CPU_HAS_SSE2 ) {
|
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
return SDL_FALSE;
|
2004-01-24 05:47:19 +00:00
|
|
|
}
|
|
|
|
|
2004-02-10 15:31:35 +00:00
|
|
|
SDL_bool SDL_HasAltiVec()
|
2004-01-24 05:47:19 +00:00
|
|
|
{
|
2004-02-10 15:31:35 +00:00
|
|
|
if ( SDL_GetCPUFeatures() & CPU_HAS_ALTIVEC ) {
|
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
return SDL_FALSE;
|
2004-01-24 05:47:19 +00:00
|
|
|
}
|
|
|
|
|
2003-11-18 01:27:06 +00:00
|
|
|
#ifdef TEST_MAIN
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2004-01-06 17:18:38 +00:00
|
|
|
printf("RDTSC: %d\n", SDL_HasRDTSC());
|
2003-11-18 01:27:06 +00:00
|
|
|
printf("MMX: %d\n", SDL_HasMMX());
|
2004-01-24 05:47:19 +00:00
|
|
|
printf("MMXExt: %d\n", SDL_HasMMXExt());
|
2003-11-18 01:27:06 +00:00
|
|
|
printf("3DNow: %d\n", SDL_Has3DNow());
|
2004-01-24 05:47:19 +00:00
|
|
|
printf("3DNowExt: %d\n", SDL_Has3DNowExt());
|
2003-11-18 01:27:06 +00:00
|
|
|
printf("SSE: %d\n", SDL_HasSSE());
|
2004-01-24 05:47:19 +00:00
|
|
|
printf("SSE2: %d\n", SDL_HasSSE2());
|
2004-01-06 17:18:38 +00:00
|
|
|
printf("AltiVec: %d\n", SDL_HasAltiVec());
|
2003-11-24 09:16:52 +00:00
|
|
|
return 0;
|
2003-11-18 01:27:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* TEST_MAIN */
|