Try harder to get WCHAR_MIN and WCHAR_MAX.
[gnulib.git] / m4 / stdint.m4
1 # stdint.m4 serial 10
2 dnl Copyright (C) 2001-2002, 2004-2006 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 dnl From Bruno Haible.
8 dnl Test whether <stdint.h> is supported or must be substituted.
9
10 AC_DEFUN([gl_STDINT_H],
11 [
12   dnl Check for <wchar.h>.
13   AC_CHECK_HEADERS_ONCE([wchar.h])
14   if test $ac_cv_header_wchar_h = yes; then
15     HAVE_WCHAR_H=1
16   else
17     HAVE_WCHAR_H=0
18   fi
19   AC_SUBST([HAVE_WCHAR_H])
20
21   dnl Check for <stdint.h> that doesn't clash with <sys/types.h>.
22   gl_HEADER_STDINT_H
23   if test $gl_cv_header_stdint_h = yes; then
24     ac_cv_header_stdint_h=yes; dnl Hack for gl_FULL_HEADER_PATH.
25     gl_FULL_HEADER_PATH([stdint.h])
26     FULL_PATH_STDINT_H='<'$gl_cv_full_path_stdint_h'>'
27     AC_SUBST([FULL_PATH_STDINT_H])
28     HAVE_STDINT_H=1
29   else
30     HAVE_STDINT_H=0
31   fi
32   AC_SUBST([HAVE_STDINT_H])
33
34   dnl Check for <inttypes.h> that doesn't clash with <sys/types.h>.
35   gl_HEADER_INTTYPES_H
36   if test $gl_cv_header_inttypes_h = yes; then
37     ac_cv_header_inttypes_h=yes; dnl Hack for gl_FULL_HEADER_PATH.
38     gl_FULL_HEADER_PATH([inttypes.h])
39     FULL_PATH_INTTYPES_H='<'$gl_cv_full_path_inttypes_h'>'
40     AC_SUBST([FULL_PATH_INTTYPES_H])
41     HAVE_INTTYPES_H=1
42   else
43     HAVE_INTTYPES_H=0
44   fi
45   AC_SUBST([HAVE_INTTYPES_H])
46
47   dnl Check for <sys/inttypes.h>.
48   AC_CHECK_HEADERS([sys/inttypes.h])
49   if test $ac_cv_header_sys_inttypes_h = yes; then
50     HAVE_SYS_INTTYPES_H=1
51   else
52     HAVE_SYS_INTTYPES_H=0
53   fi
54   AC_SUBST([HAVE_SYS_INTTYPES_H])
55
56   dnl Check for <sys/bitypes.h> (used in Linux libc4 >= 4.6.7 and libc5).
57   AC_CHECK_HEADERS([sys/bitypes.h])
58   if test $ac_cv_header_sys_bitypes_h = yes; then
59     HAVE_SYS_BITYPES_H=1
60   else
61     HAVE_SYS_BITYPES_H=0
62   fi
63   AC_SUBST([HAVE_SYS_BITYPES_H])
64
65   dnl Is long == int64_t ?
66   AC_CACHE_CHECK([whether 'long' is 64 bit wide], gl_cv_long_bitsize_64, [
67     AC_TRY_COMPILE([
68 #define POW63  ((((((long) 1 << 15) << 15) << 15) << 15) << 3)
69 #define POW64  ((((((long) 1 << 15) << 15) << 15) << 15) << 4)
70 typedef int array [2 * (POW63 != 0 && POW64 == 0) - 1];
71 ], , gl_cv_long_bitsize_64=yes, gl_cv_long_bitsize_64=no)])
72   if test $gl_cv_long_bitsize_64 = yes; then
73     HAVE_LONG_64BIT=1
74   else
75     HAVE_LONG_64BIT=0
76   fi
77   AC_SUBST(HAVE_LONG_64BIT)
78
79   dnl Is long long == int64_t ?
80   AC_CACHE_CHECK([whether 'long long' is 64 bit wide], gl_cv_longlong_bitsize_64, [
81     AC_TRY_COMPILE([
82 #define POW63  ((((((long long) 1 << 15) << 15) << 15) << 15) << 3)
83 #define POW64  ((((((long long) 1 << 15) << 15) << 15) << 15) << 4)
84 typedef int array [2 * (POW63 != 0 && POW64 == 0) - 1];
85 ], , gl_cv_longlong_bitsize_64=yes, gl_cv_longlong_bitsize_64=no)])
86   if test $gl_cv_longlong_bitsize_64 = yes; then
87     HAVE_LONG_LONG_64BIT=1
88   else
89     HAVE_LONG_LONG_64BIT=0
90   fi
91   AC_SUBST(HAVE_LONG_LONG_64BIT)
92
93   dnl Here we use FULL_PATH_INTTYPES_H and FULL_PATH_STDINT_H, not just
94   dnl <inttypes.h> and <stdint.h>, so that it also works during a
95   dnl "config.status --recheck" if an inttypes.h or stdint.h have been
96   dnl created in the build directory.
97   other_includes='
98 /* Get those types that are already defined in other system include files.  */
99 #if defined(__FreeBSD__) && (__FreeBSD__ >= 3) && (__FreeBSD__ <= 4)
100 # include <sys/inttypes.h>
101 #endif
102 #if defined(__OpenBSD__) || defined(__bsdi__) || defined(__sgi)
103 # include <sys/types.h>
104 # if HAVE_INTTYPES_H
105 #  include FULL_PATH_INTTYPES_H
106 # endif
107 #endif
108 #if defined(__linux__) && HAVE_SYS_BITYPES_H
109 # include <sys/bitypes.h>
110 #endif
111 #if defined(__sun) && HAVE_SYS_INTTYPES_H
112 # include <sys/inttypes.h>
113 #endif
114 #if (defined(__hpux) || defined(_AIX)) && HAVE_INTTYPES_H
115 # include FULL_PATH_INTTYPES_H
116 #endif
117 #if HAVE_STDINT_H && !(defined(__sgi) && HAVE_INTTYPES_H && !defined(_c99))
118 # include FULL_PATH_STDINT_H
119 #endif
120 '
121   gl_STDINT_CHECK_TYPES(
122     [int8_t int16_t int32_t int64_t \
123      uint8_t uint16_t uint32_t uint64_t \
124      int_least8_t int_least16_t int_least32_t int_least64_t \
125      uint_least8_t uint_least16_t uint_least32_t uint_least64_t \
126      int_fast8_t int_fast16_t int_fast32_t int_fast64_t \
127      uint_fast8_t uint_fast16_t uint_fast32_t uint_fast64_t \
128      intptr_t uintptr_t \
129      intmax_t uintmax_t],
130     [$other_includes],
131     [gl_cv_type_], [], [])
132
133   dnl Now see if we need a substitute <stdint.h>.
134   gl_cv_header_working_stdint_h=no
135   if test $gl_cv_header_stdint_h = yes; then
136     gl_STDINT_CHECK_TYPES(
137       [int64_t uint64_t \
138        int_least64_t uint_least64_t \
139        int_fast64_t uint_fast64_t],
140       [#include <stdint.h>],
141       [gl_cv_stdint_], [_IN_STDINT_H], [in <stdint.h>])
142     AC_COMPILE_IFELSE([
143       AC_LANG_PROGRAM([
144 #define __STDC_LIMIT_MACROS 1 /* to make it work also in C++ mode */
145 #include <stdint.h>
146 int8_t a1 = INT8_C (17);
147 int16_t a2 = INT16_C (17);
148 int32_t a3 = INT32_C (17);
149 #if HAVE_INT64_T_IN_STDINT_H
150 int64_t a4 = INT64_C (17);
151 #endif
152 uint8_t b1 = UINT8_C (17);
153 uint16_t b2 = UINT16_C (17);
154 uint32_t b3 = UINT32_C (17);
155 #if HAVE_UINT64_T_IN_STDINT_H
156 uint64_t b4 = UINT64_C (17);
157 #endif
158 int_least8_t c1 = 17;
159 int_least16_t c2 = 17;
160 int_least32_t c3 = 17;
161 #if HAVE_INT_LEAST64_T_IN_STDINT_H
162 int_least64_t c4 = 17;
163 #endif
164 uint_least8_t d1 = 17;
165 uint_least16_t d2 = 17;
166 uint_least32_t d3 = 17;
167 #if HAVE_UINT_LEAST64_T_IN_STDINT_H
168 uint_least64_t d4 = 17;
169 #endif
170 int_fast8_t e1 = 17;
171 int_fast16_t e2 = 17;
172 int_fast32_t e3 = 17;
173 #if HAVE_INT_FAST64_T_IN_STDINT_H
174 int_fast64_t e4 = 17;
175 #endif
176 uint_fast8_t f1 = 17;
177 uint_fast16_t f2 = 17;
178 uint_fast32_t f3 = 17;
179 #if HAVE_UINT_FAST64_T_IN_STDINT_H
180 uint_fast64_t f4 = 17;
181 #endif
182 intptr_t g = 17;
183 uintptr_t h = 17;
184 intmax_t i = INTMAX_C (17);
185 uintmax_t j = UINTMAX_C (17);
186       ])],
187       [gl_cv_header_working_stdint_h=yes])
188   fi
189   if test $gl_cv_header_working_stdint_h = yes; then
190     dnl Use the existing <stdint.h>, adding missing macro definitions.
191     suff64=
192     suffu64=
193     if test $HAVE_LONG_64BIT = 1; then
194       suff64=L
195       suffu64=UL
196     else
197       if test $HAVE_LONG_LONG_64BIT = 1; then
198         suff64=LL
199         suffu64=ULL
200       else
201         AC_EGREP_CPP([msvc compiler], [
202 #ifdef _MSC_VER
203 msvc compiler
204 #endif
205           ], [
206           suff64=i64
207           suffu64=ui64
208         ])
209       fi
210     fi
211     dnl Here we assume a standard architecture where the hardware integer
212     dnl types have 8, 16, 32, optionally 64 bits.
213     gl_STDINT_MISSING_BOUND([INT8_MIN], [-128],
214       [Define if <stdint.h> doesn't define it.])
215     gl_STDINT_MISSING_BOUND([INT8_MAX], [127],
216       [Define if <stdint.h> doesn't define it.])
217     gl_STDINT_MISSING_BOUND([UINT8_MAX], [255],
218       [Define if <stdint.h> doesn't define it.])
219     gl_STDINT_MISSING_BOUND([INT16_MIN], [-32768],
220       [Define if <stdint.h> doesn't define it.])
221     gl_STDINT_MISSING_BOUND([INT16_MAX], [32767],
222       [Define if <stdint.h> doesn't define it.])
223     gl_STDINT_MISSING_BOUND([UINT16_MAX], [65535],
224       [Define if <stdint.h> doesn't define it.])
225     gl_STDINT_MISSING_BOUND([INT32_MIN], [(~INT32_MAX)],
226       [Define if <stdint.h> doesn't define it.])
227     gl_STDINT_MISSING_BOUND([INT32_MAX], [2147483647],
228       [Define if <stdint.h> doesn't define it.])
229     gl_STDINT_MISSING_BOUND([UINT32_MAX], [4294967295U],
230       [Define if <stdint.h> doesn't define it.])
231     if test $gl_cv_stdint_int64_t = yes; then
232       gl_STDINT_MISSING_BOUND([INT64_MIN], [(~INT64_MAX)],
233         [Define if <stdint.h> doesn't define it but has the int64_t type.])
234       gl_STDINT_MISSING_BOUND([INT64_MAX], [9223372036854775807${suff64}],
235         [Define if <stdint.h> doesn't define it but has the int64_t type.])
236     fi
237     if test $gl_cv_stdint_uint64_t = yes; then
238       gl_STDINT_MISSING_BOUND([UINT64_MAX], [18446744073709551615${suffu64}],
239         [Define if <stdint.h> doesn't define it but has the uint64_t type.])
240     fi
241     dnl Here we assume a standard architecture where the hardware integer
242     dnl types have 8, 16, 32, optionally 64 bits. Therefore the leastN_t types
243     dnl are the same as the corresponding N_t types.
244     gl_STDINT_MISSING_BOUND([INT_LEAST8_MIN], [INT8_MIN],
245       [Define if <stdint.h> doesn't define it.])
246     gl_STDINT_MISSING_BOUND([INT_LEAST8_MAX], [INT8_MAX],
247       [Define if <stdint.h> doesn't define it.])
248     gl_STDINT_MISSING_BOUND([UINT_LEAST8_MAX], [UINT8_MAX],
249       [Define if <stdint.h> doesn't define it.])
250     gl_STDINT_MISSING_BOUND([INT_LEAST16_MIN], [INT16_MIN],
251       [Define if <stdint.h> doesn't define it.])
252     gl_STDINT_MISSING_BOUND([INT_LEAST16_MAX], [INT16_MAX],
253       [Define if <stdint.h> doesn't define it.])
254     gl_STDINT_MISSING_BOUND([UINT_LEAST16_MAX], [UINT16_MAX],
255       [Define if <stdint.h> doesn't define it.])
256     gl_STDINT_MISSING_BOUND([INT_LEAST32_MIN], [INT32_MIN],
257       [Define if <stdint.h> doesn't define it.])
258     gl_STDINT_MISSING_BOUND([INT_LEAST32_MAX], [INT32_MAX],
259       [Define if <stdint.h> doesn't define it.])
260     gl_STDINT_MISSING_BOUND([UINT_LEAST32_MAX], [UINT32_MAX],
261       [Define if <stdint.h> doesn't define it.])
262     if test $gl_cv_stdint_int_least64_t = yes; then
263       gl_STDINT_MISSING_BOUND([INT_LEAST64_MIN], [INT64_MIN],
264         [Define if <stdint.h> doesn't define it but has the int_least64_t type.])
265       gl_STDINT_MISSING_BOUND([INT_LEAST64_MAX], [INT64_MAX],
266         [Define if <stdint.h> doesn't define it but has the int_least64_t type.])
267     fi
268     if test $gl_cv_stdint_uint_least64_t = yes; then
269       gl_STDINT_MISSING_BOUND([UINT_LEAST64_MAX], [UINT64_MAX],
270         [Define if <stdint.h> doesn't define it but has the uint_least64_t type.])
271     fi
272     dnl Here we assume a standard architecture where the hardware integer
273     dnl types have 8, 16, 32, optionally 64 bits. Therefore the fastN_t types
274     dnl are taken from the same list of types.
275     gl_STDINT_MISSING_BOUNDS([INT_FAST8_MIN INT_FAST8_MAX UINT_FAST8_MAX \
276                               INT_FAST16_MIN INT_FAST16_MAX UINT_FAST16_MAX \
277                               INT_FAST32_MIN INT_FAST32_MAX UINT_FAST32_MAX])
278     if test $gl_cv_stdint_uint_fast64_t = yes; then
279       gl_STDINT_MISSING_BOUNDS([INT_FAST64_MIN INT_FAST64_MAX])
280     fi
281     if test $gl_cv_stdint_uint_fast64_t = yes; then
282       gl_STDINT_MISSING_BOUNDS([UINT_FAST64_MAX])
283     fi
284     gl_STDINT_MISSING_BOUNDS([INTPTR_MIN INTPTR_MAX UINTPTR_MAX \
285                               INTMAX_MIN INTMAX_MAX UINTMAX_MAX])
286     gl_STDINT_MISSING_BOUNDS([PTRDIFF_MIN PTRDIFF_MAX], [#include <stddef.h>])
287     gl_SIZE_MAX
288     gl_STDINT_MISSING_BOUNDS2([SIG_ATOMIC_MIN SIG_ATOMIC_MAX],
289       [#include <signal.h>])
290     dnl Don't bother defining WCHAR_MIN and WCHAR_MAX, since they should
291     dnl already be defined in <stddef.h> or <wchar.h>.
292     dnl For wint_t we need <wchar.h>.
293     dnl Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included
294     dnl before <wchar.h>.
295     dnl BSD/OS 4.1 has a bug: <stdio.h> and <time.h> must be included before
296     dnl <wchar.h>.
297     gl_STDINT_MISSING_BOUNDS2([WINT_MIN WINT_MAX], [
298 #include <stdio.h>
299 #include <time.h>
300 #include <wchar.h>
301 ])
302     STDINT_H=''
303   else
304
305     gl_STDINT_BITSIZEOF(
306       [int8_t int16_t int32_t int64_t \
307        uint8_t uint16_t uint32_t uint64_t \
308        int_least8_t int_least16_t int_least32_t int_least64_t \
309        uint_least8_t uint_least16_t uint_least32_t uint_least64_t \
310        int_fast8_t int_fast16_t int_fast32_t int_fast64_t \
311        uint_fast8_t uint_fast16_t uint_fast32_t uint_fast64_t \
312        intptr_t uintptr_t \
313        intmax_t uintmax_t],
314       [$other_includes])
315
316     gl_cv_type_unsigned_int=yes
317     gl_cv_type_long=yes
318     gl_cv_type_unsigned_long=yes
319     gl_STDINT_BITSIZEOF([unsigned_int long unsigned_long],
320       [typedef unsigned int unsigned_int;
321        typedef unsigned long unsigned_long;])
322
323     AC_CHECK_TYPES([ptrdiff_t])
324     gl_cv_type_ptrdiff_t=$ac_cv_type_ptrdiff_t
325     AC_REQUIRE([AC_TYPE_SIZE_T])
326     gl_cv_type_size_t=yes
327     gl_STDINT_BITSIZEOF([ptrdiff_t size_t], [#include <stddef.h>])
328     gl_CHECK_TYPE_SAME([ptrdiff_t], [long], [#include <stddef.h>])
329     gl_CHECK_TYPE_SAME([size_t], [unsigned long], [#include <stddef.h>])
330
331     AC_CHECK_TYPES([sig_atomic_t], , , [#include <signal.h>])
332     gl_cv_type_sig_atomic_t=$ac_cv_type_sig_atomic_t
333     gl_STDINT_BITSIZEOF([sig_atomic_t], [#include <signal.h>])
334     gl_CHECK_TYPES_SIGNED([sig_atomic_t], [#include <signal.h>])
335     if test $HAVE_SIGNED_SIG_ATOMIC_T = 1; then
336       gl_CHECK_TYPE_SAME([sig_atomic_t], [long], [#include <signal.h>])
337     else
338       gl_CHECK_TYPE_SAME([sig_atomic_t], [unsigned long], [#include <signal.h>])
339     fi
340
341     AC_REQUIRE([gt_TYPE_WCHAR_T])
342     gl_cv_type_wchar_t=$gt_cv_c_wchar_t
343     gl_STDINT_BITSIZEOF([wchar_t], [#include <stddef.h>])
344     gl_CHECK_TYPES_SIGNED([wchar_t], [#include <stddef.h>])
345     if test $HAVE_SIGNED_WCHAR_T = 1; then
346       gl_CHECK_TYPE_SAME([wchar_t], [long], [#include <stddef.h>])
347     else
348       gl_CHECK_TYPE_SAME([wchar_t], [unsigned long], [#include <stddef.h>])
349     fi
350
351     dnl For wint_t we need <wchar.h>.
352     dnl Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included
353     dnl before <wchar.h>.
354     dnl BSD/OS 4.1 has a bug: <stdio.h> and <time.h> must be included before
355     dnl <wchar.h>.
356     AC_CHECK_TYPES([wint_t], , , [#include <stdio.h>
357 #include <time.h>
358 #include <wchar.h>
359 ])
360     gl_cv_type_wint_t=$ac_cv_type_wint_t
361     gl_STDINT_BITSIZEOF([wint_t], [#include <stdio.h>
362 #include <time.h>
363 #include <wchar.h>
364 ])
365     gl_CHECK_TYPES_SIGNED([wint_t], [#include <stdio.h>
366 #include <time.h>
367 #include <wchar.h>
368 ])
369     if test $HAVE_SIGNED_WINT_T = 1; then
370       gl_CHECK_TYPE_SAME([wint_t], [long], [#include <stdio.h>
371 #include <time.h>
372 #include <wchar.h>
373 ])
374     else
375       gl_CHECK_TYPE_SAME([wint_t], [unsigned long], [#include <stdio.h>
376 #include <time.h>
377 #include <wchar.h>
378 ])
379     fi
380
381     STDINT_H='stdint.h'
382   fi
383   AC_SUBST(STDINT_H)
384 ])
385
386 dnl Set gl_cv_header_stdint_h to yes and define HAVE_STDINT_H if
387 dnl <stdint.h> exists and doesn't clash with <sys/types.h>.
388 AC_DEFUN([gl_HEADER_STDINT_H],
389 [
390   dnl Check for <stdint.h> that doesn't clash with <sys/types.h>.
391   AC_CACHE_CHECK([for stdint.h], gl_cv_header_stdint_h, [
392     AC_TRY_COMPILE([
393 #include <sys/types.h>
394 #include <stdint.h>],
395       [], gl_cv_header_stdint_h=yes, gl_cv_header_stdint_h=no)
396   ])
397   if test $gl_cv_header_stdint_h = yes; then
398     AC_DEFINE_UNQUOTED(HAVE_STDINT_H, 1,
399       [Define if <stdint.h> exists and doesn't clash with <sys/types.h>.])
400   fi
401 ])
402
403 dnl Set gl_cv_header_inttypes_h to yes and define HAVE_INTTYPES_H if
404 dnl <inttypes.h> exists and doesn't clash with <sys/types.h>.
405 AC_DEFUN([gl_HEADER_INTTYPES_H],
406 [
407   dnl Check for <inttypes.h> that doesn't clash with <sys/types.h>.
408   dnl On IRIX 5.3, <inttypes.h> conflicts with <sys/types.h>.
409   AC_CACHE_CHECK([for inttypes.h], gl_cv_header_inttypes_h, [
410     AC_TRY_COMPILE([
411 #include <sys/types.h>
412 #include <inttypes.h>],
413       [], gl_cv_header_inttypes_h=yes, gl_cv_header_inttypes_h=no)
414   ])
415   if test $gl_cv_header_inttypes_h = yes; then
416     AC_DEFINE_UNQUOTED(HAVE_INTTYPES_H, 1,
417       [Define if <inttypes.h> exists and doesn't clash with <sys/types.h>.])
418   fi
419 ])
420
421 dnl gl_STDINT_CHECK_TYPES(TYPES, INCLUDES, CACHE_VAR_PREFIX, MACRO_SUFFIX, DESCRIPTION_SUFFIX)
422 dnl Check each of the given types, whether they are defined in the given
423 dnl include files.
424 AC_DEFUN([gl_STDINT_CHECK_TYPES],
425 [
426   dnl Use a shell loop, to avoid bloating configure, and
427   dnl - extra AH_TEMPLATE calls, so that autoheader knows what to put into
428   dnl   config.h.in,
429   dnl - extra AC_SUBST calls, so that the right substitutions are made.
430   AC_FOREACH([gltype], [$1],
431     [AH_TEMPLATE([HAVE_]translit(gltype,[abcdefghijklmnopqrstuvwxyz],[ABCDEFGHIJKLMNOPQRSTUVWXYZ])[$4],
432        [Define to 1 if the type ']gltype[' is already defined$5.])])
433   for gltype in $1 ; do
434     AC_MSG_CHECKING([for $gltype])
435     AC_COMPILE_IFELSE([
436       AC_LANG_PROGRAM([$2
437 /* Test if the type exists.  */
438 $gltype x = 17;
439       ])],
440       result=yes, result=no)
441     eval $3${gltype}=\$result
442     AC_MSG_RESULT($result)
443     GLTYPE=`echo "$gltype" | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
444     if test $result = yes; then
445       AC_DEFINE_UNQUOTED([HAVE_${GLTYPE}$4], 1)
446       eval HAVE_${GLTYPE}$4=1
447     else
448       eval HAVE_${GLTYPE}$4=0
449     fi
450   done
451   AC_FOREACH([gltype], [$1],
452     [AC_SUBST([HAVE_]translit(gltype,[abcdefghijklmnopqrstuvwxyz],[ABCDEFGHIJKLMNOPQRSTUVWXYZ])[$4])])
453 ])
454
455 dnl gl_STDINT_MISSING_BOUND(TYPE_BOUND, DEFAULT, DESCRIPTION)
456 dnl assumes an otherwise complete <stdint.h> and defines TYPE_BOUND if
457 dnl <stdint.h> doesn't define it.
458 AC_DEFUN([gl_STDINT_MISSING_BOUND],
459 [
460   AC_CACHE_CHECK([for $1], [gl_cv_stdint_$1],
461     [AC_EGREP_CPP([found it], [#include <stdint.h>
462 #ifdef $1
463 found it
464 #endif
465        ], [gl_cv_stdint_$1=yes], [gl_cv_stdint_$1="$2"])])
466   if test "$gl_cv_stdint_$1" != yes; then
467     AC_DEFINE_UNQUOTED([$1], [$2], [$3])
468   fi
469 ])
470
471 dnl gl_STDINT_MISSING_BOUNDS(BOUNDS, INCLUDES)
472 dnl assumes an otherwise complete <stdint.h> and defines each element of BOUNDS
473 dnl if <stdint.h> doesn't define it.
474 dnl Use this for types whose signedness is determined by the first letter
475 dnl ('u' or not).
476 AC_DEFUN([gl_STDINT_MISSING_BOUNDS],
477 [
478   dnl Use a shell loop, to avoid bloating configure, and
479   dnl - extra AH_TEMPLATE calls, so that autoheader knows what to put into
480   dnl   config.h.in.
481   AC_FOREACH([bound], [$1],
482     [AH_TEMPLATE(bound, [Define if <stdint.h> doesn't define it.])])
483 changequote(,)dnl
484   sed_unsigned='s,^\(U*\).*,\1,'
485   sed_limitkind='s,^.*\(_[^_]*\)$,\1,'
486 changequote([,])dnl
487   for bound in $1; do
488     type=`echo $bound | sed -e 's,_MAX,_t,' -e 's,_MIN,_t,' | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz`
489     unsigned=`echo $bound | sed -e "$sed_unsigned" | tr U u`
490     width=`echo $bound | sed -e 's,^U*INT,,' -e 's,_MIN,,' -e 's,_MAX,,'`
491     limitkind=`echo $bound | sed -e "$sed_limitkind"`
492     AC_CACHE_CHECK([for $bound], [gl_cv_stdint_$bound],
493       [AC_EGREP_CPP([found it], [#include <stdint.h>
494 #ifdef $bound
495 found it
496 #endif
497          ], [eval gl_cv_stdint_$bound=yes],
498          [result=
499           case $width in
500             *8) widthlist="8 16 32 64" ;;
501             *16) widthlist="16 32 64" ;;
502             *32 | PTR | MAX | PTRDIFF) widthlist="32 64" ;;
503             *64) widthlist="64" ;;
504           esac
505           for w in $widthlist; do
506             if test -z "$result"; then
507               AC_COMPILE_IFELSE([[$2
508 #include <stdint.h>
509 int verify[2 * (sizeof ($type) == sizeof (${unsigned}int${w}_t)) - 1];
510                 ]], [result=`echo "$unsigned" | tr u U`INT${w}${limitkind}])
511             else
512               break
513             fi
514           done
515           if test -z "$result"; then
516             result=no
517           fi
518           eval gl_cv_stdint_$bound=\$result
519          ])])
520     eval result=\$gl_cv_stdint_$bound
521     if test "$result" != yes && test "$result" != no; then
522       AC_DEFINE_UNQUOTED([$bound], [$result],
523         [Define if <stdint.h> doesn't define it.])
524     fi
525   done
526 ])
527
528 dnl gl_STDINT_MISSING_BOUNDS2(BOUNDS, INCLUDES)
529 dnl assumes an otherwise complete <stdint.h> and defines each element of BOUNDS
530 dnl if <stdint.h> doesn't define it.
531 dnl Use this for types whose signedness is a priori unknown.
532 AC_DEFUN([gl_STDINT_MISSING_BOUNDS2],
533 [
534   dnl Use a shell loop, to avoid bloating configure, and
535   dnl - extra AH_TEMPLATE calls, so that autoheader knows what to put into
536   dnl   config.h.in.
537   AC_FOREACH([bound], [$1],
538     [AH_TEMPLATE(bound, [Define if <stdint.h> doesn't define it.])])
539 changequote(,)dnl
540   sed_limitkind='s,^.*\(_[^_]*\)$,\1,'
541 changequote([,])dnl
542   for bound in $1; do
543     type=`echo $bound | sed -e 's,_MAX,_t,' -e 's,_MIN,_t,' | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz`
544     limitkind=`echo $bound | sed -e "$sed_limitkind"`
545     AC_CACHE_CHECK([for $bound], [gl_cv_stdint_$bound],
546       [AC_EGREP_CPP([found it], [#include <stdint.h>
547 #ifdef $bound
548 found it
549 #endif
550          ], [eval gl_cv_stdint_$bound=yes],
551          [result=
552           AC_COMPILE_IFELSE([[$2
553             int verify[2 * (($type) -1 >= ($type) 0) - 1];
554             ]],
555             [eval gl_cv_${type}_signed=no],
556             [eval gl_cv_${type}_signed=yes])
557           if eval test \$gl_cv_${type}_signed = yes; then
558             for w in 8 16 32 64; do
559               if test -z "$result"; then
560                 AC_COMPILE_IFELSE([[$2
561 #include <stdint.h>
562 int verify[2 * (sizeof ($type) == sizeof (int${w}_t)) - 1];
563                   ]], [result=INT${w}${limitkind}])
564               else
565                 break
566               fi
567             done
568           else
569             if test ${limitkind} = _MIN; then
570               result=0
571             else
572               for w in 8 16 32 64; do
573                 if test -z "$result"; then
574                   AC_COMPILE_IFELSE([[$2
575 #include <stdint.h>
576 int verify[2 * (sizeof ($type) == sizeof (uint${w}_t)) - 1];
577                     ]], [result=UINT${w}${limitkind}])
578                 else
579                   break
580                 fi
581               done
582             fi
583           fi
584           if test -z "$result"; then
585             result=no
586           fi
587           eval gl_cv_stdint_$bound=\$result
588          ])])
589     eval result=\$gl_cv_stdint_$bound
590     if test "$result" != yes && test "$result" != no; then
591       AC_DEFINE_UNQUOTED([$bound], [$result],
592         [Define if <stdint.h> doesn't define it.])
593     fi
594   done
595 ])
596
597 dnl gl_STDINT_BITSIZEOF(TYPES, INCLUDES)
598 dnl Determine the size of each of the given types in bits.
599 AC_DEFUN([gl_STDINT_BITSIZEOF],
600 [
601   dnl Use a shell loop, to avoid bloating configure, and
602   dnl - extra AH_TEMPLATE calls, so that autoheader knows what to put into
603   dnl   config.h.in,
604   dnl - extra AC_SUBST calls, so that the right substitutions are made.
605   AC_FOREACH([gltype], [$1],
606     [AH_TEMPLATE([BITSIZEOF_]translit(gltype,[abcdefghijklmnopqrstuvwxyz ],[ABCDEFGHIJKLMNOPQRSTUVWXYZ_]),
607        [Define to the number of bits in type ']gltype['.])])
608   for gltype in $1 ; do
609     if eval test \$gl_cv_type_${gltype} = yes; then
610       AC_CACHE_CHECK([for bit size of $gltype], [gl_cv_bitsizeof_${gltype}],
611         [_AC_COMPUTE_INT([sizeof ($gltype) * CHAR_BIT], result,
612            [$2
613 #include <limits.h>], result=unknown)
614          eval gl_cv_bitsizeof_${gltype}=\$result
615         ])
616       eval result=\$gl_cv_bitsizeof_${gltype}
617       GLTYPE=`echo "$gltype" | tr 'abcdefghijklmnopqrstuvwxyz ' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_'`
618       AC_DEFINE_UNQUOTED([BITSIZEOF_${GLTYPE}], [$result])
619       eval BITSIZEOF_${GLTYPE}=\$result
620     fi
621   done
622   AC_FOREACH([gltype], [$1],
623     [AC_SUBST([BITSIZEOF_]translit(gltype,[abcdefghijklmnopqrstuvwxyz ],[ABCDEFGHIJKLMNOPQRSTUVWXYZ_]))])
624 ])
625
626 dnl gl_CHECK_TYPES_SIGNED(TYPES, INCLUDES)
627 dnl Determine the signedness of each of the given types.
628 dnl Define HAVE_SIGNED_TYPE if type is signed.
629 AC_DEFUN([gl_CHECK_TYPES_SIGNED],
630 [
631   dnl Use a shell loop, to avoid bloating configure, and
632   dnl - extra AH_TEMPLATE calls, so that autoheader knows what to put into
633   dnl   config.h.in,
634   dnl - extra AC_SUBST calls, so that the right substitutions are made.
635   AC_FOREACH([gltype], [$1],
636     [AH_TEMPLATE([HAVE_SIGNED_]translit(gltype,[abcdefghijklmnopqrstuvwxyz ],[ABCDEFGHIJKLMNOPQRSTUVWXYZ_]),
637        [Define to 1 if ']gltype[' is a signed integer type.])])
638   for gltype in $1 ; do
639     AC_CACHE_CHECK([whether $gltype is signed], [gl_cv_type_${gltype}_signed],
640       [AC_COMPILE_IFELSE(
641          [AC_LANG_PROGRAM([[$2
642             int verify[2 * (($1) -1 < ($1) 0) - 1];
643             ]])],
644          result=yes, result=no)
645        eval gl_cv_type_${gltype}_signed=\$result
646       ])
647     eval result=\$gl_cv_type_${gltype}_signed
648     GLTYPE=`echo $gltype | tr 'abcdefghijklmnopqrstuvwxyz ' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_'`
649     if test "$result" = yes; then
650       AC_DEFINE_UNQUOTED([HAVE_SIGNED_${GLTYPE}], 1)
651       eval HAVE_SIGNED_${GLTYPE}=1
652     else
653       eval HAVE_SIGNED_${GLTYPE}=0
654     fi
655   done
656   AC_FOREACH([gltype], [$1],
657     [AC_SUBST([HAVE_SIGNED_]translit(gltype,[abcdefghijklmnopqrstuvwxyz ],[ABCDEFGHIJKLMNOPQRSTUVWXYZ_]))])
658 ])
659
660 dnl gl_CHECK_TYPE_SAME(TYPE, KNOWNTYPE, INCLUDES)
661 dnl Determines whether two types are the same.
662 AC_DEFUN([gl_CHECK_TYPE_SAME],
663 [
664   AC_TRY_COMPILE([$3
665     extern $1 foo;
666     extern $2 foo;], [],
667     [SAME_TYPE_]AS_TR_CPP([$1])[_]AS_TR_CPP([$2])[=1],
668     [SAME_TYPE_]AS_TR_CPP([$1])[_]AS_TR_CPP([$2])[=0])
669   AC_SUBST([SAME_TYPE_]AS_TR_CPP([$1])[_]AS_TR_CPP([$2]))
670 ])