Add run-time CPU feature detection for ISA extensions on Apple Silicon
platforms.
---
 source/CMakeLists.txt       |  2 +-
 source/common/aarch64/cpu.h | 40 ++++++++++++++++++++++++++++++++++---
 2 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt
index cc114bc95..cb513db8e 100755
--- a/source/CMakeLists.txt
+++ b/source/CMakeLists.txt
@@ -88,7 +88,7 @@ elseif(ARM64MATCH GREATER "-1")
     option(AARCH64_WARNINGS_AS_ERRORS "Build with -Werror for AArch64 
Intrinsics files" OFF)
 
     option(AARCH64_RUNTIME_CPU_DETECT "Enable AArch64 run-time CPU feature 
detection" ON)
-    if(NOT CMAKE_SYSTEM_NAME MATCHES "Linux")
+    if(NOT CMAKE_SYSTEM_NAME MATCHES "Linux|Darwin")
         set(AARCH64_RUNTIME_CPU_DETECT OFF CACHE BOOL "" FORCE)
         message(STATUS "Run-time CPU feature detection unsupported on this 
platform")
     endif()
diff --git a/source/common/aarch64/cpu.h b/source/common/aarch64/cpu.h
index 88ce2e310..857ba980a 100644
--- a/source/common/aarch64/cpu.h
+++ b/source/common/aarch64/cpu.h
@@ -28,7 +28,41 @@
 
 #if AARCH64_RUNTIME_CPU_DETECT
 
-#if defined(__linux__)
+#if defined(__APPLE__)
+
+#include <sys/sysctl.h>
+
+static inline bool have_feature(const char *feature)
+{
+    int64_t feature_present = 0;
+    size_t size = sizeof(feature_present);
+    if (sysctlbyname(feature, &feature_present, &size, NULL, 0) != 0)
+    {
+        return false;
+    }
+    return feature_present;
+}
+
+static inline int aarch64_get_cpu_flags()
+{
+    int flags = 0;
+
+#if HAVE_NEON
+    flags |= X265_CPU_NEON;
+#endif
+#if HAVE_NEON_DOTPROD
+    if (have_feature("hw.optional.arm.FEAT_DotProd"))
+        flags |= X265_CPU_NEON_DOTPROD;
+#endif
+#if HAVE_NEON_I8MM
+    if (have_feature("hw.optional.arm.FEAT_I8MM"))
+        flags |= X265_CPU_NEON_I8MM;
+#endif
+
+    return flags;
+}
+
+#elif defined(__linux__)
 
 #include <sys/auxv.h>
 
@@ -67,12 +101,12 @@ static inline int aarch64_get_cpu_flags()
     return flags;
 }
 
-#else // defined(__linux__)
+#else // defined(__APPLE__)
 #error                                                                 \
     "Run-time CPU feature detection selected, but no detection method" \
     "available for your platform. Rerun cmake configure with"          \
     "-DAARCH64_RUNTIME_CPU_DETECT=OFF."
-#endif // defined(__linux__)
+#endif // defined(__APPLE__)
 
 static inline int aarch64_cpu_detect()
 {
-- 
2.42.1

>From 03133cfff1f2669e8f92b53f1e25c8fec3fc322d Mon Sep 17 00:00:00 2001
Message-ID: <03133cfff1f2669e8f92b53f1e25c8fec3fc322d.1729809914.git.hari.lim...@arm.com>
In-Reply-To: <cover.1729809914.git.hari.lim...@arm.com>
References: <cover.1729809914.git.hari.lim...@arm.com>
From: Hari Limaye <hari.lim...@arm.com>
Date: Tue, 22 Oct 2024 18:35:02 +0100
Subject: [PATCH 3/6] Add Apple Silicon run-time CPU feature detection

Add run-time CPU feature detection for ISA extensions on Apple Silicon
platforms.

---
 source/CMakeLists.txt       |  2 +-
 source/common/aarch64/cpu.h | 40 ++++++++++++++++++++++++++++++++++---
 2 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt
index cc114bc95..cb513db8e 100755
--- a/source/CMakeLists.txt
+++ b/source/CMakeLists.txt
@@ -88,7 +88,7 @@ elseif(ARM64MATCH GREATER "-1")
     option(AARCH64_WARNINGS_AS_ERRORS "Build with -Werror for AArch64 Intrinsics files" OFF)
 
     option(AARCH64_RUNTIME_CPU_DETECT "Enable AArch64 run-time CPU feature detection" ON)
-    if(NOT CMAKE_SYSTEM_NAME MATCHES "Linux")
+    if(NOT CMAKE_SYSTEM_NAME MATCHES "Linux|Darwin")
         set(AARCH64_RUNTIME_CPU_DETECT OFF CACHE BOOL "" FORCE)
         message(STATUS "Run-time CPU feature detection unsupported on this platform")
     endif()
diff --git a/source/common/aarch64/cpu.h b/source/common/aarch64/cpu.h
index 88ce2e310..857ba980a 100644
--- a/source/common/aarch64/cpu.h
+++ b/source/common/aarch64/cpu.h
@@ -28,7 +28,41 @@
 
 #if AARCH64_RUNTIME_CPU_DETECT
 
-#if defined(__linux__)
+#if defined(__APPLE__)
+
+#include <sys/sysctl.h>
+
+static inline bool have_feature(const char *feature)
+{
+    int64_t feature_present = 0;
+    size_t size = sizeof(feature_present);
+    if (sysctlbyname(feature, &feature_present, &size, NULL, 0) != 0)
+    {
+        return false;
+    }
+    return feature_present;
+}
+
+static inline int aarch64_get_cpu_flags()
+{
+    int flags = 0;
+
+#if HAVE_NEON
+    flags |= X265_CPU_NEON;
+#endif
+#if HAVE_NEON_DOTPROD
+    if (have_feature("hw.optional.arm.FEAT_DotProd"))
+        flags |= X265_CPU_NEON_DOTPROD;
+#endif
+#if HAVE_NEON_I8MM
+    if (have_feature("hw.optional.arm.FEAT_I8MM"))
+        flags |= X265_CPU_NEON_I8MM;
+#endif
+
+    return flags;
+}
+
+#elif defined(__linux__)
 
 #include <sys/auxv.h>
 
@@ -67,12 +101,12 @@ static inline int aarch64_get_cpu_flags()
     return flags;
 }
 
-#else // defined(__linux__)
+#else // defined(__APPLE__)
 #error                                                                 \
     "Run-time CPU feature detection selected, but no detection method" \
     "available for your platform. Rerun cmake configure with"          \
     "-DAARCH64_RUNTIME_CPU_DETECT=OFF."
-#endif // defined(__linux__)
+#endif // defined(__APPLE__)
 
 static inline int aarch64_cpu_detect()
 {
-- 
2.42.1

_______________________________________________
x265-devel mailing list
x265-devel@videolan.org
https://mailman.videolan.org/listinfo/x265-devel

Reply via email to