src/cpuinfo/SDL_cpuinfo.c
changeset 785 ca06a994f03c
parent 784 a2dde6aff60e
child 786 e1e0a0a94570
     1.1 --- a/src/cpuinfo/SDL_cpuinfo.c	Sat Jan 17 20:37:06 2004 +0000
     1.2 +++ b/src/cpuinfo/SDL_cpuinfo.c	Sat Jan 24 05:47:19 2004 +0000
     1.3 @@ -36,16 +36,18 @@
     1.4  
     1.5  #define CPU_HAS_RDTSC	0x00000001
     1.6  #define CPU_HAS_MMX	0x00000002
     1.7 -#define CPU_HAS_3DNOW	0x00000004
     1.8 -#define CPU_HAS_SSE	0x00000008
     1.9 -#define CPU_HAS_ALTIVEC	0x00000010
    1.10 +#define CPU_HAS_MMXEXT	0x00000004
    1.11 +#define CPU_HAS_3DNOW	0x00000010
    1.12 +#define CPU_HAS_3DNOWEXT 0x00000020
    1.13 +#define CPU_HAS_SSE	0x00000040
    1.14 +#define CPU_HAS_SSE2	0x00000080
    1.15 +#define CPU_HAS_ALTIVEC	0x00000100
    1.16  
    1.17  static __inline__ int CPU_haveCPUID()
    1.18  {
    1.19  	int has_CPUID = 0;
    1.20  #if defined(__GNUC__) && defined(i386)
    1.21  	__asm__ (
    1.22 -"push %%ecx\n"
    1.23  "        pushfl                      # Get original EFLAGS             \n"
    1.24  "        popl    %%eax                                                 \n"
    1.25  "        movl    %%eax,%%ecx                                           \n"
    1.26 @@ -58,7 +60,6 @@
    1.27  "        jz      1f                  # Processor=80486                 \n"
    1.28  "        movl    $1,%0               # We have CPUID support           \n"
    1.29  "1:                                                                    \n"
    1.30 -"pop %%ecx\n"
    1.31  	: "=m" (has_CPUID)
    1.32  	:
    1.33  	: "%eax", "%ecx"
    1.34 @@ -87,9 +88,7 @@
    1.35  	int features = 0;
    1.36  #if defined(__GNUC__) && defined(i386)
    1.37  	__asm__ (
    1.38 -"push %%ebx\n"
    1.39 -"push %%ecx\n"
    1.40 -"push %%edx\n"
    1.41 +"        movl    %%ebx,%%edi\n"
    1.42  "        xorl    %%eax,%%eax         # Set up for CPUID instruction    \n"
    1.43  "        cpuid                       # Get and save vendor ID          \n"
    1.44  "        cmpl    $1,%%eax            # Make sure 1 is valid input for CPUID\n"
    1.45 @@ -99,12 +98,10 @@
    1.46  "        cpuid                       # Get family/model/stepping/features\n"
    1.47  "        movl    %%edx,%0                                              \n"
    1.48  "1:                                                                    \n"
    1.49 -"pop %%edx\n"
    1.50 -"pop %%ecx\n"
    1.51 -"pop %%ebx\n"
    1.52 +"        movl    %%edi,%%ebx\n"
    1.53  	: "=m" (features)
    1.54  	:
    1.55 -	: "%eax", "%ebx", "%ecx", "%edx"
    1.56 +	: "%eax", "%ebx", "%ecx", "%edx", "%edi"
    1.57  	);
    1.58  #elif defined(_MSC_VER)
    1.59  	__asm {
    1.60 @@ -122,6 +119,40 @@
    1.61  	return features;
    1.62  }
    1.63  
    1.64 +static __inline__ int CPU_getCPUIDFeaturesExt()
    1.65 +{
    1.66 +	int features = 0;
    1.67 +#if defined(__GNUC__) && defined(i386)
    1.68 +	__asm__ (
    1.69 +"        movl    %%ebx,%%edi\n"
    1.70 +"        movl    $0x80000000,%%eax   # Query for extended functions    \n"
    1.71 +"        cpuid                       # Get extended function limit     \n"
    1.72 +"        cmpl    $0x80000001,%%eax                                     \n"
    1.73 +"        jbe     1f                  # Nope, we dont have function 800000001h\n"
    1.74 +"        movl    $0x80000001,%%eax   # Setup extended function 800000001h\n"
    1.75 +"        cpuid                       # and get the information         \n"
    1.76 +"        movl    %%edx,%0                                              \n"
    1.77 +"1:                                                                    \n"
    1.78 +"        movl    %%edi,%%ebx\n"
    1.79 +	: "=m" (features)
    1.80 +	:
    1.81 +	: "%eax", "%ebx", "%ecx", "%edx", "%edi"
    1.82 +	);
    1.83 +#elif defined(_MSC_VER)
    1.84 +	__asm {
    1.85 +        mov     eax,80000000h       ; Query for extended functions
    1.86 +        cpuid                       ; Get extended function limit
    1.87 +        cmp     eax,80000001h
    1.88 +        jbe     done                ; Nope, we dont have function 800000001h
    1.89 +        mov     eax,80000001h       ; Setup extended function 800000001h
    1.90 +        cpuid                       ; and get the information
    1.91 +        mov     features,edx
    1.92 +done:
    1.93 +	}
    1.94 +#endif
    1.95 +	return features;
    1.96 +}
    1.97 +
    1.98  static __inline__ int CPU_haveRDTSC()
    1.99  {
   1.100  	if ( CPU_haveCPUID() ) {
   1.101 @@ -138,49 +169,28 @@
   1.102  	return 0;
   1.103  }
   1.104  
   1.105 +static __inline__ int CPU_haveMMXExt()
   1.106 +{
   1.107 +	if ( CPU_haveCPUID() ) {
   1.108 +		return (CPU_getCPUIDFeaturesExt() & 0x00400000);
   1.109 +	}
   1.110 +	return 0;
   1.111 +}
   1.112 +
   1.113  static __inline__ int CPU_have3DNow()
   1.114  {
   1.115 -	int has_3DNow = 0;
   1.116 -	if ( !CPU_haveCPUID() ) {
   1.117 -		return 0;
   1.118 +	if ( CPU_haveCPUID() ) {
   1.119 +		return (CPU_getCPUIDFeaturesExt() & 0x80000000);
   1.120  	}
   1.121 -#if defined(__GNUC__) && defined(i386)
   1.122 -	__asm__ (
   1.123 -"push %%ebx\n"
   1.124 -"push %%ecx\n"
   1.125 -"push %%edx\n"
   1.126 -"        movl    $0x80000000,%%eax   # Query for extended functions    \n"
   1.127 -"        cpuid                       # Get extended function limit     \n"
   1.128 -"        cmpl    $0x80000001,%%eax                                     \n"
   1.129 -"        jbe     1f                  # Nope, we dont have function 800000001h\n"
   1.130 -"        movl    $0x80000001,%%eax   # Setup extended function 800000001h\n"
   1.131 -"        cpuid                       # and get the information         \n"
   1.132 -"        testl   $0x80000000,%%edx   # Bit 31 is set if 3DNow! present \n"
   1.133 -"        jz      1f                  # Nope, we dont have 3DNow support\n"
   1.134 -"        movl    $1,%0               # Yep, we have 3DNow! support!    \n"
   1.135 -"1:                                                                    \n"
   1.136 -"pop %%edx\n"
   1.137 -"pop %%ecx\n"
   1.138 -"pop %%ebx\n"
   1.139 -	: "=m" (has_3DNow)
   1.140 -	:
   1.141 -	: "%eax", "%ebx", "%ecx", "%edx"
   1.142 -	);
   1.143 -#elif defined(_MSC_VER)
   1.144 -	__asm {
   1.145 -        mov     eax,80000000h       ; Query for extended functions
   1.146 -        cpuid                       ; Get extended function limit
   1.147 -        cmp     eax,80000001h
   1.148 -        jbe     done                ; Nope, we dont have function 800000001h
   1.149 -        mov     eax,80000001h       ; Setup extended function 800000001h
   1.150 -        cpuid                       ; and get the information
   1.151 -        test    edx,80000000h       ; Bit 31 is set if 3DNow! present
   1.152 -        jz      done                ; Nope, we dont have 3DNow support
   1.153 -        mov     has_3DNow,1         ; Yep, we have 3DNow! support!
   1.154 -done:
   1.155 +	return 0;
   1.156 +}
   1.157 +
   1.158 +static __inline__ int CPU_have3DNowExt()
   1.159 +{
   1.160 +	if ( CPU_haveCPUID() ) {
   1.161 +		return (CPU_getCPUIDFeaturesExt() & 0x40000000);
   1.162  	}
   1.163 -#endif
   1.164 -	return has_3DNow;
   1.165 +	return 0;
   1.166  }
   1.167  
   1.168  static __inline__ int CPU_haveSSE()
   1.169 @@ -191,6 +201,14 @@
   1.170  	return 0;
   1.171  }
   1.172  
   1.173 +static __inline__ int CPU_haveSSE2()
   1.174 +{
   1.175 +	if ( CPU_haveCPUID() ) {
   1.176 +		return (CPU_getCPUIDFeatures() & 0x04000000);
   1.177 +	}
   1.178 +	return 0;
   1.179 +}
   1.180 +
   1.181  static __inline__ int CPU_haveAltiVec()
   1.182  {
   1.183  #ifdef MACOSX
   1.184 @@ -229,6 +247,15 @@
   1.185  		if ( CPU_haveAltiVec() ) {
   1.186  			SDL_CPUFeatures |= CPU_HAS_ALTIVEC;
   1.187  		}
   1.188 +      if ( CPU_haveMMXExt() ) {
   1.189 +         SDL_CPUFeatures |= CPU_HAS_MMXEXT;
   1.190 +      }
   1.191 +      if ( CPU_have3DNowExt() ) {
   1.192 +         SDL_CPUFeatures |= CPU_HAS_3DNOWEXT;
   1.193 +      }
   1.194 +      if ( CPU_haveSSE2() ) {
   1.195 +         SDL_CPUFeatures |= CPU_HAS_SSE2;
   1.196 +      }
   1.197  	}
   1.198  	return SDL_CPUFeatures;
   1.199  }
   1.200 @@ -273,6 +300,30 @@
   1.201  	return SDL_FALSE;
   1.202  }
   1.203  
   1.204 +SDL_bool SDL_HasMMXExt()
   1.205 +{
   1.206 +   if ( SDL_GetCPUFeatures() & CPU_HAS_MMXEXT ) {
   1.207 +      return SDL_TRUE;
   1.208 +   }
   1.209 +   return SDL_FALSE;
   1.210 +}
   1.211 +
   1.212 +SDL_bool SDL_Has3DNowExt()
   1.213 +{
   1.214 +   if ( SDL_GetCPUFeatures() & CPU_HAS_3DNOWEXT ) {
   1.215 +      return SDL_TRUE;
   1.216 +   }
   1.217 +   return SDL_FALSE;
   1.218 +}
   1.219 +
   1.220 +SDL_bool SDL_HasSSE2()
   1.221 +{
   1.222 +   if ( SDL_GetCPUFeatures() & CPU_HAS_SSE2 ) {
   1.223 +      return SDL_TRUE;
   1.224 +   }
   1.225 +   return SDL_FALSE;
   1.226 +}
   1.227 +
   1.228  #ifdef TEST_MAIN
   1.229  
   1.230  #include <stdio.h>
   1.231 @@ -281,8 +332,11 @@
   1.232  {
   1.233  	printf("RDTSC: %d\n", SDL_HasRDTSC());
   1.234  	printf("MMX: %d\n", SDL_HasMMX());
   1.235 +	printf("MMXExt: %d\n", SDL_HasMMXExt());
   1.236  	printf("3DNow: %d\n", SDL_Has3DNow());
   1.237 +	printf("3DNowExt: %d\n", SDL_Has3DNowExt());
   1.238  	printf("SSE: %d\n", SDL_HasSSE());
   1.239 +	printf("SSE2: %d\n", SDL_HasSSE2());
   1.240  	printf("AltiVec: %d\n", SDL_HasAltiVec());
   1.241  	return 0;
   1.242  }