From 8b237f5381cecb92a8890498f32f9948b5352beb Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Fri, 5 Mar 2021 09:15:52 -0500 Subject: Update cpu.cpp features for Apple M1 --- cpu.cpp | 59 ++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 32 insertions(+), 27 deletions(-) (limited to 'cpu.cpp') diff --git a/cpu.cpp b/cpu.cpp index aec57731..c6f460a8 100644 --- a/cpu.cpp +++ b/cpu.cpp @@ -48,6 +48,7 @@ unsigned long int getauxval(unsigned long int) { return 0; } #if defined(__APPLE__) # include +# include #endif // The cpu-features header and source file are located in @@ -140,7 +141,7 @@ class AppleMachineInfo { public: enum { PowerMac=1, Mac, iPhone, iPod, iPad, AppleTV, AppleWatch }; - enum { PowerPC=1, I386, I686, X86_64, ARM32, ARMV8, ARMV82, ARMV83, ARMV84 }; + enum { PowerPC=1, I386, I686, X86_64, ARM32, ARMV8, ARMV82, ARMV83 }; AppleMachineInfo() : m_device(0), m_version(0), m_arch(0) { @@ -210,9 +211,36 @@ public: } else if (machine.find("arm64") != std::string::npos) { - // M1 machine. - m_device = Mac; - m_arch = ARMV82; + // M1 machine? + std::string brand; + size_t size = 0; + + if (sysctlbyname("machdep.cpu.brand_string", NULL, &size, NULL, 0) == 0 && size > 0) + { + brand.resize(size); + if (sysctlbyname("machdep.cpu.brand_string", &brand[0], &size, NULL, 0) == 0 && size > 0) + { + if (brand[size-1] == '\0') + size--; + brand.resize(size); + } + } + + if (brand == "Apple M1") + { + m_device = Mac; + m_arch = ARMV82; + } + else + { + // ??? + m_device = 0; + m_arch = ARMV8; + } + } + else + { + CRYPTOPP_ASSERT(0); } } @@ -244,10 +272,6 @@ public: return m_arch >= ARMV83; } - bool IsARMv84() const { - return m_arch >= ARMV84; - } - private: unsigned int m_device, m_version, m_arch; }; @@ -310,17 +334,6 @@ inline bool IsAppleMachineARMv83() return arch >= AppleMachineInfo::ARMV83; } -inline bool IsAppleMachineARMv84() -{ - static unsigned int arch; - if (arch == 0) - { - unsigned int unused; - GetAppleMachineInfo(unused, unused, arch); - } - return arch >= AppleMachineInfo::ARMV84; -} - #endif // __APPLE__ ANONYMOUS_NAMESPACE_END @@ -1040,10 +1053,6 @@ inline bool CPU_QuerySM3() if ((getauxval(AT_HWCAP2) & HWCAP2_SM3) != 0) return true; #elif defined(__APPLE__) && defined(__aarch64__) && 0 - // M1 processor - if (IsAppleMachineARMv83()) - return true; - // Nope... return false; #endif return false; @@ -1067,10 +1076,6 @@ inline bool CPU_QuerySM4() if ((getauxval(AT_HWCAP2) & HWCAP2_SM4) != 0) return true; #elif defined(__APPLE__) && defined(__aarch64__) && 0 - // M1 processor - if (IsAppleMachineARMv83()) - return true; - // Nope... return false; #endif return false; -- cgit v1.2.1