xref: /netbsd-src/external/gpl2/xcvs/dist/lib/stdint_.h (revision a7c918477dd5f12c1da816ba05caf44eab2d06d6)
1*a7c91847Schristos /* Copyright (C) 2001-2002, 2004-2005 Free Software Foundation, Inc.
2*a7c91847Schristos    Written by Bruno Haible, Sam Steingold, Peter Burwood.
3*a7c91847Schristos    This file is part of gnulib.
4*a7c91847Schristos 
5*a7c91847Schristos    This program is free software; you can redistribute it and/or modify
6*a7c91847Schristos    it under the terms of the GNU General Public License as published by
7*a7c91847Schristos    the Free Software Foundation; either version 2, or (at your option)
8*a7c91847Schristos    any later version.
9*a7c91847Schristos 
10*a7c91847Schristos    This program is distributed in the hope that it will be useful,
11*a7c91847Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
12*a7c91847Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*a7c91847Schristos    GNU General Public License for more details.
14*a7c91847Schristos 
15*a7c91847Schristos    You should have received a copy of the GNU General Public License
16*a7c91847Schristos    along with this program; if not, write to the Free Software Foundation,
17*a7c91847Schristos    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18*a7c91847Schristos 
19*a7c91847Schristos #ifndef _STDINT_H
20*a7c91847Schristos #define _STDINT_H
21*a7c91847Schristos 
22*a7c91847Schristos /*
23*a7c91847Schristos  * ISO C 99 <stdint.h> for platforms that lack it.
24*a7c91847Schristos  * <http://www.opengroup.org/onlinepubs/007904975/basedefs/stdint.h.html>
25*a7c91847Schristos  */
26*a7c91847Schristos 
27*a7c91847Schristos /* Get wchar_t, WCHAR_MIN, WCHAR_MAX.  */
28*a7c91847Schristos #include <stddef.h>
29*a7c91847Schristos /* Get CHAR_BIT, LONG_MIN, LONG_MAX, ULONG_MAX.  */
30*a7c91847Schristos #include <limits.h>
31*a7c91847Schristos 
32*a7c91847Schristos /* Get those types that are already defined in other system include files.  */
33*a7c91847Schristos #if defined(__FreeBSD__)
34*a7c91847Schristos # include <sys/inttypes.h>
35*a7c91847Schristos #endif
36*a7c91847Schristos #if defined(__sun) && HAVE_SYS_INTTYPES_H
37*a7c91847Schristos # include <sys/inttypes.h>
38*a7c91847Schristos   /* Solaris 7 <sys/inttypes.h> has the types except the *_fast*_t types, and
39*a7c91847Schristos      the macros except for *_FAST*_*, INTPTR_MIN, PTRDIFF_MIN, PTRDIFF_MAX.
40*a7c91847Schristos      But note that <sys/int_types.h> contains only the type definitions!  */
41*a7c91847Schristos # define _STDINT_H_HAVE_SYSTEM_INTTYPES
42*a7c91847Schristos #endif
43*a7c91847Schristos #if (defined(__hpux) || defined(_AIX)) && HAVE_INTTYPES_H
44*a7c91847Schristos # include <inttypes.h>
45*a7c91847Schristos   /* HP-UX 10 <inttypes.h> has nearly everything, except UINT_LEAST8_MAX,
46*a7c91847Schristos      UINT_FAST8_MAX, PTRDIFF_MIN, PTRDIFF_MAX.  */
47*a7c91847Schristos   /* AIX 4 <inttypes.h> has nearly everything, except INTPTR_MIN, INTPTR_MAX,
48*a7c91847Schristos      UINTPTR_MAX, PTRDIFF_MIN, PTRDIFF_MAX.  */
49*a7c91847Schristos # define _STDINT_H_HAVE_SYSTEM_INTTYPES
50*a7c91847Schristos #endif
51*a7c91847Schristos #if !(defined(UNIX_CYGWIN32) && defined(__BIT_TYPES_DEFINED__))
52*a7c91847Schristos # define _STDINT_H_NEED_SIGNED_INT_TYPES
53*a7c91847Schristos #endif
54*a7c91847Schristos 
55*a7c91847Schristos #if !defined(_STDINT_H_HAVE_SYSTEM_INTTYPES)
56*a7c91847Schristos 
57*a7c91847Schristos /* 7.18.1.1. Exact-width integer types */
58*a7c91847Schristos 
59*a7c91847Schristos #if !defined(__FreeBSD__)
60*a7c91847Schristos 
61*a7c91847Schristos #ifdef _STDINT_H_NEED_SIGNED_INT_TYPES
62*a7c91847Schristos typedef signed char    int8_t;
63*a7c91847Schristos #endif
64*a7c91847Schristos typedef unsigned char  uint8_t;
65*a7c91847Schristos 
66*a7c91847Schristos #ifdef _STDINT_H_NEED_SIGNED_INT_TYPES
67*a7c91847Schristos typedef short          int16_t;
68*a7c91847Schristos #endif
69*a7c91847Schristos typedef unsigned short uint16_t;
70*a7c91847Schristos 
71*a7c91847Schristos #ifdef _STDINT_H_NEED_SIGNED_INT_TYPES
72*a7c91847Schristos typedef int            int32_t;
73*a7c91847Schristos #endif
74*a7c91847Schristos typedef unsigned int   uint32_t;
75*a7c91847Schristos 
76*a7c91847Schristos #if @HAVE_LONG_64BIT@
77*a7c91847Schristos #ifdef _STDINT_H_NEED_SIGNED_INT_TYPES
78*a7c91847Schristos typedef long           int64_t;
79*a7c91847Schristos #endif
80*a7c91847Schristos typedef unsigned long  uint64_t;
81*a7c91847Schristos #define _STDINT_H_HAVE_INT64
82*a7c91847Schristos #elif @HAVE_LONG_LONG_64BIT@
83*a7c91847Schristos #ifdef _STDINT_H_NEED_SIGNED_INT_TYPES
84*a7c91847Schristos typedef long long          int64_t;
85*a7c91847Schristos #endif
86*a7c91847Schristos typedef unsigned long long uint64_t;
87*a7c91847Schristos #define _STDINT_H_HAVE_INT64
88*a7c91847Schristos #elif defined(_MSC_VER)
89*a7c91847Schristos typedef __int64          int64_t;
90*a7c91847Schristos typedef unsigned __int64 uint64_t;
91*a7c91847Schristos #define _STDINT_H_HAVE_INT64
92*a7c91847Schristos #endif
93*a7c91847Schristos 
94*a7c91847Schristos #endif /* !FreeBSD */
95*a7c91847Schristos 
96*a7c91847Schristos /* 7.18.1.2. Minimum-width integer types */
97*a7c91847Schristos 
98*a7c91847Schristos typedef int8_t   int_least8_t;
99*a7c91847Schristos typedef uint8_t  uint_least8_t;
100*a7c91847Schristos typedef int16_t  int_least16_t;
101*a7c91847Schristos typedef uint16_t uint_least16_t;
102*a7c91847Schristos typedef int32_t  int_least32_t;
103*a7c91847Schristos typedef uint32_t uint_least32_t;
104*a7c91847Schristos #ifdef _STDINT_H_HAVE_INT64
105*a7c91847Schristos typedef int64_t  int_least64_t;
106*a7c91847Schristos typedef uint64_t uint_least64_t;
107*a7c91847Schristos #endif
108*a7c91847Schristos 
109*a7c91847Schristos /* 7.18.1.3. Fastest minimum-width integer types */
110*a7c91847Schristos 
111*a7c91847Schristos typedef int32_t  int_fast8_t;
112*a7c91847Schristos typedef uint32_t uint_fast8_t;
113*a7c91847Schristos typedef int32_t  int_fast16_t;
114*a7c91847Schristos typedef uint32_t uint_fast16_t;
115*a7c91847Schristos typedef int32_t  int_fast32_t;
116*a7c91847Schristos typedef uint32_t uint_fast32_t;
117*a7c91847Schristos #ifdef _STDINT_H_HAVE_INT64
118*a7c91847Schristos typedef int64_t  int_fast64_t;
119*a7c91847Schristos typedef uint64_t uint_fast64_t;
120*a7c91847Schristos #endif
121*a7c91847Schristos 
122*a7c91847Schristos /* 7.18.1.4. Integer types capable of holding object pointers */
123*a7c91847Schristos 
124*a7c91847Schristos #if !defined(__FreeBSD__)
125*a7c91847Schristos 
126*a7c91847Schristos /* On some platforms (like IRIX6 MIPS with -n32) sizeof(void*) < sizeof(long),
127*a7c91847Schristos    but this doesn't matter here.  */
128*a7c91847Schristos typedef long          intptr_t;
129*a7c91847Schristos typedef unsigned long uintptr_t;
130*a7c91847Schristos 
131*a7c91847Schristos #endif /* !FreeBSD */
132*a7c91847Schristos 
133*a7c91847Schristos /* 7.18.1.5. Greatest-width integer types */
134*a7c91847Schristos 
135*a7c91847Schristos #ifdef _STDINT_H_HAVE_INT64
136*a7c91847Schristos typedef int64_t  intmax_t;
137*a7c91847Schristos typedef uint64_t uintmax_t;
138*a7c91847Schristos #else
139*a7c91847Schristos typedef int32_t  intmax_t;
140*a7c91847Schristos typedef uint32_t uintmax_t;
141*a7c91847Schristos #endif
142*a7c91847Schristos 
143*a7c91847Schristos /* 7.18.2. Limits of specified-width integer types */
144*a7c91847Schristos 
145*a7c91847Schristos #if !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS)
146*a7c91847Schristos 
147*a7c91847Schristos /* 7.18.2.1. Limits of exact-width integer types */
148*a7c91847Schristos 
149*a7c91847Schristos #define INT8_MIN  -128
150*a7c91847Schristos #define INT8_MAX   127
151*a7c91847Schristos #define UINT8_MAX  255U
152*a7c91847Schristos #define INT16_MIN  -32768
153*a7c91847Schristos #define INT16_MAX   32767
154*a7c91847Schristos #define UINT16_MAX  65535U
155*a7c91847Schristos #define INT32_MIN   (~INT32_MAX)
156*a7c91847Schristos #define INT32_MAX   2147483647
157*a7c91847Schristos #define UINT32_MAX  4294967295U
158*a7c91847Schristos #ifdef _STDINT_H_HAVE_INT64
159*a7c91847Schristos #define INT64_MIN   (~INT64_MAX)
160*a7c91847Schristos #if @HAVE_LONG_64BIT@
161*a7c91847Schristos #define INT64_MAX   9223372036854775807L
162*a7c91847Schristos #define UINT64_MAX 18446744073709551615UL
163*a7c91847Schristos #elif @HAVE_LONG_LONG_64BIT@
164*a7c91847Schristos #define INT64_MAX   9223372036854775807LL
165*a7c91847Schristos #define UINT64_MAX 18446744073709551615ULL
166*a7c91847Schristos #elif defined(_MSC_VER)
167*a7c91847Schristos #define INT64_MAX   9223372036854775807i64
168*a7c91847Schristos #define UINT64_MAX 18446744073709551615ui64
169*a7c91847Schristos #endif
170*a7c91847Schristos #endif
171*a7c91847Schristos 
172*a7c91847Schristos /* 7.18.2.2. Limits of minimum-width integer types */
173*a7c91847Schristos 
174*a7c91847Schristos #define INT_LEAST8_MIN INT8_MIN
175*a7c91847Schristos #define INT_LEAST8_MAX INT8_MAX
176*a7c91847Schristos #define UINT_LEAST8_MAX UINT8_MAX
177*a7c91847Schristos #define INT_LEAST16_MIN INT16_MIN
178*a7c91847Schristos #define INT_LEAST16_MAX INT16_MAX
179*a7c91847Schristos #define UINT_LEAST16_MAX UINT16_MAX
180*a7c91847Schristos #define INT_LEAST32_MIN INT32_MIN
181*a7c91847Schristos #define INT_LEAST32_MAX INT32_MAX
182*a7c91847Schristos #define UINT_LEAST32_MAX UINT32_MAX
183*a7c91847Schristos #ifdef _STDINT_H_HAVE_INT64
184*a7c91847Schristos #define INT_LEAST64_MIN INT64_MIN
185*a7c91847Schristos #define INT_LEAST64_MAX INT64_MAX
186*a7c91847Schristos #define UINT_LEAST64_MAX UINT64_MAX
187*a7c91847Schristos #endif
188*a7c91847Schristos 
189*a7c91847Schristos /* 7.18.2.3. Limits of fastest minimum-width integer types */
190*a7c91847Schristos 
191*a7c91847Schristos #define INT_FAST8_MIN INT32_MIN
192*a7c91847Schristos #define INT_FAST8_MAX INT32_MAX
193*a7c91847Schristos #define UINT_FAST8_MAX UINT32_MAX
194*a7c91847Schristos #define INT_FAST16_MIN INT32_MIN
195*a7c91847Schristos #define INT_FAST16_MAX INT32_MAX
196*a7c91847Schristos #define UINT_FAST16_MAX UINT32_MAX
197*a7c91847Schristos #define INT_FAST32_MIN INT32_MIN
198*a7c91847Schristos #define INT_FAST32_MAX INT32_MAX
199*a7c91847Schristos #define UINT_FAST32_MAX UINT32_MAX
200*a7c91847Schristos #ifdef _STDINT_H_HAVE_INT64
201*a7c91847Schristos #define INT_FAST64_MIN INT64_MIN
202*a7c91847Schristos #define INT_FAST64_MAX INT64_MAX
203*a7c91847Schristos #define UINT_FAST64_MAX UINT64_MAX
204*a7c91847Schristos #endif
205*a7c91847Schristos 
206*a7c91847Schristos /* 7.18.2.4. Limits of integer types capable of holding object pointers */
207*a7c91847Schristos 
208*a7c91847Schristos #define INTPTR_MIN LONG_MIN
209*a7c91847Schristos #define INTPTR_MAX LONG_MAX
210*a7c91847Schristos #define UINTPTR_MAX ULONG_MAX
211*a7c91847Schristos 
212*a7c91847Schristos /* 7.18.2.5. Limits of greatest-width integer types */
213*a7c91847Schristos 
214*a7c91847Schristos #ifdef _STDINT_H_HAVE_INT64
215*a7c91847Schristos #define INTMAX_MIN INT64_MIN
216*a7c91847Schristos #define INTMAX_MAX INT64_MAX
217*a7c91847Schristos #define UINTMAX_MAX UINT64_MAX
218*a7c91847Schristos #else
219*a7c91847Schristos #define INTMAX_MIN INT32_MIN
220*a7c91847Schristos #define INTMAX_MAX INT32_MAX
221*a7c91847Schristos #define UINTMAX_MAX UINT32_MAX
222*a7c91847Schristos #endif
223*a7c91847Schristos 
224*a7c91847Schristos /* 7.18.3. Limits of other integer types */
225*a7c91847Schristos 
226*a7c91847Schristos #define PTRDIFF_MIN (~(ptrdiff_t)0 << (sizeof(ptrdiff_t)*CHAR_BIT-1))
227*a7c91847Schristos #define PTRDIFF_MAX (~PTRDIFF_MIN)
228*a7c91847Schristos 
229*a7c91847Schristos /* This may be wrong...  */
230*a7c91847Schristos #define SIG_ATOMIC_MIN 0
231*a7c91847Schristos #define SIG_ATOMIC_MAX 127
232*a7c91847Schristos 
233*a7c91847Schristos #define SIZE_MAX (~(size_t)0)
234*a7c91847Schristos 
235*a7c91847Schristos /* wchar_t limits already defined in <stddef.h>.  */
236*a7c91847Schristos /* wint_t limits already defined in <wchar.h>.  */
237*a7c91847Schristos 
238*a7c91847Schristos #endif
239*a7c91847Schristos 
240*a7c91847Schristos /* 7.18.4. Macros for integer constants */
241*a7c91847Schristos 
242*a7c91847Schristos #if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS)
243*a7c91847Schristos 
244*a7c91847Schristos /* 7.18.4.1. Macros for minimum-width integer constants */
245*a7c91847Schristos 
246*a7c91847Schristos #define INT8_C(x) x
247*a7c91847Schristos #define UINT8_C(x) x##U
248*a7c91847Schristos #define INT16_C(x) x
249*a7c91847Schristos #define UINT16_C(x) x##U
250*a7c91847Schristos #define INT32_C(x) x
251*a7c91847Schristos #define UINT32_C(x) x##U
252*a7c91847Schristos #if @HAVE_LONG_64BIT@
253*a7c91847Schristos #define INT64_C(x) x##L
254*a7c91847Schristos #define UINT64_C(x) x##UL
255*a7c91847Schristos #elif @HAVE_LONG_LONG_64BIT@
256*a7c91847Schristos #define INT64_C(x) x##LL
257*a7c91847Schristos #define UINT64_C(x) x##ULL
258*a7c91847Schristos #elif defined(_MSC_VER)
259*a7c91847Schristos #define INT64_C(x) x##i64
260*a7c91847Schristos #define UINT64_C(x) x##ui64
261*a7c91847Schristos #endif
262*a7c91847Schristos 
263*a7c91847Schristos /* 7.18.4.2. Macros for greatest-width integer constants */
264*a7c91847Schristos 
265*a7c91847Schristos #if @HAVE_LONG_64BIT@
266*a7c91847Schristos #define INTMAX_C(x) x##L
267*a7c91847Schristos #define UINTMAX_C(x) x##UL
268*a7c91847Schristos #elif @HAVE_LONG_LONG_64BIT@
269*a7c91847Schristos #define INTMAX_C(x) x##LL
270*a7c91847Schristos #define UINTMAX_C(x) x##ULL
271*a7c91847Schristos #elif defined(_MSC_VER)
272*a7c91847Schristos #define INTMAX_C(x) x##i64
273*a7c91847Schristos #define UINTMAX_C(x) x##ui64
274*a7c91847Schristos #else
275*a7c91847Schristos #define INTMAX_C(x) x
276*a7c91847Schristos #define UINTMAX_C(x) x##U
277*a7c91847Schristos #endif
278*a7c91847Schristos 
279*a7c91847Schristos #endif
280*a7c91847Schristos 
281*a7c91847Schristos #endif  /* !_STDINT_H_HAVE_SYSTEM_INTTYPES */
282*a7c91847Schristos 
283*a7c91847Schristos #endif /* _STDINT_H */
284