Ticket #2038: stdint.h

File stdint.h, 13.9 KB (added by Sebastian Huber, on 03/30/12 at 14:29:47)

Alternative stdint.h

Line 
1/*
2 * Copyright (c) 2004, 2005 by
3 * Ralf Corsepius, Ulm/Germany. All rights reserved.
4 *
5 * Permission to use, copy, modify, and distribute this software
6 * is freely granted, provided that this notice is preserved.
7 */
8
9#ifndef _STDINT_H
10#define _STDINT_H
11
12#ifdef __cplusplus
13extern "C" {
14#endif
15
16#if defined(__GNUC__) && \
17  ( (__GNUC__ >= 4) || \
18    ( (__GNUC__ >= 3) && defined(__GNUC_MINOR__) && (__GNUC_MINOR__ > 2) ) )
19/* gcc > 3.2 implicitly defines the values we are interested */
20#define __STDINT_EXP(x) __##x##__
21#else
22#define __STDINT_EXP(x) x
23#include <limits.h>
24#endif
25
26/* Check if "long long" is 64bit wide */
27/* Modern GCCs provide __LONG_LONG_MAX__, SUSv3 wants LLONG_MAX */
28#if ( defined(__LONG_LONG_MAX__) && (__LONG_LONG_MAX__ > 0x7fffffff) ) \
29  || ( defined(LLONG_MAX) && (LLONG_MAX > 0x7fffffff) )
30#define __have_longlong64 1
31#endif
32
33/* Check if "long" is 64bit or 32bit wide */
34#if __STDINT_EXP(LONG_MAX) > 0x7fffffff
35#define __have_long64 1
36#elif __STDINT_EXP(LONG_MAX) == 0x7fffffff
37#define __have_long32 1
38#endif
39
40#if __STDINT_EXP(SCHAR_MAX) == 0x7f
41typedef signed char int8_t ;
42typedef unsigned char uint8_t ;
43#define __int8_t_defined 1
44#endif
45
46#if __int8_t_defined
47typedef signed char int_least8_t;
48typedef unsigned char uint_least8_t;
49#define __int_least8_t_defined 1
50#endif
51
52#if __STDINT_EXP(INT_MAX) == 0x7fff
53typedef signed int int16_t;
54typedef unsigned int uint16_t;
55#define __int16_t_defined 1
56#elif __STDINT_EXP(SHRT_MAX) == 0x7fff
57typedef signed short int16_t;
58typedef unsigned short uint16_t;
59#define __int16_t_defined 1
60#elif __STDINT_EXP(SCHAR_MAX) == 0x7fff
61typedef signed char int16_t;
62typedef unsigned char uint16_t;
63#define __int16_t_defined 1
64#endif
65
66#if __int16_t_defined
67typedef int16_t         int_least16_t;
68typedef uint16_t        uint_least16_t;
69#define __int_least16_t_defined 1
70
71#if !__int_least8_t_defined
72typedef int16_t         int_least8_t;
73typedef uint16_t        uint_least8_t;
74#define __int_least8_t_defined 1
75#endif
76#endif
77
78#if __have_long32
79typedef signed long int32_t;
80typedef unsigned long uint32_t;
81#define __int32_t_defined 1
82#elif __STDINT_EXP(INT_MAX) == 0x7fffffffL
83typedef signed int int32_t;
84typedef unsigned int uint32_t;
85#define __int32_t_defined 1
86#elif __STDINT_EXP(SHRT_MAX) == 0x7fffffffL
87typedef signed short int32_t;
88typedef unsigned short uint32_t;
89#define __int32_t_defined 1
90#elif __STDINT_EXP(SCHAR_MAX) == 0x7fffffffL
91typedef signed char int32_t;
92typedef unsigned char uint32_t;
93#define __int32_t_defined 1
94#endif
95
96#if __int32_t_defined
97typedef int32_t         int_least32_t;
98typedef uint32_t        uint_least32_t;
99#define __int_least32_t_defined 1
100
101#if !__int_least8_t_defined
102typedef int32_t         int_least8_t;
103typedef uint32_t        uint_least8_t;
104#define __int_least8_t_defined 1
105#endif
106
107#if !__int_least16_t_defined
108typedef int32_t         int_least16_t;
109typedef uint32_t        uint_least16_t;
110#define __int_least16_t_defined 1
111#endif
112#endif
113
114#if __have_long64
115typedef signed long int64_t;
116typedef unsigned long uint64_t;
117#define __int64_t_defined 1
118#elif __have_longlong64
119typedef signed long long int64_t;
120typedef unsigned long long uint64_t;
121#define __int64_t_defined 1
122#elif  __STDINT_EXP(INT_MAX) > 0x7fffffff
123typedef signed int int64_t;
124typedef unsigned int uint64_t;
125#define __int64_t_defined 1
126#endif
127
128#if __int64_t_defined
129typedef int64_t         int_least64_t;
130typedef uint64_t        uint_least64_t;
131#define __int_least64_t_defined 1
132
133#if !__int_least8_t_defined
134typedef int64_t         int_least8_t;
135typedef uint64_t        uint_least8_t;
136#define __int_least8_t_defined 1
137#endif
138
139#if !__int_least16_t_defined
140typedef int64_t         int_least16_t;
141typedef uint64_t        uint_least16_t;
142#define __int_least16_t_defined 1
143#endif
144
145#if !__int_least32_t_defined
146typedef int64_t         int_least32_t;
147typedef uint64_t        uint_least32_t;
148#define __int_least32_t_defined 1
149#endif
150#endif
151
152/*
153 * Fastest minimum-width integer types
154 *
155 * Assume int to be the fastest type for all types with a width
156 * less than __INT_MAX__ rsp. INT_MAX
157 */
158#if __STDINT_EXP(INT_MAX) >= 0x7f
159  typedef signed int int_fast8_t;
160  typedef unsigned int uint_fast8_t;
161#define __int_fast8_t_defined 1
162#endif
163
164#if __STDINT_EXP(INT_MAX) >= 0x7fff
165  typedef signed int int_fast16_t;
166  typedef unsigned int uint_fast16_t;
167#define __int_fast16_t_defined 1
168#endif
169
170#if __STDINT_EXP(INT_MAX) >= 0x7fffffff
171  typedef signed int int_fast32_t;
172  typedef unsigned int uint_fast32_t;
173#define __int_fast32_t_defined 1
174#endif
175
176#if __STDINT_EXP(INT_MAX) > 0x7fffffff
177  typedef signed int int_fast64_t;
178  typedef unsigned int uint_fast64_t;
179#define __int_fast64_t_defined 1
180#endif
181
182/*
183 * Fall back to [u]int_least<N>_t for [u]int_fast<N>_t types
184 * not having been defined, yet.
185 * Leave undefined, if [u]int_least<N>_t should not be available.
186 */
187#if !__int_fast8_t_defined
188#if __int_least8_t_defined
189  typedef int_least8_t int_fast8_t;
190  typedef uint_least8_t uint_fast8_t;
191#define __int_fast8_t_defined 1
192#endif
193#endif
194
195#if !__int_fast16_t_defined
196#if __int_least16_t_defined
197  typedef int_least16_t int_fast16_t;
198  typedef uint_least16_t uint_fast16_t;
199#define __int_fast16_t_defined 1
200#endif
201#endif
202
203#if !__int_fast32_t_defined
204#if __int_least32_t_defined
205  typedef int_least32_t int_fast32_t;
206  typedef uint_least32_t uint_fast32_t;
207#define __int_fast32_t_defined 1
208#endif
209#endif
210
211#if !__int_fast64_t_defined
212#if __int_least64_t_defined
213  typedef int_least64_t int_fast64_t;
214  typedef uint_least64_t uint_fast64_t;
215#define __int_fast64_t_defined 1
216#endif
217#endif
218
219/* Greatest-width integer types */
220/* Modern GCCs provide __INTMAX_TYPE__ */
221#if defined(__INTMAX_TYPE__)
222  typedef __INTMAX_TYPE__ intmax_t;
223#elif __have_longlong64
224  typedef signed long long intmax_t;
225#else
226  typedef signed long intmax_t;
227#endif
228
229/* Modern GCCs provide __UINTMAX_TYPE__ */
230#if defined(__UINTMAX_TYPE__)
231  typedef __UINTMAX_TYPE__ uintmax_t;
232#elif __have_longlong64
233  typedef unsigned long long uintmax_t;
234#else
235  typedef unsigned long uintmax_t;
236#endif
237
238/* intptr_t */
239#if !defined(__INTPTR_TYPE__)
240 #if defined(__SIZEOF_POINTER__)
241  #if __SIZEOF_POINTER__ == 8
242   typedef int64_t __INTPTR_TYPE__;
243  #elif __SIZEOF_POINTER__ == 4
244   typedef int32_t __INTPTR_TYPE__;
245  #elif __SIZEOF_POINTER__ == 2
246   typedef int16_t __INTPTR_TYPE__;
247  #else
248   #error cannot determine intptr_t
249  #endif
250 #else
251  #if defined(__PTRDIFF_TYPE__)
252   typedef signed __PTRDIFF_TYPE__ __INTPTR_TYPE__;
253  #else
254/* Fallback to long */
255   typedef signed long __INTPTR_TYPE__;
256  #endif
257 #endif
258#endif
259typedef __INTPTR_TYPE__ intptr_t;
260
261/* uintptr_t */
262#if !defined(__UINTPTR_TYPE__)
263 #if defined(__SIZEOF_POINTER__)
264  #if __SIZEOF_POINTER__ == 8
265   typedef uint64_t __UINTPTR_TYPE__;
266  #elif __SIZEOF_POINTER__ == 4
267   typedef uint32_t __UINTPTR_TYPE__;
268  #elif __SIZEOF_POINTER__ == 2
269   typedef uint16_t __UINTPTR_TYPE__;
270  #else
271  #error cannot determine uintptr_t
272  #endif
273 #else
274  #if defined(__PTRDIFF_TYPE__)
275   typedef unsigned __PTRDIFF_TYPE__ __UINTPTR_TYPE__;
276  #else
277/* Fallback to long */
278   typedef unsigned long __UINTPTR_TYPE__;
279  #endif
280 #endif
281#endif
282typedef __UINTPTR_TYPE__ uintptr_t;
283
284/* Limits of pointer types */
285#if defined(__SIZEOF_POINTER__)
286  #if __SIZEOF_POINTER__ == 8
287    #if INT_MAX == 0x7fffffffffffffff
288      #define INTPTR_MAX INT_MAX
289      #define INTPTR_MIN INT_MIN
290      #define UINTPTR_MAX UINT_MAX
291    #else
292      #define INTPTR_MAX INT64_MAX
293      #define INTPTR_MIN INT64_MIN
294      #define UINTPTR_MAX UINT64_MAX
295    #endif
296  #elif __SIZEOF_POINTER__ == 4
297    #if INT_MAX == 0x7fffffff
298      #define INTPTR_MAX INT_MAX
299      #define INTPTR_MIN INT_MIN
300      #define UINTPTR_MAX UINT_MAX
301    #else
302      #define INTPTR_MAX INT32_MAX
303      #define INTPTR_MIN INT32_MIN
304      #define UINTPTR_MAX UINT32_MAX
305    #endif
306  #elif __SIZEOF_POINTER__ == 2
307    #if INT_MAX == 0x7fff
308      #define INTPTR_MAX INT_MAX
309      #define INTPTR_MIN INT_MIN
310      #define UINTPTR_MAX UINT_MAX
311    #else
312      #define INTPTR_MAX INT16_MAX
313      #define INTPTR_MIN INT16_MIN
314      #define UINTPTR_MAX UINT16_MAX
315    #endif
316  #else
317    #error cannot determine intptr_t
318  #endif
319#else
320  #if defined(__PTRDIFF_TYPE__)
321    #define INTPTR_MAX PTRDIFF_MAX
322    #define INTPTR_MIN PTRDIFF_MIN
323    #ifdef __UINTPTR_MAX__
324      #define UINTPTR_MAX __UINTPTR_MAX__
325    #else
326      #define UINTPTR_MAX (2UL * PTRDIFF_MAX + 1)
327    #endif
328  #else
329    /* Fallback to long */
330    #define INTPTR_MAX __STDINT_EXP(LONG_MAX)
331    #define INTPTR_MIN (-__STDINT_EXP(LONG_MAX) - 1)
332    #define UINTPTR_MAX (__STDINT_EXP(LONG_MAX) * 2UL + 1)
333  #endif
334#endif
335
336/* Limits of Specified-Width Integer Types */
337
338#if __int8_t_defined
339#define INT8_MIN        -128
340#define INT8_MAX         127
341#define UINT8_MAX        255
342#endif
343
344#if __int_least8_t_defined
345#define INT_LEAST8_MIN  -128
346#define INT_LEAST8_MAX   127
347#define UINT_LEAST8_MAX  255
348#else
349#error required type int_least8_t missing
350#endif
351
352#if __int16_t_defined
353#define INT16_MIN       -32768
354#define INT16_MAX        32767
355#define UINT16_MAX       65535
356#endif
357
358#if __int_least16_t_defined
359#define INT_LEAST16_MIN -32768
360#define INT_LEAST16_MAX  32767
361#define UINT_LEAST16_MAX 65535
362#else
363#error required type int_least16_t missing
364#endif
365
366#if __int32_t_defined
367#if __have_long32
368#define INT32_MIN        (-2147483647L-1)
369#define INT32_MAX        2147483647L
370#define UINT32_MAX       4294967295UL
371#else
372#define INT32_MIN        (-2147483647-1)
373#define INT32_MAX        2147483647
374#define UINT32_MAX       4294967295U
375#endif
376#endif
377
378#if __int_least32_t_defined
379#if __have_long32
380#define INT_LEAST32_MIN  (-2147483647L-1)
381#define INT_LEAST32_MAX  2147483647L
382#define UINT_LEAST32_MAX 4294967295UL
383#else
384#define INT_LEAST32_MIN  (-2147483647-1)
385#define INT_LEAST32_MAX  2147483647
386#define UINT_LEAST32_MAX 4294967295U
387#endif
388#else
389#error required type int_least32_t missing
390#endif
391
392#if __int64_t_defined
393#if __have_long64
394#define INT64_MIN       (-9223372036854775807L-1L)
395#define INT64_MAX        9223372036854775807L
396#define UINT64_MAX      18446744073709551615U
397#elif __have_longlong64
398#define INT64_MIN       (-9223372036854775807LL-1LL)
399#define INT64_MAX        9223372036854775807LL
400#define UINT64_MAX      18446744073709551615ULL
401#endif
402#endif
403
404#if __int_least64_t_defined
405#if __have_long64
406#define INT_LEAST64_MIN  (-9223372036854775807L-1L)
407#define INT_LEAST64_MAX  9223372036854775807L
408#define UINT_LEAST64_MAX 18446744073709551615U
409#elif __have_longlong64
410#define INT_LEAST64_MIN  (-9223372036854775807LL-1LL)
411#define INT_LEAST64_MAX  9223372036854775807LL
412#define UINT_LEAST64_MAX 18446744073709551615ULL
413#endif
414#endif
415
416#if __int_fast8_t_defined
417#if __STDINT_EXP(INT_MAX) >= 0x7f
418#define INT_FAST8_MIN   (-__STDINT_EXP(INT_MAX)-1)
419#define INT_FAST8_MAX   __STDINT_EXP(INT_MAX)
420#define UINT_FAST8_MAX  (__STDINT_EXP(INT_MAX)*2U+1U)
421#else
422#define INT_FAST8_MIN   INT_LEAST8_MIN
423#define INT_FAST8_MAX   INT_LEAST8_MAX
424#define UINT_FAST8_MAX  UINT_LEAST8_MAX
425#endif
426#endif
427
428#if __int_fast16_t_defined
429#if __STDINT_EXP(INT_MAX) >= 0x7fff
430#define INT_FAST16_MIN  (-__STDINT_EXP(INT_MAX)-1)
431#define INT_FAST16_MAX  __STDINT_EXP(INT_MAX)
432#define UINT_FAST16_MAX (__STDINT_EXP(INT_MAX)*2U+1U)
433#else
434#define INT_FAST16_MIN  INT_LEAST16_MIN
435#define INT_FAST16_MAX  INT_LEAST16_MAX
436#define UINT_FAST16_MAX UINT_LEAST16_MAX
437#endif
438#endif
439
440#if __int_fast32_t_defined
441#if __STDINT_EXP(INT_MAX) >= 0x7fffffff
442#define INT_FAST32_MIN  (-__STDINT_EXP(INT_MAX)-1)
443#define INT_FAST32_MAX  __STDINT_EXP(INT_MAX)
444#define UINT_FAST32_MAX (__STDINT_EXP(INT_MAX)*2U+1U)
445#else
446#define INT_FAST32_MIN  INT_LEAST32_MIN
447#define INT_FAST32_MAX  INT_LEAST32_MAX
448#define UINT_FAST32_MAX UINT_LEAST32_MAX
449#endif
450#endif
451
452#if __int_fast64_t_defined
453#if __STDINT_EXP(INT_MAX) > 0x7fffffff
454#define INT_FAST64_MIN  (-__STDINT_EXP(INT_MAX)-1)
455#define INT_FAST64_MAX  __STDINT_EXP(INT_MAX)
456#define UINT_FAST64_MAX (__STDINT_EXP(INT_MAX)*2U+1U)
457#else
458#define INT_FAST64_MIN  INT_LEAST64_MIN
459#define INT_FAST64_MAX  INT_LEAST64_MAX
460#define UINT_FAST64_MAX UINT_LEAST64_MAX
461#endif
462#endif
463
464#ifdef __INTMAX_MAX__
465#define INTMAX_MAX __INTMAX_MAX__
466#define INTMAX_MIN (-INTMAX_MAX - 1)
467#elif defined(__INTMAX_TYPE__)
468/* All relevant GCC versions prefer long to long long for intmax_t.  */
469#define INTMAX_MAX INT64_MAX
470#define INTMAX_MIN INT64_MIN
471#endif
472
473#ifdef __UINTMAX_MAX__
474#define UINTMAX_MAX __UINTMAX_MAX__
475#elif defined(__UINTMAX_TYPE__)
476/* All relevant GCC versions prefer long to long long for intmax_t.  */
477#define UINTMAX_MAX UINT64_MAX
478#endif
479
480/* This must match size_t in stddef.h, currently long unsigned int */
481#ifdef __SIZE_MAX__
482#define SIZE_MAX __SIZE_MAX__
483#elif defined(__SIZEOF_SIZE_T__) && defined(__CHAR_BIT__)
484#define SIZE_MAX (((1UL << (__SIZEOF_SIZE_T__ * __CHAR_BIT__ - 1)) - 1) * 2 + 1)
485#else
486#define SIZE_MAX (__STDINT_EXP(LONG_MAX) * 2UL + 1)
487#endif
488
489/* This must match sig_atomic_t in <signal.h> (currently int) */
490#define SIG_ATOMIC_MIN (-__STDINT_EXP(INT_MAX) - 1)
491#define SIG_ATOMIC_MAX __STDINT_EXP(INT_MAX)
492
493/* This must match ptrdiff_t  in <stddef.h> (currently long int) */
494#ifdef __PTRDIFF_MAX__
495#define PTRDIFF_MAX __PTRDIFF_MAX__
496#else
497#define PTRDIFF_MAX __STDINT_EXP(LONG_MAX)
498#endif
499#define PTRDIFF_MIN (-PTRDIFF_MAX - 1)
500
501#ifdef __WCHAR_MAX__
502#define WCHAR_MAX __WCHAR_MAX__
503#endif
504#ifdef __WCHAR_MIN__
505#define WCHAR_MIN __WCHAR_MIN__
506#endif
507
508/* wint_t is unsigned int on almost all GCC targets.  */
509#ifdef __WINT_MAX__
510#define WINT_MAX __WINT_MAX__
511#else
512#define WINT_MAX (__STDINT_EXP(INT_MAX) * 2U + 1U)
513#endif
514#ifdef __WINT_MIN__
515#define WINT_MIN __WINT_MIN__
516#else
517#define WINT_MIN 0U
518#endif
519
520/** Macros for minimum-width integer constant expressions */
521#define INT8_C(x)       x
522#if __STDINT_EXP(INT_MAX) > 0x7f
523#define UINT8_C(x)      x
524#else
525#define UINT8_C(x)      x##U
526#endif
527
528#define INT16_C(x)      x
529#if __STDINT_EXP(INT_MAX) > 0x7fff
530#define UINT16_C(x)     x
531#else
532#define UINT16_C(x)     x##U
533#endif
534
535#if __have_long32
536#define INT32_C(x)      x##L
537#define UINT32_C(x)     x##UL
538#else
539#define INT32_C(x)      x
540#define UINT32_C(x)     x##U
541#endif
542
543#if __int64_t_defined
544#if __have_long64
545#define INT64_C(x)      x##L
546#define UINT64_C(x)     x##UL
547#else
548#define INT64_C(x)      x##LL
549#define UINT64_C(x)     x##ULL
550#endif
551#endif
552
553/** Macros for greatest-width integer constant expression */
554#if __have_long64
555#define INTMAX_C(x)     x##L
556#define UINTMAX_C(x)    x##UL
557#else
558#define INTMAX_C(x)     x##LL
559#define UINTMAX_C(x)    x##ULL
560#endif
561
562
563#ifdef __cplusplus
564}
565#endif
566
567#endif /* _STDINT_H */