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