Merge the ChangeLogs into one ChangeLog, at the root.
[gnulib.git] / m4 / ptrdiff_max.m4
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.
8
9 dnl From Bruno Haible.
10
11 AC_DEFUN([gl_PTRDIFF_MAX],
12 [
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.])
16     ])
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])
20   result=
21   AC_EGREP_CPP([Found it], [
22 #include <limits.h>
23 #if HAVE_STDINT_H
24 #include <stdint.h>
25 #endif
26 #ifdef PTRDIFF_MAX
27 Found it
28 #endif
29 ], result=yes)
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, [
36 #include <stddef.h>
37 #include <limits.h>
38 #define STYPE_MINIMUM(t) (~ (t) 0 << (sizeof (t) * CHAR_BIT - 1))
39 #define STYPE_MAXIMUM(t) ((t) (~ (t) 0 - STYPE_MINIMUM (t)))
40 ], result=?)
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>
47         extern ptrdiff_t foo;
48         extern long foo;
49         ], [], fits_in_int=0)
50     fi
51     if test -z "$result"; then
52       if test "$fits_in_int" = 1; then
53         result="$res"
54       else
55         result="$res"L
56       fi
57     else
58       dnl Shouldn't happen, but who knows...
59       result='((ptrdiff_t)(~(ptrdiff_t)0-(~(ptrdiff_t)0<<(sizeof(ptrdiff_t)*CHAR_BIT-1))))'
60     fi
61   fi
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.])
66   fi
67 ])