src/cpuinfo/SDL_cpuinfo.c
changeset 5259 6a65c1fc07af
parent 5120 b3f075368b1f
child 5262 b530ef003506
     1.1 --- a/src/cpuinfo/SDL_cpuinfo.c	Fri Feb 11 14:42:58 2011 -0800
     1.2 +++ b/src/cpuinfo/SDL_cpuinfo.c	Fri Feb 11 14:51:04 2011 -0800
     1.3 @@ -32,36 +32,17 @@
     1.4  #include <sys/types.h>
     1.5  #include <sys/sysctl.h>
     1.6  #endif
     1.7 -#if defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))
     1.8 -#include <sys/sysctl.h>         /* For AltiVec check */
     1.9 -#elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
    1.10 -#include <signal.h>
    1.11 -#include <setjmp.h>
    1.12 -#endif
    1.13  #ifdef __WIN32__
    1.14  #include "../core/windows/SDL_windows.h"
    1.15  #endif
    1.16  
    1.17  #define CPU_HAS_RDTSC   0x00000001
    1.18  #define CPU_HAS_MMX     0x00000002
    1.19 -#define CPU_HAS_MMXEXT  0x00000004
    1.20 -#define CPU_HAS_3DNOW   0x00000010
    1.21 -#define CPU_HAS_3DNOWEXT 0x00000020
    1.22 -#define CPU_HAS_SSE     0x00000040
    1.23 -#define CPU_HAS_SSE2    0x00000080
    1.24 -#define CPU_HAS_ALTIVEC 0x00000100
    1.25 +#define CPU_HAS_SSE     0x00000010
    1.26 +#define CPU_HAS_SSE2    0x00000020
    1.27 +#define CPU_HAS_SSE3    0x00000040
    1.28 +#define CPU_HAS_SSE4    0x00000080
    1.29  
    1.30 -#if SDL_ALTIVEC_BLITTERS && HAVE_SETJMP && !__MACOSX__
    1.31 -/* This is the brute force way of detecting instruction sets...
    1.32 -   the idea is borrowed from the libmpeg2 library - thanks!
    1.33 - */
    1.34 -static jmp_buf jmpbuf;
    1.35 -static void
    1.36 -illegal_instruction(int sig)
    1.37 -{
    1.38 -    longjmp(jmpbuf, 1);
    1.39 -}
    1.40 -#endif /* HAVE_SETJMP */
    1.41  
    1.42  static __inline__ int
    1.43  CPU_haveCPUID(void)
    1.44 @@ -202,20 +183,6 @@
    1.45  }
    1.46  
    1.47  static __inline__ int
    1.48 -CPU_getCPUIDFeaturesExt(void)
    1.49 -{
    1.50 -    int features = 0;
    1.51 -    int a, b, c, d;
    1.52 -
    1.53 -    cpuid(0x80000000, a, b, c, d);
    1.54 -    if (a >= 0x80000001) {
    1.55 -        cpuid(0x80000001, a, b, c, d);
    1.56 -        features = d;
    1.57 -    }
    1.58 -    return features;
    1.59 -}
    1.60 -
    1.61 -static __inline__ int
    1.62  CPU_haveRDTSC(void)
    1.63  {
    1.64      if (CPU_haveCPUID()) {
    1.65 @@ -234,33 +201,6 @@
    1.66  }
    1.67  
    1.68  static __inline__ int
    1.69 -CPU_haveMMXExt(void)
    1.70 -{
    1.71 -    if (CPU_haveCPUID()) {
    1.72 -        return (CPU_getCPUIDFeaturesExt() & 0x00400000);
    1.73 -    }
    1.74 -    return 0;
    1.75 -}
    1.76 -
    1.77 -static __inline__ int
    1.78 -CPU_have3DNow(void)
    1.79 -{
    1.80 -    if (CPU_haveCPUID()) {
    1.81 -        return (CPU_getCPUIDFeaturesExt() & 0x80000000);
    1.82 -    }
    1.83 -    return 0;
    1.84 -}
    1.85 -
    1.86 -static __inline__ int
    1.87 -CPU_have3DNowExt(void)
    1.88 -{
    1.89 -    if (CPU_haveCPUID()) {
    1.90 -        return (CPU_getCPUIDFeaturesExt() & 0x40000000);
    1.91 -    }
    1.92 -    return 0;
    1.93 -}
    1.94 -
    1.95 -static __inline__ int
    1.96  CPU_haveSSE(void)
    1.97  {
    1.98      if (CPU_haveCPUID()) {
    1.99 @@ -279,26 +219,33 @@
   1.100  }
   1.101  
   1.102  static __inline__ int
   1.103 -CPU_haveAltiVec(void)
   1.104 +CPU_haveSSE3(void)
   1.105  {
   1.106 -    volatile int altivec = 0;
   1.107 -#if defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))
   1.108 -    int selectors[2] = { CTL_HW, HW_VECTORUNIT };
   1.109 -    int hasVectorUnit = 0;
   1.110 -    size_t length = sizeof(hasVectorUnit);
   1.111 -    int error = sysctl(selectors, 2, &hasVectorUnit, &length, NULL, 0);
   1.112 -    if (0 == error)
   1.113 -        altivec = (hasVectorUnit != 0);
   1.114 -#elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
   1.115 -    void (*handler) (int sig);
   1.116 -    handler = signal(SIGILL, illegal_instruction);
   1.117 -    if (setjmp(jmpbuf) == 0) {
   1.118 -        asm volatile ("mtspr 256, %0\n\t" "vand %%v0, %%v0, %%v0"::"r" (-1));
   1.119 -        altivec = 1;
   1.120 +    if (CPU_haveCPUID()) {
   1.121 +        int a, b, c, d;
   1.122 +
   1.123 +        cpuid(0, a, b, c, d);
   1.124 +        if (a >= 1) {
   1.125 +            cpuid(1, a, b, c, d);
   1.126 +            return (c & 0x00000001);
   1.127 +        }
   1.128      }
   1.129 -    signal(SIGILL, handler);
   1.130 -#endif
   1.131 -    return altivec;
   1.132 +    return 0;
   1.133 +}
   1.134 +
   1.135 +static __inline__ int
   1.136 +CPU_haveSSE4(void)
   1.137 +{
   1.138 +    if (CPU_haveCPUID()) {
   1.139 +        int a, b, c, d;
   1.140 +
   1.141 +        cpuid(0, a, b, c, d);
   1.142 +        if (a >= 1) {
   1.143 +            cpuid(1, a, b, c, d);
   1.144 +            return (c & 0x00000100);
   1.145 +        }
   1.146 +    }
   1.147 +    return 0;
   1.148  }
   1.149  
   1.150  static int SDL_CPUCount = 0;
   1.151 @@ -471,23 +418,17 @@
   1.152          if (CPU_haveMMX()) {
   1.153              SDL_CPUFeatures |= CPU_HAS_MMX;
   1.154          }
   1.155 -        if (CPU_haveMMXExt()) {
   1.156 -            SDL_CPUFeatures |= CPU_HAS_MMXEXT;
   1.157 -        }
   1.158 -        if (CPU_have3DNow()) {
   1.159 -            SDL_CPUFeatures |= CPU_HAS_3DNOW;
   1.160 -        }
   1.161 -        if (CPU_have3DNowExt()) {
   1.162 -            SDL_CPUFeatures |= CPU_HAS_3DNOWEXT;
   1.163 -        }
   1.164          if (CPU_haveSSE()) {
   1.165              SDL_CPUFeatures |= CPU_HAS_SSE;
   1.166          }
   1.167          if (CPU_haveSSE2()) {
   1.168              SDL_CPUFeatures |= CPU_HAS_SSE2;
   1.169          }
   1.170 -        if (CPU_haveAltiVec()) {
   1.171 -            SDL_CPUFeatures |= CPU_HAS_ALTIVEC;
   1.172 +        if (CPU_haveSSE3()) {
   1.173 +            SDL_CPUFeatures |= CPU_HAS_SSE3;
   1.174 +        }
   1.175 +        if (CPU_haveSSE4()) {
   1.176 +            SDL_CPUFeatures |= CPU_HAS_SSE4;
   1.177          }
   1.178      }
   1.179      return SDL_CPUFeatures;
   1.180 @@ -512,33 +453,6 @@
   1.181  }
   1.182  
   1.183  SDL_bool
   1.184 -SDL_HasMMXExt(void)
   1.185 -{
   1.186 -    if (SDL_GetCPUFeatures() & CPU_HAS_MMXEXT) {
   1.187 -        return SDL_TRUE;
   1.188 -    }
   1.189 -    return SDL_FALSE;
   1.190 -}
   1.191 -
   1.192 -SDL_bool
   1.193 -SDL_Has3DNow(void)
   1.194 -{
   1.195 -    if (SDL_GetCPUFeatures() & CPU_HAS_3DNOW) {
   1.196 -        return SDL_TRUE;
   1.197 -    }
   1.198 -    return SDL_FALSE;
   1.199 -}
   1.200 -
   1.201 -SDL_bool
   1.202 -SDL_Has3DNowExt(void)
   1.203 -{
   1.204 -    if (SDL_GetCPUFeatures() & CPU_HAS_3DNOWEXT) {
   1.205 -        return SDL_TRUE;
   1.206 -    }
   1.207 -    return SDL_FALSE;
   1.208 -}
   1.209 -
   1.210 -SDL_bool
   1.211  SDL_HasSSE(void)
   1.212  {
   1.213      if (SDL_GetCPUFeatures() & CPU_HAS_SSE) {
   1.214 @@ -557,9 +471,18 @@
   1.215  }
   1.216  
   1.217  SDL_bool
   1.218 -SDL_HasAltiVec(void)
   1.219 +SDL_HasSSE3(void)
   1.220  {
   1.221 -    if (SDL_GetCPUFeatures() & CPU_HAS_ALTIVEC) {
   1.222 +    if (SDL_GetCPUFeatures() & CPU_HAS_SSE3) {
   1.223 +        return SDL_TRUE;
   1.224 +    }
   1.225 +    return SDL_FALSE;
   1.226 +}
   1.227 +
   1.228 +SDL_bool
   1.229 +SDL_HasSSE4(void)
   1.230 +{
   1.231 +    if (SDL_GetCPUFeatures() & CPU_HAS_SSE4) {
   1.232          return SDL_TRUE;
   1.233      }
   1.234      return SDL_FALSE;
   1.235 @@ -578,12 +501,10 @@
   1.236      printf("CacheLine size: %d\n", SDL_GetCPUCacheLineSize());
   1.237      printf("RDTSC: %d\n", SDL_HasRDTSC());
   1.238      printf("MMX: %d\n", SDL_HasMMX());
   1.239 -    printf("MMXExt: %d\n", SDL_HasMMXExt());
   1.240 -    printf("3DNow: %d\n", SDL_Has3DNow());
   1.241 -    printf("3DNowExt: %d\n", SDL_Has3DNowExt());
   1.242      printf("SSE: %d\n", SDL_HasSSE());
   1.243      printf("SSE2: %d\n", SDL_HasSSE2());
   1.244 -    printf("AltiVec: %d\n", SDL_HasAltiVec());
   1.245 +    printf("SSE3: %d\n", SDL_HasSSE3());
   1.246 +    printf("SSE4: %d\n", SDL_HasSSE4());
   1.247      return 0;
   1.248  }
   1.249