x86UNIXCPUInfo.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "string/stringTable.h"
  26. #include <math.h>
  27. extern void PlatformBlitInit();
  28. extern void SetProcessorInfo(TorqueSystemInfo::Processor& pInfo,
  29. char* vendor, U32 processor, U32 properties); // platform/platformCPU.cc
  30. // asm cpu detection routine from platform code
  31. extern "C"
  32. {
  33. void detectX86CPUInfo(char *vendor, U32 *processor, U32 *properties);
  34. }
  35. /* used in the asm */
  36. static U32 Ttime[2];
  37. static U32 clockticks = 0;
  38. static char vendor[13] = {0,};
  39. static U32 properties = 0;
  40. static U32 processor = 0;
  41. static U32 timeLo1, timeHi1 = 0;
  42. static U32 timeLo2, timeHi2 = 0;
  43. void Processor::init()
  44. {
  45. // Reference:
  46. // www.cyrix.com
  47. // www.amd.com
  48. // www.intel.com
  49. // http://developer.intel.com/design/PentiumII/manuals/24512701.pdf
  50. PlatformSystemInfo.processor.type = CPU_X86Compatible;
  51. PlatformSystemInfo.processor.name = StringTable->insert("Unknown x86 Compatible");
  52. PlatformSystemInfo.processor.mhz = 0;
  53. PlatformSystemInfo.processor.properties = CPU_PROP_C;
  54. clockticks = properties = processor = Ttime[0] = 0;
  55. dStrcpy(vendor, "");
  56. detectX86CPUInfo(vendor, &processor, &properties);
  57. SetProcessorInfo(PlatformSystemInfo.processor,
  58. vendor, processor, properties);
  59. U32 mhz = 0;
  60. //--------------------------------------
  61. // if RDTSC support calculate the aproximate Mhz of the CPU
  62. if (PlatformSystemInfo.processor.properties & CPU_PROP_RDTSC &&
  63. PlatformSystemInfo.processor.properties & CPU_PROP_FPU)
  64. {
  65. const U32 MS_INTERVAL = 250; // Bigger = more accurate, but slower startup
  66. #if defined(TORQUE_COMPILER_GCC) && ((__GNUC__ >= 3) && (__GNUC_MINOR__ >=4)) || ((__GNUC__ >= 4) && (__GNUC_MINOR__ >=0))
  67. asm("rdtsc" : "=a" (timeLo1), "=d" (timeHi1));
  68. #else
  69. __asm__(
  70. "pushl %eax\n"
  71. "pushl %edx\n"
  72. "rdtsc\n"
  73. "movl %eax, (Ttime)\n"
  74. "movl %edx, (Ttime+4)\n"
  75. "popl %edx\n"
  76. "popl %eax\n"
  77. );
  78. #endif
  79. U32 ms = Platform::getRealMilliseconds();
  80. while ( Platform::getRealMilliseconds() < ms+MS_INTERVAL )
  81. { /* empty */ }
  82. ms = Platform::getRealMilliseconds()-ms;
  83. #if defined(TORQUE_COMPILER_GCC) && ((__GNUC__ >= 3) && (__GNUC_MINOR__ >= 4)) || ((__GNUC__ >= 4) && (__GNUC_MINOR__ >=0))
  84. asm("rdtsc" : "=a" (timeLo2), "=d" (timeHi2));
  85. // TODO: This will need to be fixed for x64 support to include bits from timeHi
  86. clockticks = timeLo2 - timeLo1;
  87. #else
  88. asm(
  89. "pushl %eax\n"
  90. "pushl %edx\n"
  91. "rdtsc\n"
  92. "sub (Ttime+4), %edx\n"
  93. "sbb (Ttime), %eax\n"
  94. "mov %eax, (clockticks)\n"
  95. "popl %edx\n"
  96. "popl %eax\n"
  97. );
  98. #endif
  99. mhz = (U32)(F32(clockticks) / F32(ms) / 1000.0f);
  100. // catch-22 the timing method used above to calc Mhz is generally
  101. // wrong by a few percent so we want to round to the nearest clock
  102. // multiple but we also want to be careful to not touch overclocked
  103. // results
  104. // measure how close the Raw Mhz number is to the center of each clock
  105. // bucket
  106. U32 bucket25 = mhz % 25;
  107. U32 bucket33 = mhz % 33;
  108. U32 bucket50 = mhz % 50;
  109. if (bucket50 < 8 || bucket50 > 42)
  110. PlatformSystemInfo.processor.mhz =
  111. U32((mhz+(50.0f/2.0f))/50.0f) * 50;
  112. else if (bucket25 < 5 || bucket25 > 20)
  113. PlatformSystemInfo.processor.mhz =
  114. U32((mhz+(25.0f/2.0f))/25.0f) * 25;
  115. else if (bucket33 < 5 || bucket33 > 28)
  116. PlatformSystemInfo.processor.mhz =
  117. U32((mhz+(33.0f/2.0f))/33.0f) * 33;
  118. else
  119. PlatformSystemInfo.processor.mhz = U32(mhz);
  120. }
  121. Con::printf("Processor Init:");
  122. if (PlatformSystemInfo.processor.mhz >= 1000) {
  123. Con::printf(" %s, %.2f Ghz", PlatformSystemInfo.processor.name, ((float)PlatformSystemInfo.processor.mhz)/1000.0f);
  124. } else {
  125. Con::printf(" %s, %d Mhz", PlatformSystemInfo.processor.name, PlatformSystemInfo.processor.mhz);
  126. }
  127. if (PlatformSystemInfo.processor.mhz != mhz) {
  128. if (mhz >= 1000) {
  129. Con::printf(" (timed at roughly %.2f Ghz)", ((float)mhz)/1000.0f);
  130. } else {
  131. Con::printf(" (timed at roughly %d Mhz)", mhz);
  132. }
  133. }
  134. if (PlatformSystemInfo.processor.properties & CPU_PROP_FPU)
  135. Con::printf(" FPU detected");
  136. if (PlatformSystemInfo.processor.properties & CPU_PROP_MMX)
  137. Con::printf(" MMX detected");
  138. if (PlatformSystemInfo.processor.properties & CPU_PROP_3DNOW)
  139. Con::printf(" 3DNow detected");
  140. if (PlatformSystemInfo.processor.properties & CPU_PROP_SSE)
  141. Con::printf(" SSE detected");
  142. Con::printf(" ");
  143. PlatformBlitInit();
  144. }