]> git.ipfire.org Git - thirdparty/xz.git/commitdiff
Build: Add support for ARM64 CRC32 instruction detection.
authorJia Tan <jiat0218@gmail.com>
Sun, 21 Jan 2024 16:36:47 +0000 (00:36 +0800)
committerJia Tan <jiat0218@gmail.com>
Thu, 1 Feb 2024 12:09:09 +0000 (20:09 +0800)
This adds --enable-arm64-crc32/--disable-arm64-crc32 (enabled by
default) for using the ARM64 CRC32 instruction. This can be disabled if
one knows the binary will never need to run on an ARM64 machine
with this instruction extension.

configure.ac

index bb2697f6d2e3b7ac51883a6f5e8fb20fdb3f3464..d2fa730c5dbfe1abbe9e3d563c5067d3376571cf 100644 (file)
@@ -380,6 +380,16 @@ AC_ARG_ENABLE([clmul-crc], AS_HELP_STRING([--disable-clmul-crc],
        [], [enable_clmul_crc=yes])
 
 
+############################
+# ARM64 CRC32 Instructions #
+############################
+
+AC_ARG_ENABLE([arm64-crc32], AS_HELP_STRING([--disable-arm64-crc32],
+               [Do not use ARM64 CRC32 instructions even if support for it
+               is detected.]),
+       [], [enable_arm64_crc32=yes])
+
+
 #####################
 # Size optimization #
 #####################
@@ -1087,6 +1097,48 @@ __m128i my_clmul(__m128i a)
        AC_MSG_RESULT([$enable_clmul_crc])
 ])
 
+# ARM64 C Language Extensions define CRC32 functions in arm_acle.h.
+# These are supported by at least GCC and Clang which both need
+# __attribute__((__target__("+crc"))), unless the needed compiler flags
+# are used to support the CRC instruction.
+AC_MSG_CHECKING([if ARM64 CRC32 instruction is usable])
+AS_IF([test "x$enable_arm64_crc32" = xno], [
+       AC_MSG_RESULT([no, --disable-arm64-crc32 was used])
+], [
+       AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
+#include <arm_acle.h>
+#include <stdint.h>
+
+#if (defined(__GNUC__) || defined(__clang__)) && !defined(__EDG__)
+__attribute__((__target__("+crc")))
+#endif
+uint32_t my_crc(uint32_t a, uint64_t b)
+{
+       return __crc32d(a, b);
+}
+       ]])], [
+               AC_DEFINE([HAVE_ARM64_CRC32], [1],
+                       [Define to 1 if ARM64 CRC32 instruction is supported.
+                       See configure.ac for details.])
+               enable_arm64_crc32=yes
+       ], [
+               enable_arm64_crc32=no
+       ])
+       AC_MSG_RESULT([$enable_arm64_crc32])
+])
+
+# Check for ARM64 CRC32 instruction runtime detection.
+# getauxval() is supported on Linux, elf_aux_info() on FreeBSD, and
+# sysctlbyname("hw.optional.armv8_crc32", ...) is supported on Darwin
+# (macOS, iOS, etc.). Note that sysctlbyname() is supported on FreeBSD,
+# NetBSD, and possibly others too but the string is specific to Apple OSes.
+# The C code is responsible for checking defined(__APPLE__) before using
+# sysctlbyname("hw.optional.armv8_crc32", ...).
+AS_IF([test "x$enable_arm64_crc32" = xyes], [
+       AC_CHECK_FUNCS([getauxval elf_aux_info sysctlbyname])
+])
+
+
 # Check for sandbox support. If one is found, set enable_sandbox=found.
 #
 # About -fsanitize: Of our three sandbox methods, only Landlock is