00001 #ifndef CRYPTOPP_CPU_H
00002 #define CRYPTOPP_CPU_H
00003
00004 #ifdef CRYPTOPP_GENERATE_X64_MASM
00005
00006 #define CRYPTOPP_X86_ASM_AVAILABLE
00007 #define CRYPTOPP_BOOL_X64 1
00008 #define CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE 1
00009 #define NAMESPACE_END
00010
00011 #else
00012
00013 #include "config.h"
00014
00015 #ifdef CRYPTOPP_MSVC6PP_OR_LATER
00016 #include <emmintrin.h>
00017 #endif
00018
00019 NAMESPACE_BEGIN(CryptoPP)
00020
00021 #if defined(CRYPTOPP_X86_ASM_AVAILABLE) || (_MSC_VER >= 1400 && CRYPTOPP_BOOL_X64)
00022
00023 #define CRYPTOPP_CPUID_AVAILABLE
00024
00025
00026 extern CRYPTOPP_DLL bool g_x86DetectionDone;
00027 extern CRYPTOPP_DLL bool g_hasSSE2;
00028 extern CRYPTOPP_DLL bool g_hasISSE;
00029 extern CRYPTOPP_DLL bool g_hasMMX;
00030 extern CRYPTOPP_DLL bool g_hasSSSE3;
00031 extern CRYPTOPP_DLL bool g_isP4;
00032 extern CRYPTOPP_DLL word32 g_cacheLineSize;
00033 CRYPTOPP_DLL void CRYPTOPP_API DetectX86Features();
00034
00035 CRYPTOPP_DLL bool CRYPTOPP_API CpuId(word32 input, word32 *output);
00036
00037 #if CRYPTOPP_BOOL_X64
00038 inline bool HasSSE2() {return true;}
00039 inline bool HasISSE() {return true;}
00040 inline bool HasMMX() {return true;}
00041 #else
00042
00043 inline bool HasSSE2()
00044 {
00045 if (!g_x86DetectionDone)
00046 DetectX86Features();
00047 return g_hasSSE2;
00048 }
00049
00050 inline bool HasISSE()
00051 {
00052 if (!g_x86DetectionDone)
00053 DetectX86Features();
00054 return g_hasISSE;
00055 }
00056
00057 inline bool HasMMX()
00058 {
00059 if (!g_x86DetectionDone)
00060 DetectX86Features();
00061 return g_hasMMX;
00062 }
00063
00064 #endif
00065
00066 inline bool HasSSSE3()
00067 {
00068 if (!g_x86DetectionDone)
00069 DetectX86Features();
00070 return g_hasSSSE3;
00071 }
00072
00073 inline bool IsP4()
00074 {
00075 if (!g_x86DetectionDone)
00076 DetectX86Features();
00077 return g_isP4;
00078 }
00079
00080 inline int GetCacheLineSize()
00081 {
00082 if (!g_x86DetectionDone)
00083 DetectX86Features();
00084 return g_cacheLineSize;
00085 }
00086
00087 #else
00088
00089 inline int GetCacheLineSize()
00090 {
00091 return CRYPTOPP_L1_CACHE_LINE_SIZE;
00092 }
00093
00094 inline bool HasSSSE3() {return false;}
00095 inline bool IsP4() {return false;}
00096
00097
00098 #if CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE || CRYPTOPP_BOOL_X64
00099 inline bool HasSSE2() {return true;}
00100 inline bool HasISSE() {return true;}
00101 inline bool HasMMX() {return true;}
00102 #else
00103 inline bool HasSSE2() {return false;}
00104 inline bool HasISSE() {return false;}
00105 inline bool HasMMX() {return false;}
00106 #endif
00107
00108 #endif // #ifdef CRYPTOPP_X86_ASM_AVAILABLE || _MSC_VER >= 1400
00109
00110 #endif
00111
00112 #ifdef CRYPTOPP_GENERATE_X64_MASM
00113 #define AS1(x) x*newline*
00114 #define AS2(x, y) x, y*newline*
00115 #define AS3(x, y, z) x, y, z*newline*
00116 #define ASS(x, y, a, b, c, d) x, y, a*64+b*16+c*4+d*newline*
00117 #define ASL(x) label##x:*newline*
00118 #define ASJ(x, y, z) x label##y*newline*
00119 #define ASC(x, y) x label##y*newline*
00120 #define AS_HEX(y) 0##y##h
00121 #elif defined(__GNUC__)
00122
00123 #define GNU_AS1(x) #x ";"
00124 #define GNU_AS2(x, y) #x ", " #y ";"
00125 #define GNU_AS3(x, y, z) #x ", " #y ", " #z ";"
00126 #define GNU_ASL(x) "\n" #x ":"
00127 #define GNU_ASJ(x, y, z) #x " " #y #z ";"
00128 #define AS1(x) GNU_AS1(x)
00129 #define AS2(x, y) GNU_AS2(x, y)
00130 #define AS3(x, y, z) GNU_AS3(x, y, z)
00131 #define ASS(x, y, a, b, c, d) #x ", " #y ", " #a "*64+" #b "*16+" #c "*4+" #d ";"
00132 #define ASL(x) GNU_ASL(x)
00133 #define ASJ(x, y, z) GNU_ASJ(x, y, z)
00134 #define ASC(x, y) #x " " #y ";"
00135 #define CRYPTOPP_NAKED
00136 #define AS_HEX(y) 0x##y
00137 #else
00138 #define AS1(x) __asm {x}
00139 #define AS2(x, y) __asm {x, y}
00140 #define AS3(x, y, z) __asm {x, y, z}
00141 #define ASS(x, y, a, b, c, d) __asm {x, y, _MM_SHUFFLE(a, b, c, d)}
00142 #define ASL(x) __asm {label##x:}
00143 #define ASJ(x, y, z) __asm {x label##y}
00144 #define ASC(x, y) __asm {x label##y}
00145 #define CRYPTOPP_NAKED __declspec(naked)
00146 #define AS_HEX(y) 0x##y
00147 #endif
00148
00149 #define IF0(y)
00150 #define IF1(y) y
00151
00152 #ifdef CRYPTOPP_GENERATE_X64_MASM
00153 #define ASM_MOD(x, y) ((x) MOD (y))
00154 #define XMMWORD_PTR XMMWORD PTR
00155 #else
00156
00157 #define ASM_MOD(x, y) ((x)-((x)/(y))*(y))
00158
00159 #define XMMWORD_PTR
00160 #endif
00161
00162 #if CRYPTOPP_BOOL_X86
00163 #define AS_REG_1 ecx
00164 #define AS_REG_2 edx
00165 #define AS_REG_3 esi
00166 #define AS_REG_4 edi
00167 #define AS_REG_5 eax
00168 #define AS_REG_6 ebx
00169 #define AS_REG_7 ebp
00170 #define AS_REG_1d ecx
00171 #define AS_REG_2d edx
00172 #define AS_REG_3d esi
00173 #define AS_REG_4d edi
00174 #define AS_REG_5d eax
00175 #define AS_REG_6d ebx
00176 #define AS_REG_7d ebp
00177 #define WORD_SZ 4
00178 #define WORD_REG(x) e##x
00179 #define WORD_PTR DWORD PTR
00180 #define AS_PUSH_IF86(x) AS1(push e##x)
00181 #define AS_POP_IF86(x) AS1(pop e##x)
00182 #define AS_JCXZ jecxz
00183 #elif CRYPTOPP_BOOL_X64
00184 #ifdef CRYPTOPP_GENERATE_X64_MASM
00185 #define AS_REG_1 rcx
00186 #define AS_REG_2 rdx
00187 #define AS_REG_3 r8
00188 #define AS_REG_4 r9
00189 #define AS_REG_5 rax
00190 #define AS_REG_6 r10
00191 #define AS_REG_7 r11
00192 #define AS_REG_1d ecx
00193 #define AS_REG_2d edx
00194 #define AS_REG_3d r8d
00195 #define AS_REG_4d r9d
00196 #define AS_REG_5d eax
00197 #define AS_REG_6d r10d
00198 #define AS_REG_7d r11d
00199 #else
00200 #define AS_REG_1 rdi
00201 #define AS_REG_2 rsi
00202 #define AS_REG_3 rdx
00203 #define AS_REG_4 rcx
00204 #define AS_REG_5 r8
00205 #define AS_REG_6 r9
00206 #define AS_REG_7 r10
00207 #define AS_REG_1d edi
00208 #define AS_REG_2d esi
00209 #define AS_REG_3d edx
00210 #define AS_REG_4d ecx
00211 #define AS_REG_5d r8d
00212 #define AS_REG_6d r9d
00213 #define AS_REG_7d r10d
00214 #endif
00215 #define WORD_SZ 8
00216 #define WORD_REG(x) r##x
00217 #define WORD_PTR QWORD PTR
00218 #define AS_PUSH_IF86(x)
00219 #define AS_POP_IF86(x)
00220 #define AS_JCXZ jrcxz
00221 #endif
00222
00223
00224 #define AS_XMM_OUTPUT4(labelPrefix, inputPtr, outputPtr, x0, x1, x2, x3, t, p0, p1, p2, p3, increment)\
00225 AS2( test inputPtr, inputPtr)\
00226 ASC( jz, labelPrefix##3)\
00227 AS2( test inputPtr, 15)\
00228 ASC( jnz, labelPrefix##7)\
00229 AS2( pxor xmm##x0, [inputPtr+p0*16])\
00230 AS2( pxor xmm##x1, [inputPtr+p1*16])\
00231 AS2( pxor xmm##x2, [inputPtr+p2*16])\
00232 AS2( pxor xmm##x3, [inputPtr+p3*16])\
00233 AS2( add inputPtr, increment*16)\
00234 ASC( jmp, labelPrefix##3)\
00235 ASL(labelPrefix##7)\
00236 AS2( movdqu xmm##t, [inputPtr+p0*16])\
00237 AS2( pxor xmm##x0, xmm##t)\
00238 AS2( movdqu xmm##t, [inputPtr+p1*16])\
00239 AS2( pxor xmm##x1, xmm##t)\
00240 AS2( movdqu xmm##t, [inputPtr+p2*16])\
00241 AS2( pxor xmm##x2, xmm##t)\
00242 AS2( movdqu xmm##t, [inputPtr+p3*16])\
00243 AS2( pxor xmm##x3, xmm##t)\
00244 AS2( add inputPtr, increment*16)\
00245 ASL(labelPrefix##3)\
00246 AS2( test outputPtr, 15)\
00247 ASC( jnz, labelPrefix##8)\
00248 AS2( movdqa [outputPtr+p0*16], xmm##x0)\
00249 AS2( movdqa [outputPtr+p1*16], xmm##x1)\
00250 AS2( movdqa [outputPtr+p2*16], xmm##x2)\
00251 AS2( movdqa [outputPtr+p3*16], xmm##x3)\
00252 ASC( jmp, labelPrefix##9)\
00253 ASL(labelPrefix##8)\
00254 AS2( movdqu [outputPtr+p0*16], xmm##x0)\
00255 AS2( movdqu [outputPtr+p1*16], xmm##x1)\
00256 AS2( movdqu [outputPtr+p2*16], xmm##x2)\
00257 AS2( movdqu [outputPtr+p3*16], xmm##x3)\
00258 ASL(labelPrefix##9)\
00259 AS2( add outputPtr, increment*16)
00260
00261 NAMESPACE_END
00262
00263 #endif