platformCPU.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 "platform/platformCPUCount.h"
  24. #include "core/strings/stringFunctions.h"
  25. #include "core/stringTable.h"
  26. #include "core/util/tSignal.h"
  27. Signal<void(void)> Platform::SystemInfoReady;
  28. enum CPUFlags
  29. {
  30. // EDX Register flags
  31. BIT_RDTSC = BIT(4),
  32. BIT_MMX = BIT(23),
  33. BIT_SSE = BIT(25),
  34. BIT_SSE2 = BIT(26),
  35. BIT_3DNOW = BIT(31), // only available for amd cpus in x86
  36. // These use a different value for comparison than the above flags (ECX Register)
  37. BIT_SSE3 = BIT(0),
  38. BIT_SSE3xt = BIT(9),
  39. BIT_SSE4_1 = BIT(19),
  40. BIT_SSE4_2 = BIT(20),
  41. };
  42. // fill the specified structure with information obtained from asm code
  43. void SetProcessorInfo(Platform::SystemInfo_struct::Processor& pInfo,
  44. char* vendor, char* brand, U32 processor, U32 properties, U32 properties2)
  45. {
  46. // always assume FPU is available in 2021...
  47. pInfo.properties |= CPU_PROP_FPU;
  48. #if defined(TORQUE_CPU_X86) || defined(TORQUE_CPU_X64) || defined(TORQUE_CPU_ARM64)
  49. pInfo.properties |= CPU_PROP_LE;
  50. #endif
  51. #if defined(TORQUE_CPU_X64) || defined(TORQUE_CPU_ARM64)
  52. pInfo.properties |= CPU_PROP_64bit;
  53. #endif
  54. #if defined(TORQUE_CPU_X86) || defined(TORQUE_CPU_X64)
  55. pInfo.properties |= (properties & BIT_RDTSC) ? CPU_PROP_RDTSC : 0;
  56. pInfo.properties |= (properties & BIT_MMX) ? CPU_PROP_MMX : 0;
  57. pInfo.properties |= (properties & BIT_SSE) ? CPU_PROP_SSE : 0;
  58. pInfo.properties |= (properties & BIT_SSE2) ? CPU_PROP_SSE2 : 0;
  59. pInfo.properties |= (properties2 & BIT_SSE3) ? CPU_PROP_SSE3 : 0;
  60. pInfo.properties |= (properties2 & BIT_SSE3xt) ? CPU_PROP_SSE3xt : 0;
  61. pInfo.properties |= (properties2 & BIT_SSE4_1) ? CPU_PROP_SSE4_1 : 0;
  62. pInfo.properties |= (properties2 & BIT_SSE4_2) ? CPU_PROP_SSE4_2 : 0;
  63. #endif
  64. if (dStricmp(vendor, "GenuineIntel") == 0)
  65. {
  66. pInfo.type = CPU_Intel;
  67. pInfo.name = StringTable->insert(brand ? brand : "Intel (Unknown)");
  68. }
  69. //--------------------------------------
  70. else if (dStricmp(vendor, "AuthenticAMD") == 0)
  71. {
  72. pInfo.name = StringTable->insert(brand ? brand : "AMD (unknown)");
  73. pInfo.type = CPU_AMD;
  74. // 3dnow! is only available in AMD cpus on x86. Otherwise its not reliably set.
  75. pInfo.properties |= (properties & BIT_3DNOW) ? CPU_PROP_3DNOW : 0;
  76. }
  77. else if (dStricmp(vendor, "Apple") == 0)
  78. {
  79. pInfo.name = StringTable->insert(brand ? brand : "Apple (unknown)");
  80. pInfo.type = CPU_Apple;
  81. }
  82. else
  83. {
  84. #if defined(TORQUE_CPU_X86) || defined(TORQUE_CPU_X64)
  85. pInfo.name = StringTable->insert(brand ? brand : "x86 Compatible (unknown)");
  86. pInfo.type = CPU_X86Compatible;
  87. #elif defined(TORQUE_CPU_ARM64)
  88. pInfo.name = StringTable->insert(brand ? brand : "Arm Compatible (unknown)");
  89. pInfo.type = CPU_ArmCompatible;
  90. #else
  91. #error "Unknown CPU Architecture"
  92. #endif
  93. }
  94. // Get multithreading caps.
  95. CPUInfo::EConfig config = CPUInfo::CPUCount( pInfo.numLogicalProcessors, pInfo.numAvailableCores, pInfo.numPhysicalProcessors );
  96. pInfo.isHyperThreaded = CPUInfo::isHyperThreaded( config );
  97. pInfo.isMultiCore = CPUInfo::isMultiCore( config );
  98. // Trigger the signal
  99. Platform::SystemInfoReady.trigger();
  100. }