2222
2323// For IsProcessorFeaturePresent on Microsoft Arm64 platforms,
2424// https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-isprocessorfeaturepresent
25- #if defined(_WIN32) && defined(_M_ARM64)
25+ #if defined(_WIN32) && ( defined(_M_ARM64) || defined(_M_ARM64EC) )
2626# include < Windows.h>
2727# include < processthreadsapi.h>
2828#endif
@@ -147,6 +147,14 @@ inline bool IsVIA(const word32 output[4])
147147 (output[3 ] /* EDX*/ == 0x32414956 ));
148148}
149149
150+ inline bool IsZhaoxin (const word32 output[4 ])
151+ {
152+ // This is the " Shanghai " string.
153+ return ((output[1 ] /* EBX*/ == 0x68532020 ) &&
154+ (output[2 ] /* ECX*/ == 0x20206961 ) &&
155+ (output[3 ] /* EDX*/ == 0x68676E61 ));
156+ }
157+
150158#endif // X86, X32 and X64
151159
152160#if defined(__APPLE__)
@@ -694,15 +702,34 @@ void DetectX86Features()
694702 }
695703 }
696704 }
697- else if (IsVIA (cpuid0))
705+ else if (IsVIA (cpuid0) || IsZhaoxin (cpuid0) )
698706 {
707+ CRYPTOPP_CONSTANT (RDRAND_FLAG = (1 << 30 ));
708+ CRYPTOPP_CONSTANT (RDSEED_FLAG = (1 << 18 ));
709+ CRYPTOPP_CONSTANT (ADX_FLAG = (1 << 19 ));
710+ CRYPTOPP_CONSTANT (SHA_FLAG = (1 << 29 ));
711+ CRYPTOPP_CONSTANT (AVX2_FLAG = (1 << 5 ));
712+
699713 // Two bits: available and enabled
700714 CRYPTOPP_CONSTANT ( RNG_FLAGS = (0x3 << 2 ));
701715 CRYPTOPP_CONSTANT ( ACE_FLAGS = (0x3 << 6 ));
702716 CRYPTOPP_CONSTANT (ACE2_FLAGS = (0x3 << 8 ));
703717 CRYPTOPP_CONSTANT ( PHE_FLAGS = (0x3 << 10 ));
704718 CRYPTOPP_CONSTANT ( PMM_FLAGS = (0x3 << 12 ));
705719
720+ g_hasRDRAND = (cpuid1[ECX_REG] & RDRAND_FLAG) != 0 ;
721+
722+ if (cpuid0[EAX_REG] >= 7 )
723+ {
724+ if (CpuId (7 , 0 , cpuid2))
725+ {
726+ g_hasRDSEED = (cpuid2[EBX_REG] & RDSEED_FLAG) != 0 ;
727+ g_hasADX = (cpuid2[EBX_REG] & ADX_FLAG) != 0 ;
728+ g_hasSHA = (cpuid2[EBX_REG] & SHA_FLAG) != 0 ;
729+ g_hasAVX2 = (cpuid2[EBX_REG] & AVX2_FLAG) != 0 ;
730+ }
731+ }
732+
706733 CpuId (0xC0000000 , 0 , cpuid2);
707734 word32 extendedFeatures = cpuid2[0 ];
708735
@@ -716,9 +743,10 @@ void DetectX86Features()
716743 g_hasPadlockPMM = (cpuid2[EDX_REG] & PMM_FLAGS) != 0 ;
717744 }
718745
719- if (extendedFeatures >= 0xC0000005 )
720- {
721- CpuId (0xC0000005 , 0 , cpuid2);
746+ CpuId (0x80000000 , 0 , cpuid2);
747+ extendedFeatures = cpuid2[EAX_REG];
748+ if (extendedFeatures >= 0x80000005 ) {
749+ CpuId (0x80000005 , 0 , cpuid2);
722750 g_cacheLineSize = GETBYTE (cpuid2[ECX_REG], 0 );
723751 }
724752 }
@@ -846,19 +874,24 @@ inline bool CPU_QueryARMv7()
846874#if defined(__ANDROID__) && defined(__arm__)
847875 if (((android_getCpuFamily () & ANDROID_CPU_FAMILY_ARM) != 0 ) &&
848876 ((android_getCpuFeatures () & ANDROID_CPU_ARM_FEATURE_ARMv7) != 0 ))
877+ # define LOCALRET true
849878 return true ;
850879#elif defined(__linux__) && defined(__arm__)
851880 if ((getauxval (AT_HWCAP) & HWCAP_ARMv7) != 0 ||
852881 (getauxval (AT_HWCAP) & HWCAP_NEON) != 0 )
853- return true ;
882+ # define LOCALRET true
854883#elif defined(__APPLE__) && defined(__arm__)
855884 // Apple hardware is ARMv7 or above.
856885 return true ;
857- #elif defined(_WIN32) && defined(_M_ARM64)
886+ #elif defined(_WIN32) && ( defined(_M_ARM64) || defined(_M_ARM64EC) )
858887 // Windows 10 ARM64 is only supported on Armv8a and above
859- return true ;
888+ # define LOCALRET true
860889#endif
861- return false ;
890+ #ifndef LOCALRET
891+ # define LOCALRET false
892+ #endif
893+ return LOCALRET;
894+ #undef LOCALRET
862895}
863896
864897inline bool CPU_QueryNEON ()
@@ -884,7 +917,7 @@ inline bool CPU_QueryNEON()
884917 // Core feature set for Aarch32 and Aarch64.
885918 if (IsAppleMachineARMv8 ())
886919 return true ;
887- #elif defined(_WIN32) && defined(_M_ARM64)
920+ #elif defined(_WIN32) && ( defined(_M_ARM64) || defined(_M_ARM64EC) )
888921 // Windows 10 ARM64 is only supported on Armv8a and above
889922 if (IsProcessorFeaturePresent (PF_ARM_V8_INSTRUCTIONS_AVAILABLE) != 0 )
890923 return true ;
@@ -912,7 +945,7 @@ inline bool CPU_QueryCRC32()
912945 // M1 processor
913946 if (IsAppleMachineARMv82 ())
914947 return true ;
915- #elif defined(_WIN32) && defined(_M_ARM64)
948+ #elif defined(_WIN32) && ( defined(_M_ARM64) || defined(_M_ARM64EC) )
916949 if (IsProcessorFeaturePresent (PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE) != 0 )
917950 return true ;
918951#endif
@@ -939,7 +972,7 @@ inline bool CPU_QueryPMULL()
939972 // M1 processor
940973 if (IsAppleMachineARMv82 ())
941974 return true ;
942- #elif defined(_WIN32) && defined(_M_ARM64)
975+ #elif defined(_WIN32) && ( defined(_M_ARM64) || defined(_M_ARM64EC) )
943976 if (IsProcessorFeaturePresent (PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE) != 0 )
944977 return true ;
945978#endif
@@ -966,7 +999,7 @@ inline bool CPU_QueryAES()
966999 // M1 processor
9671000 if (IsAppleMachineARMv82 ())
9681001 return true ;
969- #elif defined(_WIN32) && defined(_M_ARM64)
1002+ #elif defined(_WIN32) && ( defined(_M_ARM64) || defined(_M_ARM64EC) )
9701003 if (IsProcessorFeaturePresent (PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE) != 0 )
9711004 return true ;
9721005#endif
@@ -993,7 +1026,7 @@ inline bool CPU_QuerySHA1()
9931026 // M1 processor
9941027 if (IsAppleMachineARMv82 ())
9951028 return true ;
996- #elif defined(_WIN32) && defined(_M_ARM64)
1029+ #elif defined(_WIN32) && ( defined(_M_ARM64) || defined(_M_ARM64EC) )
9971030 if (IsProcessorFeaturePresent (PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE) != 0 )
9981031 return true ;
9991032#endif
@@ -1020,7 +1053,7 @@ inline bool CPU_QuerySHA256()
10201053 // M1 processor
10211054 if (IsAppleMachineARMv82 ())
10221055 return true ;
1023- #elif defined(_WIN32) && defined(_M_ARM64)
1056+ #elif defined(_WIN32) && ( defined(_M_ARM64) || defined(_M_ARM64EC) )
10241057 if (IsProcessorFeaturePresent (PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE) != 0 )
10251058 return true ;
10261059#endif
0 commit comments