1#
2# SYNOPSIS
3#
4#   TUKLIB_INTEGER
5#
6# DESCRIPTION
7#
8#   Checks for tuklib_integer.h:
9#     - Endianness
10#     - Does operating system provide byte swapping macros
11#     - Does the hardware support fast unaligned access to 16-bit
12#       and 32-bit integers
13#
14# COPYING
15#
16#   Author: Lasse Collin
17#
18#   This file has been put into the public domain.
19#   You can do whatever you want with this file.
20#
21
22AC_DEFUN_ONCE([TUKLIB_INTEGER], [
23AC_REQUIRE([TUKLIB_COMMON])
24AC_REQUIRE([AC_C_BIGENDIAN])
25AC_CHECK_HEADERS([byteswap.h sys/endian.h sys/byteorder.h], [break])
26
27# Even if we have byteswap.h, we may lack the specific macros/functions.
28if test x$ac_cv_header_byteswap_h = xyes ; then
29	m4_foreach([FUNC], [bswap_16,bswap_32,bswap_64], [
30		AC_MSG_CHECKING([if FUNC is available])
31		AC_LINK_IFELSE([AC_LANG_SOURCE([
32#include <byteswap.h>
33int
34main(void)
35{
36	FUNC[](42);
37	return 0;
38}
39		])], [
40			AC_DEFINE(HAVE_[]m4_toupper(FUNC), [1],
41					[Define to 1 if] FUNC [is available.])
42			AC_MSG_RESULT([yes])
43		], [AC_MSG_RESULT([no])])
44
45	])dnl
46fi
47
48AC_MSG_CHECKING([if unaligned memory access should be used])
49AC_ARG_ENABLE([unaligned-access], AC_HELP_STRING([--enable-unaligned-access],
50		[Enable if the system supports *fast* unaligned memory access
51		with 16-bit and 32-bit integers. By default, this is enabled
52		only on x86, x86_64, and big endian PowerPC.]),
53	[], [enable_unaligned_access=auto])
54if test "x$enable_unaligned_access" = xauto ; then
55	# TODO: There may be other architectures, on which unaligned access
56	# is OK.
57	case $host_cpu in
58		i?86|x86_64|powerpc|powerpc64)
59			enable_unaligned_access=yes
60			;;
61		*)
62			enable_unaligned_access=no
63			;;
64	esac
65fi
66if test "x$enable_unaligned_access" = xyes ; then
67	AC_DEFINE([TUKLIB_FAST_UNALIGNED_ACCESS], [1], [Define to 1 if
68		the system supports fast unaligned access to 16-bit and
69		32-bit integers.])
70	AC_MSG_RESULT([yes])
71else
72	AC_MSG_RESULT([no])
73fi
74])dnl
75