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