]> git.ipfire.org Git - thirdparty/nettle.git/commitdiff
CPU feature detection for Apple M1 devices.
authorTim Kosse <tim.kosse@filezilla-project.org>
Tue, 5 Dec 2023 20:19:16 +0000 (21:19 +0100)
committerNiels Möller <nisse@lysator.liu.se>
Tue, 5 Dec 2023 20:19:16 +0000 (21:19 +0100)
ChangeLog
fat-arm64.c

index eb9e70062c6619d6377b4f3009bab8934bba7839..c2cbf32e94e0b81027d963ad21ad43b8ce64d150 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2023-12-05  Niels Möller  <nisse@lysator.liu.se>
 
+       From Tim Kosse:
+       * fat-arm64.c (check_sysctlbyname) [__APPLE__]: New function.
+       (get_arm64_features) [__APPLE__]: Fix feature detection for Apple
+       M1 devices.
+
        * configure.ac: In openssl tests, check for the headers actually
        used by the benchmarking code, and for a subset of the relevant
        functions.
index aec99f663623de9c43a3c4f729fa1d09ce4eca0d..240302a6d1aec1bcd172754cb8434710c05abf92 100644 (file)
@@ -50,6 +50,8 @@
 # include <sys/sysctl.h>
 # include <machine/cpu.h>
 # include <machine/armreg.h>
+#elif defined(__APPLE__)
+# include <sys/sysctl.h>
 #endif
 
 #include "nettle-types.h"
@@ -86,6 +88,16 @@ struct arm64_features
 #define MATCH(s, slen, literal, llen) \
   ((slen) == (llen) && memcmp ((s), (literal), llen) == 0)
 
+#if defined(__APPLE__)
+static int
+check_sysctlbyname(const char* name)
+{
+  int val;
+  size_t s = sizeof(val);
+  return sysctlbyname(name, &val, &s, NULL, 0) ? 0 : val;
+}
+#endif
+
 static void
 get_arm64_features (struct arm64_features *features)
 {
@@ -141,6 +153,12 @@ get_arm64_features (struct arm64_features *features)
         = (ID_AA64ISAR0_SHA1(isar0) >= ID_AA64ISAR0_SHA1_BASE);
       features->have_sha2
         = (ID_AA64ISAR0_SHA2(isar0) >= ID_AA64ISAR0_SHA2_BASE);
+#elif defined(__APPLE__)
+      /* See https://developer.apple.com/documentation/kernel/1387446-sysctlbyname/determining_instruction_set_characteristics */
+      features->have_aes = check_sysctlbyname("hw.optional.arm.FEAT_AES");
+      features->have_pmull = check_sysctlbyname("hw.optional.arm.FEAT_PMULL");
+      features->have_sha1 = check_sysctlbyname("hw.optional.arm.FEAT_SHA1");
+      features->have_sha2 = check_sysctlbyname("hw.optional.arm.FEAT_SHA256");
 #endif
     }
 }