Build: Change --enable-ifunc handling.
[xz.git] / m4 / tuklib_integer.m4
blob89a53fe1d0695ac380e0d0400cfcab12209a2c7c
2 # SYNOPSIS
4 #   TUKLIB_INTEGER
6 # DESCRIPTION
8 #   Checks for tuklib_integer.h:
9 #     - Endianness
10 #     - Does the compiler or the operating system provide byte swapping macros
11 #     - Does the hardware support fast unaligned access to 16-bit, 32-bit,
12 #       and 64-bit integers
14 # COPYING
16 #   Author: Lasse Collin
18 #   This file has been put into the public domain.
19 #   You can do whatever you want with this file.
22 AC_DEFUN_ONCE([TUKLIB_INTEGER], [
23 AC_REQUIRE([TUKLIB_COMMON])
24 AC_REQUIRE([AC_C_BIGENDIAN])
26 AC_MSG_CHECKING([if __builtin_bswap16/32/64 are supported])
27 AC_LINK_IFELSE([AC_LANG_PROGRAM([[]],
28                         [[__builtin_bswap16(1);
29                         __builtin_bswap32(1);
30                         __builtin_bswap64(1);]])],
32         AC_DEFINE([HAVE___BUILTIN_BSWAPXX], [1],
33                 [Define to 1 if the GNU C extensions
34                 __builtin_bswap16/32/64 are supported.])
35         AC_MSG_RESULT([yes])
36 ], [
37         AC_MSG_RESULT([no])
39         # Look for other byteswapping methods.
40         AC_CHECK_HEADERS([byteswap.h sys/endian.h sys/byteorder.h], [break])
42         # Even if we have byteswap.h we may lack the specific macros/functions.
43         if test x$ac_cv_header_byteswap_h = xyes ; then
44                 m4_foreach([FUNC], [bswap_16,bswap_32,bswap_64], [
45                         AC_MSG_CHECKING([if FUNC is available])
46                         AC_LINK_IFELSE([AC_LANG_SOURCE([
47 #include <byteswap.h>
48 int
49 main(void)
51         FUNC[](42);
52         return 0;
54                         ])], [
55                                 AC_DEFINE(HAVE_[]m4_toupper(FUNC), [1],
56                                         [Define to 1 if] FUNC [is available.])
57                                 AC_MSG_RESULT([yes])
58                         ], [AC_MSG_RESULT([no])])
60                 ])dnl
61         fi
64 AC_MSG_CHECKING([if unaligned memory access should be used])
65 AC_ARG_ENABLE([unaligned-access], AS_HELP_STRING([--enable-unaligned-access],
66                 [Enable if the system supports *fast* unaligned memory access
67                 with 16-bit, 32-bit, and 64-bit integers. By default,
68                 this is enabled on x86, x86-64,
69                 32/64-bit big endian PowerPC,
70                 64-bit little endian PowerPC,
71                 and some ARM, ARM64, and RISC-V systems.]),
72         [], [enable_unaligned_access=auto])
73 if test "x$enable_unaligned_access" = xauto ; then
74         # NOTE: There might be other architectures on which unaligned access
75         # is fast.
76         case $host_cpu in
77                 i?86|x86_64|powerpc|powerpc64|powerpc64le)
78                         enable_unaligned_access=yes
79                         ;;
80                 arm*|aarch64*|riscv*)
81                         # On 32-bit and 64-bit ARM, GCC and Clang
82                         # #define __ARM_FEATURE_UNALIGNED if
83                         # unaligned access is supported.
84                         #
85                         # Exception: GCC at least up to 13.2.0
86                         # defines it even when using -mstrict-align
87                         # so in that case this autodetection goes wrong.
88                         # Most of the time -mstrict-align isn't used so it
89                         # shouldn't be a common problem in practice. See:
90                         # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111555
91                         #
92                         # RISC-V C API Specification says that if
93                         # __riscv_misaligned_fast is defined then
94                         # unaligned access is known to be fast.
95                         #
96                         # MSVC is handled as a special case: We assume that
97                         # 32/64-bit ARM supports fast unaligned access.
98                         # If MSVC gets RISC-V support then this will assume
99                         # fast unaligned access on RISC-V too.
100                         AC_COMPILE_IFELSE([AC_LANG_SOURCE([
101 #if !defined(__ARM_FEATURE_UNALIGNED) \
102                 && !defined(__riscv_misaligned_fast) \
103                 && !defined(_MSC_VER)
104 compile error
105 #endif
106 int main(void) { return 0; }
107 ])], [enable_unaligned_access=yes], [enable_unaligned_access=no])
108                         ;;
109                 *)
110                         enable_unaligned_access=no
111                         ;;
112         esac
114 if test "x$enable_unaligned_access" = xyes ; then
115         AC_DEFINE([TUKLIB_FAST_UNALIGNED_ACCESS], [1], [Define to 1 if
116                 the system supports fast unaligned access to 16-bit,
117                 32-bit, and 64-bit integers.])
118         AC_MSG_RESULT([yes])
119 else
120         AC_MSG_RESULT([no])
123 AC_MSG_CHECKING([if unsafe type punning should be used])
124 AC_ARG_ENABLE([unsafe-type-punning],
125         AS_HELP_STRING([--enable-unsafe-type-punning],
126                 [This introduces strict aliasing violations and may result
127                 in broken code. However, this might improve performance in
128                 some cases, especially with old compilers (e.g.
129                 GCC 3 and early 4.x on x86, GCC < 6 on ARMv6 and ARMv7).]),
130         [], [enable_unsafe_type_punning=no])
131 if test "x$enable_unsafe_type_punning" = xyes ; then
132         AC_DEFINE([TUKLIB_USE_UNSAFE_TYPE_PUNNING], [1], [Define to 1 to use
133                 unsafe type punning, e.g. char *x = ...; *(int *)x = 123;
134                 which violates strict aliasing rules and thus is
135                 undefined behavior and might result in broken code.])
136         AC_MSG_RESULT([yes])
137 else
138         AC_MSG_RESULT([no])
141 AC_MSG_CHECKING([if __builtin_assume_aligned is supported])
142 AC_LINK_IFELSE([AC_LANG_PROGRAM([[]], [[__builtin_assume_aligned("", 1);]])],
143         [
144                 AC_DEFINE([HAVE___BUILTIN_ASSUME_ALIGNED], [1],
145                         [Define to 1 if the GNU C extension
146                         __builtin_assume_aligned is supported.])
147                 AC_MSG_RESULT([yes])
148         ], [
149                 AC_MSG_RESULT([no])
150         ])
151 ])dnl