Counter Strike : Global Offensive Source Code
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.

345 lines
8.6 KiB

  1. #ifndef CRYPTOPP_CPU_H
  2. #define CRYPTOPP_CPU_H
  3. #ifdef CRYPTOPP_GENERATE_X64_MASM
  4. #define CRYPTOPP_X86_ASM_AVAILABLE
  5. #define CRYPTOPP_BOOL_X64 1
  6. #define CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE 1
  7. #define NAMESPACE_END
  8. #else
  9. #include "config.h"
  10. #if CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE
  11. #include <emmintrin.h>
  12. #endif
  13. #if CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE
  14. #if !defined(__GNUC__) || defined(__SSSE3__) || defined(__INTEL_COMPILER)
  15. #include <tmmintrin.h>
  16. #else
  17. __inline __m128i __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  18. _mm_shuffle_epi8 (__m128i a, __m128i b)
  19. {
  20. asm ("pshufb %1, %0" : "+x"(a) : "xm"(b));
  21. return a;
  22. }
  23. #endif
  24. #if !defined(__GNUC__) || defined(__SSE4_1__) || defined(__INTEL_COMPILER)
  25. #include <smmintrin.h>
  26. #else
  27. __inline int __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  28. _mm_extract_epi32 (__m128i a, const int i)
  29. {
  30. int r;
  31. asm ("pextrd %2, %1, %0" : "=rm"(r) : "x"(a), "i"(i));
  32. return r;
  33. }
  34. __inline __m128i __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  35. _mm_insert_epi32 (__m128i a, int b, const int i)
  36. {
  37. asm ("pinsrd %2, %1, %0" : "+x"(a) : "rm"(b), "i"(i));
  38. return a;
  39. }
  40. #endif
  41. #if !defined(__GNUC__) || (defined(__AES__) && defined(__PCLMUL__)) || defined(__INTEL_COMPILER)
  42. #include <wmmintrin.h>
  43. #else
  44. __inline __m128i __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  45. _mm_clmulepi64_si128 (__m128i a, __m128i b, const int i)
  46. {
  47. asm ("pclmulqdq %2, %1, %0" : "+x"(a) : "xm"(b), "i"(i));
  48. return a;
  49. }
  50. __inline __m128i __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  51. _mm_aeskeygenassist_si128 (__m128i a, const int i)
  52. {
  53. __m128i r;
  54. asm ("aeskeygenassist %2, %1, %0" : "=x"(r) : "xm"(a), "i"(i));
  55. return r;
  56. }
  57. __inline __m128i __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  58. _mm_aesimc_si128 (__m128i a)
  59. {
  60. __m128i r;
  61. asm ("aesimc %1, %0" : "=x"(r) : "xm"(a));
  62. return r;
  63. }
  64. __inline __m128i __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  65. _mm_aesenc_si128 (__m128i a, __m128i b)
  66. {
  67. asm ("aesenc %1, %0" : "+x"(a) : "xm"(b));
  68. return a;
  69. }
  70. __inline __m128i __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  71. _mm_aesenclast_si128 (__m128i a, __m128i b)
  72. {
  73. asm ("aesenclast %1, %0" : "+x"(a) : "xm"(b));
  74. return a;
  75. }
  76. __inline __m128i __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  77. _mm_aesdec_si128 (__m128i a, __m128i b)
  78. {
  79. asm ("aesdec %1, %0" : "+x"(a) : "xm"(b));
  80. return a;
  81. }
  82. __inline __m128i __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  83. _mm_aesdeclast_si128 (__m128i a, __m128i b)
  84. {
  85. asm ("aesdeclast %1, %0" : "+x"(a) : "xm"(b));
  86. return a;
  87. }
  88. #endif
  89. #endif
  90. NAMESPACE_BEGIN(CryptoPP)
  91. #if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X64
  92. #define CRYPTOPP_CPUID_AVAILABLE
  93. // these should not be used directly
  94. extern CRYPTOPP_DLL bool g_x86DetectionDone;
  95. extern CRYPTOPP_DLL bool g_hasSSSE3;
  96. extern CRYPTOPP_DLL bool g_hasAESNI;
  97. extern CRYPTOPP_DLL bool g_hasCLMUL;
  98. extern CRYPTOPP_DLL bool g_isP4;
  99. extern CRYPTOPP_DLL word32 g_cacheLineSize;
  100. CRYPTOPP_DLL void CRYPTOPP_API DetectX86Features();
  101. CRYPTOPP_DLL bool CRYPTOPP_API CpuId(word32 input, word32 *output);
  102. #if CRYPTOPP_BOOL_X64
  103. inline bool HasSSE2() {return true;}
  104. inline bool HasISSE() {return true;}
  105. inline bool HasMMX() {return true;}
  106. #else
  107. extern CRYPTOPP_DLL bool g_hasSSE2;
  108. extern CRYPTOPP_DLL bool g_hasISSE;
  109. extern CRYPTOPP_DLL bool g_hasMMX;
  110. inline bool HasSSE2()
  111. {
  112. if (!g_x86DetectionDone)
  113. DetectX86Features();
  114. return g_hasSSE2;
  115. }
  116. inline bool HasISSE()
  117. {
  118. if (!g_x86DetectionDone)
  119. DetectX86Features();
  120. return g_hasISSE;
  121. }
  122. inline bool HasMMX()
  123. {
  124. if (!g_x86DetectionDone)
  125. DetectX86Features();
  126. return g_hasMMX;
  127. }
  128. #endif
  129. inline bool HasSSSE3()
  130. {
  131. if (!g_x86DetectionDone)
  132. DetectX86Features();
  133. return g_hasSSSE3;
  134. }
  135. inline bool HasAESNI()
  136. {
  137. if (!g_x86DetectionDone)
  138. DetectX86Features();
  139. return g_hasAESNI;
  140. }
  141. inline bool HasCLMUL()
  142. {
  143. if (!g_x86DetectionDone)
  144. DetectX86Features();
  145. return g_hasCLMUL;
  146. }
  147. inline bool IsP4()
  148. {
  149. if (!g_x86DetectionDone)
  150. DetectX86Features();
  151. return g_isP4;
  152. }
  153. inline int GetCacheLineSize()
  154. {
  155. if (!g_x86DetectionDone)
  156. DetectX86Features();
  157. return g_cacheLineSize;
  158. }
  159. #else
  160. inline int GetCacheLineSize()
  161. {
  162. return CRYPTOPP_L1_CACHE_LINE_SIZE;
  163. }
  164. #endif
  165. #endif
  166. #ifdef CRYPTOPP_GENERATE_X64_MASM
  167. #define AS1(x) x*newline*
  168. #define AS2(x, y) x, y*newline*
  169. #define AS3(x, y, z) x, y, z*newline*
  170. #define ASS(x, y, a, b, c, d) x, y, a*64+b*16+c*4+d*newline*
  171. #define ASL(x) label##x:*newline*
  172. #define ASJ(x, y, z) x label##y*newline*
  173. #define ASC(x, y) x label##y*newline*
  174. #define AS_HEX(y) 0##y##h
  175. #elif defined(_MSC_VER) || defined(__BORLANDC__)
  176. #define CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY
  177. #define AS1(x) __asm {x}
  178. #define AS2(x, y) __asm {x, y}
  179. #define AS3(x, y, z) __asm {x, y, z}
  180. #define ASS(x, y, a, b, c, d) __asm {x, y, (a)*64+(b)*16+(c)*4+(d)}
  181. #define ASL(x) __asm {label##x:}
  182. #define ASJ(x, y, z) __asm {x label##y}
  183. #define ASC(x, y) __asm {x label##y}
  184. #define CRYPTOPP_NAKED __declspec(naked)
  185. #define AS_HEX(y) 0x##y
  186. #else
  187. #define CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY
  188. // define these in two steps to allow arguments to be expanded
  189. #define GNU_AS1(x) #x ";"
  190. #define GNU_AS2(x, y) #x ", " #y ";"
  191. #define GNU_AS3(x, y, z) #x ", " #y ", " #z ";"
  192. #define GNU_ASL(x) "\n" #x ":"
  193. #define GNU_ASJ(x, y, z) #x " " #y #z ";"
  194. #define AS1(x) GNU_AS1(x)
  195. #define AS2(x, y) GNU_AS2(x, y)
  196. #define AS3(x, y, z) GNU_AS3(x, y, z)
  197. #define ASS(x, y, a, b, c, d) #x ", " #y ", " #a "*64+" #b "*16+" #c "*4+" #d ";"
  198. #define ASL(x) GNU_ASL(x)
  199. #define ASJ(x, y, z) GNU_ASJ(x, y, z)
  200. #define ASC(x, y) #x " " #y ";"
  201. #define CRYPTOPP_NAKED
  202. #define AS_HEX(y) 0x##y
  203. #endif
  204. #define IF0(y)
  205. #define IF1(y) y
  206. #ifdef CRYPTOPP_GENERATE_X64_MASM
  207. #define ASM_MOD(x, y) ((x) MOD (y))
  208. #define XMMWORD_PTR XMMWORD PTR
  209. #else
  210. // GNU assembler doesn't seem to have mod operator
  211. #define ASM_MOD(x, y) ((x)-((x)/(y))*(y))
  212. // GAS 2.15 doesn't support XMMWORD PTR. it seems necessary only for MASM
  213. #define XMMWORD_PTR
  214. #endif
  215. #if CRYPTOPP_BOOL_X86
  216. #define AS_REG_1 ecx
  217. #define AS_REG_2 edx
  218. #define AS_REG_3 esi
  219. #define AS_REG_4 edi
  220. #define AS_REG_5 eax
  221. #define AS_REG_6 ebx
  222. #define AS_REG_7 ebp
  223. #define AS_REG_1d ecx
  224. #define AS_REG_2d edx
  225. #define AS_REG_3d esi
  226. #define AS_REG_4d edi
  227. #define AS_REG_5d eax
  228. #define AS_REG_6d ebx
  229. #define AS_REG_7d ebp
  230. #define WORD_SZ 4
  231. #define WORD_REG(x) e##x
  232. #define WORD_PTR DWORD PTR
  233. #define AS_PUSH_IF86(x) AS1(push e##x)
  234. #define AS_POP_IF86(x) AS1(pop e##x)
  235. #define AS_JCXZ jecxz
  236. #elif CRYPTOPP_BOOL_X64
  237. #ifdef CRYPTOPP_GENERATE_X64_MASM
  238. #define AS_REG_1 rcx
  239. #define AS_REG_2 rdx
  240. #define AS_REG_3 r8
  241. #define AS_REG_4 r9
  242. #define AS_REG_5 rax
  243. #define AS_REG_6 r10
  244. #define AS_REG_7 r11
  245. #define AS_REG_1d ecx
  246. #define AS_REG_2d edx
  247. #define AS_REG_3d r8d
  248. #define AS_REG_4d r9d
  249. #define AS_REG_5d eax
  250. #define AS_REG_6d r10d
  251. #define AS_REG_7d r11d
  252. #else
  253. #define AS_REG_1 rdi
  254. #define AS_REG_2 rsi
  255. #define AS_REG_3 rdx
  256. #define AS_REG_4 rcx
  257. #define AS_REG_5 r8
  258. #define AS_REG_6 r9
  259. #define AS_REG_7 r10
  260. #define AS_REG_1d edi
  261. #define AS_REG_2d esi
  262. #define AS_REG_3d edx
  263. #define AS_REG_4d ecx
  264. #define AS_REG_5d r8d
  265. #define AS_REG_6d r9d
  266. #define AS_REG_7d r10d
  267. #endif
  268. #define WORD_SZ 8
  269. #define WORD_REG(x) r##x
  270. #define WORD_PTR QWORD PTR
  271. #define AS_PUSH_IF86(x)
  272. #define AS_POP_IF86(x)
  273. #define AS_JCXZ jrcxz
  274. #endif
  275. // helper macro for stream cipher output
  276. #define AS_XMM_OUTPUT4(labelPrefix, inputPtr, outputPtr, x0, x1, x2, x3, t, p0, p1, p2, p3, increment)\
  277. AS2( test inputPtr, inputPtr)\
  278. ASC( jz, labelPrefix##3)\
  279. AS2( test inputPtr, 15)\
  280. ASC( jnz, labelPrefix##7)\
  281. AS2( pxor xmm##x0, [inputPtr+p0*16])\
  282. AS2( pxor xmm##x1, [inputPtr+p1*16])\
  283. AS2( pxor xmm##x2, [inputPtr+p2*16])\
  284. AS2( pxor xmm##x3, [inputPtr+p3*16])\
  285. AS2( add inputPtr, increment*16)\
  286. ASC( jmp, labelPrefix##3)\
  287. ASL(labelPrefix##7)\
  288. AS2( movdqu xmm##t, [inputPtr+p0*16])\
  289. AS2( pxor xmm##x0, xmm##t)\
  290. AS2( movdqu xmm##t, [inputPtr+p1*16])\
  291. AS2( pxor xmm##x1, xmm##t)\
  292. AS2( movdqu xmm##t, [inputPtr+p2*16])\
  293. AS2( pxor xmm##x2, xmm##t)\
  294. AS2( movdqu xmm##t, [inputPtr+p3*16])\
  295. AS2( pxor xmm##x3, xmm##t)\
  296. AS2( add inputPtr, increment*16)\
  297. ASL(labelPrefix##3)\
  298. AS2( test outputPtr, 15)\
  299. ASC( jnz, labelPrefix##8)\
  300. AS2( movdqa [outputPtr+p0*16], xmm##x0)\
  301. AS2( movdqa [outputPtr+p1*16], xmm##x1)\
  302. AS2( movdqa [outputPtr+p2*16], xmm##x2)\
  303. AS2( movdqa [outputPtr+p3*16], xmm##x3)\
  304. ASC( jmp, labelPrefix##9)\
  305. ASL(labelPrefix##8)\
  306. AS2( movdqu [outputPtr+p0*16], xmm##x0)\
  307. AS2( movdqu [outputPtr+p1*16], xmm##x1)\
  308. AS2( movdqu [outputPtr+p2*16], xmm##x2)\
  309. AS2( movdqu [outputPtr+p3*16], xmm##x3)\
  310. ASL(labelPrefix##9)\
  311. AS2( add outputPtr, increment*16)
  312. NAMESPACE_END
  313. #endif