x86UNIXCPUInfo.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 "core/stringTable.h"
  26. #include <math.h>
  27. Platform::SystemInfo_struct Platform::SystemInfo;
  28. extern void PlatformBlitInit();
  29. extern void SetProcessorInfo(Platform::SystemInfo_struct::Processor& pInfo,
  30. char* vendor, U32 processor, U32 properties); // platform/platformCPU.cc
  31. // asm cpu detection routine from platform code
  32. extern "C"
  33. {
  34. void detectX86CPUInfo(char *vendor, U32 *processor, U32 *properties);
  35. }
  36. /* used in the asm */
  37. static U32 time[2];
  38. static U32 clockticks = 0;
  39. static char vendor[13] = {0,};
  40. static U32 properties = 0;
  41. static U32 processor = 0;
  42. static U32 timeHi = 0;
  43. static U32 timeLo = 0;
  44. void Processor::init()
  45. {
  46. // Reference:
  47. // www.cyrix.com
  48. // www.amd.com
  49. // www.intel.com
  50. // http://developer.intel.com/design/PentiumII/manuals/24512701.pdf
  51. Platform::SystemInfo.processor.type = CPU_X86Compatible;
  52. Platform::SystemInfo.processor.name = StringTable->insert("Unknown x86 Compatible");
  53. Platform::SystemInfo.processor.mhz = 0;
  54. Platform::SystemInfo.processor.properties = CPU_PROP_C;
  55. clockticks = properties = processor = time[0] = 0;
  56. dStrcpy(vendor, "");
  57. detectX86CPUInfo(vendor, &processor, &properties);
  58. SetProcessorInfo(Platform::SystemInfo.processor,
  59. vendor, processor, properties);
  60. //--------------------------------------
  61. // if RDTSC support calculate the aproximate Mhz of the CPU
  62. if (Platform::SystemInfo.processor.properties & CPU_PROP_RDTSC &&
  63. Platform::SystemInfo.processor.properties & CPU_PROP_FPU)
  64. {
  65. const U32 MS_INTERVAL = 750;
  66. #if defined(TORQUE_COMPILER_GCC) && ((__GNUC__ >= 3) && (__GNUC_MINOR__ >=4)) || ((__GNUC__ >= 4) && (__GNUC_MINOR__ >=0))
  67. asm("rdtsc" : "=a" (timeLo), "=d" (timeHi));
  68. #else
  69. __asm__(
  70. "pushl %eax\n"
  71. "pushl %edx\n"
  72. "rdtsc\n"
  73. "movl %eax, (time)\n"
  74. "movl %edx, (time+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(
  85. "pushl %eax\n"
  86. "pushl %edx\n"
  87. "rdtsc\n"
  88. "sub (timeHi), %edx\n"
  89. "sbb (timeLo), %eax\n"
  90. "mov %eax, (clockticks)\n"
  91. "popl %edx\n"
  92. "popl %eax\n"
  93. );
  94. #else
  95. asm(
  96. "pushl %eax\n"
  97. "pushl %edx\n"
  98. "rdtsc\n"
  99. "sub (time+4), %edx\n"
  100. "sbb (time), %eax\n"
  101. "mov %eax, (clockticks)\n"
  102. "popl %edx\n"
  103. "popl %eax\n"
  104. );
  105. #endif
  106. U32 mhz = static_cast<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. Platform::SystemInfo.processor.mhz =
  118. U32((mhz+(50.0f/2.0f))/50.0f) * 50;
  119. else if (bucket25 < 5 || bucket25 > 20)
  120. Platform::SystemInfo.processor.mhz =
  121. U32((mhz+(25.0f/2.0f))/25.0f) * 25;
  122. else if (bucket33 < 5 || bucket33 > 28)
  123. Platform::SystemInfo.processor.mhz =
  124. U32((mhz+(33.0f/2.0f))/33.0f) * 33;
  125. else
  126. Platform::SystemInfo.processor.mhz = U32(mhz);
  127. }
  128. Con::printf("Processor Init:");
  129. Con::printf(" %s, %d Mhz", Platform::SystemInfo.processor.name, Platform::SystemInfo.processor.mhz);
  130. if (Platform::SystemInfo.processor.properties & CPU_PROP_FPU)
  131. Con::printf(" FPU detected");
  132. if (Platform::SystemInfo.processor.properties & CPU_PROP_MMX)
  133. Con::printf(" MMX detected");
  134. if (Platform::SystemInfo.processor.properties & CPU_PROP_3DNOW)
  135. Con::printf(" 3DNow detected");
  136. if (Platform::SystemInfo.processor.properties & CPU_PROP_SSE)
  137. Con::printf(" SSE detected");
  138. Con::printf(" ");
  139. PlatformBlitInit();
  140. }