inttypes.h 7.9 KB

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