From 567cfc00d8ab14fa9cf2ebc148b5cf6fe1273102 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Tue, 3 Nov 2020 23:28:20 +0000 Subject: [PATCH] Improve ARM runtime feature detection on Linux MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit getauxval(AT_HWCAP) is the best way to check for features on ARM and AArch64, as it doesn’t require parsing a file, instead it just returns a value provided by the kernel in our address space. --- features/features_cpu.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/features/features_cpu.c b/features/features_cpu.c index 0ad815e2..8d3015d0 100644 --- a/features/features_cpu.c +++ b/features/features_cpu.c @@ -337,8 +337,27 @@ static void arm_enable_runfast_mode(void) #endif #if defined(__linux__) && !defined(CPU_X86) +#if __ARM_ARCH +#include +#endif + static unsigned char check_arm_cpu_feature(const char* feature) { +#if __ARM_ARCH < 8 + uint64_t hwcap = getauxval(AT_HWCAP); + if (!strcmp(feature, "neon")) + return (hwcap & HWCAP_ARM_NEON) != 0; + if (!strcmp(feature, "vfpv3")) + return (hwcap & HWCAP_ARM_VFPv3) != 0; + if (!strcmp(feature, "vfpv4")) + return (hwcap & HWCAP_ARM_VFPv4) != 0; + return 0; +#elif __ARM_ARCH == 8 + uint64_t hwcap = getauxval(AT_HWCAP); + if (!strcmp(feature, "asimd")) + return (hwcap & HWCAP_ASIMD) != 0; + return 0; +#else char line[1024]; unsigned char status = 0; RFILE *fp = filestream_open("/proc/cpuinfo", @@ -362,6 +381,7 @@ static unsigned char check_arm_cpu_feature(const char* feature) filestream_close(fp); return status; +#endif } #if !defined(_SC_NPROCESSORS_ONLN)