x86UNIXCPUInfo.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "platformX86UNIX/platformX86UNIX.h"
  24. #include "console/console.h"
  25. #include "core/stringTable.h"
  26. #include "core/strings/stringFunctions.h"
  27. #include <math.h>
  28. Platform::SystemInfo_struct Platform::SystemInfo;
  29. extern void PlatformBlitInit();
  30. extern void SetProcessorInfo(Platform::SystemInfo_struct::Processor& pInfo,
  31. char* vendor, U32 processor, U32 properties, U32 properties2); // platform/platformCPU.cc
  32. // asm cpu detection routine from platform code
  33. extern "C"
  34. {
  35. void detectX86CPUInfo(char *vendor, U32 *processor, U32 *properties);
  36. }
  37. /* used in the asm */
  38. static U32 time[2];
  39. static char vendor[13] = {0,};
  40. static U32 properties = 0;
  41. static U32 processor = 0;
  42. U32 clockticks = 0;
  43. U32 timeHi = 0;
  44. U32 timeLo = 0;
  45. void Processor::init()
  46. {
  47. // Reference:
  48. // www.cyrix.com
  49. // www.amd.com
  50. // www.intel.com
  51. // http://developer.intel.com/design/PentiumII/manuals/24512701.pdf
  52. Platform::SystemInfo.processor.type = CPU_X86Compatible;
  53. Platform::SystemInfo.processor.name = StringTable->insert("Unknown x86 Compatible");
  54. Platform::SystemInfo.processor.mhz = 0;
  55. Platform::SystemInfo.processor.properties = CPU_PROP_C;
  56. clockticks = properties = processor = time[0] = 0;
  57. dStrcpy(vendor, "");
  58. detectX86CPUInfo(vendor, &processor, &properties);
  59. SetProcessorInfo(Platform::SystemInfo.processor,
  60. vendor, processor, properties, 0);
  61. //--------------------------------------
  62. // if RDTSC support calculate the aproximate Mhz of the CPU
  63. if (Platform::SystemInfo.processor.properties & CPU_PROP_RDTSC &&
  64. Platform::SystemInfo.processor.properties & CPU_PROP_FPU)
  65. {
  66. const U32 MS_INTERVAL = 750;
  67. #if defined(TORQUE_COMPILER_GCC) && ((__GNUC__ >= 3) && (__GNUC_MINOR__ >=4)) || ((__GNUC__ >= 4) && (__GNUC_MINOR__ >=0))
  68. asm("rdtsc" : "=a" (timeLo), "=d" (timeHi));
  69. #else
  70. __asm__(
  71. "pushl %eax\n"
  72. "pushl %edx\n"
  73. "rdtsc\n"
  74. "movl %eax, (time)\n"
  75. "movl %edx, (time+4)\n"
  76. "popl %edx\n"
  77. "popl %eax\n"
  78. );
  79. #endif
  80. U32 ms = Platform::getRealMilliseconds();
  81. while ( Platform::getRealMilliseconds() < ms+MS_INTERVAL )
  82. { /* empty */ }
  83. ms = Platform::getRealMilliseconds()-ms;
  84. #if defined(TORQUE_COMPILER_GCC) && ((__GNUC__ >= 3) && (__GNUC_MINOR__ >= 4)) || ((__GNUC__ >= 4) && (__GNUC_MINOR__ >=0))
  85. asm(
  86. "pushl %eax\n"
  87. "pushl %edx\n"
  88. "rdtsc\n"
  89. "sub (timeHi), %edx\n"
  90. "sbb (timeLo), %eax\n"
  91. "mov %eax, (clockticks)\n"
  92. "popl %edx\n"
  93. "popl %eax\n"
  94. );
  95. #else
  96. asm(
  97. "pushl %eax\n"
  98. "pushl %edx\n"
  99. "rdtsc\n"
  100. "sub (time+4), %edx\n"
  101. "sbb (time), %eax\n"
  102. "mov %eax, (clockticks)\n"
  103. "popl %edx\n"
  104. "popl %eax\n"
  105. );
  106. #endif
  107. U32 mhz = static_cast<U32>(F32(clockticks) / F32(ms) / 1000.0f);
  108. // catch-22 the timing method used above to calc Mhz is generally
  109. // wrong by a few percent so we want to round to the nearest clock
  110. // multiple but we also want to be careful to not touch overclocked
  111. // results
  112. // measure how close the Raw Mhz number is to the center of each clock
  113. // bucket
  114. U32 bucket25 = mhz % 25;
  115. U32 bucket33 = mhz % 33;
  116. U32 bucket50 = mhz % 50;
  117. if (bucket50 < 8 || bucket50 > 42)
  118. Platform::SystemInfo.processor.mhz =
  119. U32((mhz+(50.0f/2.0f))/50.0f) * 50;
  120. else if (bucket25 < 5 || bucket25 > 20)
  121. Platform::SystemInfo.processor.mhz =
  122. U32((mhz+(25.0f/2.0f))/25.0f) * 25;
  123. else if (bucket33 < 5 || bucket33 > 28)
  124. Platform::SystemInfo.processor.mhz =
  125. U32((mhz+(33.0f/2.0f))/33.0f) * 33;
  126. else
  127. Platform::SystemInfo.processor.mhz = U32(mhz);
  128. }
  129. Con::printf("Processor Init:");
  130. Con::printf(" %s, %d Mhz", Platform::SystemInfo.processor.name, Platform::SystemInfo.processor.mhz);
  131. if (Platform::SystemInfo.processor.properties & CPU_PROP_FPU)
  132. Con::printf(" FPU detected");
  133. if (Platform::SystemInfo.processor.properties & CPU_PROP_MMX)
  134. Con::printf(" MMX detected");
  135. if (Platform::SystemInfo.processor.properties & CPU_PROP_3DNOW)
  136. Con::printf(" 3DNow detected");
  137. if (Platform::SystemInfo.processor.properties & CPU_PROP_SSE)
  138. Con::printf(" SSE detected");
  139. Con::printf(" ");
  140. PlatformBlitInit();
  141. }