x86UNIXCPUInfo.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #ifndef TORQUE_64
  57. detectX86CPUInfo(vendor, &processor, &properties);
  58. SetProcessorInfo(PlatformSystemInfo.processor,
  59. vendor, processor, properties);
  60. # else
  61. // 32 bit code to detect processor name and extensions is around, but feels kinda dinky in 2021.
  62. // Porting it to 64 bit? Hardcode the bare minimum instead.
  63. PlatformSystemInfo.processor.mhz = 1337;
  64. PlatformSystemInfo.processor.name = StringTable->insert("AMD64 Compatible");
  65. #endif
  66. U32 mhz = 0;
  67. //--------------------------------------
  68. // if RDTSC support calculate the aproximate Mhz of the CPU
  69. if (PlatformSystemInfo.processor.properties & CPU_PROP_RDTSC &&
  70. PlatformSystemInfo.processor.properties & CPU_PROP_FPU)
  71. {
  72. const U32 MS_INTERVAL = 250; // Bigger = more accurate, but slower startup
  73. #if defined(TORQUE_COMPILER_GCC) && ((__GNUC__ >= 3) && (__GNUC_MINOR__ >=4)) || ((__GNUC__ >= 4) && (__GNUC_MINOR__ >=0))
  74. asm("rdtsc" : "=a" (timeLo1), "=d" (timeHi1));
  75. #else
  76. __asm__(
  77. "pushl %eax\n"
  78. "pushl %edx\n"
  79. "rdtsc\n"
  80. "movl %eax, (Ttime)\n"
  81. "movl %edx, (Ttime+4)\n"
  82. "popl %edx\n"
  83. "popl %eax\n"
  84. );
  85. #endif
  86. U32 ms = Platform::getRealMilliseconds();
  87. while ( Platform::getRealMilliseconds() < ms+MS_INTERVAL )
  88. { /* empty */ }
  89. ms = Platform::getRealMilliseconds()-ms;
  90. #if defined(TORQUE_COMPILER_GCC) && ((__GNUC__ >= 3) && (__GNUC_MINOR__ >= 4)) || ((__GNUC__ >= 4) && (__GNUC_MINOR__ >=0))
  91. asm("rdtsc" : "=a" (timeLo2), "=d" (timeHi2));
  92. // TODO: This will need to be fixed for x64 support to include bits from timeHi
  93. clockticks = timeLo2 - timeLo1;
  94. #else
  95. asm(
  96. "pushl %eax\n"
  97. "pushl %edx\n"
  98. "rdtsc\n"
  99. "sub (Ttime+4), %edx\n"
  100. "sbb (Ttime), %eax\n"
  101. "mov %eax, (clockticks)\n"
  102. "popl %edx\n"
  103. "popl %eax\n"
  104. );
  105. #endif
  106. mhz = (U32)(F32(clockticks) / F32(ms) / 1000.0f);
  107. // catch-22 the timing method used above to calc Mhz is generally
  108. // wrong by a few percent so we want to round to the nearest clock
  109. // multiple but we also want to be careful to not touch overclocked
  110. // results
  111. // measure how close the Raw Mhz number is to the center of each clock
  112. // bucket
  113. U32 bucket25 = mhz % 25;
  114. U32 bucket33 = mhz % 33;
  115. U32 bucket50 = mhz % 50;
  116. if (bucket50 < 8 || bucket50 > 42)
  117. PlatformSystemInfo.processor.mhz =
  118. U32((mhz+(50.0f/2.0f))/50.0f) * 50;
  119. else if (bucket25 < 5 || bucket25 > 20)
  120. PlatformSystemInfo.processor.mhz =
  121. U32((mhz+(25.0f/2.0f))/25.0f) * 25;
  122. else if (bucket33 < 5 || bucket33 > 28)
  123. PlatformSystemInfo.processor.mhz =
  124. U32((mhz+(33.0f/2.0f))/33.0f) * 33;
  125. else
  126. PlatformSystemInfo.processor.mhz = U32(mhz);
  127. }
  128. Con::printf("Processor Init:");
  129. if (PlatformSystemInfo.processor.mhz >= 1000) {
  130. Con::printf(" %s, %.2f Ghz", PlatformSystemInfo.processor.name, ((float)PlatformSystemInfo.processor.mhz)/1000.0f);
  131. } else {
  132. Con::printf(" %s, %d Mhz", PlatformSystemInfo.processor.name, PlatformSystemInfo.processor.mhz);
  133. }
  134. #ifndef TORQUE_64
  135. if (PlatformSystemInfo.processor.mhz != mhz) {
  136. if (mhz >= 1000) {
  137. Con::printf(" (timed at roughly %.2f Ghz)", ((float)mhz)/1000.0f);
  138. } else {
  139. Con::printf(" (timed at roughly %d Mhz)", mhz);
  140. }
  141. }
  142. if (PlatformSystemInfo.processor.properties & CPU_PROP_FPU)
  143. Con::printf(" FPU detected");
  144. if (PlatformSystemInfo.processor.properties & CPU_PROP_MMX)
  145. Con::printf(" MMX detected");
  146. if (PlatformSystemInfo.processor.properties & CPU_PROP_3DNOW)
  147. Con::printf(" 3DNow detected");
  148. if (PlatformSystemInfo.processor.properties & CPU_PROP_SSE)
  149. Con::printf(" SSE detected");
  150. Con::printf(" ");
  151. #endif
  152. PlatformBlitInit();
  153. }