winCPUInfo.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "platform/platformCPUCount.h"
  27. #include <math.h>
  28. #include <intrin.h>
  29. Platform::SystemInfo_struct Platform::SystemInfo;
  30. extern void PlatformBlitInit();
  31. static void getBrand(char* brand)
  32. {
  33. S32 extendedInfo[4];
  34. __cpuid(extendedInfo, 0x80000000);
  35. S32 numberExtendedIds = extendedInfo[0];
  36. // Sets brand
  37. if (numberExtendedIds >= 0x80000004)
  38. {
  39. int offset = 0;
  40. for (int i = 0; i < 3; ++i)
  41. {
  42. S32 brandInfo[4];
  43. __cpuidex(brandInfo, 0x80000002 + i, 0);
  44. *reinterpret_cast<int*>(brand + offset + 0) = brandInfo[0];
  45. *reinterpret_cast<int*>(brand + offset + 4) = brandInfo[1];
  46. *reinterpret_cast<int*>(brand + offset + 8) = brandInfo[2];
  47. *reinterpret_cast<int*>(brand + offset + 12) = brandInfo[3];
  48. offset += sizeof(S32) * 4;
  49. }
  50. }
  51. }
  52. enum CpuFlags
  53. {
  54. // EDX Register flags
  55. BIT_MMX = BIT(23),
  56. BIT_SSE = BIT(25),
  57. BIT_SSE2 = BIT(26),
  58. BIT_3DNOW = BIT(31), // only available for amd cpus in x86
  59. // These use a different value for comparison than the above flags (ECX Register)
  60. BIT_SSE3 = BIT(0),
  61. BIT_SSE3ex = BIT(9),
  62. BIT_SSE4_1 = BIT(19),
  63. BIT_SSE4_2 = BIT(20),
  64. BIT_XSAVE_RESTORE = BIT(27),
  65. BIT_AVX = BIT(28),
  66. };
  67. static void detectCpuFeatures(Platform::SystemInfo_struct::Processor &processor)
  68. {
  69. S32 cpuInfo[4];
  70. __cpuid(cpuInfo, 1);
  71. U32 eax = cpuInfo[0]; // eax
  72. U32 edx = cpuInfo[3]; // edx
  73. U32 ecx = cpuInfo[2]; // ecx
  74. processor.properties |= (edx & BIT_MMX) ? CPU_PROP_MMX : 0;
  75. processor.properties |= (edx & BIT_SSE) ? CPU_PROP_SSE : 0;
  76. processor.properties |= (edx & BIT_SSE2) ? CPU_PROP_SSE2 : 0;
  77. processor.properties |= (ecx & BIT_SSE3) ? CPU_PROP_SSE3 : 0;
  78. processor.properties |= (ecx & BIT_SSE3ex) ? CPU_PROP_SSE3ex : 0;
  79. processor.properties |= (ecx & BIT_SSE4_1) ? CPU_PROP_SSE4_1 : 0;
  80. processor.properties |= (ecx & BIT_SSE4_2) ? CPU_PROP_SSE4_2 : 0;
  81. // AVX detection requires that xsaverestore is supported
  82. if (ecx & BIT_XSAVE_RESTORE && ecx & BIT_AVX)
  83. {
  84. bool supportsAVX = _xgetbv(_XCR_XFEATURE_ENABLED_MASK) & 0x6;
  85. if (supportsAVX)
  86. {
  87. processor.properties |= CPU_PROP_AVX;
  88. }
  89. }
  90. if (processor.isMultiCore)
  91. processor.properties |= CPU_PROP_MP;
  92. #ifdef TORQUE_CPU_X64
  93. processor.properties |= CPU_PROP_64bit;
  94. #endif
  95. }
  96. void Processor::init()
  97. {
  98. // Reference:
  99. // www.cyrix.com
  100. // www.amd.com
  101. // www.intel.com
  102. // http://developer.intel.com/design/PentiumII/manuals/24512701.pdf
  103. Platform::SystemInfo.processor.type = CPU_X86Compatible;
  104. Platform::SystemInfo.processor.name = StringTable->insert("Unknown x86 Compatible");
  105. Platform::SystemInfo.processor.mhz = 0;
  106. Platform::SystemInfo.processor.properties = CPU_PROP_C | CPU_PROP_FPU | CPU_PROP_LE;
  107. char vendor[0x20];
  108. dMemset(vendor, 0, sizeof(vendor));
  109. S32 vendorInfo[4];
  110. __cpuid(vendorInfo, 0);
  111. *reinterpret_cast<int*>(vendor) = vendorInfo[1]; // ebx
  112. *reinterpret_cast<int*>(vendor + 4) = vendorInfo[3]; // edx
  113. *reinterpret_cast<int*>(vendor + 8) = vendorInfo[2]; // ecx
  114. char brand[0x40];
  115. dMemset(brand, 0, sizeof(brand));
  116. getBrand(brand);
  117. SetProcessorInfo(Platform::SystemInfo.processor, vendor, brand);
  118. detectCpuFeatures(Platform::SystemInfo.processor);
  119. U32 mhz = 1000; // default if it can't be found
  120. LONG result;
  121. DWORD data = 0;
  122. DWORD dataSize = 4;
  123. HKEY hKey;
  124. result = ::RegOpenKeyExA (HKEY_LOCAL_MACHINE,"Hardware\\Description\\System\\CentralProcessor\\0", 0, KEY_QUERY_VALUE, &hKey);
  125. if (result == ERROR_SUCCESS)
  126. {
  127. result = ::RegQueryValueExA (hKey, "~MHz",NULL, NULL,(LPBYTE)&data, &dataSize);
  128. if (result == ERROR_SUCCESS)
  129. mhz = data;
  130. ::RegCloseKey(hKey);
  131. }
  132. Platform::SystemInfo.processor.mhz = mhz;
  133. Con::printf("Processor Init:");
  134. Con::printf(" Processor: %s", Platform::SystemInfo.processor.name);
  135. if (Platform::SystemInfo.processor.properties & CPU_PROP_MMX)
  136. Con::printf(" MMX detected" );
  137. if (Platform::SystemInfo.processor.properties & CPU_PROP_SSE)
  138. Con::printf(" SSE detected" );
  139. if (Platform::SystemInfo.processor.properties & CPU_PROP_SSE2)
  140. Con::printf(" SSE2 detected" );
  141. if (Platform::SystemInfo.processor.properties & CPU_PROP_SSE3)
  142. Con::printf(" SSE3 detected" );
  143. if (Platform::SystemInfo.processor.properties & CPU_PROP_SSE3ex)
  144. Con::printf(" SSE3ex detected ");
  145. if (Platform::SystemInfo.processor.properties & CPU_PROP_SSE4_1)
  146. Con::printf(" SSE4.1 detected" );
  147. if (Platform::SystemInfo.processor.properties & CPU_PROP_SSE4_2)
  148. Con::printf(" SSE4.2 detected" );
  149. if (Platform::SystemInfo.processor.properties & CPU_PROP_AVX)
  150. Con::printf(" AVX detected");
  151. if (Platform::SystemInfo.processor.properties & CPU_PROP_MP)
  152. Con::printf(" MultiCore CPU detected [%i cores, %i logical]", Platform::SystemInfo.processor.numPhysicalProcessors, Platform::SystemInfo.processor.numLogicalProcessors);
  153. Con::printf(" ");
  154. PlatformBlitInit();
  155. }