Blame m4/size_max.m4

Packit fc043f
# size_max.m4 serial 4
Packit fc043f
dnl Copyright (C) 2003, 2005-2006 Free Software Foundation, Inc.
Packit fc043f
dnl This file is free software; the Free Software Foundation
Packit fc043f
dnl gives unlimited permission to copy and/or distribute it,
Packit fc043f
dnl with or without modifications, as long as this notice is preserved.
Packit fc043f
Packit fc043f
dnl From Bruno Haible.
Packit fc043f
Packit fc043f
AC_DEFUN([gl_SIZE_MAX],
Packit fc043f
[
Packit fc043f
  AC_CHECK_HEADERS(stdint.h)
Packit fc043f
  dnl First test whether the system already has SIZE_MAX.
Packit fc043f
  AC_MSG_CHECKING([for SIZE_MAX])
Packit fc043f
  result=
Packit fc043f
  AC_EGREP_CPP([Found it], [
Packit fc043f
#include <limits.h>
Packit fc043f
#if HAVE_STDINT_H
Packit fc043f
#include <stdint.h>
Packit fc043f
#endif
Packit fc043f
#ifdef SIZE_MAX
Packit fc043f
Found it
Packit fc043f
#endif
Packit fc043f
], result=yes)
Packit fc043f
  if test -z "$result"; then
Packit fc043f
    dnl Define it ourselves. Here we assume that the type 'size_t' is not wider
Packit fc043f
    dnl than the type 'unsigned long'. Try hard to find a definition that can
Packit fc043f
    dnl be used in a preprocessor #if, i.e. doesn't contain a cast.
Packit fc043f
    _AC_COMPUTE_INT([sizeof (size_t) * CHAR_BIT - 1], size_t_bits_minus_1,
Packit fc043f
      [#include <stddef.h>
Packit fc043f
#include <limits.h>], size_t_bits_minus_1=)
Packit fc043f
    _AC_COMPUTE_INT([sizeof (size_t) <= sizeof (unsigned int)], fits_in_uint,
Packit fc043f
      [#include <stddef.h>], fits_in_uint=)
Packit fc043f
    if test -n "$size_t_bits_minus_1" && test -n "$fits_in_uint"; then
Packit fc043f
      if test $fits_in_uint = 1; then
Packit fc043f
        dnl Even though SIZE_MAX fits in an unsigned int, it must be of type
Packit fc043f
        dnl 'unsigned long' if the type 'size_t' is the same as 'unsigned long'.
Packit fc043f
        AC_TRY_COMPILE([#include <stddef.h>
Packit fc043f
          extern size_t foo;
Packit fc043f
          extern unsigned long foo;
Packit fc043f
          ], [], fits_in_uint=0)
Packit fc043f
      fi
Packit fc043f
      dnl We cannot use 'expr' to simplify this expression, because 'expr'
Packit fc043f
      dnl works only with 'long' integers in the host environment, while we
Packit fc043f
      dnl might be cross-compiling from a 32-bit platform to a 64-bit platform.
Packit fc043f
      if test $fits_in_uint = 1; then
Packit fc043f
        result="(((1U << $size_t_bits_minus_1) - 1) * 2 + 1)"
Packit fc043f
      else
Packit fc043f
        result="(((1UL << $size_t_bits_minus_1) - 1) * 2 + 1)"
Packit fc043f
      fi
Packit fc043f
    else
Packit fc043f
      dnl Shouldn't happen, but who knows...
Packit fc043f
      result='((size_t)~(size_t)0)'
Packit fc043f
    fi
Packit fc043f
  fi
Packit fc043f
  AC_MSG_RESULT([$result])
Packit fc043f
  if test "$result" != yes; then
Packit fc043f
    AC_DEFINE_UNQUOTED([SIZE_MAX], [$result],
Packit fc043f
      [Define as the maximum value of type 'size_t', if the system doesn't define it.])
Packit fc043f
  fi
Packit fc043f
])