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(CRYPTOPP_ARM_ASIMD_AVAILABLE)
474 #elif defined(CRYPTOPP_ARM_NEON_AVAILABLE)
475 if (!g_ArmDetectionDone)
493 inline bool HasCRC32()
495 #if defined(CRYPTOPP_ARM_CRC32_AVAILABLE)
496 if (!g_ArmDetectionDone)
515 #if defined(CRYPTOPP_ARM_AES_AVAILABLE)
516 if (!g_ArmDetectionDone)
533 inline bool HasPMULL()
535 #if defined(CRYPTOPP_ARM_PMULL_AVAILABLE)
536 if (!g_ArmDetectionDone)
553 inline bool HasSHA1()
555 #if defined(CRYPTOPP_ARM_SHA1_AVAILABLE)
556 if (!g_ArmDetectionDone)
573 inline bool HasSHA2()
575 #if defined(CRYPTOPP_ARM_SHA2_AVAILABLE)
576 if (!g_ArmDetectionDone)
593 inline bool HasSHA3()
595 #if defined(CRYPTOPP_ARM_SHA3_AVAILABLE)
596 if (!g_ArmDetectionDone)
613 inline bool HasSHA512()
615 #if defined(CRYPTOPP_ARM_SHA512_AVAILABLE)
616 if (!g_ArmDetectionDone)
635 #if defined(CRYPTOPP_ARM_SM3_AVAILABLE)
636 if (!g_ArmDetectionDone)
655 #if defined(CRYPTOPP_ARM_SM4_AVAILABLE)
656 if (!g_ArmDetectionDone)
666 #endif // CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARMV8
670 #if CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64 || CRYPTOPP_DOXYGEN_PROCESSING
673 #ifndef CRYPTOPP_DOXYGEN_PROCESSING
674 extern bool g_PowerpcDetectionDone;
675 extern bool g_hasAltivec;
676 extern bool g_hasPower7;
677 extern bool g_hasPower8;
678 extern bool g_hasPower9;
679 extern bool g_hasAES;
680 extern bool g_hasPMULL;
681 extern bool g_hasSHA256;
682 extern bool g_hasSHA512;
683 extern bool g_hasDARN;
684 extern word32 g_cacheLineSize;
686 #endif // CRYPTOPP_DOXYGEN_PROCESSING
698 inline bool HasAltivec()
700 #if CRYPTOPP_ALTIVEC_AVAILABLE
701 if (!g_PowerpcDetectionDone)
702 DetectPowerpcFeatures();
715 inline bool HasPower7()
717 #if CRYPTOPP_POWER7_AVAILABLE
718 if (!g_PowerpcDetectionDone)
719 DetectPowerpcFeatures();
732 inline bool HasPower8()
734 #if CRYPTOPP_POWER8_AVAILABLE
735 if (!g_PowerpcDetectionDone)
736 DetectPowerpcFeatures();
749 inline bool HasPower9()
751 #if CRYPTOPP_POWER9_AVAILABLE
752 if (!g_PowerpcDetectionDone)
753 DetectPowerpcFeatures();
769 #if CRYPTOPP_POWER8_AES_AVAILABLE
770 if (!g_PowerpcDetectionDone)
771 DetectPowerpcFeatures();
785 inline bool HasPMULL()
787 #if CRYPTOPP_POWER8_VMULL_AVAILABLE
788 if (!g_PowerpcDetectionDone)
789 DetectPowerpcFeatures();
803 inline bool HasSHA256()
805 #if CRYPTOPP_POWER8_SHA_AVAILABLE
806 if (!g_PowerpcDetectionDone)
807 DetectPowerpcFeatures();
821 inline bool HasSHA512()
823 #if CRYPTOPP_POWER8_SHA_AVAILABLE
824 if (!g_PowerpcDetectionDone)
825 DetectPowerpcFeatures();
838 inline bool HasDARN()
840 #if CRYPTOPP_POWER9_AVAILABLE
841 if (!g_PowerpcDetectionDone)
842 DetectPowerpcFeatures();
844 # if defined(__ibmxl__) && defined(__linux__)
862 inline int GetCacheLineSize()
864 if (!g_PowerpcDetectionDone)
865 DetectPowerpcFeatures();
866 return g_cacheLineSize;
871 #endif // CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64
876 #if !(CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64)
885 inline int GetCacheLineSize()
889 #endif // Non-Intel systems
891 #endif // CRYPTOPP_GENERATE_X64_MASM
895 #ifndef CRYPTOPP_DOXYGEN_PROCESSING
897 #if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64
899 #ifdef CRYPTOPP_GENERATE_X64_MASM
900 #define AS1(x) x*newline*
901 #define AS2(x, y) x, y*newline*
902 #define AS3(x, y, z) x, y, z*newline*
903 #define ASS(x, y, a, b, c, d) x, y, a*64+b*16+c*4+d*newline*
904 #define ASL(x) label##x:*newline*
905 #define ASJ(x, y, z) x label##y*newline*
906 #define ASC(x, y) x label##y*newline*
907 #define AS_HEX(y) 0##y##h
908 #elif defined(_MSC_VER) || defined(__BORLANDC__)
909 #define AS1(x) __asm {x}
910 #define AS2(x, y) __asm {x, y}
911 #define AS3(x, y, z) __asm {x, y, z}
912 #define ASS(x, y, a, b, c, d) __asm {x, y, (a)*64+(b)*16+(c)*4+(d)}
913 #define ASL(x) __asm {label##x:}
914 #define ASJ(x, y, z) __asm {x label##y}
915 #define ASC(x, y) __asm {x label##y}
916 #define CRYPTOPP_NAKED __declspec(naked)
917 #define AS_HEX(y) 0x##y
920 #define GNU_AS1(x) #x ";" NEW_LINE
921 #define GNU_AS2(x, y) #x ", " #y ";" NEW_LINE
922 #define GNU_AS3(x, y, z) #x ", " #y ", " #z ";" NEW_LINE
923 #define GNU_ASL(x) "\n" #x ":" NEW_LINE
925 #if (CRYPTOPP_LLVM_CLANG_VERSION >= 50000) || (CRYPTOPP_APPLE_CLANG_VERSION >= 90000)
926 #define GNU_ASJ(x, y, z) ATT_PREFIX ";" NEW_LINE #x " " #y #z ";" NEW_LINE INTEL_PREFIX ";" NEW_LINE
928 #define GNU_ASJ(x, y, z) #x " " #y #z ";" NEW_LINE
930 #define AS1(x) GNU_AS1(x)
931 #define AS2(x, y) GNU_AS2(x, y)
932 #define AS3(x, y, z) GNU_AS3(x, y, z)
933 #define ASS(x, y, a, b, c, d) #x ", " #y ", " #a "*64+" #b "*16+" #c "*4+" #d ";"
934 #define ASL(x) GNU_ASL(x)
935 #define ASJ(x, y, z) GNU_ASJ(x, y, z)
936 #define ASC(x, y) #x " " #y ";"
937 #define CRYPTOPP_NAKED
938 #define AS_HEX(y) 0x##y
944 #ifdef CRYPTOPP_GENERATE_X64_MASM
945 #define ASM_MOD(x, y) ((x) MOD (y))
946 #define XMMWORD_PTR XMMWORD PTR
949 #define ASM_MOD(x, y) ((x)-((x)/(y))*(y))
954 #if CRYPTOPP_BOOL_X86
962 #define AS_REG_1d ecx
963 #define AS_REG_2d edx
964 #define AS_REG_3d esi
965 #define AS_REG_4d edi
966 #define AS_REG_5d eax
967 #define AS_REG_6d ebx
968 #define AS_REG_7d ebp
970 #define WORD_REG(x) e##x
971 #define WORD_PTR DWORD PTR
972 #define AS_PUSH_IF86(x) AS1(push e##x)
973 #define AS_POP_IF86(x) AS1(pop e##x)
974 #define AS_JCXZ jecxz
975 #elif CRYPTOPP_BOOL_X32
981 #define AS_REG_6 r10d
982 #define AS_REG_7 r11d
983 #define AS_REG_1d ecx
984 #define AS_REG_2d edx
985 #define AS_REG_3d r8d
986 #define AS_REG_4d r9d
987 #define AS_REG_5d eax
988 #define AS_REG_6d r10d
989 #define AS_REG_7d r11d
991 #define WORD_REG(x) e##x
992 #define WORD_PTR DWORD PTR
993 #define AS_PUSH_IF86(x) AS1(push r##x)
994 #define AS_POP_IF86(x) AS1(pop r##x)
995 #define AS_JCXZ jecxz
996 #elif CRYPTOPP_BOOL_X64
997 #ifdef CRYPTOPP_GENERATE_X64_MASM
1002 #define AS_REG_5 rax
1003 #define AS_REG_6 r10
1004 #define AS_REG_7 r11
1005 #define AS_REG_1d ecx
1006 #define AS_REG_2d edx
1007 #define AS_REG_3d r8d
1008 #define AS_REG_4d r9d
1009 #define AS_REG_5d eax
1010 #define AS_REG_6d r10d
1011 #define AS_REG_7d r11d
1013 #define AS_REG_1 rdi
1014 #define AS_REG_2 rsi
1015 #define AS_REG_3 rdx
1016 #define AS_REG_4 rcx
1019 #define AS_REG_7 r10
1020 #define AS_REG_1d edi
1021 #define AS_REG_2d esi
1022 #define AS_REG_3d edx
1023 #define AS_REG_4d ecx
1024 #define AS_REG_5d r8d
1025 #define AS_REG_6d r9d
1026 #define AS_REG_7d r10d
1029 #define WORD_REG(x) r##x
1030 #define WORD_PTR QWORD PTR
1031 #define AS_PUSH_IF86(x)
1032 #define AS_POP_IF86(x)
1033 #define AS_JCXZ jrcxz
1037 #define AS_XMM_OUTPUT4(labelPrefix, inputPtr, outputPtr, x0, x1, x2, x3, t, p0, p1, p2, p3, increment)\
1038 AS2( test inputPtr, inputPtr)\
1039 ASC( jz, labelPrefix##3)\
1040 AS2( test inputPtr, 15)\
1041 ASC( jnz, labelPrefix##7)\
1042 AS2( pxor xmm##x0, [inputPtr+p0*16])\
1043 AS2( pxor xmm##x1, [inputPtr+p1*16])\
1044 AS2( pxor xmm##x2, [inputPtr+p2*16])\
1045 AS2( pxor xmm##x3, [inputPtr+p3*16])\
1046 AS2( add inputPtr, increment*16)\
1047 ASC( jmp, labelPrefix##3)\
1048 ASL(labelPrefix##7)\
1049 AS2( movdqu xmm##t, [inputPtr+p0*16])\
1050 AS2( pxor xmm##x0, xmm##t)\
1051 AS2( movdqu xmm##t, [inputPtr+p1*16])\
1052 AS2( pxor xmm##x1, xmm##t)\
1053 AS2( movdqu xmm##t, [inputPtr+p2*16])\
1054 AS2( pxor xmm##x2, xmm##t)\
1055 AS2( movdqu xmm##t, [inputPtr+p3*16])\
1056 AS2( pxor xmm##x3, xmm##t)\
1057 AS2( add inputPtr, increment*16)\
1058 ASL(labelPrefix##3)\
1059 AS2( test outputPtr, 15)\
1060 ASC( jnz, labelPrefix##8)\
1061 AS2( movdqa [outputPtr+p0*16], xmm##x0)\
1062 AS2( movdqa [outputPtr+p1*16], xmm##x1)\
1063 AS2( movdqa [outputPtr+p2*16], xmm##x2)\
1064 AS2( movdqa [outputPtr+p3*16], xmm##x3)\
1065 ASC( jmp, labelPrefix##9)\
1066 ASL(labelPrefix##8)\
1067 AS2( movdqu [outputPtr+p0*16], xmm##x0)\
1068 AS2( movdqu [outputPtr+p1*16], xmm##x1)\
1069 AS2( movdqu [outputPtr+p2*16], xmm##x2)\
1070 AS2( movdqu [outputPtr+p3*16], xmm##x3)\
1071 ASL(labelPrefix##9)\
1072 AS2( add outputPtr, increment*16)
1074 #endif // CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64
1076 #endif // Not CRYPTOPP_DOXYGEN_PROCESSING
1081 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
1082 # pragma GCC diagnostic pop
1085 #endif // CRYPTOPP_CPU_H