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 
47 typedef 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 // The fprintf macros for signed integers are:
57 #define PRId8       "d"
58 #define PRIi8       "i"
59 #define PRIdLEAST8  "d"
60 #define PRIiLEAST8  "i"
61 #define PRIdFAST8   "d"
62 #define PRIiFAST8   "i"
63 
64 #define PRId16       "hd"
65 #define PRIi16       "hi"
66 #define PRIdLEAST16  "hd"
67 #define PRIiLEAST16  "hi"
68 #define PRIdFAST16   "hd"
69 #define PRIiFAST16   "hi"
70 
71 #define PRId32       "I32d"
72 #define PRIi32       "I32i"
73 #define PRIdLEAST32  "I32d"
74 #define PRIiLEAST32  "I32i"
75 #define PRIdFAST32   "I32d"
76 #define PRIiFAST32   "I32i"
77 
78 #define PRId64       "I64d"
79 #define PRIi64       "I64i"
80 #define PRIdLEAST64  "I64d"
81 #define PRIiLEAST64  "I64i"
82 #define PRIdFAST64   "I64d"
83 #define PRIiFAST64   "I64i"
84 
85 #define PRIdMAX     "I64d"
86 #define PRIiMAX     "I64i"
87 
88 #define PRIdPTR     "Id"
89 #define PRIiPTR     "Ii"
90 
91 // The fprintf macros for unsigned integers are:
92 #define PRIo8       "o"
93 #define PRIu8       "u"
94 #define PRIx8       "x"
95 #define PRIX8       "X"
96 #define PRIoLEAST8  "o"
97 #define PRIuLEAST8  "u"
98 #define PRIxLEAST8  "x"
99 #define PRIXLEAST8  "X"
100 #define PRIoFAST8   "o"
101 #define PRIuFAST8   "u"
102 #define PRIxFAST8   "x"
103 #define PRIXFAST8   "X"
104 
105 #define PRIo16       "ho"
106 #define PRIu16       "hu"
107 #define PRIx16       "hx"
108 #define PRIX16       "hX"
109 #define PRIoLEAST16  "ho"
110 #define PRIuLEAST16  "hu"
111 #define PRIxLEAST16  "hx"
112 #define PRIXLEAST16  "hX"
113 #define PRIoFAST16   "ho"
114 #define PRIuFAST16   "hu"
115 #define PRIxFAST16   "hx"
116 #define PRIXFAST16   "hX"
117 
118 #define PRIo32       "I32o"
119 #define PRIu32       "I32u"
120 #define PRIx32       "I32x"
121 #define PRIX32       "I32X"
122 #define PRIoLEAST32  "I32o"
123 #define PRIuLEAST32  "I32u"
124 #define PRIxLEAST32  "I32x"
125 #define PRIXLEAST32  "I32X"
126 #define PRIoFAST32   "I32o"
127 #define PRIuFAST32   "I32u"
128 #define PRIxFAST32   "I32x"
129 #define PRIXFAST32   "I32X"
130 
131 #define PRIo64       "I64o"
132 #define PRIu64       "I64u"
133 #define PRIx64       "I64x"
134 #define PRIX64       "I64X"
135 #define PRIoLEAST64  "I64o"
136 #define PRIuLEAST64  "I64u"
137 #define PRIxLEAST64  "I64x"
138 #define PRIXLEAST64  "I64X"
139 #define PRIoFAST64   "I64o"
140 #define PRIuFAST64   "I64u"
141 #define PRIxFAST64   "I64x"
142 #define PRIXFAST64   "I64X"
143 
144 #define PRIoMAX     "I64o"
145 #define PRIuMAX     "I64u"
146 #define PRIxMAX     "I64x"
147 #define PRIXMAX     "I64X"
148 
149 #define PRIoPTR     "Io"
150 #define PRIuPTR     "Iu"
151 #define PRIxPTR     "Ix"
152 #define PRIXPTR     "IX"
153 
154 // The fscanf macros for signed integers are:
155 #define SCNd16       "hd"
156 #define SCNi16       "hi"
157 #define SCNdLEAST16  "hd"
158 #define SCNiLEAST16  "hi"
159 #define SCNdFAST16   "hd"
160 #define SCNiFAST16   "hi"
161 
162 #define SCNd32       "ld"
163 #define SCNi32       "li"
164 #define SCNdLEAST32  "ld"
165 #define SCNiLEAST32  "li"
166 #define SCNdFAST32   "ld"
167 #define SCNiFAST32   "li"
168 
169 #define SCNd64       "I64d"
170 #define SCNi64       "I64i"
171 #define SCNdLEAST64  "I64d"
172 #define SCNiLEAST64  "I64i"
173 #define SCNdFAST64   "I64d"
174 #define SCNiFAST64   "I64i"
175 
176 #define SCNdMAX     "I64d"
177 #define SCNiMAX     "I64i"
178 
179 #ifdef _WIN64 // [
180 #  define SCNdPTR     "I64d"
181 #  define SCNiPTR     "I64i"
182 #else  // _WIN64 ][
183 #  define SCNdPTR     "ld"
184 #  define SCNiPTR     "li"
185 #endif  // _WIN64 ]
186 
187 // The fscanf macros for unsigned integers are:
188 #define SCNo16       "ho"
189 #define SCNu16       "hu"
190 #define SCNx16       "hx"
191 #define SCNX16       "hX"
192 #define SCNoLEAST16  "ho"
193 #define SCNuLEAST16  "hu"
194 #define SCNxLEAST16  "hx"
195 #define SCNXLEAST16  "hX"
196 #define SCNoFAST16   "ho"
197 #define SCNuFAST16   "hu"
198 #define SCNxFAST16   "hx"
199 #define SCNXFAST16   "hX"
200 
201 #define SCNo32       "lo"
202 #define SCNu32       "lu"
203 #define SCNx32       "lx"
204 #define SCNX32       "lX"
205 #define SCNoLEAST32  "lo"
206 #define SCNuLEAST32  "lu"
207 #define SCNxLEAST32  "lx"
208 #define SCNXLEAST32  "lX"
209 #define SCNoFAST32   "lo"
210 #define SCNuFAST32   "lu"
211 #define SCNxFAST32   "lx"
212 #define SCNXFAST32   "lX"
213 
214 #define SCNo64       "I64o"
215 #define SCNu64       "I64u"
216 #define SCNx64       "I64x"
217 #define SCNX64       "I64X"
218 #define SCNoLEAST64  "I64o"
219 #define SCNuLEAST64  "I64u"
220 #define SCNxLEAST64  "I64x"
221 #define SCNXLEAST64  "I64X"
222 #define SCNoFAST64   "I64o"
223 #define SCNuFAST64   "I64u"
224 #define SCNxFAST64   "I64x"
225 #define SCNXFAST64   "I64X"
226 
227 #define SCNoMAX     "I64o"
228 #define SCNuMAX     "I64u"
229 #define SCNxMAX     "I64x"
230 #define SCNXMAX     "I64X"
231 
232 #ifdef _WIN64 // [
233 #  define SCNoPTR     "I64o"
234 #  define SCNuPTR     "I64u"
235 #  define SCNxPTR     "I64x"
236 #  define SCNXPTR     "I64X"
237 #else  // _WIN64 ][
238 #  define SCNoPTR     "lo"
239 #  define SCNuPTR     "lu"
240 #  define SCNxPTR     "lx"
241 #  define SCNXPTR     "lX"
242 #endif  // _WIN64 ]
243 
244 #endif // __STDC_FORMAT_MACROS ]
245 
246 // 7.8.2 Functions for greatest-width integer types
247 
248 // 7.8.2.1 The imaxabs function
249 #define imaxabs _abs64
250 
251 // 7.8.2.2 The imaxdiv function
252 
253 // This is modified version of div() function from Microsoft's div.c found
254 // in %MSVC.NET%\crt\src\div.c
255 #ifdef STATIC_IMAXDIV // [
256 static
257 #else // STATIC_IMAXDIV ][
258 _inline
259 #endif // STATIC_IMAXDIV ]
imaxdiv(intmax_t numer,intmax_t denom)260 imaxdiv_t __cdecl imaxdiv(intmax_t numer, intmax_t denom)
261 {
262    imaxdiv_t result;
263 
264    result.quot = numer / denom;
265    result.rem = numer % denom;
266 
267    if (numer < 0 && result.rem > 0) {
268       // did division wrong; must fix up
269       ++result.quot;
270       result.rem -= denom;
271    }
272 
273    return result;
274 }
275 
276 // 7.8.2.3 The strtoimax and strtoumax functions
277 #define strtoimax _strtoi64
278 #define strtoumax _strtoui64
279 
280 // 7.8.2.4 The wcstoimax and wcstoumax functions
281 #define wcstoimax _wcstoi64
282 #define wcstoumax _wcstoui64
283 
284 
285 #endif // _MSC_INTTYPES_H_ ]
286