1# size_max.m4 serial 6
2dnl Copyright (C) 2003, 2005-2006, 2010 Free Software Foundation, Inc.
3dnl This file is free software; the Free Software Foundation
4dnl gives unlimited permission to copy and/or distribute it,
5dnl with or without modifications, as long as this notice is preserved.
6
7dnl From Bruno Haible.
8dnl Change by Vincent Lefevre: added <inttypes.h> for consistency with MPFR
9
10AC_DEFUN([gl_SIZE_MAX],
11[
12  AC_CHECK_HEADERS(stdint.h)
13  dnl First test whether the system already has SIZE_MAX.
14  AC_MSG_CHECKING([for SIZE_MAX])
15  AC_CACHE_VAL([gl_cv_size_max], [
16    gl_cv_size_max=
17    AC_EGREP_CPP([Found it], [
18#include <limits.h>
19#ifdef HAVE_INTTYPES_H
20# include <inttypes.h>
21#endif
22#if HAVE_STDINT_H
23# include <stdint.h>
24#endif
25#ifdef SIZE_MAX
26Found it
27#endif
28], gl_cv_size_max=yes)
29    if test -z "$gl_cv_size_max"; then
30      dnl Define it ourselves. Here we assume that the type 'size_t' is not wider
31      dnl than the type 'unsigned long'. Try hard to find a definition that can
32      dnl be used in a preprocessor #if, i.e. doesn't contain a cast.
33      AC_COMPUTE_INT([size_t_bits_minus_1], [sizeof (size_t) * CHAR_BIT - 1],
34        [#include <stddef.h>
35#include <limits.h>], size_t_bits_minus_1=)
36      AC_COMPUTE_INT([fits_in_uint], [sizeof (size_t) <= sizeof (unsigned int)],
37        [#include <stddef.h>], fits_in_uint=)
38      if test -n "$size_t_bits_minus_1" && test -n "$fits_in_uint"; then
39        if test $fits_in_uint = 1; then
40          dnl Even though SIZE_MAX fits in an unsigned int, it must be of type
41          dnl 'unsigned long' if the type 'size_t' is the same as 'unsigned long'.
42          AC_TRY_COMPILE([#include <stddef.h>
43            extern size_t foo;
44            extern unsigned long foo;
45            ], [], fits_in_uint=0)
46        fi
47        dnl We cannot use 'expr' to simplify this expression, because 'expr'
48        dnl works only with 'long' integers in the host environment, while we
49        dnl might be cross-compiling from a 32-bit platform to a 64-bit platform.
50        if test $fits_in_uint = 1; then
51          gl_cv_size_max="(((1U << $size_t_bits_minus_1) - 1) * 2 + 1)"
52        else
53          gl_cv_size_max="(((1UL << $size_t_bits_minus_1) - 1) * 2 + 1)"
54        fi
55      else
56        dnl Shouldn't happen, but who knows...
57        gl_cv_size_max='((size_t)~(size_t)0)'
58      fi
59    fi
60  ])
61  AC_MSG_RESULT([$gl_cv_size_max])
62  if test "$gl_cv_size_max" != yes; then
63    AC_DEFINE_UNQUOTED([SIZE_MAX], [$gl_cv_size_max],
64      [Define as the maximum value of type 'size_t', if the system doesn't define it.])
65  fi
66])
67
68dnl Autoconf >= 2.61 has AC_COMPUTE_INT built-in.
69dnl Remove this when we can assume autoconf >= 2.61.
70m4_ifdef([AC_COMPUTE_INT], [], [
71  AC_DEFUN([AC_COMPUTE_INT], [_AC_COMPUTE_INT([$2],[$1],[$3],[$4])])
72])
73