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 /*
10  * @todo - Add fast<N>_t types.
11  * @todo - Add support for wint_t types.
12  */
13 
14 #ifndef _STDINT_H
15 #define _STDINT_H
16 
17 #include <sys/types.h>
18 #include <bits/wordsize.h>
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #if defined(__GNUC__) && (__GNUC__ >= 3 ) \
25   && defined(__GNUC_MINOR__) && (__GNUC_MINOR__ > 2 )
26 #define __STDINT_EXP(x) __##x##__
27 #else
28 #define __STDINT_EXP(x) x
29 #include <limits.h>
30 #endif
31 
32 #if __STDINT_EXP(SCHAR_MAX) == 0x7f
33 #define __int8_t_defined 1
34 #endif
35 
36 #if __int8_t_defined
37 typedef signed char int_least8_t;
38 typedef unsigned char uint_least8_t;
39 #define __int_least8_t_defined 1
40 #endif
41 
42 #if __STDINT_EXP(SHRT_MAX) == 0x7fff
43 #define __int16_t_defined 1
44 #elif __STDINT_EXP(INT_MAX) == 0x7fff
45 #define __int16_t_defined 1
46 #elif __STDINT_EXP(SCHAR_MAX) == 0x7fff
47 #define __int16_t_defined 1
48 #endif
49 
50 #if __int16_t_defined
51 typedef int16_t   	int_least16_t;
52 typedef uint16_t 	uint_least16_t;
53 #define __int_least16_t_defined 1
54 
55 #ifndef __int_least8_t_defined
56 typedef int16_t	   	int_least8_t;
57 typedef uint16_t  	uint_least8_t;
58 #define __int_least8_t_defined 1
59 #endif
60 #endif
61 
62 #if __STDINT_EXP(INT_MAX) == 0x7fffffffL
63 #define __int32_t_defined 1
64 #elif __STDINT_EXP(LONG_MAX) == 0x7fffffffL
65 #define __int32_t_defined 1
66 #define __have_long32 1
67 #elif __STDINT_EXP(SHRT_MAX) == 0x7fffffffL
68 #define __int32_t_defined 1
69 #elif __STDINT_EXP(SCHAR_MAX) == 0x7fffffffL
70 #define __int32_t_defined 1
71 #endif
72 
73 #if __int32_t_defined
74 typedef int32_t   	int_least32_t;
75 typedef uint32_t 	uint_least32_t;
76 #define __int_least32_t_defined 1
77 
78 #ifndef __int_least8_t_defined
79 typedef int32_t	   	int_least8_t;
80 typedef uint32_t  	uint_least8_t;
81 #define __int_least8_t_defined 1
82 #endif
83 
84 #ifndef __int_least16_t_defined
85 typedef int32_t	   	int_least16_t;
86 typedef uint32_t  	uint_least16_t;
87 #define __int_least16_t_defined 1
88 #endif
89 #endif
90 
91 /* Fast types.  */
92 
93 /* Signed.  */
94 typedef signed char             int_fast8_t;
95 #if __WORDSIZE == 64
96 typedef long int                int_fast16_t;
97 typedef long int                int_fast32_t;
98 typedef long int                int_fast64_t;
99 #else
100 typedef int                     int_fast16_t;
101 typedef int                     int_fast32_t;
102 __extension__
103 typedef long long int           int_fast64_t;
104 #endif
105 
106 /* Unsigned.  */
107 typedef unsigned char           uint_fast8_t;
108 #if __WORDSIZE == 64
109 typedef unsigned long int       uint_fast16_t;
110 typedef unsigned long int       uint_fast32_t;
111 typedef unsigned long int       uint_fast64_t;
112 #else
113 typedef unsigned int            uint_fast16_t;
114 typedef unsigned int            uint_fast32_t;
115 __extension__
116 typedef unsigned long long int  uint_fast64_t;
117 #endif
118 
119 #if __STDINT_EXP(LONG_MAX) > 0x7fffffff
120 #define __int64_t_defined 1
121 #define __have_long64 1
122 #elif  defined(__LONG_LONG_MAX__) && (__LONG_LONG_MAX__ > 0x7fffffff)
123 #define __int64_t_defined 1
124 #define __have_longlong64 1
125 #elif  defined(LLONG_MAX) && (LLONG_MAX > 0x7fffffff)
126 #define __int64_t_defined 1
127 #define __have_longlong64 1
128 #elif  __STDINT_EXP(INT_MAX) > 0x7fffffff
129 #define __int64_t_defined 1
130 #endif
131 
132 #if __int64_t_defined
133 typedef int64_t   	int_least64_t;
134 typedef uint64_t 	uint_least64_t;
135 #define __int_least64_t_defined 1
136 
137 #ifndef __int_least8_t_defined
138 typedef int64_t	   	int_least8_t;
139 typedef uint64_t  	uint_least8_t;
140 #define __int_least8_t_defined 1
141 #endif
142 
143 #ifndef __int_least16_t_defined
144 typedef int64_t	   	int_least16_t;
145 typedef uint64_t  	uint_least16_t;
146 #define __int_least16_t_defined 1
147 #endif
148 
149 #ifndef __int_least32_t_defined
150 typedef int64_t	   	int_least32_t;
151 typedef uint64_t  	uint_least32_t;
152 #define __int_least32_t_defined 1
153 #endif
154 #endif
155 
156 /* Greatest-width integer types */
157 /* Modern GCCs provide __INTMAX_TYPE__ */
158 #if defined(__INTMAX_TYPE__)
159   typedef __INTMAX_TYPE__ intmax_t;
160 #elif __have_longlong64
161   typedef signed long long intmax_t;
162 #else
163   typedef signed long intmax_t;
164 #endif
165 
166 /* Modern GCCs provide __UINTMAX_TYPE__ */
167 #if defined(__UINTMAX_TYPE__)
168   typedef __UINTMAX_TYPE__ uintmax_t;
169 #elif __have_longlong64
170   typedef unsigned long long uintmax_t;
171 #else
172   typedef unsigned long uintmax_t;
173 #endif
174 
175 /* Limits of Specified-Width Integer Types */
176 
177 #if __int8_t_defined
178 #define INT8_MIN 	-128
179 #define INT8_MAX 	 127
180 #define UINT8_MAX 	 255
181 #endif
182 
183 #if __int_least8_t_defined
184 #define INT_LEAST8_MIN 	-128
185 #define INT_LEAST8_MAX 	 127
186 #define UINT_LEAST8_MAX	 255
187 #else
188 #error required type int_least8_t missing
189 #endif
190 
191 #if __int16_t_defined
192 #define INT16_MIN 	-32768
193 #define INT16_MAX 	 32767
194 #define UINT16_MAX 	 65535
195 #endif
196 
197 #if __int_least16_t_defined
198 #define INT_LEAST16_MIN	-32768
199 #define INT_LEAST16_MAX	 32767
200 #define UINT_LEAST16_MAX 65535
201 #else
202 #error required type int_least16_t missing
203 #endif
204 
205 #if __int32_t_defined
206 #define INT32_MIN 	 (-2147483647-1)
207 #define INT32_MAX 	 2147483647
208 #define UINT32_MAX       4294967295U
209 #endif
210 
211 #if __int_least32_t_defined
212 #define INT_LEAST32_MIN  (-2147483647-1)
213 #define INT_LEAST32_MAX  2147483647
214 #define UINT_LEAST32_MAX 4294967295U
215 #else
216 #error required type int_least32_t missing
217 #endif
218 
219 #if __int64_t_defined
220 #ifdef __have_long64
221 #define INT64_MIN 	(-9223372036854775807L-1L)
222 #define INT64_MAX 	 9223372036854775807L
223 #define UINT64_MAX 	18446744073709551615U
224 #elif defined(__have_longlong64)
225 #define INT64_MIN 	(-9223372036854775807LL-1LL)
226 #define INT64_MAX 	 9223372036854775807LL
227 #define UINT64_MAX 	18446744073709551615ULL
228 #endif
229 #endif
230 
231 #if __int_least64_t_defined
232 #ifdef __have_long64
233 #define INT_LEAST64_MIN  (-9223372036854775807L-1L)
234 #define INT_LEAST64_MAX  9223372036854775807L
235 #define UINT_LEAST64_MAX 18446744073709551615U
236 #elif defined(__have_longlong64)
237 #define INT_LEAST64_MIN  (-9223372036854775807LL-1LL)
238 #define INT_LEAST64_MAX  9223372036854775807LL
239 #define UINT_LEAST64_MAX 18446744073709551615ULL
240 #endif
241 #endif
242 
243 /* This must match size_t in stddef.h, currently long unsigned int */
244 #define SIZE_MAX (__STDINT_EXP(LONG_MAX) * 2UL + 1)
245 
246 /* This must match sig_atomic_t in <signal.h> (currently int) */
247 #define SIG_ATOMIC_MIN (-__STDINT_EXP(INT_MAX) - 1)
248 #define SIG_ATOMIC_MAX __STDINT_EXP(INT_MAX)
249 
250 /* This must match ptrdiff_t  in <stddef.h> (currently long int) */
251 #define PTRDIFF_MIN (-__STDINT_EXP(LONG_MAX) - 1L)
252 #define PTRDIFF_MAX __STDINT_EXP(LONG_MAX)
253 
254 /** Macros for minimum-width integer constant expressions */
255 #define INT8_C(x)	x
256 #define UINT8_C(x)	x##U
257 
258 #define INT16_C(x)	x
259 #define UINT16_C(x)	x##U
260 
261 #if __have_long32
262 #define INT32_C(x)	x##L
263 #define UINT32_C(x)	x##UL
264 #else
265 #define INT32_C(x)	x
266 #define UINT32_C(x)	x##U
267 #endif
268 
269 #if __int64_t_defined
270 #if __have_longlong64
271 #define INT64_C(x)	x##LL
272 #define UINT64_C(x)	x##ULL
273 #else
274 #define INT64_C(x)	x##L
275 #define UINT64_C(x)	x##UL
276 #endif
277 #endif
278 
279 /** Macros for greatest-width integer constant expression */
280 #if __have_longlong64
281 #define INTMAX_C(x)	x##LL
282 #define UINTMAX_C(x)	x##ULL
283 #else
284 #define INTMAX_C(x)	x##L
285 #define UINTMAX_C(x)	x##UL
286 #endif
287 
288 
289 #ifdef __cplusplus
290 }
291 #endif
292 
293 #endif /* _STDINT_H */
294