1 # SPDX-License-Identifier: 0BSD
3 #############################################################################
11 # Checks for tuklib_integer.h:
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,
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);
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.])
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([
56 AC_DEFINE(HAVE_[]m4_toupper(FUNC), [1],
57 [Define to 1 if] FUNC [is available.])
59 ], [AC_MSG_RESULT([no])])
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
78 i?86|x86_64|powerpc|powerpc64|powerpc64le)
79 enable_unaligned_access=yes
82 # On 32-bit and 64-bit ARM, GCC and Clang
83 # #define __ARM_FEATURE_UNALIGNED if
84 # unaligned access is supported.
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
93 # RISC-V C API Specification says that if
94 # __riscv_misaligned_fast is defined then
95 # unaligned access is known to be fast.
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)
107 int main(void) { return 0; }
108 ])], [enable_unaligned_access=yes], [enable_unaligned_access=no])
111 enable_unaligned_access=no
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.])
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.])
142 AC_MSG_CHECKING([if __builtin_assume_aligned is supported])
143 AC_LINK_IFELSE([AC_LANG_PROGRAM([[]], [[__builtin_assume_aligned("", 1);]])],
145 AC_DEFINE([HAVE___BUILTIN_ASSUME_ALIGNED], [1],
146 [Define to 1 if the GNU C extension
147 __builtin_assume_aligned is supported.])