30 #ifndef CRYPTOPP_CPU_H
31 #define CRYPTOPP_CPU_H
36 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
37 # pragma GCC diagnostic push
38 # pragma GCC diagnostic ignored "-Wconversion"
39 # pragma GCC diagnostic ignored "-Wsign-conversion"
43 #if defined(CRYPTOPP_LLVM_CLANG_VERSION) || defined(CRYPTOPP_APPLE_CLANG_VERSION)
45 #define INTEL_PREFIX ".intel_syntax;"
46 #define INTEL_NOPREFIX ".intel_syntax;"
47 #define ATT_PREFIX ".att_syntax;"
48 #define ATT_NOPREFIX ".att_syntax;"
49 #elif defined(__GNUC__)
51 #define INTEL_PREFIX ".intel_syntax prefix;"
52 #define INTEL_NOPREFIX ".intel_syntax noprefix;"
53 #define ATT_PREFIX ".att_syntax prefix;"
54 #define ATT_NOPREFIX ".att_syntax noprefix;"
58 #define INTEL_NOPREFIX
63 #ifdef CRYPTOPP_GENERATE_X64_MASM
65 #define CRYPTOPP_X86_ASM_AVAILABLE
66 #define CRYPTOPP_BOOL_X64 1
67 #define CRYPTOPP_SSE2_ASM_AVAILABLE 1
76 #if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64 || CRYPTOPP_DOXYGEN_PROCESSING
78 #define CRYPTOPP_CPUID_AVAILABLE 1
81 #ifndef CRYPTOPP_DOXYGEN_PROCESSING
83 extern CRYPTOPP_DLL
bool g_x86DetectionDone;
84 extern CRYPTOPP_DLL
bool g_hasSSE2;
85 extern CRYPTOPP_DLL
bool g_hasSSSE3;
86 extern CRYPTOPP_DLL
bool g_hasSSE41;
87 extern CRYPTOPP_DLL
bool g_hasSSE42;
88 extern CRYPTOPP_DLL
bool g_hasMOVBE;
89 extern CRYPTOPP_DLL
bool g_hasAESNI;
90 extern CRYPTOPP_DLL
bool g_hasCLMUL;
91 extern CRYPTOPP_DLL
bool g_hasAVX;
92 extern CRYPTOPP_DLL
bool g_hasAVX2;
93 extern CRYPTOPP_DLL
bool g_hasSHA;
94 extern CRYPTOPP_DLL
bool g_hasADX;
95 extern CRYPTOPP_DLL
bool g_isP4;
96 extern CRYPTOPP_DLL
bool g_hasRDRAND;
97 extern CRYPTOPP_DLL
bool g_hasRDSEED;
98 extern CRYPTOPP_DLL
bool g_hasPadlockRNG;
99 extern CRYPTOPP_DLL
bool g_hasPadlockACE;
100 extern CRYPTOPP_DLL
bool g_hasPadlockACE2;
101 extern CRYPTOPP_DLL
bool g_hasPadlockPHE;
102 extern CRYPTOPP_DLL
bool g_hasPadlockPMM;
103 extern CRYPTOPP_DLL
word32 g_cacheLineSize;
107 #endif // CRYPTOPP_DOXYGEN_PROCESSING
118 inline bool HasSSE2()
120 #if (CRYPTOPP_SSE2_ASM_AVAILABLE || CRYPTOPP_SSE2_INTRIN_AVAILABLE)
121 if (!g_x86DetectionDone)
133 inline bool HasSSSE3()
135 #if CRYPTOPP_SSSE3_AVAILABLE
136 if (!g_x86DetectionDone)
148 inline bool HasSSE41()
150 #if CRYPTOPP_SSE41_AVAILABLE
151 if (!g_x86DetectionDone)
163 inline bool HasSSE42()
165 #if CRYPTOPP_SSE42_AVAILABLE
166 if (!g_x86DetectionDone)
179 inline bool HasMOVBE()
181 #if CRYPTOPP_SSE42_AVAILABLE
182 if (!g_x86DetectionDone)
195 inline bool HasAESNI()
197 #if CRYPTOPP_AESNI_AVAILABLE
198 if (!g_x86DetectionDone)
211 inline bool HasCLMUL()
213 #if CRYPTOPP_CLMUL_AVAILABLE
214 if (!g_x86DetectionDone)
229 #if CRYPTOPP_SHANI_AVAILABLE
230 if (!g_x86DetectionDone)
245 #if CRYPTOPP_ADX_AVAILABLE
246 if (!g_x86DetectionDone)
261 #if CRYPTOPP_AVX_AVAILABLE
262 if (!g_x86DetectionDone)
275 inline bool HasAVX2()
277 #if CRYPTOPP_AVX2_AVAILABLE
278 if (!g_x86DetectionDone)
290 inline bool HasRDRAND()
292 #if CRYPTOPP_RDRAND_AVAILABLE
293 if (!g_x86DetectionDone)
305 inline bool HasRDSEED()
307 #if CRYPTOPP_RDSEED_AVAILABLE
308 if (!g_x86DetectionDone)
320 inline bool HasPadlockRNG()
322 #if CRYPTOPP_PADLOCK_RNG_AVAILABLE
323 if (!g_x86DetectionDone)
325 return g_hasPadlockRNG;
335 inline bool HasPadlockACE()
337 #if CRYPTOPP_PADLOCK_ACE_AVAILABLE
338 if (!g_x86DetectionDone)
340 return g_hasPadlockACE;
350 inline bool HasPadlockACE2()
352 #if CRYPTOPP_PADLOCK_ACE2_AVAILABLE
353 if (!g_x86DetectionDone)
355 return g_hasPadlockACE2;
365 inline bool HasPadlockPHE()
367 #if CRYPTOPP_PADLOCK_PHE_AVAILABLE
368 if (!g_x86DetectionDone)
370 return g_hasPadlockPHE;
380 inline bool HasPadlockPMM()
382 #if CRYPTOPP_PADLOCK_PMM_AVAILABLE
383 if (!g_x86DetectionDone)
385 return g_hasPadlockPMM;
397 if (!g_x86DetectionDone)
410 inline int GetCacheLineSize()
412 if (!g_x86DetectionDone)
414 return g_cacheLineSize;
418 #endif // CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64
422 #if CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARMV8 || CRYPTOPP_DOXYGEN_PROCESSING
425 #ifndef CRYPTOPP_DOXYGEN_PROCESSING
426 extern bool g_ArmDetectionDone;
427 extern bool g_hasARMv7;
428 extern bool g_hasNEON;
429 extern bool g_hasPMULL;
430 extern bool g_hasCRC32;
431 extern bool g_hasAES;
432 extern bool g_hasSHA1;
433 extern bool g_hasSHA2;
434 extern bool g_hasSHA512;
435 extern bool g_hasSHA3;
436 extern bool g_hasSM3;
437 extern bool g_hasSM4;
439 #endif // CRYPTOPP_DOXYGEN_PROCESSING
449 inline bool HasARMv7()
452 #if defined(__aarch32__) || defined(__aarch64__)
455 if (!g_ArmDetectionDone)
469 inline bool HasNEON()
472 #if defined(__aarch32__) || defined(__aarch64__)
475 if (!g_ArmDetectionDone)
490 inline bool HasPMULL()
492 #if defined(__aarch32__) || defined(__aarch64__)
493 if (!g_ArmDetectionDone)
511 inline bool HasCRC32()
513 #if defined(__aarch32__) || defined(__aarch64__)
514 if (!g_ArmDetectionDone)
533 #if defined(__aarch32__) || defined(__aarch64__)
534 if (!g_ArmDetectionDone)
551 inline bool HasSHA1()
553 #if defined(__aarch32__) || defined(__aarch64__)
554 if (!g_ArmDetectionDone)
571 inline bool HasSHA2()
573 #if defined(__aarch32__) || defined(__aarch64__)
574 if (!g_ArmDetectionDone)
591 inline bool HasSHA512()
593 #if defined(__aarch32__) || defined(__aarch64__)
594 if (!g_ArmDetectionDone)
611 inline bool HasSHA3()
613 #if defined(__aarch32__) || defined(__aarch64__)
614 if (!g_ArmDetectionDone)
633 #if defined(__aarch32__) || defined(__aarch64__)
634 if (!g_ArmDetectionDone)
653 #if defined(__aarch32__) || defined(__aarch64__)
654 if (!g_ArmDetectionDone)
664 #endif // CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARMV8
668 #if CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64 || CRYPTOPP_DOXYGEN_PROCESSING
671 #ifndef CRYPTOPP_DOXYGEN_PROCESSING
672 extern bool g_PowerpcDetectionDone;
673 extern bool g_hasAltivec;
674 extern bool g_hasPower7;
675 extern bool g_hasPower8;
676 extern bool g_hasPower9;
677 extern bool g_hasAES;
678 extern bool g_hasPMULL;
679 extern bool g_hasSHA256;
680 extern bool g_hasSHA512;
681 extern bool g_hasDARN;
682 extern word32 g_cacheLineSize;
684 #endif // CRYPTOPP_DOXYGEN_PROCESSING
696 inline bool HasAltivec()
698 #if CRYPTOPP_ALTIVEC_AVAILABLE
699 if (!g_PowerpcDetectionDone)
700 DetectPowerpcFeatures();
713 inline bool HasPower7()
715 #if CRYPTOPP_POWER7_AVAILABLE
716 if (!g_PowerpcDetectionDone)
717 DetectPowerpcFeatures();
730 inline bool HasPower8()
732 #if CRYPTOPP_POWER8_AVAILABLE
733 if (!g_PowerpcDetectionDone)
734 DetectPowerpcFeatures();
747 inline bool HasPower9()
749 #if CRYPTOPP_POWER9_AVAILABLE
750 if (!g_PowerpcDetectionDone)
751 DetectPowerpcFeatures();
767 #if CRYPTOPP_POWER8_AES_AVAILABLE
768 if (!g_PowerpcDetectionDone)
769 DetectPowerpcFeatures();
783 inline bool HasPMULL()
785 #if CRYPTOPP_POWER8_VMULL_AVAILABLE
786 if (!g_PowerpcDetectionDone)
787 DetectPowerpcFeatures();
801 inline bool HasSHA256()
803 #if CRYPTOPP_POWER8_SHA_AVAILABLE
804 if (!g_PowerpcDetectionDone)
805 DetectPowerpcFeatures();
819 inline bool HasSHA512()
821 #if CRYPTOPP_POWER8_SHA_AVAILABLE
822 if (!g_PowerpcDetectionDone)
823 DetectPowerpcFeatures();
836 inline bool HasDARN()
838 #if CRYPTOPP_POWER9_AVAILABLE
839 if (!g_PowerpcDetectionDone)
840 DetectPowerpcFeatures();
842 # if defined(__ibmxl__) && defined(__linux__)
860 inline int GetCacheLineSize()
862 if (!g_PowerpcDetectionDone)
863 DetectPowerpcFeatures();
864 return g_cacheLineSize;
869 #endif // CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64
874 #if !(CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64)
883 inline int GetCacheLineSize()
887 #endif // Non-Intel systems
889 #endif // CRYPTOPP_GENERATE_X64_MASM
893 #ifndef CRYPTOPP_DOXYGEN_PROCESSING
895 #if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64
897 #ifdef CRYPTOPP_GENERATE_X64_MASM
898 #define AS1(x) x*newline*
899 #define AS2(x, y) x, y*newline*
900 #define AS3(x, y, z) x, y, z*newline*
901 #define ASS(x, y, a, b, c, d) x, y, a*64+b*16+c*4+d*newline*
902 #define ASL(x) label##x:*newline*
903 #define ASJ(x, y, z) x label##y*newline*
904 #define ASC(x, y) x label##y*newline*
905 #define AS_HEX(y) 0##y##h
906 #elif defined(_MSC_VER) || defined(__BORLANDC__)
907 #define AS1(x) __asm {x}
908 #define AS2(x, y) __asm {x, y}
909 #define AS3(x, y, z) __asm {x, y, z}
910 #define ASS(x, y, a, b, c, d) __asm {x, y, (a)*64+(b)*16+(c)*4+(d)}
911 #define ASL(x) __asm {label##x:}
912 #define ASJ(x, y, z) __asm {x label##y}
913 #define ASC(x, y) __asm {x label##y}
914 #define CRYPTOPP_NAKED __declspec(naked)
915 #define AS_HEX(y) 0x##y
918 #define GNU_AS1(x) #x ";" NEW_LINE
919 #define GNU_AS2(x, y) #x ", " #y ";" NEW_LINE
920 #define GNU_AS3(x, y, z) #x ", " #y ", " #z ";" NEW_LINE
921 #define GNU_ASL(x) "\n" #x ":" NEW_LINE
923 #if (CRYPTOPP_LLVM_CLANG_VERSION >= 50000) || (CRYPTOPP_APPLE_CLANG_VERSION >= 90000)
924 #define GNU_ASJ(x, y, z) ATT_PREFIX ";" NEW_LINE #x " " #y #z ";" NEW_LINE INTEL_PREFIX ";" NEW_LINE
926 #define GNU_ASJ(x, y, z) #x " " #y #z ";" NEW_LINE
928 #define AS1(x) GNU_AS1(x)
929 #define AS2(x, y) GNU_AS2(x, y)
930 #define AS3(x, y, z) GNU_AS3(x, y, z)
931 #define ASS(x, y, a, b, c, d) #x ", " #y ", " #a "*64+" #b "*16+" #c "*4+" #d ";"
932 #define ASL(x) GNU_ASL(x)
933 #define ASJ(x, y, z) GNU_ASJ(x, y, z)
934 #define ASC(x, y) #x " " #y ";"
935 #define CRYPTOPP_NAKED
936 #define AS_HEX(y) 0x##y
942 #ifdef CRYPTOPP_GENERATE_X64_MASM
943 #define ASM_MOD(x, y) ((x) MOD (y))
944 #define XMMWORD_PTR XMMWORD PTR
947 #define ASM_MOD(x, y) ((x)-((x)/(y))*(y))
952 #if CRYPTOPP_BOOL_X86
960 #define AS_REG_1d ecx
961 #define AS_REG_2d edx
962 #define AS_REG_3d esi
963 #define AS_REG_4d edi
964 #define AS_REG_5d eax
965 #define AS_REG_6d ebx
966 #define AS_REG_7d ebp
968 #define WORD_REG(x) e##x
969 #define WORD_PTR DWORD PTR
970 #define AS_PUSH_IF86(x) AS1(push e##x)
971 #define AS_POP_IF86(x) AS1(pop e##x)
972 #define AS_JCXZ jecxz
973 #elif CRYPTOPP_BOOL_X32
979 #define AS_REG_6 r10d
980 #define AS_REG_7 r11d
981 #define AS_REG_1d ecx
982 #define AS_REG_2d edx
983 #define AS_REG_3d r8d
984 #define AS_REG_4d r9d
985 #define AS_REG_5d eax
986 #define AS_REG_6d r10d
987 #define AS_REG_7d r11d
989 #define WORD_REG(x) e##x
990 #define WORD_PTR DWORD PTR
991 #define AS_PUSH_IF86(x) AS1(push r##x)
992 #define AS_POP_IF86(x) AS1(pop r##x)
993 #define AS_JCXZ jecxz
994 #elif CRYPTOPP_BOOL_X64
995 #ifdef CRYPTOPP_GENERATE_X64_MASM
1000 #define AS_REG_5 rax
1001 #define AS_REG_6 r10
1002 #define AS_REG_7 r11
1003 #define AS_REG_1d ecx
1004 #define AS_REG_2d edx
1005 #define AS_REG_3d r8d
1006 #define AS_REG_4d r9d
1007 #define AS_REG_5d eax
1008 #define AS_REG_6d r10d
1009 #define AS_REG_7d r11d
1011 #define AS_REG_1 rdi
1012 #define AS_REG_2 rsi
1013 #define AS_REG_3 rdx
1014 #define AS_REG_4 rcx
1017 #define AS_REG_7 r10
1018 #define AS_REG_1d edi
1019 #define AS_REG_2d esi
1020 #define AS_REG_3d edx
1021 #define AS_REG_4d ecx
1022 #define AS_REG_5d r8d
1023 #define AS_REG_6d r9d
1024 #define AS_REG_7d r10d
1027 #define WORD_REG(x) r##x
1028 #define WORD_PTR QWORD PTR
1029 #define AS_PUSH_IF86(x)
1030 #define AS_POP_IF86(x)
1031 #define AS_JCXZ jrcxz
1035 #define AS_XMM_OUTPUT4(labelPrefix, inputPtr, outputPtr, x0, x1, x2, x3, t, p0, p1, p2, p3, increment)\
1036 AS2( test inputPtr, inputPtr)\
1037 ASC( jz, labelPrefix##3)\
1038 AS2( test inputPtr, 15)\
1039 ASC( jnz, labelPrefix##7)\
1040 AS2( pxor xmm##x0, [inputPtr+p0*16])\
1041 AS2( pxor xmm##x1, [inputPtr+p1*16])\
1042 AS2( pxor xmm##x2, [inputPtr+p2*16])\
1043 AS2( pxor xmm##x3, [inputPtr+p3*16])\
1044 AS2( add inputPtr, increment*16)\
1045 ASC( jmp, labelPrefix##3)\
1046 ASL(labelPrefix##7)\
1047 AS2( movdqu xmm##t, [inputPtr+p0*16])\
1048 AS2( pxor xmm##x0, xmm##t)\
1049 AS2( movdqu xmm##t, [inputPtr+p1*16])\
1050 AS2( pxor xmm##x1, xmm##t)\
1051 AS2( movdqu xmm##t, [inputPtr+p2*16])\
1052 AS2( pxor xmm##x2, xmm##t)\
1053 AS2( movdqu xmm##t, [inputPtr+p3*16])\
1054 AS2( pxor xmm##x3, xmm##t)\
1055 AS2( add inputPtr, increment*16)\
1056 ASL(labelPrefix##3)\
1057 AS2( test outputPtr, 15)\
1058 ASC( jnz, labelPrefix##8)\
1059 AS2( movdqa [outputPtr+p0*16], xmm##x0)\
1060 AS2( movdqa [outputPtr+p1*16], xmm##x1)\
1061 AS2( movdqa [outputPtr+p2*16], xmm##x2)\
1062 AS2( movdqa [outputPtr+p3*16], xmm##x3)\
1063 ASC( jmp, labelPrefix##9)\
1064 ASL(labelPrefix##8)\
1065 AS2( movdqu [outputPtr+p0*16], xmm##x0)\
1066 AS2( movdqu [outputPtr+p1*16], xmm##x1)\
1067 AS2( movdqu [outputPtr+p2*16], xmm##x2)\
1068 AS2( movdqu [outputPtr+p3*16], xmm##x3)\
1069 ASL(labelPrefix##9)\
1070 AS2( add outputPtr, increment*16)
1072 #endif // CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64
1074 #endif // Not CRYPTOPP_DOXYGEN_PROCESSING
1079 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
1080 # pragma GCC diagnostic pop
1083 #endif // CRYPTOPP_CPU_H