4 #ifdef CRYPTOPP_GENERATE_X64_MASM
6 #define CRYPTOPP_X86_ASM_AVAILABLE
7 #define CRYPTOPP_BOOL_X64 1
8 #define CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE 1
15 #ifdef CRYPTOPP_MSVC6PP_OR_LATER
16 #include <emmintrin.h>
19 NAMESPACE_BEGIN(CryptoPP
)
21 #if defined(CRYPTOPP_X86_ASM_AVAILABLE) || (_MSC_VER >= 1400 && CRYPTOPP_BOOL_X64)
23 #define CRYPTOPP_CPUID_AVAILABLE
25 // these should not be used directly
26 extern CRYPTOPP_DLL
bool g_x86DetectionDone
;
27 extern CRYPTOPP_DLL
bool g_hasSSE2
;
28 extern CRYPTOPP_DLL
bool g_hasISSE
;
29 extern CRYPTOPP_DLL
bool g_hasMMX
;
30 extern CRYPTOPP_DLL
bool g_hasSSSE3
;
31 extern CRYPTOPP_DLL
bool g_isP4
;
32 extern CRYPTOPP_DLL word32 g_cacheLineSize
;
33 CRYPTOPP_DLL
void CRYPTOPP_API
DetectX86Features();
35 CRYPTOPP_DLL
bool CRYPTOPP_API
CpuId(word32 input
, word32
*output
);
38 inline bool HasSSE2() {return true;}
39 inline bool HasISSE() {return true;}
40 inline bool HasMMX() {return true;}
45 if (!g_x86DetectionDone
)
52 if (!g_x86DetectionDone
)
59 if (!g_x86DetectionDone
)
66 inline bool HasSSSE3()
68 if (!g_x86DetectionDone
)
75 if (!g_x86DetectionDone
)
80 inline int GetCacheLineSize()
82 if (!g_x86DetectionDone
)
84 return g_cacheLineSize
;
89 inline int GetCacheLineSize()
91 return CRYPTOPP_L1_CACHE_LINE_SIZE
;
94 inline bool HasSSSE3() {return false;}
95 inline bool IsP4() {return false;}
97 // assume MMX and SSE2 if intrinsics are enabled
98 #if CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE || CRYPTOPP_BOOL_X64
99 inline bool HasSSE2() {return true;}
100 inline bool HasISSE() {return true;}
101 inline bool HasMMX() {return true;}
103 inline bool HasSSE2() {return false;}
104 inline bool HasISSE() {return false;}
105 inline bool HasMMX() {return false;}
108 #endif // #ifdef CRYPTOPP_X86_ASM_AVAILABLE || _MSC_VER >= 1400
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
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
152 #ifdef CRYPTOPP_GENERATE_X64_MASM
153 #define ASM_MOD(x, y) ((x) MOD (y))
154 #define XMMWORD_PTR XMMWORD PTR
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
162 #if CRYPTOPP_BOOL_X86
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
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
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
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
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
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)\
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)\
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)\
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)\
259 AS2( add outputPtr, increment*16)