aboutsummaryrefslogtreecommitdiff
path: root/m4/size_max.m4
diff options
context:
space:
mode:
Diffstat (limited to 'm4/size_max.m4')
-rw-r--r--m4/size_max.m4102
1 files changed, 60 insertions, 42 deletions
diff --git a/m4/size_max.m4 b/m4/size_max.m4
index 5762fc33..7e192d5e 100644
--- a/m4/size_max.m4
+++ b/m4/size_max.m4
@@ -1,20 +1,18 @@
1# size_max.m4 serial 2 1# size_max.m4 serial 10
2dnl Copyright (C) 2003 Free Software Foundation, Inc. 2dnl Copyright (C) 2003, 2005-2006, 2008-2014 Free Software Foundation, Inc.
3dnl This file is free software, distributed under the terms of the GNU 3dnl This file is free software; the Free Software Foundation
4dnl General Public License. As a special exception to the GNU General 4dnl gives unlimited permission to copy and/or distribute it,
5dnl Public License, this file may be distributed as part of a program 5dnl with or without modifications, as long as this notice is preserved.
6dnl that contains a configuration script generated by Autoconf, under
7dnl the same distribution terms as the rest of that program.
8 6
9dnl From Bruno Haible. 7dnl From Bruno Haible.
10 8
11AC_DEFUN([gl_SIZE_MAX], 9AC_DEFUN([gl_SIZE_MAX],
12[ 10[
13 AC_CHECK_HEADERS(stdint.h) 11 AC_CHECK_HEADERS([stdint.h])
14 dnl First test whether the system already has SIZE_MAX. 12 dnl First test whether the system already has SIZE_MAX.
15 AC_MSG_CHECKING([for SIZE_MAX]) 13 AC_CACHE_CHECK([for SIZE_MAX], [gl_cv_size_max], [
16 result= 14 gl_cv_size_max=
17 AC_EGREP_CPP([Found it], [ 15 AC_EGREP_CPP([Found it], [
18#include <limits.h> 16#include <limits.h>
19#if HAVE_STDINT_H 17#if HAVE_STDINT_H
20#include <stdint.h> 18#include <stdint.h>
@@ -22,40 +20,60 @@ AC_DEFUN([gl_SIZE_MAX],
22#ifdef SIZE_MAX 20#ifdef SIZE_MAX
23Found it 21Found it
24#endif 22#endif
25], result=yes) 23], [gl_cv_size_max=yes])
26 if test -z "$result"; then 24 if test -z "$gl_cv_size_max"; then
27 dnl Define it ourselves. Here we assume that the type 'size_t' is not wider 25 dnl Define it ourselves. Here we assume that the type 'size_t' is not wider
28 dnl than the type 'unsigned long'. 26 dnl than the type 'unsigned long'. Try hard to find a definition that can
29 dnl The _AC_COMPUTE_INT macro works up to LONG_MAX, since it uses 'expr', 27 dnl be used in a preprocessor #if, i.e. doesn't contain a cast.
30 dnl which is guaranteed to work from LONG_MIN to LONG_MAX. 28 AC_COMPUTE_INT([size_t_bits_minus_1], [sizeof (size_t) * CHAR_BIT - 1],
31 _AC_COMPUTE_INT([~(size_t)0 / 10], res_hi, 29 [#include <stddef.h>
32 [#include <stddef.h>], result=?) 30#include <limits.h>], [size_t_bits_minus_1=])
33 _AC_COMPUTE_INT([~(size_t)0 % 10], res_lo, 31 AC_COMPUTE_INT([fits_in_uint], [sizeof (size_t) <= sizeof (unsigned int)],
34 [#include <stddef.h>], result=?) 32 [#include <stddef.h>], [fits_in_uint=])
35 _AC_COMPUTE_INT([sizeof (size_t) <= sizeof (unsigned int)], fits_in_uint, 33 if test -n "$size_t_bits_minus_1" && test -n "$fits_in_uint"; then
36 [#include <stddef.h>], result=?) 34 if test $fits_in_uint = 1; then
37 if test "$fits_in_uint" = 1; then 35 dnl Even though SIZE_MAX fits in an unsigned int, it must be of type
38 dnl Even though SIZE_MAX fits in an unsigned int, it must be of type 36 dnl 'unsigned long' if the type 'size_t' is the same as 'unsigned long'.
39 dnl 'unsigned long' if the type 'size_t' is the same as 'unsigned long'. 37 AC_COMPILE_IFELSE(
40 AC_TRY_COMPILE([#include <stddef.h> 38 [AC_LANG_PROGRAM(
41 extern size_t foo; 39 [[#include <stddef.h>
42 extern unsigned long foo; 40 extern size_t foo;
43 ], [], fits_in_uint=0) 41 extern unsigned long foo;
44 fi 42 ]],
45 if test -z "$result"; then 43 [[]])],
46 if test "$fits_in_uint" = 1; then 44 [fits_in_uint=0])
47 result="$res_hi$res_lo"U 45 fi
46 dnl We cannot use 'expr' to simplify this expression, because 'expr'
47 dnl works only with 'long' integers in the host environment, while we
48 dnl might be cross-compiling from a 32-bit platform to a 64-bit platform.
49 if test $fits_in_uint = 1; then
50 gl_cv_size_max="(((1U << $size_t_bits_minus_1) - 1) * 2 + 1)"
51 else
52 gl_cv_size_max="(((1UL << $size_t_bits_minus_1) - 1) * 2 + 1)"
53 fi
48 else 54 else
49 result="$res_hi$res_lo"UL 55 dnl Shouldn't happen, but who knows...
56 gl_cv_size_max='((size_t)~(size_t)0)'
50 fi 57 fi
51 else
52 dnl Shouldn't happen, but who knows...
53 result='~(size_t)0'
54 fi 58 fi
55 fi 59 ])
56 AC_MSG_RESULT([$result]) 60 if test "$gl_cv_size_max" != yes; then
57 if test "$result" != yes; then 61 AC_DEFINE_UNQUOTED([SIZE_MAX], [$gl_cv_size_max],
58 AC_DEFINE_UNQUOTED([SIZE_MAX], [$result],
59 [Define as the maximum value of type 'size_t', if the system doesn't define it.]) 62 [Define as the maximum value of type 'size_t', if the system doesn't define it.])
60 fi 63 fi
64 dnl Don't redefine SIZE_MAX in config.h if config.h is re-included after
65 dnl <stdint.h>. Remember that the #undef in AH_VERBATIM gets replaced with
66 dnl #define by AC_DEFINE_UNQUOTED.
67 AH_VERBATIM([SIZE_MAX],
68[/* Define as the maximum value of type 'size_t', if the system doesn't define
69 it. */
70#ifndef SIZE_MAX
71# undef SIZE_MAX
72#endif])
73])
74
75dnl Autoconf >= 2.61 has AC_COMPUTE_INT built-in.
76dnl Remove this when we can assume autoconf >= 2.61.
77m4_ifdef([AC_COMPUTE_INT], [], [
78 AC_DEFUN([AC_COMPUTE_INT], [_AC_COMPUTE_INT([$2],[$1],[$3],[$4])])
61]) 79])