cpuinfo: Removed code duplication, cached CPUID details.
authorRyan C. Gordon <icculus@icculus.org>
Wed, 16 Nov 2016 22:49:04 -0500
changeset 106198786a8d1088e
parent 10618 01ff51434145
child 10620 c37e02dd1a9b
cpuinfo: Removed code duplication, cached CPUID details.
src/cpuinfo/SDL_cpuinfo.c
     1.1 --- a/src/cpuinfo/SDL_cpuinfo.c	Wed Nov 16 22:09:40 2016 +0100
     1.2 +++ b/src/cpuinfo/SDL_cpuinfo.c	Wed Nov 16 22:49:04 2016 -0500
     1.3 @@ -78,6 +78,7 @@
     1.4  CPU_haveCPUID(void)
     1.5  {
     1.6      int has_CPUID = 0;
     1.7 +
     1.8  /* *INDENT-OFF* */
     1.9  #ifndef SDL_CPUINFO_DISABLED
    1.10  #if defined(__GNUC__) && defined(i386)
    1.11 @@ -215,59 +216,47 @@
    1.12      a = b = c = d = 0
    1.13  #endif
    1.14  
    1.15 -static int
    1.16 -CPU_getCPUIDFeatures(void)
    1.17 +static int CPU_CPUIDFeatures[4];
    1.18 +static int CPU_CPUIDMaxFunction = 0;
    1.19 +static SDL_bool CPU_OSSavesYMM = SDL_FALSE;
    1.20 +
    1.21 +static void
    1.22 +CPU_calcCPUIDFeatures(void)
    1.23  {
    1.24 -    int features = 0;
    1.25 -    int a, b, c, d;
    1.26 +    static SDL_bool checked = SDL_FALSE;
    1.27 +    if (!checked) {
    1.28 +        checked = SDL_TRUE;
    1.29 +        if (CPU_haveCPUID()) {
    1.30 +            int a, b, c, d;
    1.31 +            cpuid(0, a, b, c, d);
    1.32 +            CPU_CPUIDMaxFunction = a;
    1.33 +            if (CPU_CPUIDMaxFunction >= 1) {
    1.34 +                cpuid(1, a, b, c, d);
    1.35 +                CPU_CPUIDFeatures[0] = a;
    1.36 +                CPU_CPUIDFeatures[1] = b;
    1.37 +                CPU_CPUIDFeatures[2] = c;
    1.38 +                CPU_CPUIDFeatures[3] = d;
    1.39  
    1.40 -    cpuid(0, a, b, c, d);
    1.41 -    if (a >= 1) {
    1.42 -        cpuid(1, a, b, c, d);
    1.43 -        features = d;
    1.44 +                /* Check to make sure we can call xgetbv */
    1.45 +                if (c & 0x08000000) {
    1.46 +                    /* Call xgetbv to see if YMM register state is saved */
    1.47 +#if defined(__GNUC__) && (defined(i386) || defined(__x86_64__))
    1.48 +                    asm(".byte 0x0f, 0x01, 0xd0" : "=a" (a) : "c" (0) : "%edx");
    1.49 +#elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64)) && (_MSC_FULL_VER >= 160040219) /* VS2010 SP1 */
    1.50 +                    a = (int)_xgetbv(0);
    1.51 +#elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
    1.52 +                    __asm
    1.53 +                    {
    1.54 +                        xor ecx, ecx
    1.55 +                        _asm _emit 0x0f _asm _emit 0x01 _asm _emit 0xd0
    1.56 +                        mov a, eax
    1.57 +                    }
    1.58 +#endif
    1.59 +                    CPU_OSSavesYMM = ((a & 6) == 6) ? SDL_TRUE : SDL_FALSE;
    1.60 +                }
    1.61 +            }
    1.62 +        }
    1.63      }
    1.64 -    return features;
    1.65 -}
    1.66 -
    1.67 -static SDL_bool
    1.68 -CPU_OSSavesYMM(void)
    1.69 -{
    1.70 -    int a, b, c, d;
    1.71 -
    1.72 -    /* Check to make sure we can call xgetbv */
    1.73 -    cpuid(0, a, b, c, d);
    1.74 -    if (a < 1) {
    1.75 -        return SDL_FALSE;
    1.76 -    }
    1.77 -    cpuid(1, a, b, c, d);
    1.78 -    if (!(c & 0x08000000)) {
    1.79 -        return SDL_FALSE;
    1.80 -    }
    1.81 -
    1.82 -    /* Call xgetbv to see if YMM register state is saved */
    1.83 -    a = 0;
    1.84 -#if defined(__GNUC__) && (defined(i386) || defined(__x86_64__))
    1.85 -    asm(".byte 0x0f, 0x01, 0xd0" : "=a" (a) : "c" (0) : "%edx");
    1.86 -#elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64)) && (_MSC_FULL_VER >= 160040219) /* VS2010 SP1 */
    1.87 -    a = (int)_xgetbv(0);
    1.88 -#elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
    1.89 -    __asm
    1.90 -    {
    1.91 -        xor ecx, ecx
    1.92 -        _asm _emit 0x0f _asm _emit 0x01 _asm _emit 0xd0
    1.93 -        mov a, eax
    1.94 -    }
    1.95 -#endif
    1.96 -    return ((a & 6) == 6) ? SDL_TRUE : SDL_FALSE;
    1.97 -}
    1.98 -
    1.99 -static int
   1.100 -CPU_haveRDTSC(void)
   1.101 -{
   1.102 -    if (CPU_haveCPUID()) {
   1.103 -        return (CPU_getCPUIDFeatures() & 0x00000010);
   1.104 -    }
   1.105 -    return 0;
   1.106  }
   1.107  
   1.108  static int
   1.109 @@ -300,20 +289,10 @@
   1.110  }
   1.111  
   1.112  static int
   1.113 -CPU_haveMMX(void)
   1.114 -{
   1.115 -    if (CPU_haveCPUID()) {
   1.116 -        return (CPU_getCPUIDFeatures() & 0x00800000);
   1.117 -    }
   1.118 -    return 0;
   1.119 -}
   1.120 -
   1.121 -static int
   1.122  CPU_have3DNow(void)
   1.123  {
   1.124 -    if (CPU_haveCPUID()) {
   1.125 +    if (CPU_CPUIDMaxFunction > 0) {  /* that is, do we have CPUID at all? */
   1.126          int a, b, c, d;
   1.127 -
   1.128          cpuid(0x80000000, a, b, c, d);
   1.129          if (a >= 0x80000001) {
   1.130              cpuid(0x80000001, a, b, c, d);
   1.131 @@ -323,95 +302,22 @@
   1.132      return 0;
   1.133  }
   1.134  
   1.135 -static int
   1.136 -CPU_haveSSE(void)
   1.137 -{
   1.138 -    if (CPU_haveCPUID()) {
   1.139 -        return (CPU_getCPUIDFeatures() & 0x02000000);
   1.140 -    }
   1.141 -    return 0;
   1.142 -}
   1.143 -
   1.144 -static int
   1.145 -CPU_haveSSE2(void)
   1.146 -{
   1.147 -    if (CPU_haveCPUID()) {
   1.148 -        return (CPU_getCPUIDFeatures() & 0x04000000);
   1.149 -    }
   1.150 -    return 0;
   1.151 -}
   1.152 -
   1.153 -static int
   1.154 -CPU_haveSSE3(void)
   1.155 -{
   1.156 -    if (CPU_haveCPUID()) {
   1.157 -        int a, b, c, d;
   1.158 -
   1.159 -        cpuid(0, a, b, c, d);
   1.160 -        if (a >= 1) {
   1.161 -            cpuid(1, a, b, c, d);
   1.162 -            return (c & 0x00000001);
   1.163 -        }
   1.164 -    }
   1.165 -    return 0;
   1.166 -}
   1.167 -
   1.168 -static int
   1.169 -CPU_haveSSE41(void)
   1.170 -{
   1.171 -    if (CPU_haveCPUID()) {
   1.172 -        int a, b, c, d;
   1.173 -
   1.174 -        cpuid(0, a, b, c, d);
   1.175 -        if (a >= 1) {
   1.176 -            cpuid(1, a, b, c, d);
   1.177 -            return (c & 0x00080000);
   1.178 -        }
   1.179 -    }
   1.180 -    return 0;
   1.181 -}
   1.182 -
   1.183 -static int
   1.184 -CPU_haveSSE42(void)
   1.185 -{
   1.186 -    if (CPU_haveCPUID()) {
   1.187 -        int a, b, c, d;
   1.188 -
   1.189 -        cpuid(0, a, b, c, d);
   1.190 -        if (a >= 1) {
   1.191 -            cpuid(1, a, b, c, d);
   1.192 -            return (c & 0x00100000);
   1.193 -        }
   1.194 -    }
   1.195 -    return 0;
   1.196 -}
   1.197 -
   1.198 -static int
   1.199 -CPU_haveAVX(void)
   1.200 -{
   1.201 -    if (CPU_haveCPUID() && CPU_OSSavesYMM()) {
   1.202 -        int a, b, c, d;
   1.203 -
   1.204 -        cpuid(0, a, b, c, d);
   1.205 -        if (a >= 1) {
   1.206 -            cpuid(1, a, b, c, d);
   1.207 -            return (c & 0x10000000);
   1.208 -        }
   1.209 -    }
   1.210 -    return 0;
   1.211 -}
   1.212 +#define CPU_haveRDTSC() (CPU_CPUIDFeatures[3] & 0x00000010)
   1.213 +#define CPU_haveMMX() (CPU_CPUIDFeatures[3] & 0x00800000)
   1.214 +#define CPU_haveSSE() (CPU_CPUIDFeatures[3] & 0x02000000)
   1.215 +#define CPU_haveSSE2() (CPU_CPUIDFeatures[3] & 0x04000000)
   1.216 +#define CPU_haveSSE3() (CPU_CPUIDFeatures[2] & 0x00000001)
   1.217 +#define CPU_haveSSE41() (CPU_CPUIDFeatures[2] & 0x00080000)
   1.218 +#define CPU_haveSSE42() (CPU_CPUIDFeatures[2] & 0x00100000)
   1.219 +#define CPU_haveAVX() (CPU_OSSavesYMM && (CPU_CPUIDFeatures[2] & 0x10000000))
   1.220  
   1.221  static int
   1.222  CPU_haveAVX2(void)
   1.223  {
   1.224 -    if (CPU_haveCPUID() && CPU_OSSavesYMM()) {
   1.225 +    if (CPU_OSSavesYMM && (CPU_CPUIDMaxFunction >= 7)) {
   1.226          int a, b, c, d;
   1.227 -
   1.228 -        cpuid(0, a, b, c, d);
   1.229 -        if (a >= 7) {
   1.230 -            cpuid(7, a, b, c, d);
   1.231 -            return (b & 0x00000020);
   1.232 -        }
   1.233 +        cpuid(7, a, b, c, d);
   1.234 +        return (b & 0x00000020);
   1.235      }
   1.236      return 0;
   1.237  }
   1.238 @@ -459,7 +365,8 @@
   1.239      if (!SDL_CPUType[0]) {
   1.240          int i = 0;
   1.241  
   1.242 -        if (CPU_haveCPUID()) {
   1.243 +        CPU_calcCPUIDFeatures();
   1.244 +        if (CPU_CPUIDMaxFunction > 0) {  /* do we have CPUID at all? */
   1.245              int a, b, c, d;
   1.246              cpuid(0x00000000, a, b, c, d);
   1.247              (void) a;
   1.248 @@ -496,7 +403,8 @@
   1.249          int i = 0;
   1.250          int a, b, c, d;
   1.251  
   1.252 -        if (CPU_haveCPUID()) {
   1.253 +        CPU_calcCPUIDFeatures();
   1.254 +        if (CPU_CPUIDMaxFunction > 0) {  /* do we have CPUID at all? */
   1.255              cpuid(0x80000000, a, b, c, d);
   1.256              if (a >= 0x80000004) {
   1.257                  cpuid(0x80000002, a, b, c, d);
   1.258 @@ -584,6 +492,7 @@
   1.259  SDL_GetCPUFeatures(void)
   1.260  {
   1.261      if (SDL_CPUFeatures == 0xFFFFFFFF) {
   1.262 +        CPU_calcCPUIDFeatures();
   1.263          SDL_CPUFeatures = 0;
   1.264          if (CPU_haveRDTSC()) {
   1.265              SDL_CPUFeatures |= CPU_HAS_RDTSC;
   1.266 @@ -622,103 +531,71 @@
   1.267      return SDL_CPUFeatures;
   1.268  }
   1.269  
   1.270 -SDL_bool
   1.271 -SDL_HasRDTSC(void)
   1.272 +#define CPU_FEATURE_AVAILABLE(f) ((SDL_GetCPUFeatures() & f) ? SDL_TRUE : SDL_FALSE)
   1.273 +
   1.274 +SDL_bool SDL_HasRDTSC(void)
   1.275  {
   1.276 -    if (SDL_GetCPUFeatures() & CPU_HAS_RDTSC) {
   1.277 -        return SDL_TRUE;
   1.278 -    }
   1.279 -    return SDL_FALSE;
   1.280 +    return CPU_FEATURE_AVAILABLE(CPU_HAS_RDTSC);
   1.281  }
   1.282  
   1.283  SDL_bool
   1.284  SDL_HasAltiVec(void)
   1.285  {
   1.286 -    if (SDL_GetCPUFeatures() & CPU_HAS_ALTIVEC) {
   1.287 -        return SDL_TRUE;
   1.288 -    }
   1.289 -    return SDL_FALSE;
   1.290 +    return CPU_FEATURE_AVAILABLE(CPU_HAS_ALTIVEC);
   1.291  }
   1.292  
   1.293  SDL_bool
   1.294  SDL_HasMMX(void)
   1.295  {
   1.296 -    if (SDL_GetCPUFeatures() & CPU_HAS_MMX) {
   1.297 -        return SDL_TRUE;
   1.298 -    }
   1.299 -    return SDL_FALSE;
   1.300 +    return CPU_FEATURE_AVAILABLE(CPU_HAS_MMX);
   1.301  }
   1.302  
   1.303  SDL_bool
   1.304  SDL_Has3DNow(void)
   1.305  {
   1.306 -    if (SDL_GetCPUFeatures() & CPU_HAS_3DNOW) {
   1.307 -        return SDL_TRUE;
   1.308 -    }
   1.309 -    return SDL_FALSE;
   1.310 +    return CPU_FEATURE_AVAILABLE(CPU_HAS_3DNOW);
   1.311  }
   1.312  
   1.313  SDL_bool
   1.314  SDL_HasSSE(void)
   1.315  {
   1.316 -    if (SDL_GetCPUFeatures() & CPU_HAS_SSE) {
   1.317 -        return SDL_TRUE;
   1.318 -    }
   1.319 -    return SDL_FALSE;
   1.320 +    return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE);
   1.321  }
   1.322  
   1.323  SDL_bool
   1.324  SDL_HasSSE2(void)
   1.325  {
   1.326 -    if (SDL_GetCPUFeatures() & CPU_HAS_SSE2) {
   1.327 -        return SDL_TRUE;
   1.328 -    }
   1.329 -    return SDL_FALSE;
   1.330 +    return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE2);
   1.331  }
   1.332  
   1.333  SDL_bool
   1.334  SDL_HasSSE3(void)
   1.335  {
   1.336 -    if (SDL_GetCPUFeatures() & CPU_HAS_SSE3) {
   1.337 -        return SDL_TRUE;
   1.338 -    }
   1.339 -    return SDL_FALSE;
   1.340 +    return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE3);
   1.341  }
   1.342  
   1.343  SDL_bool
   1.344  SDL_HasSSE41(void)
   1.345  {
   1.346 -    if (SDL_GetCPUFeatures() & CPU_HAS_SSE41) {
   1.347 -        return SDL_TRUE;
   1.348 -    }
   1.349 -    return SDL_FALSE;
   1.350 +    return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE41);
   1.351  }
   1.352  
   1.353  SDL_bool
   1.354  SDL_HasSSE42(void)
   1.355  {
   1.356 -    if (SDL_GetCPUFeatures() & CPU_HAS_SSE42) {
   1.357 -        return SDL_TRUE;
   1.358 -    }
   1.359 -    return SDL_FALSE;
   1.360 +    return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE42);
   1.361  }
   1.362  
   1.363  SDL_bool
   1.364  SDL_HasAVX(void)
   1.365  {
   1.366 -    if (SDL_GetCPUFeatures() & CPU_HAS_AVX) {
   1.367 -        return SDL_TRUE;
   1.368 -    }
   1.369 -    return SDL_FALSE;
   1.370 +    return CPU_FEATURE_AVAILABLE(CPU_HAS_AVX);
   1.371  }
   1.372  
   1.373  SDL_bool
   1.374  SDL_HasAVX2(void)
   1.375  {
   1.376 -    if (SDL_GetCPUFeatures() & CPU_HAS_AVX2) {
   1.377 -        return SDL_TRUE;
   1.378 -    }
   1.379 -    return SDL_FALSE;
   1.380 +    return CPU_FEATURE_AVAILABLE(CPU_HAS_AVX2);
   1.381  }
   1.382  
   1.383  static int SDL_SystemRAM = 0;