Date: Tue, 6 Jan 2004 12:42:19 +0100
authorSam Lantinga <slouken@libsdl.org>
Tue, 06 Jan 2004 17:18:38 +0000
changeset 7788ac3f46f9d09
parent 777 6f4fe7f2063b
child 779 68c8da837fc0
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 ;-)
include/SDL_cpuinfo.h
src/cpuinfo/SDL_cpuinfo.c
test/testcpuinfo.c
     1.1 --- a/include/SDL_cpuinfo.h	Tue Jan 06 17:16:02 2004 +0000
     1.2 +++ b/include/SDL_cpuinfo.h	Tue Jan 06 17:18:38 2004 +0000
     1.3 @@ -53,6 +53,10 @@
     1.4   */
     1.5  extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE();
     1.6  
     1.7 +/* This function returns true if the CPU has AltiVec features
     1.8 + */
     1.9 +extern DECLSPEC SDL_bool SDLCALL SDL_HasAltiVec();
    1.10 +
    1.11  /* Ends C function definitions when using C++ */
    1.12  #ifdef __cplusplus
    1.13  }
     2.1 --- a/src/cpuinfo/SDL_cpuinfo.c	Tue Jan 06 17:16:02 2004 +0000
     2.2 +++ b/src/cpuinfo/SDL_cpuinfo.c	Tue Jan 06 17:18:38 2004 +0000
     2.3 @@ -30,10 +30,15 @@
     2.4  #include "SDL.h"
     2.5  #include "SDL_cpuinfo.h"
     2.6  
     2.7 +#ifdef MACOSX
     2.8 +#include <sys/sysctl.h> /* For AltiVec check */
     2.9 +#endif
    2.10 +
    2.11  #define CPU_HAS_RDTSC	0x00000001
    2.12  #define CPU_HAS_MMX	0x00000002
    2.13  #define CPU_HAS_3DNOW	0x00000004
    2.14  #define CPU_HAS_SSE	0x00000008
    2.15 +#define CPU_HAS_ALTIVEC	0x00000010
    2.16  
    2.17  static __inline__ int CPU_haveCPUID()
    2.18  {
    2.19 @@ -186,6 +191,23 @@
    2.20  	return 0;
    2.21  }
    2.22  
    2.23 +static __inline__ int CPU_haveAltiVec()
    2.24 +{
    2.25 +#ifdef MACOSX
    2.26 +	/* TODO: This check works on OS X. It would be nice to detect AltiVec
    2.27 +	   properly on for example Linux/PPC, too. But I don't know how that
    2.28 +	   is done in Linux (or FreeBSD, or whatever other OS you run PPC :-)
    2.29 +	 */
    2.30 +	int selectors[2] = { CTL_HW, HW_VECTORUNIT }; 
    2.31 +	int hasVectorUnit = 0; 
    2.32 +	size_t length = sizeof(hasVectorUnit); 
    2.33 +	int error = sysctl(selectors, 2, &hasVectorUnit, &length, NULL, 0); 
    2.34 +	if( 0 == error )
    2.35 +		return hasVectorUnit != 0; 
    2.36 +#endif
    2.37 +	return 0; 
    2.38 +}
    2.39 +
    2.40  static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
    2.41  
    2.42  static Uint32 SDL_GetCPUFeatures()
    2.43 @@ -204,6 +226,9 @@
    2.44  		if ( CPU_haveSSE() ) {
    2.45  			SDL_CPUFeatures |= CPU_HAS_SSE;
    2.46  		}
    2.47 +		if ( CPU_haveAltiVec() ) {
    2.48 +			SDL_CPUFeatures |= CPU_HAS_ALTIVEC;
    2.49 +		}
    2.50  	}
    2.51  	return SDL_CPUFeatures;
    2.52  }
    2.53 @@ -240,15 +265,25 @@
    2.54  	return SDL_FALSE;
    2.55  }
    2.56  
    2.57 +SDL_bool SDL_HasAltiVec()
    2.58 +{
    2.59 +	if ( SDL_GetCPUFeatures() & CPU_HAS_ALTIVEC ) {
    2.60 +		return SDL_TRUE;
    2.61 +	}
    2.62 +	return SDL_FALSE;
    2.63 +}
    2.64 +
    2.65  #ifdef TEST_MAIN
    2.66  
    2.67  #include <stdio.h>
    2.68  
    2.69  int main()
    2.70  {
    2.71 +	printf("RDTSC: %d\n", SDL_HasRDTSC());
    2.72  	printf("MMX: %d\n", SDL_HasMMX());
    2.73  	printf("3DNow: %d\n", SDL_Has3DNow());
    2.74  	printf("SSE: %d\n", SDL_HasSSE());
    2.75 +	printf("AltiVec: %d\n", SDL_HasAltiVec());
    2.76  	return 0;
    2.77  }
    2.78  
     3.1 --- a/test/testcpuinfo.c	Tue Jan 06 17:16:02 2004 +0000
     3.2 +++ b/test/testcpuinfo.c	Tue Jan 06 17:18:38 2004 +0000
     3.3 @@ -8,8 +8,10 @@
     3.4  
     3.5  int main(int argc, char *argv[])
     3.6  {
     3.7 +	printf("RDTSC %s\n", SDL_HasRDTSC() ? "detected" : "not detected");
     3.8  	printf("MMX %s\n", SDL_HasMMX() ? "detected" : "not detected");
     3.9  	printf("3DNow %s\n", SDL_Has3DNow() ? "detected" : "not detected");
    3.10  	printf("SSE %s\n", SDL_HasSSE() ? "detected" : "not detected");
    3.11 +	printf("AltiVec %s\n", SDL_HasAltiVec() ? "detected" : "not detected");
    3.12  	return(0);
    3.13  }