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) 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 #ifdef CRYPTOPP_GENERATE_X64_MASM
00150 #define ASM_MOD(x, y) ((x) MOD (y))
00151 #define XMMWORD_PTR XMMWORD PTR
00152 #else
00153
00154 #define ASM_MOD(x, y) ((x)-((x)/(y))*(y))
00155
00156 #define XMMWORD_PTR
00157 #endif
00158
00159 #if CRYPTOPP_BOOL_X86
00160 #define AS_REG_1 ecx
00161 #define AS_REG_2 edx
00162 #define AS_REG_3 esi
00163 #define AS_REG_4 edi
00164 #define AS_REG_5 eax
00165 #define AS_REG_6 ebx
00166 #define AS_REG_7 ebp
00167 #define AS_REG_1d ecx
00168 #define AS_REG_2d edx
00169 #define AS_REG_3d esi
00170 #define AS_REG_4d edi
00171 #define AS_REG_5d eax
00172 #define AS_REG_6d ebx
00173 #define AS_REG_7d ebp
00174 #define WORD_SZ 4
00175 #define WORD_REG(x) e##x
00176 #define WORD_PTR DWORD PTR
00177 #define AS_PUSH_IF86(x) AS1(push e##x)
00178 #define AS_POP_IF86(x) AS1(pop e##x)
00179 #define AS_JCXZ jecxz
00180 #elif CRYPTOPP_BOOL_X64
00181 #ifdef CRYPTOPP_GENERATE_X64_MASM
00182 #define AS_REG_1 rcx
00183 #define AS_REG_2 rdx
00184 #define AS_REG_3 r8
00185 #define AS_REG_4 r9
00186 #define AS_REG_5 rax
00187 #define AS_REG_6 r10
00188 #define AS_REG_7 r11
00189 #define AS_REG_1d ecx
00190 #define AS_REG_2d edx
00191 #define AS_REG_3d r8d
00192 #define AS_REG_4d r9d
00193 #define AS_REG_5d eax
00194 #define AS_REG_6d r10d
00195 #define AS_REG_7d r11d
00196 #else
00197 #define AS_REG_1 rdi
00198 #define AS_REG_2 rsi
00199 #define AS_REG_3 rdx
00200 #define AS_REG_4 rcx
00201 #define AS_REG_5 r8
00202 #define AS_REG_6 r9
00203 #define AS_REG_7 r10
00204 #define AS_REG_1d edi
00205 #define AS_REG_2d esi
00206 #define AS_REG_3d edx
00207 #define AS_REG_4d ecx
00208 #define AS_REG_5d r8d
00209 #define AS_REG_6d r9d
00210 #define AS_REG_7d r10d
00211 #endif
00212 #define WORD_SZ 8
00213 #define WORD_REG(x) r##x
00214 #define WORD_PTR QWORD PTR
00215 #define AS_PUSH_IF86(x)
00216 #define AS_POP_IF86(x)
00217 #define AS_JCXZ jrcxz
00218 #endif
00219
00220
00221 #define AS_XMM_OUTPUT4(labelPrefix, inputPtr, outputPtr, x0, x1, x2, x3, t, p0, p1, p2, p3, increment)\
00222 AS2( test inputPtr, inputPtr)\
00223 ASC( jz, labelPrefix##3)\
00224 AS2( test inputPtr, 15)\
00225 ASC( jnz, labelPrefix##7)\
00226 AS2( pxor xmm##x0, [inputPtr+p0*16])\
00227 AS2( pxor xmm##x1, [inputPtr+p1*16])\
00228 AS2( pxor xmm##x2, [inputPtr+p2*16])\
00229 AS2( pxor xmm##x3, [inputPtr+p3*16])\
00230 AS2( add inputPtr, increment*16)\
00231 ASC( jmp, labelPrefix##3)\
00232 ASL(labelPrefix##7)\
00233 AS2( movdqu xmm##t, [inputPtr+p0*16])\
00234 AS2( pxor xmm##x0, xmm##t)\
00235 AS2( movdqu xmm##t, [inputPtr+p1*16])\
00236 AS2( pxor xmm##x1, xmm##t)\
00237 AS2( movdqu xmm##t, [inputPtr+p2*16])\
00238 AS2( pxor xmm##x2, xmm##t)\
00239 AS2( movdqu xmm##t, [inputPtr+p3*16])\
00240 AS2( pxor xmm##x3, xmm##t)\
00241 AS2( add inputPtr, increment*16)\
00242 ASL(labelPrefix##3)\
00243 AS2( test outputPtr, 15)\
00244 ASC( jnz, labelPrefix##8)\
00245 AS2( movdqa [outputPtr+p0*16], xmm##x0)\
00246 AS2( movdqa [outputPtr+p1*16], xmm##x1)\
00247 AS2( movdqa [outputPtr+p2*16], xmm##x2)\
00248 AS2( movdqa [outputPtr+p3*16], xmm##x3)\
00249 ASC( jmp, labelPrefix##9)\
00250 ASL(labelPrefix##8)\
00251 AS2( movdqu [outputPtr+p0*16], xmm##x0)\
00252 AS2( movdqu [outputPtr+p1*16], xmm##x1)\
00253 AS2( movdqu [outputPtr+p2*16], xmm##x2)\
00254 AS2( movdqu [outputPtr+p3*16], xmm##x3)\
00255 ASL(labelPrefix##9)\
00256 AS2( add outputPtr, increment*16)
00257
00258 NAMESPACE_END
00259
00260 #endif