Simplification rewrite for stdint module.
[gnulib.git] / m4 / stdint.m4
1 # stdint.m4 serial 12
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 Paul Eggert and Bruno Haible.
8 dnl Test whether <stdint.h> is supported or must be substituted.
9
10 AC_DEFUN([gl_STDINT_H],
11 [
12   AC_PREREQ(2.59)dnl
13
14   dnl Check for long long int.
15   AC_REQUIRE([AC_TYPE_LONG_LONG_INT])
16   if test $ac_cv_type_long_long_int = yes; then
17     HAVE_LONG_LONG_INT=1
18   else
19     HAVE_LONG_LONG_INT=0
20   fi
21   AC_SUBST([HAVE_LONG_LONG_INT])
22
23   dnl Check for <wchar.h>.
24   AC_CHECK_HEADERS_ONCE([wchar.h])
25   if test $ac_cv_header_wchar_h = yes; then
26     HAVE_WCHAR_H=1
27   else
28     HAVE_WCHAR_H=0
29   fi
30   AC_SUBST([HAVE_WCHAR_H])
31
32   dnl Check for <inttypes.h>.
33   dnl AC_INCLUDES_DEFAULT defines $ac_cv_header_inttypes_h.
34   if test $ac_cv_header_inttypes_h = yes; then
35     HAVE_INTTYPES_H=1
36   else
37     HAVE_INTTYPES_H=0
38   fi
39   AC_SUBST([HAVE_INTTYPES_H])
40
41   dnl Check for <sys/types.h>.
42   dnl AC_INCLUDES_DEFAULT defines $ac_cv_header_sys_types_h.
43   if test $ac_cv_header_sys_types_h = yes; then
44     HAVE_SYS_TYPES_H=1
45   else
46     HAVE_SYS_TYPES_H=0
47   fi
48   AC_SUBST([HAVE_SYS_TYPES_H])
49
50   dnl AC_INCLUDES_DEFAULT defines $ac_cv_header_stdint_h.
51   if test $ac_cv_header_stdint_h = yes; then
52     gl_FULL_HEADER_PATH([stdint.h])
53     FULL_PATH_STDINT_H="<$gl_cv_full_path_stdint_h>"
54     AC_SUBST([FULL_PATH_STDINT_H])
55     HAVE_STDINT_H=1
56   else
57     HAVE_STDINT_H=0
58   fi
59   AC_SUBST([HAVE_STDINT_H])
60
61   dnl Now see whether we need a substitute <stdint.h>.  Use
62   dnl FULL_PATH_STDINT_H, not <stdint.h>, so that it also works during
63   dnl a "config.status --recheck" if a stdint.h has been
64   dnl created in the build directory.
65   if test $ac_cv_header_stdint_h = yes; then
66     AC_CACHE_CHECK([whether stdint.h conforms to C99],
67       [gl_cv_header_working_stdint_h],
68       [gl_cv_header_working_stdint_h=no
69        AC_COMPILE_IFELSE([
70          AC_LANG_PROGRAM([[
71 #include <stddef.h>
72 #define __STDC_LIMIT_MACROS 1 /* to make it work also in C++ mode */
73 #include FULL_PATH_STDINT_H
74 #ifdef INT8_MAX
75 int8_t a1 = INT8_MAX;
76 #endif
77 #ifdef INT16_MAX
78 int16_t a2 = INT16_MAX;
79 #endif
80 #ifdef INT32_MAX
81 int32_t a3 = INT32_MAX;
82 #endif
83 #ifdef INT64_MAX
84 int64_t a4 = INT64_MAX;
85 #endif
86 #ifdef UINT8_MAX
87 uint8_t b1 = UINT8_MAX;
88 #else
89 typedef int b1[(unsigned char) -1 != 255 ? 1 : -1];
90 #endif
91 #ifdef UINT16_MAX
92 uint16_t b2 = UINT16_MAX;
93 #endif
94 #ifdef UINT32_MAX
95 uint32_t b3 = UINT32_MAX;
96 #endif
97 #ifdef UINT64_MAX
98 uint64_t b4 = UINT64_MAX;
99 #endif
100 int_least8_t c1 = INT8_C (0x7f);
101 int_least16_t c2 = INT16_C (0x7fff);
102 int_least32_t c3 = INT32_C (0x7fffffff);
103 int_least64_t c4 = INT64_C (0x7fffffffffffffff);
104 uint_least8_t d1 = UINT8_C (0xff);
105 uint_least16_t d2 = UINT16_C (0xffff);
106 uint_least32_t d3 = UINT32_C (0xffffffff);
107 uint_least64_t d4 = UINT64_C (0xffffffffffffffff);
108 int_fast8_t e1 = INT_FAST8_MAX;
109 int_fast16_t e2 = INT_FAST16_MAX;
110 int_fast32_t e3 = INT_FAST32_MAX;
111 int_fast64_t e4 = INT_FAST64_MAX;
112 uint_fast8_t f1 = UINT_FAST8_MAX;
113 uint_fast16_t f2 = UINT_FAST16_MAX;
114 uint_fast32_t f3 = UINT_FAST32_MAX;
115 uint_fast64_t f4 = UINT_FAST64_MAX;
116 #ifdef INTPTR_MAX
117 intptr_t g = INTPTR_MAX;
118 #endif
119 #ifdef UINTPTR_MAX
120 uintptr_t h = UINTPTR_MAX;
121 #endif
122 intmax_t i = INTMAX_MAX;
123 uintmax_t j = UINTMAX_MAX;
124 struct s {
125   int a: PTRDIFF_MIN < 0 && 0 < PTRDIFF_MAX ? 1 : -1;
126   int b: SIG_ATOMIC_MIN <= 0 && 0 < SIG_ATOMIC_MAX ? 1 : -1;
127   int c: 0 < SIZE_MAX ? 1 : -1;
128   int d: WCHAR_MIN <= 0 && 0 < WCHAR_MAX ? 1 : -1;
129   int e: WINT_MIN <= 0 && 0 < WINT_MAX ? 1 : -1;
130   /* Detect bugs in glibc 2.4 and Solaris 10 stdint.h, among others.  */
131   int f: (-1 < UINT8_C (0)) == (-1 < (uint_least8_t) 0) ? 1 : -1;
132   int g: (-1 < UINT16_C (0)) == (-1 < (uint_least16_t) 0) ? 1 : -1;
133
134   /* Detect bugs in OpenBSD 3.9 stdint.h.  */
135 #ifdef UINT8_MAX
136   int uint8: (uint8_t) -1 == UINT8_MAX ? 1 : -1;
137 #endif
138 #ifdef UINT16_MAX
139   int uint16: (uint16_t) -1 == UINT16_MAX ? 1 : -1;
140 #endif
141 #ifdef UINT32_MAX
142   int uint32: (uint32_t) -1 == UINT32_MAX ? 1 : -1;
143 #endif
144 #ifdef UINT64_MAX
145   int uint64: (uint64_t) -1 == UINT64_MAX ? 1 : -1;
146 #endif
147   int uint_least8: (uint_least8_t) -1 == UINT_LEAST8_MAX ? 1 : -1;
148   int uint_least16: (uint_least16_t) -1 == UINT_LEAST16_MAX ? 1 : -1;
149   int uint_least32: (uint_least32_t) -1 == UINT_LEAST32_MAX ? 1 : -1;
150   int uint_least64: (uint_least64_t) -1 == UINT_LEAST64_MAX ? 1 : -1;
151   int uint_fast8: (uint_fast8_t) -1 == UINT_FAST8_MAX ? 1 : -1;
152   int uint_fast16: (uint_fast16_t) -1 == UINT_FAST16_MAX ? 1 : -1;
153   int uint_fast32: (uint_fast32_t) -1 == UINT_FAST32_MAX ? 1 : -1;
154   int uint_fast64: (uint_fast64_t) -1 == UINT_FAST64_MAX ? 1 : -1;
155   int uintptr: (uintptr_t) -1 == UINTPTR_MAX ? 1 : -1;
156   int uintmax: (uintmax_t) -1 == UINTMAX_MAX ? 1 : -1;
157   int size: (size_t) -1 == SIZE_MAX ? 1 : -1;
158 };
159          ]])],
160          [gl_cv_header_working_stdint_h=yes])])
161   fi
162   if test "$gl_cv_header_working_stdint_h" != yes; then
163
164     dnl Check for <sys/inttypes.h>, and for
165     dnl <sys/bitypes.h> (used in Linux libc4 >= 4.6.7 and libc5).
166     AC_CHECK_HEADERS([sys/inttypes.h sys/bitypes.h])
167     if test $ac_cv_header_sys_inttypes_h = yes; then
168       HAVE_SYS_INTTYPES_H=1
169     else
170       HAVE_SYS_INTTYPES_H=0
171     fi
172     AC_SUBST([HAVE_SYS_INTTYPES_H])
173     if test $ac_cv_header_sys_bitypes_h = yes; then
174       HAVE_SYS_BITYPES_H=1
175     else
176       HAVE_SYS_BITYPES_H=0
177     fi
178     AC_SUBST([HAVE_SYS_BITYPES_H])
179
180     gl_STDINT_TYPE_PROPERTIES
181     STDINT_H=stdint.h
182   fi
183   AC_SUBST(STDINT_H)
184 ])
185
186 dnl gl_STDINT_BITSIZEOF(TYPES, INCLUDES)
187 dnl Determine the size of each of the given types in bits.
188 AC_DEFUN([gl_STDINT_BITSIZEOF],
189 [
190   dnl Use a shell loop, to avoid bloating configure, and
191   dnl - extra AH_TEMPLATE calls, so that autoheader knows what to put into
192   dnl   config.h.in,
193   dnl - extra AC_SUBST calls, so that the right substitutions are made.
194   AC_FOREACH([gltype], [$1],
195     [AH_TEMPLATE([BITSIZEOF_]translit(gltype,[abcdefghijklmnopqrstuvwxyz ],[ABCDEFGHIJKLMNOPQRSTUVWXYZ_]),
196        [Define to the number of bits in type ']gltype['.])])
197   for gltype in $1 ; do
198     AC_CACHE_CHECK([for bit size of $gltype], [gl_cv_bitsizeof_${gltype}],
199       [_AC_COMPUTE_INT([sizeof ($gltype) * CHAR_BIT], result,
200          [$2
201 #include <limits.h>], [result=unknown])
202        eval gl_cv_bitsizeof_${gltype}=\$result
203       ])
204     eval result=\$gl_cv_bitsizeof_${gltype}
205     if test $result = unknown; then
206       dnl Use a nonempty default, because some compilers, such as IRIX 5 cc,
207       dnl do a syntax check even on unused #if conditions and give an error
208       dnl on valid C code like this:
209       dnl   #if 0
210       dnl   # if  > 32
211       dnl   # endif
212       dnl   #endif
213       result=0
214     fi
215     GLTYPE=`echo "$gltype" | tr 'abcdefghijklmnopqrstuvwxyz ' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_'`
216     AC_DEFINE_UNQUOTED([BITSIZEOF_${GLTYPE}], [$result])
217     eval BITSIZEOF_${GLTYPE}=\$result
218   done
219   AC_FOREACH([gltype], [$1],
220     [AC_SUBST([BITSIZEOF_]translit(gltype,[abcdefghijklmnopqrstuvwxyz ],[ABCDEFGHIJKLMNOPQRSTUVWXYZ_]))])
221 ])
222
223 dnl gl_CHECK_TYPES_SIGNED(TYPES, INCLUDES)
224 dnl Determine the signedness of each of the given types.
225 dnl Define HAVE_SIGNED_TYPE if type is signed.
226 AC_DEFUN([gl_CHECK_TYPES_SIGNED],
227 [
228   dnl Use a shell loop, to avoid bloating configure, and
229   dnl - extra AH_TEMPLATE calls, so that autoheader knows what to put into
230   dnl   config.h.in,
231   dnl - extra AC_SUBST calls, so that the right substitutions are made.
232   AC_FOREACH([gltype], [$1],
233     [AH_TEMPLATE([HAVE_SIGNED_]translit(gltype,[abcdefghijklmnopqrstuvwxyz ],[ABCDEFGHIJKLMNOPQRSTUVWXYZ_]),
234        [Define to 1 if ']gltype[' is a signed integer type.])])
235   for gltype in $1 ; do
236     AC_CACHE_CHECK([whether $gltype is signed], [gl_cv_type_${gltype}_signed],
237       [AC_COMPILE_IFELSE(
238          [AC_LANG_PROGRAM([$2[
239             int verify[2 * (($gltype) -1 < ($gltype) 0) - 1];]])],
240          result=yes, result=no)
241        eval gl_cv_type_${gltype}_signed=\$result
242       ])
243     eval result=\$gl_cv_type_${gltype}_signed
244     GLTYPE=`echo $gltype | tr 'abcdefghijklmnopqrstuvwxyz ' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_'`
245     if test "$result" = yes; then
246       AC_DEFINE_UNQUOTED([HAVE_SIGNED_${GLTYPE}], 1)
247       eval HAVE_SIGNED_${GLTYPE}=1
248     else
249       eval HAVE_SIGNED_${GLTYPE}=0
250     fi
251   done
252   AC_FOREACH([gltype], [$1],
253     [AC_SUBST([HAVE_SIGNED_]translit(gltype,[abcdefghijklmnopqrstuvwxyz ],[ABCDEFGHIJKLMNOPQRSTUVWXYZ_]))])
254 ])
255
256 dnl gl_INTEGER_TYPE_SUFFIX(TYPES, INCLUDES)
257 dnl Determine the suffix to use for integer constants of the given types.
258 dnl Define t_SUFFIX for each such type.
259 AC_DEFUN([gl_INTEGER_TYPE_SUFFIX],
260 [
261   dnl Use a shell loop, to avoid bloating configure, and
262   dnl - extra AH_TEMPLATE calls, so that autoheader knows what to put into
263   dnl   config.h.in,
264   dnl - extra AC_SUBST calls, so that the right substitutions are made.
265   AC_FOREACH([gltype], [$1],
266     [AH_TEMPLATE(translit(gltype,[abcdefghijklmnopqrstuvwxyz ],[ABCDEFGHIJKLMNOPQRSTUVWXYZ_])[_SUFFIX],
267        [Define to l, ll, u, ul, ull, etc., as suitable for
268         constants of type ']gltype['.])])
269   for gltype in $1 ; do
270     AC_CACHE_CHECK([for $gltype integer literal suffix],
271       [gl_cv_type_${gltype}_suffix],
272       [eval gl_cv_type_${gltype}_suffix=no
273        eval result=\$gl_cv_type_${gltype}_signed
274        if test "$result" = yes; then
275          glsufu=
276        else
277          glsufu=u
278        fi
279        for glsuf in "$glsufu" ${glsufu}l ${glsufu}ll ${glsufu}i64; do
280          case $glsuf in
281            '')  gltype1='int';;
282            l)   gltype1='long int';;
283            ll)  gltype1='long long int';;
284            i64) gltype1='__int64';;
285            u)   gltype1='unsigned int';;
286            ul)  gltype1='unsigned long int';;
287            ull) gltype1='unsigned long long int';;
288            ui64)gltype1='unsigned __int64';;
289          esac
290          AC_COMPILE_IFELSE(
291            [AC_LANG_PROGRAM([$2
292               extern $gltype foo;
293               extern $gltype1 foo;])],
294            [eval gl_cv_type_${gltype}_suffix=\$glsuf])
295          eval test \"\$gl_cv_type_${gltype}_suffix\" != no && break
296        done])
297     GLTYPE=`echo $gltype | tr 'abcdefghijklmnopqrstuvwxyz ' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_'`
298     eval result=\$gl_cv_type_${gltype}_suffix
299     test "$result" = no && result=
300     eval ${GLTYPE}_SUFFIX=\$result
301     AC_DEFINE_UNQUOTED([${GLTYPE}_SUFFIX], $result)
302   done
303   AC_FOREACH([gltype], [$1],
304     [AC_SUBST(translit(gltype,[abcdefghijklmnopqrstuvwxyz ],[ABCDEFGHIJKLMNOPQRSTUVWXYZ_])[_SUFFIX])])
305 ])
306
307 dnl gl_STDINT_INCLUDES
308 AC_DEFUN([gl_STDINT_INCLUDES],
309 [[
310   #include <stddef.h>
311   #include <signal.h>
312   #if HAVE_WCHAR_H
313     /* BSD/OS 4.1 has a bug: <stdio.h> and <time.h> must be included before
314        <wchar.h>.  */
315   # include <stdio.h>
316   # include <time.h>
317   # include <wchar.h>
318   #endif
319 ]])
320
321 dnl gl_STDINT_TYPE_PROPERTIES
322 dnl Compute HAVE_SIGNED_t, BITSIZEOF_t and t_SUFFIX, for all the types t
323 dnl of interest to stdint_.h.
324 AC_DEFUN([gl_STDINT_TYPE_PROPERTIES],
325 [
326   gl_STDINT_BITSIZEOF([ptrdiff_t sig_atomic_t size_t wchar_t wint_t],
327     [gl_STDINT_INCLUDES])
328   gl_CHECK_TYPES_SIGNED([sig_atomic_t wchar_t wint_t],
329     [gl_STDINT_INCLUDES])
330   gl_cv_type_ptrdiff_t_signed=yes
331   gl_cv_type_size_t_signed=no
332   gl_INTEGER_TYPE_SUFFIX([ptrdiff_t sig_atomic_t size_t wchar_t wint_t],
333     [gl_STDINT_INCLUDES])
334 ])