Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
3.8 KiB

  1. /* CpuArch.h -- CPU specific code
  2. 2013-11-12: Igor Pavlov : Public domain */
  3. #ifndef __CPU_ARCH_H
  4. #define __CPU_ARCH_H
  5. #include "7zTypes.h"
  6. EXTERN_C_BEGIN
  7. /*
  8. MY_CPU_LE means that CPU is LITTLE ENDIAN.
  9. If MY_CPU_LE is not defined, we don't know about that property of platform (it can be LITTLE ENDIAN).
  10. MY_CPU_LE_UNALIGN means that CPU is LITTLE ENDIAN and CPU supports unaligned memory accesses.
  11. If MY_CPU_LE_UNALIGN is not defined, we don't know about these properties of platform.
  12. */
  13. #if defined(_M_X64) || defined(_M_AMD64) || defined(__x86_64__)
  14. #define MY_CPU_AMD64
  15. #endif
  16. #if defined(MY_CPU_AMD64) || defined(_M_IA64)
  17. #define MY_CPU_64BIT
  18. #endif
  19. #if defined(_M_IX86) || defined(__i386__)
  20. #define MY_CPU_X86
  21. #endif
  22. #if defined(MY_CPU_X86) || defined(MY_CPU_AMD64)
  23. #define MY_CPU_X86_OR_AMD64
  24. #endif
  25. #if defined(MY_CPU_X86) || defined(_M_ARM)
  26. #define MY_CPU_32BIT
  27. #endif
  28. #if defined(_WIN32) && defined(_M_ARM)
  29. #define MY_CPU_ARM_LE
  30. #endif
  31. #if defined(_WIN32) && defined(_M_IA64)
  32. #define MY_CPU_IA64_LE
  33. #endif
  34. #if defined(MY_CPU_X86_OR_AMD64)
  35. #define MY_CPU_LE_UNALIGN
  36. #endif
  37. #if defined(MY_CPU_X86_OR_AMD64) || defined(MY_CPU_ARM_LE) || defined(MY_CPU_IA64_LE) || defined(__ARMEL__) || defined(__MIPSEL__) || defined(__LITTLE_ENDIAN__)
  38. #define MY_CPU_LE
  39. #endif
  40. #if defined(__BIG_ENDIAN__) || defined(__m68k__) || defined(__ARMEB__) || defined(__MIPSEB__)
  41. #define MY_CPU_BE
  42. #endif
  43. #if defined(MY_CPU_LE) && defined(MY_CPU_BE)
  44. Stop_Compiling_Bad_Endian
  45. #endif
  46. #ifdef MY_CPU_LE_UNALIGN
  47. #define GetUi16(p) (*(const UInt16 *)(const void *)(p))
  48. #define GetUi32(p) (*(const UInt32 *)(const void *)(p))
  49. #define GetUi64(p) (*(const UInt64 *)(const void *)(p))
  50. #define SetUi16(p, d) *(UInt16 *)(p) = (d);
  51. #define SetUi32(p, d) *(UInt32 *)(p) = (d);
  52. #define SetUi64(p, d) *(UInt64 *)(p) = (d);
  53. #else
  54. #define GetUi16(p) (((const Byte *)(p))[0] | ((UInt16)((const Byte *)(p))[1] << 8))
  55. #define GetUi32(p) ( \
  56. ((const Byte *)(p))[0] | \
  57. ((UInt32)((const Byte *)(p))[1] << 8) | \
  58. ((UInt32)((const Byte *)(p))[2] << 16) | \
  59. ((UInt32)((const Byte *)(p))[3] << 24))
  60. #define GetUi64(p) (GetUi32(p) | ((UInt64)GetUi32(((const Byte *)(p)) + 4) << 32))
  61. #define SetUi16(p, d) { UInt32 _x_ = (d); \
  62. ((Byte *)(p))[0] = (Byte)_x_; \
  63. ((Byte *)(p))[1] = (Byte)(_x_ >> 8); }
  64. #define SetUi32(p, d) { UInt32 _x_ = (d); \
  65. ((Byte *)(p))[0] = (Byte)_x_; \
  66. ((Byte *)(p))[1] = (Byte)(_x_ >> 8); \
  67. ((Byte *)(p))[2] = (Byte)(_x_ >> 16); \
  68. ((Byte *)(p))[3] = (Byte)(_x_ >> 24); }
  69. #define SetUi64(p, d) { UInt64 _x64_ = (d); \
  70. SetUi32(p, (UInt32)_x64_); \
  71. SetUi32(((Byte *)(p)) + 4, (UInt32)(_x64_ >> 32)); }
  72. #endif
  73. #if defined(MY_CPU_LE_UNALIGN) && defined(_WIN64) && (_MSC_VER >= 1300)
  74. #include <stdlib.h>
  75. #pragma intrinsic(_byteswap_ulong)
  76. #pragma intrinsic(_byteswap_uint64)
  77. #define GetBe32(p) _byteswap_ulong(*(const UInt32 *)(const Byte *)(p))
  78. #define GetBe64(p) _byteswap_uint64(*(const UInt64 *)(const Byte *)(p))
  79. #else
  80. #define GetBe32(p) ( \
  81. ((UInt32)((const Byte *)(p))[0] << 24) | \
  82. ((UInt32)((const Byte *)(p))[1] << 16) | \
  83. ((UInt32)((const Byte *)(p))[2] << 8) | \
  84. ((const Byte *)(p))[3] )
  85. #define GetBe64(p) (((UInt64)GetBe32(p) << 32) | GetBe32(((const Byte *)(p)) + 4))
  86. #endif
  87. #define GetBe16(p) ((UInt16)(((UInt16)((const Byte *)(p))[0] << 8) | ((const Byte *)(p))[1]))
  88. #ifdef MY_CPU_X86_OR_AMD64
  89. typedef struct
  90. {
  91. UInt32 maxFunc;
  92. UInt32 vendor[3];
  93. UInt32 ver;
  94. UInt32 b;
  95. UInt32 c;
  96. UInt32 d;
  97. } Cx86cpuid;
  98. enum
  99. {
  100. CPU_FIRM_INTEL,
  101. CPU_FIRM_AMD,
  102. CPU_FIRM_VIA
  103. };
  104. Bool x86cpuid_CheckAndRead(Cx86cpuid *p);
  105. int x86cpuid_GetFirm(const Cx86cpuid *p);
  106. #define x86cpuid_GetFamily(p) (((p)->ver >> 8) & 0xFF00F)
  107. #define x86cpuid_GetModel(p) (((p)->ver >> 4) & 0xF00F)
  108. #define x86cpuid_GetStepping(p) ((p)->ver & 0xF)
  109. Bool CPU_Is_InOrder();
  110. Bool CPU_Is_Aes_Supported();
  111. #endif
  112. EXTERN_C_END
  113. #endif