1// ISO C9x  compliant inttypes.h for Microsoft Visual Studio
2// Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124
3//
4//  Copyright (c) 2006 Alexander Chemeris
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are met:
8//
9//   1. Redistributions of source code must retain the above copyright notice,
10//      this list of conditions and the following disclaimer.
11//
12//   2. Redistributions in binary form must reproduce the above copyright
13//      notice, this list of conditions and the following disclaimer in the
14//      documentation and/or other materials provided with the distribution.
15//
16//   3. The name of the author may be used to endorse or promote products
17//      derived from this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
20// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
22// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30///////////////////////////////////////////////////////////////////////////////
31
32#ifndef _MSC_VER // [
33#error "Use this header only with Microsoft Visual C++ compilers!"
34#endif // _MSC_VER ]
35
36#ifndef _MSC_INTTYPES_H_ // [
37#define _MSC_INTTYPES_H_
38
39#if _MSC_VER > 1000
40#pragma once
41#endif
42
43#include "stdint.h"
44
45// 7.8 Format conversion of integer types
46
47typedef struct {
48   intmax_t quot;
49   intmax_t rem;
50} imaxdiv_t;
51
52// 7.8.1 Macros for format specifiers
53
54#if !defined(__cplusplus) || defined(__STDC_FORMAT_MACROS) // [   See footnote 185 at page 198
55
56#ifdef _WIN64
57#  define __PRI64_PREFIX        "l"
58#  define __PRIPTR_PREFIX       "l"
59#else
60#  define __PRI64_PREFIX        "ll"
61#  define __PRIPTR_PREFIX
62#endif
63
64// The fprintf macros for signed integers are:
65#define PRId8       "d"
66#define PRIi8       "i"
67#define PRIdLEAST8  "d"
68#define PRIiLEAST8  "i"
69#define PRIdFAST8   "d"
70#define PRIiFAST8   "i"
71
72#define PRId16       "hd"
73#define PRIi16       "hi"
74#define PRIdLEAST16  "hd"
75#define PRIiLEAST16  "hi"
76#define PRIdFAST16   "hd"
77#define PRIiFAST16   "hi"
78
79#define PRId32       "d"
80#define PRIi32       "i"
81#define PRIdLEAST32  "d"
82#define PRIiLEAST32  "i"
83#define PRIdFAST32   "d"
84#define PRIiFAST32   "i"
85
86#define PRId64       __PRI64_PREFIX "d"
87#define PRIi64       __PRI64_PREFIX "i"
88#define PRIdLEAST64  __PRI64_PREFIX "d"
89#define PRIiLEAST64  __PRI64_PREFIX "i"
90#define PRIdFAST64   __PRI64_PREFIX "d"
91#define PRIiFAST64   __PRI64_PREFIX "i"
92
93#define PRIdMAX     __PRI64_PREFIX "d"
94#define PRIiMAX     __PRI64_PREFIX "i"
95
96#define PRIdPTR     __PRIPTR_PREFIX "d"
97#define PRIiPTR     __PRIPTR_PREFIX "i"
98
99// The fprintf macros for unsigned integers are:
100#define PRIo8       "o"
101#define PRIu8       "u"
102#define PRIx8       "x"
103#define PRIX8       "X"
104#define PRIoLEAST8  "o"
105#define PRIuLEAST8  "u"
106#define PRIxLEAST8  "x"
107#define PRIXLEAST8  "X"
108#define PRIoFAST8   "o"
109#define PRIuFAST8   "u"
110#define PRIxFAST8   "x"
111#define PRIXFAST8   "X"
112
113#define PRIo16       "ho"
114#define PRIu16       "hu"
115#define PRIx16       "hx"
116#define PRIX16       "hX"
117#define PRIoLEAST16  "ho"
118#define PRIuLEAST16  "hu"
119#define PRIxLEAST16  "hx"
120#define PRIXLEAST16  "hX"
121#define PRIoFAST16   "ho"
122#define PRIuFAST16   "hu"
123#define PRIxFAST16   "hx"
124#define PRIXFAST16   "hX"
125
126#define PRIo32       "o"
127#define PRIu32       "u"
128#define PRIx32       "x"
129#define PRIX32       "X"
130#define PRIoLEAST32  "o"
131#define PRIuLEAST32  "u"
132#define PRIxLEAST32  "x"
133#define PRIXLEAST32  "X"
134#define PRIoFAST32   "o"
135#define PRIuFAST32   "u"
136#define PRIxFAST32   "x"
137#define PRIXFAST32   "X"
138
139#define PRIo64       __PRI64_PREFIX "o"
140#define PRIu64       __PRI64_PREFIX "u"
141#define PRIx64       __PRI64_PREFIX "x"
142#define PRIX64       __PRI64_PREFIX "X"
143#define PRIoLEAST64  __PRI64_PREFIX "o"
144#define PRIuLEAST64  __PRI64_PREFIX "u"
145#define PRIxLEAST64  __PRI64_PREFIX "x"
146#define PRIXLEAST64  __PRI64_PREFIX "X"
147#define PRIoFAST64   __PRI64_PREFIX "o"
148#define PRIuFAST64   __PRI64_PREFIX "u"
149#define PRIxFAST64   __PRI64_PREFIX "x"
150#define PRIXFAST64   __PRI64_PREFIX "X"
151
152#define PRIoMAX     __PRI64_PREFIX "o"
153#define PRIuMAX     __PRI64_PREFIX "u"
154#define PRIxMAX     __PRI64_PREFIX "x"
155#define PRIXMAX     __PRI64_PREFIX "X"
156
157#define PRIoPTR     __PRIPTR_PREFIX "o"
158#define PRIuPTR     __PRIPTR_PREFIX "u"
159#define PRIxPTR     __PRIPTR_PREFIX "x"
160#define PRIXPTR     __PRIPTR_PREFIX "X"
161
162// The fscanf macros for signed integers are:
163#define SCNd8       "d"
164#define SCNi8       "i"
165#define SCNdLEAST8  "d"
166#define SCNiLEAST8  "i"
167#define SCNdFAST8   "d"
168#define SCNiFAST8   "i"
169
170#define SCNd16       "hd"
171#define SCNi16       "hi"
172#define SCNdLEAST16  "hd"
173#define SCNiLEAST16  "hi"
174#define SCNdFAST16   "hd"
175#define SCNiFAST16   "hi"
176
177#define SCNd32       "ld"
178#define SCNi32       "li"
179#define SCNdLEAST32  "ld"
180#define SCNiLEAST32  "li"
181#define SCNdFAST32   "ld"
182#define SCNiFAST32   "li"
183
184#define SCNd64       "I64d"
185#define SCNi64       "I64i"
186#define SCNdLEAST64  "I64d"
187#define SCNiLEAST64  "I64i"
188#define SCNdFAST64   "I64d"
189#define SCNiFAST64   "I64i"
190
191#define SCNdMAX     "I64d"
192#define SCNiMAX     "I64i"
193
194#ifdef _WIN64 // [
195#  define SCNdPTR     "I64d"
196#  define SCNiPTR     "I64i"
197#else  // _WIN64 ][
198#  define SCNdPTR     "ld"
199#  define SCNiPTR     "li"
200#endif  // _WIN64 ]
201
202// The fscanf macros for unsigned integers are:
203#define SCNo8       "o"
204#define SCNu8       "u"
205#define SCNx8       "x"
206#define SCNX8       "X"
207#define SCNoLEAST8  "o"
208#define SCNuLEAST8  "u"
209#define SCNxLEAST8  "x"
210#define SCNXLEAST8  "X"
211#define SCNoFAST8   "o"
212#define SCNuFAST8   "u"
213#define SCNxFAST8   "x"
214#define SCNXFAST8   "X"
215
216#define SCNo16       "ho"
217#define SCNu16       "hu"
218#define SCNx16       "hx"
219#define SCNX16       "hX"
220#define SCNoLEAST16  "ho"
221#define SCNuLEAST16  "hu"
222#define SCNxLEAST16  "hx"
223#define SCNXLEAST16  "hX"
224#define SCNoFAST16   "ho"
225#define SCNuFAST16   "hu"
226#define SCNxFAST16   "hx"
227#define SCNXFAST16   "hX"
228
229#define SCNo32       "lo"
230#define SCNu32       "lu"
231#define SCNx32       "lx"
232#define SCNX32       "lX"
233#define SCNoLEAST32  "lo"
234#define SCNuLEAST32  "lu"
235#define SCNxLEAST32  "lx"
236#define SCNXLEAST32  "lX"
237#define SCNoFAST32   "lo"
238#define SCNuFAST32   "lu"
239#define SCNxFAST32   "lx"
240#define SCNXFAST32   "lX"
241
242#define SCNo64       "I64o"
243#define SCNu64       "I64u"
244#define SCNx64       "I64x"
245#define SCNX64       "I64X"
246#define SCNoLEAST64  "I64o"
247#define SCNuLEAST64  "I64u"
248#define SCNxLEAST64  "I64x"
249#define SCNXLEAST64  "I64X"
250#define SCNoFAST64   "I64o"
251#define SCNuFAST64   "I64u"
252#define SCNxFAST64   "I64x"
253#define SCNXFAST64   "I64X"
254
255#define SCNoMAX     "I64o"
256#define SCNuMAX     "I64u"
257#define SCNxMAX     "I64x"
258#define SCNXMAX     "I64X"
259
260#ifdef _WIN64 // [
261#  define SCNoPTR     "I64o"
262#  define SCNuPTR     "I64u"
263#  define SCNxPTR     "I64x"
264#  define SCNXPTR     "I64X"
265#else  // _WIN64 ][
266#  define SCNoPTR     "lo"
267#  define SCNuPTR     "lu"
268#  define SCNxPTR     "lx"
269#  define SCNXPTR     "lX"
270#endif  // _WIN64 ]
271
272#endif // __STDC_FORMAT_MACROS ]
273
274// 7.8.2 Functions for greatest-width integer types
275
276// 7.8.2.1 The imaxabs function
277#define imaxabs _abs64
278
279// 7.8.2.2 The imaxdiv function
280
281// This is modified version of div() function from Microsoft's div.c found
282// in %MSVC.NET%\crt\src\div.c
283#ifdef STATIC_IMAXDIV // [
284static
285#else // STATIC_IMAXDIV ][
286_inline
287#endif // STATIC_IMAXDIV ]
288imaxdiv_t __cdecl imaxdiv(intmax_t numer, intmax_t denom)
289{
290   imaxdiv_t result;
291
292   result.quot = numer / denom;
293   result.rem = numer % denom;
294
295   if (numer < 0 && result.rem > 0) {
296      // did division wrong; must fix up
297      ++result.quot;
298      result.rem -= denom;
299   }
300
301   return result;
302}
303
304// 7.8.2.3 The strtoimax and strtoumax functions
305#define strtoimax _strtoi64
306#define strtoumax _strtoui64
307
308// 7.8.2.4 The wcstoimax and wcstoumax functions
309#define wcstoimax _wcstoi64
310#define wcstoumax _wcstoui64
311
312
313#endif // _MSC_INTTYPES_H_ ]
314