1 # ptrdiff_max.m4 serial 1
2 dnl Copyright (C) 2003 Free Software Foundation, Inc.
3 dnl This file is free software, distributed under the terms of the GNU
4 dnl General Public License. As a special exception to the GNU General
5 dnl Public License, this file may be distributed as part of a program
6 dnl that contains a configuration script generated by Autoconf, under
7 dnl the same distribution terms as the rest of that program.
11 AC_DEFUN([gl_PTRDIFF_MAX],
13 AC_CHECK_TYPE([ptrdiff_t], ,
14 [AC_DEFINE([ptrdiff_t], [long],
15 [Define as the type of the result of subtracting two pointers, if the system doesn't define it.])
17 AC_CHECK_HEADERS_ONCE(stdint.h)
18 dnl First test whether the system already has PTRDIFF_MAX.
19 AC_MSG_CHECKING([for PTRDIFF_MAX])
21 AC_EGREP_CPP([Found it], [
30 if test -z "$result"; then
31 dnl Define it ourselves. Here we assume that the type 'ptrdiff_t' is not
32 dnl wider than the type 'long'.
33 dnl The _AC_COMPUTE_INT macro works up to LONG_MAX, since it uses 'expr',
34 dnl which is guaranteed to work from LONG_MIN to LONG_MAX.
35 _AC_COMPUTE_INT([STYPE_MAXIMUM (ptrdiff_t)], res, [
38 #define STYPE_MINIMUM(t) (~ (t) 0 << (sizeof (t) * CHAR_BIT - 1))
39 #define STYPE_MAXIMUM(t) ((t) (~ (t) 0 - STYPE_MINIMUM (t)))
41 _AC_COMPUTE_INT([sizeof (ptrdiff_t) <= sizeof (int)], fits_in_int,
42 [#include <stddef.h>], result=?)
43 if test "$fits_in_int" = 1; then
44 dnl Even though PTRDIFF_MAX fits in an int, it must be of type
45 dnl 'long' if the type 'ptrdiff_t' is the same as 'long'.
46 AC_TRY_COMPILE([#include <stddef.h>
51 if test -z "$result"; then
52 if test "$fits_in_int" = 1; then
58 dnl Shouldn't happen, but who knows...
59 result='((ptrdiff_t)(~(ptrdiff_t)0-(~(ptrdiff_t)0<<(sizeof(ptrdiff_t)*CHAR_BIT-1))))'
62 AC_MSG_RESULT([$result])
63 if test "$result" != yes; then
64 AC_DEFINE_UNQUOTED([PTRDIFF_MAX], [$result],
65 [Define as the maximum value of type 'ptrdiff_t', if the system doesn't define it.])