comparison 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
comparison
equal deleted inserted replaced
5116:e337f792c6a7 5117:427998ff3bcf
335 335
336 /* Oh, such a sweet sweet trick, just not very useful. :) */ 336 /* Oh, such a sweet sweet trick, just not very useful. :) */
337 static const char * 337 static const char *
338 SDL_GetCPUType() 338 SDL_GetCPUType()
339 { 339 {
340 static char SDL_CPUType[48]; 340 static char SDL_CPUType[13];
341 341
342 if (!SDL_CPUType[0]) { 342 if (!SDL_CPUType[0]) {
343 int i = 0;
344 int a, b, c, d;
345
346 if (CPU_haveCPUID()) {
347 cpuid(0x00000000, a, b, c, d);
348 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
349 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
350 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
351 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
352 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
353 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
354 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
355 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
356 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
357 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
358 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
359 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
360 }
361 if (!SDL_CPUType[0]) {
362 SDL_strlcpy(SDL_CPUType, "Unknown", sizeof(SDL_CPUType));
363 }
364 }
365 return SDL_CPUType;
366 }
367
368 static const char *
369 SDL_GetCPUName()
370 {
371 static char SDL_CPUName[48];
372
373 if (!SDL_CPUName[0]) {
343 int i = 0; 374 int i = 0;
344 int a, b, c, d; 375 int a, b, c, d;
345 376
346 if (CPU_haveCPUID()) { 377 if (CPU_haveCPUID()) {
347 cpuid(0x80000000, a, b, c, d); 378 cpuid(0x80000000, a, b, c, d);
348 if (a >= 0x80000004) { 379 if (a >= 0x80000004) {
349 cpuid(0x80000002, a, b, c, d); 380 cpuid(0x80000002, a, b, c, d);
350 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8; 381 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
351 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8; 382 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
352 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8; 383 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
353 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8; 384 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
354 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8; 385 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
355 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8; 386 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
356 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8; 387 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
357 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8; 388 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
358 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8; 389 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
359 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8; 390 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
360 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8; 391 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
361 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8; 392 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
362 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8; 393 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
363 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8; 394 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
364 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8; 395 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
365 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8; 396 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
366 cpuid(0x80000003, a, b, c, d); 397 cpuid(0x80000003, a, b, c, d);
367 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8; 398 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
368 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8; 399 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
369 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8; 400 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
370 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8; 401 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
371 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8; 402 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
372 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8; 403 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
373 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8; 404 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
374 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8; 405 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
375 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8; 406 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
376 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8; 407 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
377 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8; 408 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
378 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8; 409 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
379 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8; 410 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
380 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8; 411 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
381 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8; 412 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
382 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8; 413 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
383 cpuid(0x80000004, a, b, c, d); 414 cpuid(0x80000004, a, b, c, d);
384 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8; 415 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
385 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8; 416 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
386 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8; 417 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
387 SDL_CPUType[i++] = (char)(a & 0xff); a >>= 8; 418 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
388 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8; 419 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
389 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8; 420 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
390 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8; 421 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
391 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8; 422 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
392 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8; 423 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
393 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8; 424 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
394 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8; 425 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
395 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8; 426 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
396 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8; 427 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
397 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8; 428 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
398 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8; 429 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
399 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8; 430 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
400 } 431 }
401 } 432 }
402 if (!SDL_CPUType[0]) { 433 if (!SDL_CPUName[0]) {
403 SDL_strlcpy(SDL_CPUType, "Unknown", sizeof(SDL_CPUType)); 434 SDL_strlcpy(SDL_CPUName, "Unknown", sizeof(SDL_CPUName));
404 } 435 }
405 } 436 }
406 return SDL_CPUType; 437 return SDL_CPUName;
438 }
439
440 static int
441 SDL_GetCPUCacheLineSize()
442 {
443 const char *cpuType = SDL_GetCPUType();
444
445 if (SDL_strcmp(cpuType, "GenuineIntel") == 0) {
446 int a, b, c, d;
447
448 cpuid(0x00000001, a, b, c, d);
449 return (((b >> 8) & 0xff) * 8);
450 } else if (SDL_strcmp(cpuType, "AuthenticAMD") == 0) {
451 int a, b, c, d;
452
453 cpuid(0x80000005, a, b, c, d);
454 return (c & 0xff);
455 } else {
456 /* Just make a guess here... */
457 return SDL_CACHELINE_SIZE;
458 }
407 } 459 }
408 460
409 static Uint32 SDL_CPUFeatures = 0xFFFFFFFF; 461 static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
410 462
411 static Uint32 463 static Uint32
519 571
520 int 572 int
521 main() 573 main()
522 { 574 {
523 printf("CPU count: %d\n", SDL_GetCPUCount()); 575 printf("CPU count: %d\n", SDL_GetCPUCount());
524 printf("CPU name: %s\n", SDL_GetCPUType()); 576 printf("CPU type: %s\n", SDL_GetCPUType());
577 printf("CPU name: %s\n", SDL_GetCPUName());
578 printf("CacheLine size: %d\n", SDL_GetCPUCacheLineSize());
525 printf("RDTSC: %d\n", SDL_HasRDTSC()); 579 printf("RDTSC: %d\n", SDL_HasRDTSC());
526 printf("MMX: %d\n", SDL_HasMMX()); 580 printf("MMX: %d\n", SDL_HasMMX());
527 printf("MMXExt: %d\n", SDL_HasMMXExt()); 581 printf("MMXExt: %d\n", SDL_HasMMXExt());
528 printf("3DNow: %d\n", SDL_Has3DNow()); 582 printf("3DNow: %d\n", SDL_Has3DNow());
529 printf("3DNowExt: %d\n", SDL_Has3DNowExt()); 583 printf("3DNowExt: %d\n", SDL_Has3DNowExt());