winCPUInfo.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "platformWin32/platformWin32.h"
  24. #include "console/console.h"
  25. #include "core/stringTable.h"
  26. #include <math.h>
  27. #include <intrin.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. void Processor::init()
  33. {
  34. // Reference:
  35. // www.cyrix.com
  36. // www.amd.com
  37. // www.intel.com
  38. // http://developer.intel.com/design/PentiumII/manuals/24512701.pdf
  39. Con::printf("Processor Init:");
  40. Platform::SystemInfo.processor.type = CPU_X86Compatible;
  41. Platform::SystemInfo.processor.name = StringTable->insert("Unknown x86 Compatible");
  42. Platform::SystemInfo.processor.mhz = 0;
  43. Platform::SystemInfo.processor.properties = CPU_PROP_C | CPU_PROP_LE;
  44. char vendor[0x20];
  45. dMemset(vendor, 0, sizeof(vendor));
  46. U32 properties = 0;
  47. U32 processor = 0;
  48. U32 properties2 = 0;
  49. S32 vendorInfo[4];
  50. __cpuid(vendorInfo, 0);
  51. *reinterpret_cast<int*>(vendor) = vendorInfo[1]; // ebx
  52. *reinterpret_cast<int*>(vendor + 4) = vendorInfo[3]; // edx
  53. *reinterpret_cast<int*>(vendor + 8) = vendorInfo[2]; // ecx
  54. S32 cpuInfo[4];
  55. __cpuid(cpuInfo, 1);
  56. processor = cpuInfo[0]; // eax
  57. properties = cpuInfo[3]; // edx
  58. properties2 = cpuInfo[2]; // ecx
  59. SetProcessorInfo(Platform::SystemInfo.processor, vendor, processor, properties, properties2);
  60. // now calculate speed of processor...
  61. U32 nearmhz = 0; // nearest rounded mhz
  62. U32 mhz = 0; // calculated value.
  63. LONG result;
  64. DWORD data = 0;
  65. DWORD dataSize = 4;
  66. HKEY hKey;
  67. result = ::RegOpenKeyExA (HKEY_LOCAL_MACHINE,"Hardware\\Description\\System\\CentralProcessor\\0", 0, KEY_QUERY_VALUE, &hKey);
  68. if (result == ERROR_SUCCESS)
  69. {
  70. result = ::RegQueryValueExA (hKey, "~MHz",NULL, NULL,(LPBYTE)&data, &dataSize);
  71. if (result == ERROR_SUCCESS)
  72. nearmhz = mhz = data;
  73. ::RegCloseKey(hKey);
  74. }
  75. Platform::SystemInfo.processor.mhz = mhz;
  76. if (mhz==0)
  77. {
  78. Con::printf(" %s, (Unknown) Mhz", Platform::SystemInfo.processor.name);
  79. // stick SOMETHING in so it isn't ZERO.
  80. Platform::SystemInfo.processor.mhz = 200; // seems a decent value.
  81. }
  82. else
  83. {
  84. if (nearmhz >= 1000)
  85. Con::printf(" %s, ~%.2f Ghz", Platform::SystemInfo.processor.name, ((float)nearmhz)/1000.0f);
  86. else
  87. Con::printf(" %s, ~%d Mhz", Platform::SystemInfo.processor.name, nearmhz);
  88. if (nearmhz != mhz)
  89. {
  90. if (mhz >= 1000)
  91. Con::printf(" (timed at roughly %.2f Ghz)", ((float)mhz)/1000.0f);
  92. else
  93. Con::printf(" (timed at roughly %d Mhz)", mhz);
  94. }
  95. }
  96. if( Platform::SystemInfo.processor.numAvailableCores > 0
  97. || Platform::SystemInfo.processor.numPhysicalProcessors > 0
  98. || Platform::SystemInfo.processor.isHyperThreaded )
  99. Platform::SystemInfo.processor.properties |= CPU_PROP_MP;
  100. if (Platform::SystemInfo.processor.properties & CPU_PROP_FPU)
  101. Con::printf( " FPU detected" );
  102. if (Platform::SystemInfo.processor.properties & CPU_PROP_MMX)
  103. Con::printf( " MMX detected" );
  104. if (Platform::SystemInfo.processor.properties & CPU_PROP_3DNOW)
  105. Con::printf( " 3DNow detected" );
  106. if (Platform::SystemInfo.processor.properties & CPU_PROP_SSE)
  107. Con::printf( " SSE detected" );
  108. if( Platform::SystemInfo.processor.properties & CPU_PROP_SSE2 )
  109. Con::printf( " SSE2 detected" );
  110. if( Platform::SystemInfo.processor.isHyperThreaded )
  111. Con::printf( " HT detected" );
  112. if( Platform::SystemInfo.processor.properties & CPU_PROP_MP )
  113. Con::printf( " MP detected [%i cores, %i logical, %i physical]",
  114. Platform::SystemInfo.processor.numAvailableCores,
  115. Platform::SystemInfo.processor.numLogicalProcessors,
  116. Platform::SystemInfo.processor.numPhysicalProcessors );
  117. Con::printf(" ");
  118. PlatformBlitInit();
  119. }