inttypes.h 7.4 KB

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