inttypes.h 7.9 KB

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