6#ifndef EXCEPTION_EXECUTE_HANDLER
7# define EXCEPTION_EXECUTE_HANDLER 1
10#ifndef CRYPTOPP_IMPORTS
17# include <sys/systemcfg.h>
26#if (((__GLIBC__ * 100) + __GLIBC_MINOR__) >= 216)
27# define CRYPTOPP_GETAUXV_AVAILABLE 1
30#if CRYPTOPP_GETAUXV_AVAILABLE
39unsigned long int getauxval(
unsigned long int) {
return 0; }
43# include <sys/utsname.h>
50#if defined(__ANDROID__)
51# include "cpu-features.h"
54#ifdef CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY
60#if defined(_MSC_VER) && _MSC_VER <= 1500 && defined(_M_X64)
61extern "C" unsigned long long __fastcall ExtendedControlRegister(
unsigned int);
64ANONYMOUS_NAMESPACE_BEGIN
67enum {PowerMac=1, Mac, iPhone, iPod, iPad, AppleTV, AppleWatch};
68void GetAppleMachineInfo(
unsigned int& device,
unsigned int& version)
72 struct utsname systemInfo;
73 systemInfo.machine[0] =
'\0';
76 std::string machine(systemInfo.machine);
77 if (machine.find(
"PowerMac") != std::string::npos ||
78 machine.find(
"Power Macintosh") != std::string::npos)
80 else if (machine.find(
"Mac") != std::string::npos ||
81 machine.find(
"Macintosh") != std::string::npos)
83 else if (machine.find(
"iPhone") != std::string::npos)
85 else if (machine.find(
"iPod") != std::string::npos)
87 else if (machine.find(
"iPad") != std::string::npos)
89 else if (machine.find(
"AppleTV") != std::string::npos)
91 else if (machine.find(
"AppleWatch") != std::string::npos)
94 std::string::size_type pos = machine.find_first_of(
"0123456789");
95 if (pos != std::string::npos)
96 version = std::atoi(machine.substr(pos).c_str());
100bool IsAppleMachineARMv8(
unsigned int device,
unsigned int version)
102 if ((device == iPhone && version >= 6) ||
103 (device == iPad && version >= 4))
110bool IsAppleMachineARMv84(
unsigned int device,
unsigned int version)
116ANONYMOUS_NAMESPACE_END
120#ifndef CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY
122 typedef void (*SigHandler)(int);
127 static jmp_buf s_jmpNoCPUID;
128 static void SigIllHandlerCPUID(
int unused)
130 CRYPTOPP_UNUSED(unused);
131 longjmp(s_jmpNoCPUID, 1);
138#if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
140extern bool CPU_ProbeSSE2();
144inline bool CpuId(word32 func, word32 subfunc, word32 output[4])
146 __cpuidex((
int *)output, func, subfunc);
150#elif _MSC_VER >= 1400 && CRYPTOPP_BOOL_X64
152inline bool CpuId(word32 func, word32 subfunc, word32 output[4])
157 __cpuid((
int *)output, func);
165bool CpuId(word32 func, word32 subfunc, word32 output[4])
167#if defined(CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY) || defined(__BORLANDC__)
189 __except (EXCEPTION_EXECUTE_HANDLER)
198 return output[0] != 0;
204 volatile bool result =
true;
206 volatile SigHandler oldHandler = signal(SIGILL, SigIllHandlerCPUID);
207 if (oldHandler == SIG_ERR)
211 volatile sigset_t oldMask;
212 if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask) != 0)
216 if (setjmp(s_jmpNoCPUID))
224# if CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64
225 "pushq %%rbx; cpuid; mov %%ebx, %%edi; popq %%rbx"
227 "push %%ebx; cpuid; mov %%ebx, %%edi; pop %%ebx"
229 :
"=a" (output[0]),
"=D" (output[1]),
"=c" (output[2]),
"=d" (output[3])
230 :
"a" (func),
"c" (subfunc)
236 sigprocmask(SIG_SETMASK, (sigset_t*)&oldMask, NULLPTR);
239 signal(SIGILL, oldHandler);
246bool CRYPTOPP_SECTION_INIT g_x86DetectionDone =
false;
247bool CRYPTOPP_SECTION_INIT g_hasSSE2 =
false;
248bool CRYPTOPP_SECTION_INIT g_hasSSSE3 =
false;
249bool CRYPTOPP_SECTION_INIT g_hasSSE41 =
false;
250bool CRYPTOPP_SECTION_INIT g_hasSSE42 =
false;
251bool CRYPTOPP_SECTION_INIT g_hasAVX =
false;
252bool CRYPTOPP_SECTION_INIT g_hasAVX2 =
false;
253bool CRYPTOPP_SECTION_INIT g_hasAESNI =
false;
254bool CRYPTOPP_SECTION_INIT g_hasCLMUL =
false;
255bool CRYPTOPP_SECTION_INIT g_hasADX =
false;
256bool CRYPTOPP_SECTION_INIT g_hasSHA =
false;
257bool CRYPTOPP_SECTION_INIT g_hasRDRAND =
false;
258bool CRYPTOPP_SECTION_INIT g_hasRDSEED =
false;
259bool CRYPTOPP_SECTION_INIT g_isP4 =
false;
260bool CRYPTOPP_SECTION_INIT g_hasPadlockRNG =
false;
261bool CRYPTOPP_SECTION_INIT g_hasPadlockACE =
false;
262bool CRYPTOPP_SECTION_INIT g_hasPadlockACE2 =
false;
263bool CRYPTOPP_SECTION_INIT g_hasPadlockPHE =
false;
264bool CRYPTOPP_SECTION_INIT g_hasPadlockPMM =
false;
265word32 CRYPTOPP_SECTION_INIT g_cacheLineSize = CRYPTOPP_L1_CACHE_LINE_SIZE;
267static inline bool IsIntel(
const word32 output[4])
270 return (output[1] == 0x756e6547) &&
271 (output[2] == 0x6c65746e) &&
272 (output[3] == 0x49656e69);
275static inline bool IsAMD(
const word32 output[4])
278 return (output[1] == 0x68747541) &&
279 (output[2] == 0x444D4163) &&
280 (output[3] == 0x69746E65);
283static inline bool IsHygon(
const word32 output[4])
286 return (output[1] == 0x6f677948) &&
287 (output[2] == 0x656e6975) &&
288 (output[3] == 0x6e65476e);
291static inline bool IsVIA(
const word32 output[4])
294 return (output[1] == 0x746e6543) &&
295 (output[2] == 0x736c7561) &&
296 (output[3] == 0x48727561);
299void DetectX86Features()
302 word32 cpuid0[4]={0}, cpuid1[4]={0}, cpuid2[4]={0};
303 if (!CpuId(0, 0, cpuid0))
305 if (!CpuId(1, 0, cpuid1))
310 if ((cpuid1[3] & (1 << 26)) != 0)
311 g_hasSSE2 = ((cpuid1[2] & (1 << 27)) != 0) || CPU_ProbeSSE2();
313 g_hasSSSE3 = g_hasSSE2 && ((cpuid1[2] & (1<< 9)) != 0);
314 g_hasSSE41 = g_hasSSE2 && ((cpuid1[2] & (1<<19)) != 0);
315 g_hasSSE42 = g_hasSSE2 && ((cpuid1[2] & (1<<20)) != 0);
316 g_hasAESNI = g_hasSSE2 && ((cpuid1[2] & (1<<25)) != 0);
317 g_hasCLMUL = g_hasSSE2 && ((cpuid1[2] & (1<< 1)) != 0);
321 CRYPTOPP_CONSTANT(YMM_FLAG = (3 << 1))
322 CRYPTOPP_CONSTANT(AVX_FLAG = (3 << 27))
323 if ((cpuid1[2] & AVX_FLAG) == AVX_FLAG)
327#if defined(CRYPTOPP_DISABLE_ASM)
331#elif defined(__GNUC__) || (__SUNPRO_CC >= 0x5100) || defined(__BORLANDC__)
338 ".byte 0x0f, 0x01, 0xd0" "\n\t"
339 :
"=a"(a),
"=d"(d) :
"c"(0) :
"cc"
341 word64 xcr0 = a |
static_cast<word64
>(d) << 32;
342 g_hasAVX = (xcr0 & YMM_FLAG) == YMM_FLAG;
345#elif defined(_MSC_VER) && _MSC_VER <= 1600 && defined(_M_IX86)
361 word64 xcr0 = a |
static_cast<word64
>(d) << 32;
362 g_hasAVX = (xcr0 & YMM_FLAG) == YMM_FLAG;
365#elif defined(_MSC_VER) && _MSC_VER <= 1500 && defined(_M_X64)
366 word64 xcr0 = ExtendedControlRegister(0);
367 g_hasAVX = (xcr0 & YMM_FLAG) == YMM_FLAG;
370#elif defined(__SUNPRO_CC)
375 word64 xcr0 = _xgetbv(0);
376 g_hasAVX = (xcr0 & YMM_FLAG) == YMM_FLAG;
382 CRYPTOPP_CONSTANT(RDRAND_FLAG = (1 << 30))
383 CRYPTOPP_CONSTANT(RDSEED_FLAG = (1 << 18))
384 CRYPTOPP_CONSTANT( ADX_FLAG = (1 << 19))
385 CRYPTOPP_CONSTANT( SHA_FLAG = (1 << 29))
386 CRYPTOPP_CONSTANT( AVX2_FLAG = (1 << 5))
388 g_isP4 = ((cpuid1[0] >> 8) & 0xf) == 0xf;
389 g_cacheLineSize = 8 * GETBYTE(cpuid1[1], 1);
390 g_hasRDRAND = (cpuid1[2] & RDRAND_FLAG) != 0;
394 if (CpuId(7, 0, cpuid2))
396 g_hasRDSEED = (cpuid2[1] & RDSEED_FLAG) != 0;
397 g_hasADX = (cpuid2[1] & ADX_FLAG) != 0;
398 g_hasSHA = (cpuid2[1] & SHA_FLAG) != 0;
399 g_hasAVX2 = (cpuid2[1] & AVX2_FLAG) != 0;
403 else if (IsAMD(cpuid0) || IsHygon(cpuid0))
405 CRYPTOPP_CONSTANT(RDRAND_FLAG = (1 << 30))
406 CRYPTOPP_CONSTANT(RDSEED_FLAG = (1 << 18))
407 CRYPTOPP_CONSTANT( ADX_FLAG = (1 << 19))
408 CRYPTOPP_CONSTANT( SHA_FLAG = (1 << 29))
409 CRYPTOPP_CONSTANT( AVX2_FLAG = (1 << 5))
411 CpuId(0x80000005, 0, cpuid2);
412 g_cacheLineSize = GETBYTE(cpuid2[2], 0);
413 g_hasRDRAND = (cpuid1[2] & RDRAND_FLAG) != 0;
417 if (CpuId(7, 0, cpuid2))
419 g_hasRDSEED = (cpuid2[1] & RDSEED_FLAG) != 0;
420 g_hasADX = (cpuid2[1] & ADX_FLAG) != 0;
421 g_hasSHA = (cpuid2[1] & SHA_FLAG) != 0;
422 g_hasAVX2 = (cpuid2[1] & AVX2_FLAG) != 0;
426 else if (IsVIA(cpuid0))
428 CRYPTOPP_CONSTANT( RNG_FLAGS = (0x3 << 2))
429 CRYPTOPP_CONSTANT( ACE_FLAGS = (0x3 << 6))
430 CRYPTOPP_CONSTANT(ACE2_FLAGS = (0x3 << 8))
431 CRYPTOPP_CONSTANT( PHE_FLAGS = (0x3 << 10))
432 CRYPTOPP_CONSTANT( PMM_FLAGS = (0x3 << 12))
434 CpuId(0xC0000000, 0, cpuid2);
435 if (cpuid2[0] >= 0xC0000001)
438 CpuId(0xC0000001, 0, cpuid2);
439 g_hasPadlockRNG = (cpuid2[3] & RNG_FLAGS) == RNG_FLAGS;
440 g_hasPadlockACE = (cpuid2[3] & ACE_FLAGS) == ACE_FLAGS;
441 g_hasPadlockACE2 = (cpuid2[3] & ACE2_FLAGS) == ACE2_FLAGS;
442 g_hasPadlockPHE = (cpuid2[3] & PHE_FLAGS) == PHE_FLAGS;
443 g_hasPadlockPMM = (cpuid2[3] & PMM_FLAGS) == PMM_FLAGS;
447 if (g_cacheLineSize == 0)
448 g_cacheLineSize = CRYPTOPP_L1_CACHE_LINE_SIZE;
450 *
const_cast<volatile bool*
>(&g_x86DetectionDone) =
true;
455#elif (CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARMV8)
457bool CRYPTOPP_SECTION_INIT g_ArmDetectionDone =
false;
458bool CRYPTOPP_SECTION_INIT g_hasARMv7 =
false;
459bool CRYPTOPP_SECTION_INIT g_hasNEON =
false;
460bool CRYPTOPP_SECTION_INIT g_hasPMULL =
false;
461bool CRYPTOPP_SECTION_INIT g_hasCRC32 =
false;
462bool CRYPTOPP_SECTION_INIT g_hasAES =
false;
463bool CRYPTOPP_SECTION_INIT g_hasSHA1 =
false;
464bool CRYPTOPP_SECTION_INIT g_hasSHA2 =
false;
465bool CRYPTOPP_SECTION_INIT g_hasSHA512 =
false;
466bool CRYPTOPP_SECTION_INIT g_hasSHA3 =
false;
467bool CRYPTOPP_SECTION_INIT g_hasSM3 =
false;
468bool CRYPTOPP_SECTION_INIT g_hasSM4 =
false;
469word32 CRYPTOPP_SECTION_INIT g_cacheLineSize = CRYPTOPP_L1_CACHE_LINE_SIZE;
484extern bool CPU_ProbeARMv7();
485extern bool CPU_ProbeNEON();
486extern bool CPU_ProbeCRC32();
487extern bool CPU_ProbeAES();
488extern bool CPU_ProbeSHA1();
489extern bool CPU_ProbeSHA2();
490extern bool CPU_ProbeSHA512();
491extern bool CPU_ProbeSHA3();
492extern bool CPU_ProbeSM3();
493extern bool CPU_ProbeSM4();
494extern bool CPU_ProbePMULL();
498# define HWCAP_ARMv7 (1 << 29)
501# define HWCAP_ASIMD (1 << 1)
503#ifndef HWCAP_ARM_NEON
504# define HWCAP_ARM_NEON 4096
507# define HWCAP_CRC32 (1 << 7)
510# define HWCAP2_CRC32 (1 << 4)
513# define HWCAP_PMULL (1 << 4)
516# define HWCAP2_PMULL (1 << 1)
519# define HWCAP_AES (1 << 3)
522# define HWCAP2_AES (1 << 0)
525# define HWCAP_SHA1 (1 << 5)
528# define HWCAP_SHA2 (1 << 6)
531# define HWCAP2_SHA1 (1 << 2)
534# define HWCAP2_SHA2 (1 << 3)
537# define HWCAP_SHA3 (1 << 17)
540# define HWCAP_SM3 (1 << 18)
543# define HWCAP_SM4 (1 << 19)
546# define HWCAP_SHA512 (1 << 21)
549inline bool CPU_QueryARMv7()
551#if defined(__aarch32__) || defined(__aarch64__)
554#elif defined(__ANDROID__) && defined(__arm__)
555 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM) != 0) &&
556 ((android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_ARMv7) != 0))
558#elif defined(__linux__) && defined(__arm__)
559 if ((getauxval(AT_HWCAP) & HWCAP_ARMv7) != 0)
561#elif defined(__APPLE__) && defined(__arm__)
568inline bool CPU_QueryNEON()
570#if defined(__ANDROID__) && defined(__aarch64__)
571 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM64) != 0) &&
572 ((android_getCpuFeatures() & ANDROID_CPU_ARM64_FEATURE_ASIMD) != 0))
574#elif defined(__ANDROID__) && defined(__arm__)
575 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM) != 0) &&
576 ((android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_NEON) != 0))
578#elif defined(__linux__) && defined(__aarch64__)
579 if ((getauxval(AT_HWCAP) & HWCAP_ASIMD) != 0)
581#elif defined(__linux__) && defined(__aarch32__)
582 if ((getauxval(AT_HWCAP2) & HWCAP2_ASIMD) != 0)
584#elif defined(__linux__) && defined(__arm__)
585 if ((getauxval(AT_HWCAP) & HWCAP_ARM_NEON) != 0)
587#elif defined(__APPLE__) && defined(__aarch64__)
594inline bool CPU_QueryCRC32()
596#if defined(__ANDROID__) && defined(__aarch64__)
597 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM64) != 0) &&
598 ((android_getCpuFeatures() & ANDROID_CPU_ARM64_FEATURE_CRC32) != 0))
600#elif defined(__ANDROID__) && defined(__aarch32__)
601 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM) != 0) &&
602 ((android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_CRC32) != 0))
604#elif defined(__linux__) && defined(__aarch64__)
605 if ((getauxval(AT_HWCAP) & HWCAP_CRC32) != 0)
607#elif defined(__linux__) && defined(__aarch32__)
608 if ((getauxval(AT_HWCAP2) & HWCAP2_CRC32) != 0)
610#elif defined(__APPLE__) && defined(__aarch64__)
617inline bool CPU_QueryPMULL()
619#if defined(__ANDROID__) && defined(__aarch64__)
620 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM64) != 0) &&
621 ((android_getCpuFeatures() & ANDROID_CPU_ARM64_FEATURE_PMULL) != 0))
623#elif defined(__ANDROID__) && defined(__aarch32__)
624 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM) != 0) &&
625 ((android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_PMULL) != 0))
627#elif defined(__linux__) && defined(__aarch64__)
628 if ((getauxval(AT_HWCAP) & HWCAP_PMULL) != 0)
630#elif defined(__linux__) && defined(__aarch32__)
631 if ((getauxval(AT_HWCAP2) & HWCAP2_PMULL) != 0)
633#elif defined(__APPLE__) && defined(__aarch64__)
640inline bool CPU_QueryAES()
642#if defined(__ANDROID__) && defined(__aarch64__)
643 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM64) != 0) &&
644 ((android_getCpuFeatures() & ANDROID_CPU_ARM64_FEATURE_AES) != 0))
646#elif defined(__ANDROID__) && defined(__aarch32__)
647 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM) != 0) &&
648 ((android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_AES) != 0))
650#elif defined(__linux__) && defined(__aarch64__)
651 if ((getauxval(AT_HWCAP) & HWCAP_AES) != 0)
653#elif defined(__linux__) && defined(__aarch32__)
654 if ((getauxval(AT_HWCAP2) & HWCAP2_AES) != 0)
656#elif defined(__APPLE__) && defined(__aarch64__)
657 unsigned int device, version;
658 GetAppleMachineInfo(device, version);
659 return IsAppleMachineARMv8(device, version);
664inline bool CPU_QuerySHA1()
666#if defined(__ANDROID__) && defined(__aarch64__)
667 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM64) != 0) &&
668 ((android_getCpuFeatures() & ANDROID_CPU_ARM64_FEATURE_SHA1) != 0))
670#elif defined(__ANDROID__) && defined(__aarch32__)
671 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM) != 0) &&
672 ((android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_SHA1) != 0))
674#elif defined(__linux__) && defined(__aarch64__)
675 if ((getauxval(AT_HWCAP) & HWCAP_SHA1) != 0)
677#elif defined(__linux__) && defined(__aarch32__)
678 if ((getauxval(AT_HWCAP2) & HWCAP2_SHA1) != 0)
680#elif defined(__APPLE__) && defined(__aarch64__)
681 unsigned int device, version;
682 GetAppleMachineInfo(device, version);
683 return IsAppleMachineARMv8(device, version);
688inline bool CPU_QuerySHA2()
690#if defined(__ANDROID__) && defined(__aarch64__)
691 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM64) != 0) &&
692 ((android_getCpuFeatures() & ANDROID_CPU_ARM64_FEATURE_SHA2) != 0))
694#elif defined(__ANDROID__) && defined(__aarch32__)
695 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM) != 0) &&
696 ((android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_SHA2) != 0))
698#elif defined(__linux__) && defined(__aarch64__)
699 if ((getauxval(AT_HWCAP) & HWCAP_SHA2) != 0)
701#elif defined(__linux__) && defined(__aarch32__)
702 if ((getauxval(AT_HWCAP2) & HWCAP2_SHA2) != 0)
704#elif defined(__APPLE__) && defined(__aarch64__)
705 unsigned int device, version;
706 GetAppleMachineInfo(device, version);
707 return IsAppleMachineARMv8(device, version);
712inline bool CPU_QuerySHA512()
715#if defined(__ANDROID__) && defined(__aarch64__) && 0
716 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM64) != 0) &&
717 ((android_getCpuFeatures() & ANDROID_CPU_ARM64_FEATURE_SHA512) != 0))
719#elif defined(__ANDROID__) && defined(__aarch32__) && 0
720 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM) != 0) &&
721 ((android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_SHA512) != 0))
723#elif defined(__linux__) && defined(__aarch64__)
724 if ((getauxval(AT_HWCAP) & HWCAP_SHA512) != 0)
726#elif defined(__linux__) && defined(__aarch32__)
727 if ((getauxval(AT_HWCAP2) & HWCAP2_SHA512) != 0)
729#elif defined(__APPLE__) && defined(__aarch64__) && 0
730 unsigned int device, version;
731 GetAppleMachineInfo(device, version);
732 return IsAppleMachineARMv84(device, version);
737inline bool CPU_QuerySHA3()
740#if defined(__ANDROID__) && defined(__aarch64__) && 0
741 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM64) != 0) &&
742 ((android_getCpuFeatures() & ANDROID_CPU_ARM64_FEATURE_SHA3) != 0))
744#elif defined(__ANDROID__) && defined(__aarch32__) && 0
745 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM) != 0) &&
746 ((android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_SHA3) != 0))
748#elif defined(__linux__) && defined(__aarch64__)
749 if ((getauxval(AT_HWCAP) & HWCAP_SHA3) != 0)
751#elif defined(__linux__) && defined(__aarch32__)
752 if ((getauxval(AT_HWCAP2) & HWCAP2_SHA3) != 0)
754#elif defined(__APPLE__) && defined(__aarch64__) && 0
755 unsigned int device, version;
756 GetAppleMachineInfo(device, version);
757 return IsAppleMachineARMv84(device, version);
762inline bool CPU_QuerySM3()
765#if defined(__ANDROID__) && defined(__aarch64__) && 0
766 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM64) != 0) &&
767 ((android_getCpuFeatures() & ANDROID_CPU_ARM64_FEATURE_SM3) != 0))
769#elif defined(__ANDROID__) && defined(__aarch32__) && 0
770 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM) != 0) &&
771 ((android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_SM3) != 0))
773#elif defined(__linux__) && defined(__aarch64__)
774 if ((getauxval(AT_HWCAP) & HWCAP_SM3) != 0)
776#elif defined(__linux__) && defined(__aarch32__)
777 if ((getauxval(AT_HWCAP2) & HWCAP2_SM3) != 0)
779#elif defined(__APPLE__) && defined(__aarch64__) && 0
780 unsigned int device, version;
781 GetAppleMachineInfo(device, version);
782 return IsAppleMachineARMv84(device, version);
787inline bool CPU_QuerySM4()
790#if defined(__ANDROID__) && defined(__aarch64__) && 0
791 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM64) != 0) &&
792 ((android_getCpuFeatures() & ANDROID_CPU_ARM64_FEATURE_SM4) != 0))
794#elif defined(__ANDROID__) && defined(__aarch32__) && 0
795 if (((android_getCpuFamily() & ANDROID_CPU_FAMILY_ARM) != 0) &&
796 ((android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_SM4) != 0))
798#elif defined(__linux__) && defined(__aarch64__)
799 if ((getauxval(AT_HWCAP) & HWCAP_SM4) != 0)
801#elif defined(__linux__) && defined(__aarch32__)
802 if ((getauxval(AT_HWCAP2) & HWCAP2_SM4) != 0)
804#elif defined(__APPLE__) && defined(__aarch64__) && 0
805 unsigned int device, version;
806 GetAppleMachineInfo(device, version);
807 return IsAppleMachineARMv84(device, version);
812void DetectArmFeatures()
816 g_hasARMv7 = CPU_QueryARMv7() || CPU_ProbeARMv7();
817 g_hasNEON = CPU_QueryNEON() || CPU_ProbeNEON();
818 g_hasCRC32 = CPU_QueryCRC32() || CPU_ProbeCRC32();
819 g_hasPMULL = CPU_QueryPMULL() || CPU_ProbePMULL();
820 g_hasAES = CPU_QueryAES() || CPU_ProbeAES();
821 g_hasSHA1 = CPU_QuerySHA1() || CPU_ProbeSHA1();
822 g_hasSHA2 = CPU_QuerySHA2() || CPU_ProbeSHA2();
823 g_hasSHA512 = CPU_QuerySHA512();
824 g_hasSHA3 = CPU_QuerySHA3();
825 g_hasSM3 = CPU_QuerySM3();
826 g_hasSM4 = CPU_QuerySM4();
828#if defined(__linux__) && defined(_SC_LEVEL1_DCACHE_LINESIZE)
831 int cacheLineSize = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
832 if (cacheLineSize > 0)
833 g_cacheLineSize = cacheLineSize;
836 *
const_cast<volatile bool*
>(&g_ArmDetectionDone) =
true;
841#elif (CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64)
843bool CRYPTOPP_SECTION_INIT g_PowerpcDetectionDone =
false;
844bool CRYPTOPP_SECTION_INIT g_hasAltivec =
false;
845bool CRYPTOPP_SECTION_INIT g_hasPower7 =
false;
846bool CRYPTOPP_SECTION_INIT g_hasPower8 =
false;
847bool CRYPTOPP_SECTION_INIT g_hasPower9 =
false;
848bool CRYPTOPP_SECTION_INIT g_hasAES =
false;
849bool CRYPTOPP_SECTION_INIT g_hasPMULL =
false;
850bool CRYPTOPP_SECTION_INIT g_hasSHA256 =
false;
851bool CRYPTOPP_SECTION_INIT g_hasSHA512 =
false;
852bool CRYPTOPP_SECTION_INIT g_hasDARN =
false;
853word32 CRYPTOPP_SECTION_INIT g_cacheLineSize = CRYPTOPP_L1_CACHE_LINE_SIZE;
855extern bool CPU_ProbeAltivec();
856extern bool CPU_ProbePower7();
857extern bool CPU_ProbePower8();
858extern bool CPU_ProbePower9();
859extern bool CPU_ProbeAES();
860extern bool CPU_ProbePMULL();
861extern bool CPU_ProbeSHA256();
862extern bool CPU_ProbeSHA512();
863extern bool CPU_ProbeDARN();
867#ifndef PPC_FEATURE_HAS_ALTIVEC
868# define PPC_FEATURE_HAS_ALTIVEC 0x10000000
870#ifndef PPC_FEATURE_ARCH_2_06
871# define PPC_FEATURE_ARCH_2_06 0x00000100
873#ifndef PPC_FEATURE2_ARCH_2_07
874# define PPC_FEATURE2_ARCH_2_07 0x80000000
876#ifndef PPC_FEATURE2_ARCH_3_00
877# define PPC_FEATURE2_ARCH_3_00 0x00800000
879#ifndef PPC_FEATURE2_VEC_CRYPTO
880# define PPC_FEATURE2_VEC_CRYPTO 0x02000000
887#ifndef __power_6_andup
888# define __power_6_andup() __power_set(0xffffffffU<<14)
890#ifndef __power_7_andup
891# define __power_7_andup() __power_set(0xffffffffU<<15)
893#ifndef __power_8_andup
894# define __power_8_andup() __power_set(0xffffffffU<<16)
896#ifndef __power_9_andup
897# define __power_9_andup() __power_set(0xffffffffU<<17)
902inline bool CPU_QueryAltivec()
904#if defined(__linux__)
905 if ((getauxval(AT_HWCAP) & PPC_FEATURE_HAS_ALTIVEC) != 0)
908 if (__power_6_andup() != 0)
910#elif defined(__APPLE__) && defined(__POWERPC__)
911 unsigned int device, version;
912 GetAppleMachineInfo(device, version);
913 return device == PowerMac;
918inline bool CPU_QueryPower7()
921#if defined(__linux__)
922 if ((getauxval(AT_HWCAP) & PPC_FEATURE_ARCH_2_06) != 0)
925 if (__power_7_andup() != 0)
931inline bool CPU_QueryPower8()
934#if defined(__linux__)
935 if ((getauxval(AT_HWCAP2) & PPC_FEATURE2_ARCH_2_07) != 0)
938 if (__power_8_andup() != 0)
944inline bool CPU_QueryPower9()
947#if defined(__linux__)
948 if ((getauxval(AT_HWCAP2) & PPC_FEATURE2_ARCH_3_00) != 0)
951 if (__power_9_andup() != 0)
957inline bool CPU_QueryAES()
961#if defined(__linux__)
962 if ((getauxval(AT_HWCAP2) & PPC_FEATURE2_VEC_CRYPTO) != 0)
965 if (__power_8_andup() != 0)
971inline bool CPU_QueryPMULL()
975#if defined(__linux__)
976 if ((getauxval(AT_HWCAP2) & PPC_FEATURE2_VEC_CRYPTO) != 0)
979 if (__power_8_andup() != 0)
985inline bool CPU_QuerySHA256()
989#if defined(__linux__)
990 if ((getauxval(AT_HWCAP2) & PPC_FEATURE2_VEC_CRYPTO) != 0)
993 if (__power_8_andup() != 0)
998inline bool CPU_QuerySHA512()
1002#if defined(__linux__)
1003 if ((getauxval(AT_HWCAP2) & PPC_FEATURE2_VEC_CRYPTO) != 0)
1006 if (__power_8_andup() != 0)
1013inline bool CPU_QueryDARN()
1016#if defined(__linux__)
1017 if ((getauxval(AT_HWCAP2) & PPC_FEATURE2_ARCH_3_00) != 0)
1020 if (__power_9_andup() != 0)
1026void DetectPowerpcFeatures()
1030 g_hasAltivec = CPU_QueryAltivec() || CPU_ProbeAltivec();
1031 g_hasPower7 = CPU_QueryPower7() || CPU_ProbePower7();
1032 g_hasPower8 = CPU_QueryPower8() || CPU_ProbePower8();
1033 g_hasPower9 = CPU_QueryPower9() || CPU_ProbePower9();
1034 g_hasPMULL = CPU_QueryPMULL() || CPU_ProbePMULL();
1035 g_hasAES = CPU_QueryAES() || CPU_ProbeAES();
1036 g_hasSHA256 = CPU_QuerySHA256() || CPU_ProbeSHA256();
1037 g_hasSHA512 = CPU_QuerySHA512() || CPU_ProbeSHA512();
1038 g_hasDARN = CPU_QueryDARN() || CPU_ProbeDARN();
1040#if defined(_AIX) && defined(SC_L1C_DLS)
1042 int cacheLineSize = getsystemcfg(SC_L1C_DLS);
1043 if (cacheLineSize > 0)
1044 g_cacheLineSize = cacheLineSize;
1045#elif defined(__linux__) && defined(_SC_LEVEL1_DCACHE_LINESIZE)
1048 int cacheLineSize = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
1049 if (cacheLineSize > 0)
1050 g_cacheLineSize = cacheLineSize;
1053 *
const_cast<volatile bool*
>(&g_PowerpcDetectionDone) =
true;
1061ANONYMOUS_NAMESPACE_BEGIN
1068#if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64
1069 CryptoPP::DetectX86Features();
1070#elif CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARMV8
1071 CryptoPP::DetectArmFeatures();
1072#elif CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64
1073 CryptoPP::DetectPowerpcFeatures();
1082#if HAVE_GCC_INIT_PRIORITY
1083 const InitCpu s_init __attribute__ ((init_priority (CRYPTOPP_INIT_PRIORITY + 10))) = InitCpu();
1084#elif HAVE_MSC_INIT_PRIORITY
1085 #pragma warning(disable: 4075)
1086 #pragma init_seg(".CRT$XCU")
1087 const InitCpu s_init;
1088 #pragma warning(default: 4075)
1089#elif HAVE_XLC_INIT_PRIORITY
1091 #pragma priority(270)
1092 const InitCpu s_init;
1094 const InitCpu s_init;
1097ANONYMOUS_NAMESPACE_END
Library configuration file.
Functions for CPU features and intrinsics.
Utility functions for the Crypto++ library.
Crypto++ library namespace.