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  *  @file  inttypes.h
11  */
12 
13 #ifndef _INTTYPES_H
14 #define _INTTYPES_H
15 
16 #include <sys/cdefs.h>
17 #include <stdint.h>
18 
19 _BEGIN_STD_C
20 
21 #if __BSD_VISIBLE
22 #include <sys/_locale.h>
23 #endif
24 
25 typedef struct {
26   intmax_t	quot;
27   intmax_t	rem;
28 } imaxdiv_t;
29 
30 /*
31  * Try to avoid defining wchar_t by using __WCHAR_TYPE__ when
32  * available.
33  */
34 
35 #ifdef __WCHAR_TYPE__
36 typedef __WCHAR_TYPE__ _wchar_t;
37 #else
38 #define __need_wchar_t
39 #include <stddef.h>
40 typdef wchar_t _wchar_t;
41 #endif
42 
43 #define __STRINGIFY(a) #a
44 
45 /* 8-bit types */
46 #define __PRI8(x) __INT8 __STRINGIFY(x)
47 #define __PRI8LEAST(x) __LEAST8 __STRINGIFY(x)
48 #define __PRI8FAST(x) __FAST8 __STRINGIFY(x)
49 
50 /* NOTICE: scanning 8-bit types requires use of the hh specifier
51  * which is only supported on picolibc platforms that
52  * are built with C99 I/O format support enabled.  If the flag in
53  * picolibc.h hasn't been set during configuration to indicate this, the 8-bit
54  * scanning format macros are disabled here as they result in undefined
55  * behaviour which can include memory overwrite.  Overriding the flag after the
56  * library has been built is not recommended as it will expose the underlying
57  * undefined behaviour.
58  */
59 
60 #if defined(_WANT_IO_C99_FORMATS)
61   #define __SCN8(x) __INT8 __STRINGIFY(x)
62 	#define __SCN8LEAST(x) __LEAST8 __STRINGIFY(x)
63 	#define __SCN8FAST(x) __FAST8 __STRINGIFY(x)
64 #endif /* _WANT_IO_C99_FORMATS */
65 
66 
67 #define PRId8		__PRI8(d)
68 #define PRIi8		__PRI8(i)
69 #define PRIo8		__PRI8(o)
70 #define PRIu8		__PRI8(u)
71 #define PRIx8		__PRI8(x)
72 #define PRIX8		__PRI8(X)
73 
74 /* Macros below are only enabled for a newlib built with C99 I/O format support. */
75 #if defined(_WANT_IO_C99_FORMATS)
76 
77 #define SCNd8		__SCN8(d)
78 #define SCNi8		__SCN8(i)
79 #define SCNo8		__SCN8(o)
80 #define SCNu8		__SCN8(u)
81 #define SCNx8		__SCN8(x)
82 
83 #endif /* _WANT_IO_C99_FORMATS */
84 
85 
86 #define PRIdLEAST8	__PRI8LEAST(d)
87 #define PRIiLEAST8	__PRI8LEAST(i)
88 #define PRIoLEAST8	__PRI8LEAST(o)
89 #define PRIuLEAST8	__PRI8LEAST(u)
90 #define PRIxLEAST8	__PRI8LEAST(x)
91 #define PRIXLEAST8	__PRI8LEAST(X)
92 
93 /* Macros below are only enabled for a newlib built with C99 I/O format support. */
94 #if defined(_WANT_IO_C99_FORMATS)
95 
96   #define SCNdLEAST8	__SCN8LEAST(d)
97   #define SCNiLEAST8	__SCN8LEAST(i)
98   #define SCNoLEAST8	__SCN8LEAST(o)
99   #define SCNuLEAST8	__SCN8LEAST(u)
100   #define SCNxLEAST8	__SCN8LEAST(x)
101 
102 #endif /* _WANT_IO_C99_FORMATS */
103 
104 #define PRIdFAST8	__PRI8FAST(d)
105 #define PRIiFAST8	__PRI8FAST(i)
106 #define PRIoFAST8	__PRI8FAST(o)
107 #define PRIuFAST8	__PRI8FAST(u)
108 #define PRIxFAST8	__PRI8FAST(x)
109 #define PRIXFAST8	__PRI8FAST(X)
110 
111 /* Macros below are only enabled for a newlib built with C99 I/O format support. */
112 #if defined(_WANT_IO_C99_FORMATS)
113 
114   #define SCNdFAST8	__SCN8FAST(d)
115   #define SCNiFAST8	__SCN8FAST(i)
116   #define SCNoFAST8	__SCN8FAST(o)
117   #define SCNuFAST8	__SCN8FAST(u)
118   #define SCNxFAST8	__SCN8FAST(x)
119 
120 #endif /* _WANT_IO_C99_FORMATS */
121 
122 /* 16-bit types */
123 #define __PRI16(x) __INT16 __STRINGIFY(x)
124 #define __PRI16LEAST(x) __LEAST16 __STRINGIFY(x)
125 #define __PRI16FAST(x) __FAST16 __STRINGIFY(x)
126 #define __SCN16(x) __INT16 __STRINGIFY(x)
127 #define __SCN16LEAST(x) __LEAST16 __STRINGIFY(x)
128 #define __SCN16FAST(x) __FAST16 __STRINGIFY(x)
129 
130 
131 #define PRId16		__PRI16(d)
132 #define PRIi16		__PRI16(i)
133 #define PRIo16		__PRI16(o)
134 #define PRIu16		__PRI16(u)
135 #define PRIx16		__PRI16(x)
136 #define PRIX16		__PRI16(X)
137 
138 #define SCNd16		__SCN16(d)
139 #define SCNi16		__SCN16(i)
140 #define SCNo16		__SCN16(o)
141 #define SCNu16		__SCN16(u)
142 #define SCNx16		__SCN16(x)
143 
144 
145 #define PRIdLEAST16	__PRI16LEAST(d)
146 #define PRIiLEAST16	__PRI16LEAST(i)
147 #define PRIoLEAST16	__PRI16LEAST(o)
148 #define PRIuLEAST16	__PRI16LEAST(u)
149 #define PRIxLEAST16	__PRI16LEAST(x)
150 #define PRIXLEAST16	__PRI16LEAST(X)
151 
152 #define SCNdLEAST16	__SCN16LEAST(d)
153 #define SCNiLEAST16	__SCN16LEAST(i)
154 #define SCNoLEAST16	__SCN16LEAST(o)
155 #define SCNuLEAST16	__SCN16LEAST(u)
156 #define SCNxLEAST16	__SCN16LEAST(x)
157 
158 
159 #define PRIdFAST16	__PRI16FAST(d)
160 #define PRIiFAST16	__PRI16FAST(i)
161 #define PRIoFAST16	__PRI16FAST(o)
162 #define PRIuFAST16	__PRI16FAST(u)
163 #define PRIxFAST16	__PRI16FAST(x)
164 #define PRIXFAST16	__PRI16FAST(X)
165 
166 #define SCNdFAST16	__SCN16FAST(d)
167 #define SCNiFAST16	__SCN16FAST(i)
168 #define SCNoFAST16	__SCN16FAST(o)
169 #define SCNuFAST16	__SCN16FAST(u)
170 #define SCNxFAST16	__SCN16FAST(x)
171 
172 /* 32-bit types */
173 #define __PRI32(x) __INT32 __STRINGIFY(x)
174 #define __SCN32(x) __INT32 __STRINGIFY(x)
175 #define __PRI32LEAST(x) __LEAST32 __STRINGIFY(x)
176 #define __SCN32LEAST(x) __LEAST32 __STRINGIFY(x)
177 #define __PRI32FAST(x) __FAST32 __STRINGIFY(x)
178 #define __SCN32FAST(x) __FAST32 __STRINGIFY(x)
179 
180 #define PRId32		__PRI32(d)
181 #define PRIi32		__PRI32(i)
182 #define PRIo32		__PRI32(o)
183 #define PRIu32		__PRI32(u)
184 #define PRIx32		__PRI32(x)
185 #define PRIX32		__PRI32(X)
186 
187 #define SCNd32		__SCN32(d)
188 #define SCNi32		__SCN32(i)
189 #define SCNo32		__SCN32(o)
190 #define SCNu32		__SCN32(u)
191 #define SCNx32		__SCN32(x)
192 
193 
194 #define PRIdLEAST32	__PRI32LEAST(d)
195 #define PRIiLEAST32	__PRI32LEAST(i)
196 #define PRIoLEAST32	__PRI32LEAST(o)
197 #define PRIuLEAST32	__PRI32LEAST(u)
198 #define PRIxLEAST32	__PRI32LEAST(x)
199 #define PRIXLEAST32	__PRI32LEAST(X)
200 
201 #define SCNdLEAST32	__SCN32LEAST(d)
202 #define SCNiLEAST32	__SCN32LEAST(i)
203 #define SCNoLEAST32	__SCN32LEAST(o)
204 #define SCNuLEAST32	__SCN32LEAST(u)
205 #define SCNxLEAST32	__SCN32LEAST(x)
206 
207 
208 #define PRIdFAST32	__PRI32FAST(d)
209 #define PRIiFAST32	__PRI32FAST(i)
210 #define PRIoFAST32	__PRI32FAST(o)
211 #define PRIuFAST32	__PRI32FAST(u)
212 #define PRIxFAST32	__PRI32FAST(x)
213 #define PRIXFAST32	__PRI32FAST(X)
214 
215 #define SCNdFAST32	__SCN32FAST(d)
216 #define SCNiFAST32	__SCN32FAST(i)
217 #define SCNoFAST32	__SCN32FAST(o)
218 #define SCNuFAST32	__SCN32FAST(u)
219 #define SCNxFAST32	__SCN32FAST(x)
220 
221 
222 /* 64-bit types */
223 #define __PRI64(x) __INT64 __STRINGIFY(x)
224 #define __SCN64(x) __INT64 __STRINGIFY(x)
225 
226 #define __PRI64LEAST(x) __LEAST64 __STRINGIFY(x)
227 #define __SCN64LEAST(x) __LEAST64 __STRINGIFY(x)
228 #define __PRI64FAST(x) __FAST64 __STRINGIFY(x)
229 #define __SCN64FAST(x) __FAST64 __STRINGIFY(x)
230 
231 #if __int64_t_defined
232 #define PRId64		__PRI64(d)
233 #define PRIi64		__PRI64(i)
234 #define PRIo64		__PRI64(o)
235 #define PRIu64		__PRI64(u)
236 #define PRIx64		__PRI64(x)
237 #define PRIX64		__PRI64(X)
238 
239 #define SCNd64		__SCN64(d)
240 #define SCNi64		__SCN64(i)
241 #define SCNo64		__SCN64(o)
242 #define SCNu64		__SCN64(u)
243 #define SCNx64		__SCN64(x)
244 #endif
245 
246 #if __int_least64_t_defined
247 #define PRIdLEAST64	__PRI64LEAST(d)
248 #define PRIiLEAST64	__PRI64LEAST(i)
249 #define PRIoLEAST64	__PRI64LEAST(o)
250 #define PRIuLEAST64	__PRI64LEAST(u)
251 #define PRIxLEAST64	__PRI64LEAST(x)
252 #define PRIXLEAST64	__PRI64LEAST(X)
253 
254 #define SCNdLEAST64	__SCN64LEAST(d)
255 #define SCNiLEAST64	__SCN64LEAST(i)
256 #define SCNoLEAST64	__SCN64LEAST(o)
257 #define SCNuLEAST64	__SCN64LEAST(u)
258 #define SCNxLEAST64	__SCN64LEAST(x)
259 #endif
260 
261 #if __int_fast64_t_defined
262 #define PRIdFAST64	__PRI64FAST(d)
263 #define PRIiFAST64	__PRI64FAST(i)
264 #define PRIoFAST64	__PRI64FAST(o)
265 #define PRIuFAST64	__PRI64FAST(u)
266 #define PRIxFAST64	__PRI64FAST(x)
267 #define PRIXFAST64	__PRI64FAST(X)
268 
269 #define SCNdFAST64	__SCN64FAST(d)
270 #define SCNiFAST64	__SCN64FAST(i)
271 #define SCNoFAST64	__SCN64FAST(o)
272 #define SCNuFAST64	__SCN64FAST(u)
273 #define SCNxFAST64	__SCN64FAST(x)
274 #endif
275 
276 /* max-bit types */
277 #if __have_long64
278 #define __PRIMAX(x) __STRINGIFY(l##x)
279 #define __SCNMAX(x) __STRINGIFY(l##x)
280 #elif __have_longlong64
281 #define __PRIMAX(x) __STRINGIFY(ll##x)
282 #define __SCNMAX(x) __STRINGIFY(ll##x)
283 #else
284 #define __PRIMAX(x) __STRINGIFY(x)
285 #define __SCNMAX(x) __STRINGIFY(x)
286 #endif
287 
288 #define PRIdMAX		__PRIMAX(d)
289 #define PRIiMAX		__PRIMAX(i)
290 #define PRIoMAX		__PRIMAX(o)
291 #define PRIuMAX		__PRIMAX(u)
292 #define PRIxMAX		__PRIMAX(x)
293 #define PRIXMAX		__PRIMAX(X)
294 
295 #define SCNdMAX		__SCNMAX(d)
296 #define SCNiMAX		__SCNMAX(i)
297 #define SCNoMAX		__SCNMAX(o)
298 #define SCNuMAX		__SCNMAX(u)
299 #define SCNxMAX		__SCNMAX(x)
300 
301 /* ptr types */
302 #if defined (_INTPTR_EQ_LONGLONG)
303 # define __PRIPTR(x) __STRINGIFY(ll##x)
304 # define __SCNPTR(x) __STRINGIFY(ll##x)
305 #elif defined (_INTPTR_EQ_LONG)
306 # define __PRIPTR(x) __STRINGIFY(l##x)
307 # define __SCNPTR(x) __STRINGIFY(l##x)
308 #else
309 # define __PRIPTR(x) __STRINGIFY(x)
310 # define __SCNPTR(x) __STRINGIFY(x)
311 #endif
312 
313 #define PRIdPTR		__PRIPTR(d)
314 #define PRIiPTR		__PRIPTR(i)
315 #define PRIoPTR		__PRIPTR(o)
316 #define PRIuPTR		__PRIPTR(u)
317 #define PRIxPTR		__PRIPTR(x)
318 #define PRIXPTR		__PRIPTR(X)
319 
320 #define SCNdPTR		__SCNPTR(d)
321 #define SCNiPTR		__SCNPTR(i)
322 #define SCNoPTR		__SCNPTR(o)
323 #define SCNuPTR		__SCNPTR(u)
324 #define SCNxPTR		__SCNPTR(x)
325 
326 intmax_t  imaxabs(intmax_t);
327 imaxdiv_t imaxdiv(intmax_t __numer, intmax_t __denomer);
328 intmax_t  strtoimax(const char *__restrict, char **__restrict, int);
329 uintmax_t strtoumax(const char *__restrict, char **__restrict, int);
330 intmax_t  wcstoimax(const _wchar_t *__restrict, _wchar_t **__restrict, int);
331 uintmax_t wcstoumax(const _wchar_t *__restrict, _wchar_t **__restrict, int);
332 
333 #if __BSD_VISIBLE
334 intmax_t  strtoimax_l(const char *__restrict, char **_restrict, int, locale_t);
335 uintmax_t strtoumax_l(const char *__restrict, char **_restrict, int, locale_t);
336 intmax_t  wcstoimax_l(const _wchar_t *__restrict, _wchar_t **_restrict, int, locale_t);
337 uintmax_t wcstoumax_l(const _wchar_t *__restrict, _wchar_t **_restrict, int, locale_t);
338 #endif
339 
340 _END_STD_C
341 
342 #endif
343