src/cpuinfo/SDL_cpuinfo.c
changeset 5115 427998ff3bcf
parent 5090 327f181542f1
child 5120 b3f075368b1f
     1.1 --- a/src/cpuinfo/SDL_cpuinfo.c	Thu Jan 27 15:31:00 2011 -0800
     1.2 +++ b/src/cpuinfo/SDL_cpuinfo.c	Thu Jan 27 16:46:15 2011 -0800
     1.3 @@ -337,73 +337,125 @@
     1.4  static const char *
     1.5  SDL_GetCPUType()
     1.6  {
     1.7 -    static char SDL_CPUType[48];
     1.8 +    static char SDL_CPUType[13];
     1.9  
    1.10      if (!SDL_CPUType[0]) {
    1.11          int i = 0;
    1.12          int a, b, c, d;
    1.13  
    1.14          if (CPU_haveCPUID()) {
    1.15 +            cpuid(0x00000000, a, b, c, d);
    1.16 +            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
    1.17 +            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
    1.18 +            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
    1.19 +            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
    1.20 +            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
    1.21 +            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
    1.22 +            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
    1.23 +            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
    1.24 +            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
    1.25 +            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
    1.26 +            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
    1.27 +            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
    1.28 +        }
    1.29 +        if (!SDL_CPUType[0]) {
    1.30 +            SDL_strlcpy(SDL_CPUType, "Unknown", sizeof(SDL_CPUType));
    1.31 +        }
    1.32 +    }
    1.33 +    return SDL_CPUType;
    1.34 +}
    1.35 +
    1.36 +static const char *
    1.37 +SDL_GetCPUName()
    1.38 +{
    1.39 +    static char SDL_CPUName[48];
    1.40 +
    1.41 +    if (!SDL_CPUName[0]) {
    1.42 +        int i = 0;
    1.43 +        int a, b, c, d;
    1.44 +
    1.45 +        if (CPU_haveCPUID()) {
    1.46              cpuid(0x80000000, a, b, c, d);
    1.47              if (a >= 0x80000004) {
    1.48                  cpuid(0x80000002, a, b, c, d);
    1.49 -                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
    1.50 -                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
    1.51 -                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
    1.52 -                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
    1.53 -                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
    1.54 -                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
    1.55 -                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
    1.56 -                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
    1.57 -                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
    1.58 -                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
    1.59 -                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
    1.60 -                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
    1.61 -                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
    1.62 -                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
    1.63 -                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
    1.64 -                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
    1.65 +                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
    1.66 +                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
    1.67 +                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
    1.68 +                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
    1.69 +                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
    1.70 +                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
    1.71 +                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
    1.72 +                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
    1.73 +                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
    1.74 +                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
    1.75 +                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
    1.76 +                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
    1.77 +                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
    1.78 +                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
    1.79 +                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
    1.80 +                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
    1.81                  cpuid(0x80000003, a, b, c, d);
    1.82 -                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
    1.83 -                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
    1.84 -                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
    1.85 -                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
    1.86 -                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
    1.87 -                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
    1.88 -                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
    1.89 -                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
    1.90 -                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
    1.91 -                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
    1.92 -                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
    1.93 -                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
    1.94 -                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
    1.95 -                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
    1.96 -                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
    1.97 -                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
    1.98 +                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
    1.99 +                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
   1.100 +                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
   1.101 +                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
   1.102 +                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
   1.103 +                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
   1.104 +                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
   1.105 +                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
   1.106 +                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
   1.107 +                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
   1.108 +                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
   1.109 +                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
   1.110 +                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
   1.111 +                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
   1.112 +                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
   1.113 +                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
   1.114                  cpuid(0x80000004, a, b, c, d);
   1.115 -                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
   1.116 -                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
   1.117 -                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
   1.118 -                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
   1.119 -                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
   1.120 -                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
   1.121 -                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
   1.122 -                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
   1.123 -                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
   1.124 -                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
   1.125 -                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
   1.126 -                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
   1.127 -                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
   1.128 -                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
   1.129 -                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
   1.130 -                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
   1.131 +                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
   1.132 +                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
   1.133 +                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
   1.134 +                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
   1.135 +                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
   1.136 +                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
   1.137 +                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
   1.138 +                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
   1.139 +                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
   1.140 +                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
   1.141 +                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
   1.142 +                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
   1.143 +                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
   1.144 +                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
   1.145 +                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
   1.146 +                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
   1.147              }
   1.148          }
   1.149 -        if (!SDL_CPUType[0]) {
   1.150 -            SDL_strlcpy(SDL_CPUType, "Unknown", sizeof(SDL_CPUType));
   1.151 +        if (!SDL_CPUName[0]) {
   1.152 +            SDL_strlcpy(SDL_CPUName, "Unknown", sizeof(SDL_CPUName));
   1.153          }
   1.154      }
   1.155 -    return SDL_CPUType;
   1.156 +    return SDL_CPUName;
   1.157 +}
   1.158 +
   1.159 +static int
   1.160 +SDL_GetCPUCacheLineSize()
   1.161 +{
   1.162 +    const char *cpuType = SDL_GetCPUType();
   1.163 +
   1.164 +    if (SDL_strcmp(cpuType, "GenuineIntel") == 0) {
   1.165 +        int a, b, c, d;
   1.166 +
   1.167 +        cpuid(0x00000001, a, b, c, d);
   1.168 +        return (((b >> 8) & 0xff) * 8);
   1.169 +    } else if (SDL_strcmp(cpuType, "AuthenticAMD") == 0) {
   1.170 +        int a, b, c, d;
   1.171 +
   1.172 +        cpuid(0x80000005, a, b, c, d);
   1.173 +        return (c & 0xff);
   1.174 +    } else {
   1.175 +        /* Just make a guess here... */
   1.176 +        return SDL_CACHELINE_SIZE;
   1.177 +    }
   1.178  }
   1.179  
   1.180  static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
   1.181 @@ -521,7 +573,9 @@
   1.182  main()
   1.183  {
   1.184      printf("CPU count: %d\n", SDL_GetCPUCount());
   1.185 -    printf("CPU name: %s\n", SDL_GetCPUType());
   1.186 +    printf("CPU type: %s\n", SDL_GetCPUType());
   1.187 +    printf("CPU name: %s\n", SDL_GetCPUName());
   1.188 +    printf("CacheLine size: %d\n", SDL_GetCPUCacheLineSize());
   1.189      printf("RDTSC: %d\n", SDL_HasRDTSC());
   1.190      printf("MMX: %d\n", SDL_HasMMX());
   1.191      printf("MMXExt: %d\n", SDL_HasMMXExt());