diff src/cpuinfo/SDL_cpuinfo.c @ 5117:427998ff3bcf

Added cache line size info in SDL_cpuinfo.h I also added an implementation to dynamically query it, but didn't expose it since most x86 CPUs have an L1 cache line size of 64 bytes.
author Sam Lantinga <slouken@libsdl.org>
date Thu, 27 Jan 2011 16:46:15 -0800
parents 327f181542f1
children b3f075368b1f
line wrap: on
line diff
--- a/src/cpuinfo/SDL_cpuinfo.c	Thu Jan 27 15:31:00 2011 -0800
+++ b/src/cpuinfo/SDL_cpuinfo.c	Thu Jan 27 16:46:15 2011 -0800
@@ -337,73 +337,125 @@
 static const char *
 SDL_GetCPUType()
 {
-    static char SDL_CPUType[48];
+    static char SDL_CPUType[13];
 
     if (!SDL_CPUType[0]) {
         int i = 0;
         int a, b, c, d;
 
         if (CPU_haveCPUID()) {
+            cpuid(0x00000000, a, b, c, d);
+            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
+            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
+            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
+            SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
+            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
+            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
+            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
+            SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
+            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
+            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
+            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
+            SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
+        }
+        if (!SDL_CPUType[0]) {
+            SDL_strlcpy(SDL_CPUType, "Unknown", sizeof(SDL_CPUType));
+        }
+    }
+    return SDL_CPUType;
+}
+
+static const char *
+SDL_GetCPUName()
+{
+    static char SDL_CPUName[48];
+
+    if (!SDL_CPUName[0]) {
+        int i = 0;
+        int a, b, c, d;
+
+        if (CPU_haveCPUID()) {
             cpuid(0x80000000, a, b, c, d);
             if (a >= 0x80000004) {
                 cpuid(0x80000002, a, b, c, d);
-                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
-                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
-                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
-                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
-                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
-                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
-                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
-                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
-                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
-                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
-                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
-                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
-                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
-                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
-                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
-                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
+                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
+                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
+                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
+                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
+                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
+                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
+                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
+                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
+                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
+                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
+                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
+                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
+                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
+                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
+                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
+                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
                 cpuid(0x80000003, a, b, c, d);
-                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
-                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
-                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
-                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
-                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
-                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
-                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
-                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
-                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
-                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
-                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
-                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
-                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
-                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
-                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
-                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
+                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
+                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
+                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
+                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
+                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
+                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
+                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
+                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
+                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
+                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
+                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
+                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
+                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
+                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
+                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
+                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
                 cpuid(0x80000004, a, b, c, d);
-                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
-                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
-                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
-                SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8;
-                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
-                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
-                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
-                SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
-                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
-                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
-                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
-                SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
-                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
-                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
-                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
-                SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
+                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
+                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
+                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
+                SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
+                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
+                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
+                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
+                SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
+                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
+                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
+                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
+                SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
+                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
+                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
+                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
+                SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
             }
         }
-        if (!SDL_CPUType[0]) {
-            SDL_strlcpy(SDL_CPUType, "Unknown", sizeof(SDL_CPUType));
+        if (!SDL_CPUName[0]) {
+            SDL_strlcpy(SDL_CPUName, "Unknown", sizeof(SDL_CPUName));
         }
     }
-    return SDL_CPUType;
+    return SDL_CPUName;
+}
+
+static int
+SDL_GetCPUCacheLineSize()
+{
+    const char *cpuType = SDL_GetCPUType();
+
+    if (SDL_strcmp(cpuType, "GenuineIntel") == 0) {
+        int a, b, c, d;
+
+        cpuid(0x00000001, a, b, c, d);
+        return (((b >> 8) & 0xff) * 8);
+    } else if (SDL_strcmp(cpuType, "AuthenticAMD") == 0) {
+        int a, b, c, d;
+
+        cpuid(0x80000005, a, b, c, d);
+        return (c & 0xff);
+    } else {
+        /* Just make a guess here... */
+        return SDL_CACHELINE_SIZE;
+    }
 }
 
 static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
@@ -521,7 +573,9 @@
 main()
 {
     printf("CPU count: %d\n", SDL_GetCPUCount());
-    printf("CPU name: %s\n", SDL_GetCPUType());
+    printf("CPU type: %s\n", SDL_GetCPUType());
+    printf("CPU name: %s\n", SDL_GetCPUName());
+    printf("CacheLine size: %d\n", SDL_GetCPUCacheLineSize());
     printf("RDTSC: %d\n", SDL_HasRDTSC());
     printf("MMX: %d\n", SDL_HasMMX());
     printf("MMXExt: %d\n", SDL_HasMMXExt());