From: Josh Triplett Date: Sun, 25 Apr 2021 22:21:54 +0000 (-0700) Subject: Detect hwcap flags needed for runtime detection on ARM Linux X-Git-Tag: 2.0.3~12 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a7f2956c68a97e99af5b74775b0f9a17844b8829;p=thirdparty%2Fzlib-ng.git Detect hwcap flags needed for runtime detection on ARM Linux This allows us to provide useful warning messages from cmake or configure if the system headers don't provide the necessary flags to do runtime detection. --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 56b4a839c..dd174a3df 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -649,6 +649,34 @@ endif() if(WITH_OPTIM) if(BASEARCH_ARM_FOUND) add_definitions(-DARM_FEATURES) + if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") + check_c_source_compiles( + "#include + int main() { + return (getauxval(AT_HWCAP2) & HWCAP2_CRC32); + }" + ARM_AUXV_HAS_CRC32 + ) + if(ARM_AUXV_HAS_CRC32) + add_definitions(-DARM_AUXV_HAS_CRC32) + else() + message(STATUS "HWCAP2_CRC32 not present in sys/auxv.h; cannot detect support at runtime.") + endif() + if(NOT "${ARCH}" MATCHES "aarch64") + check_c_source_compiles( + "#include + int main() { + return (getauxval(AT_HWCAP) & HWCAP_NEON); + }" + ARM_AUXV_HAS_NEON + ) + if(ARM_AUXV_HAS_NEON) + add_definitions(-DARM_AUXV_HAS_NEON) + else() + message(STATUS "HWCAP_NEON not present in sys/auxv.h; cannot detect support at runtime.") + endif() + endif() + endif() list(APPEND ZLIB_ARCH_HDRS ${ARCHDIR}/arm.h) list(APPEND ZLIB_ARCH_SRCS ${ARCHDIR}/armfeature.c) if(WITH_ACLE AND NOT MSVC) diff --git a/arch/arm/armfeature.c b/arch/arm/armfeature.c index cea19e614..e8fcc1e5e 100644 --- a/arch/arm/armfeature.c +++ b/arch/arm/armfeature.c @@ -14,7 +14,7 @@ #endif static int arm_has_crc32() { -#if defined(__linux__) && defined(HWCAP2_CRC32) +#if defined(__linux__) && defined(ARM_AUXV_HAS_CRC32) return (getauxval(AT_HWCAP2) & HWCAP2_CRC32) != 0 ? 1 : 0; #elif defined(__FreeBSD__) && defined(__aarch64__) return getenv("QEMU_EMULATING") == NULL @@ -34,7 +34,7 @@ static int arm_has_crc32() { /* AArch64 has neon. */ #if !defined(__aarch64__) && !defined(_M_ARM64) static inline int arm_has_neon() { -#if defined(__linux__) && defined(HWCAP_NEON) +#if defined(__linux__) && defined(ARM_AUXV_HAS_NEON) return (getauxval(AT_HWCAP) & HWCAP_NEON) != 0 ? 1 : 0; #elif defined(__APPLE__) int hasneon; diff --git a/configure b/configure index aafaa06ac..89388be0c 100755 --- a/configure +++ b/configure @@ -524,6 +524,14 @@ else esac fi +# Simplify some later conditionals +case "$uname" in +Linux* | linux*) + LINUX=1 ;; +*) + LINUX=0 ;; +esac + # destination names for shared library if not defined above SHAREDLIB=${SHAREDLIB-"${LIBNAME}$shared_ext"} SHAREDLIBV=${SHAREDLIBV-"${LIBNAME}$shared_ext.$VER"} @@ -1265,6 +1273,34 @@ case "${ARCH}" in SFLAGS="${SFLAGS} -DARM_FEATURES" ARCH_STATIC_OBJS="${ARCH_STATIC_OBJS} armfeature.o" ARCH_SHARED_OBJS="${ARCH_SHARED_OBJS} armfeature.lo" + + if test $LINUX -eq 1; then + cat > $test.c < +int main() { + return (getauxval(AT_HWCAP2) & HWCAP2_CRC32); +} +EOF + if try $CC -c $CFLAGS $test.c; then + CFLAGS="${CFLAGS} -DARM_AUXV_HAS_CRC32" + SFLAGS="${SFLAGS} -DARM_AUXV_HAS_CRC32" + else + echo "HWCAP2_CRC32 not present in sys/auxv.h; cannot detect support at runtime." | tee -a configure.log + fi + + cat > $test.c < +int main() { + return (getauxval(AT_HWCAP) & HWCAP_NEON); +} +EOF + if try $CC -c $CFLAGS $test.c; then + CFLAGS="${CFLAGS} -DARM_AUXV_HAS_NEON" + SFLAGS="${SFLAGS} -DARM_AUXV_HAS_NEON" + else + echo "HWCAP_NEON not present in sys/auxv.h; cannot detect support at runtime." | tee -a configure.log + fi + fi fi cat > $test.c << EOF @@ -1405,6 +1441,21 @@ EOF ARCH_STATIC_OBJS="${ARCH_STATIC_OBJS} armfeature.o" ARCH_SHARED_OBJS="${ARCH_SHARED_OBJS} armfeature.lo" + if test $LINUX -eq 1; then + cat > $test.c < +int main() { + return (getauxval(AT_HWCAP2) & HWCAP2_CRC32); +} +EOF + if try $CC -c $CFLAGS $test.c; then + CFLAGS="${CFLAGS} -DARM_AUXV_HAS_CRC32" + SFLAGS="${SFLAGS} -DARM_AUXV_HAS_CRC32" + else + echo "HWCAP2_CRC32 not present in sys/auxv.h; cannot detect support at runtime." | tee -a configure.log + fi + fi + if test $buildacle -eq 1; then if test $native -eq 0; then ARCH="${ARCH}+crc"