Blame m4/size_max.m4

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