cstdint.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. // boost cstdint.hpp header file ------------------------------------------//
  2. // (C) Copyright Beman Dawes 1999.
  3. // (C) Copyright Jens Mauer 2001
  4. // (C) Copyright John Maddock 2001
  5. // Distributed under the Boost
  6. // Software License, Version 1.0. (See accompanying file
  7. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. // See http://www.boost.org/libs/integer for documentation.
  9. // Revision History
  10. // 31 Oct 01 use BOOST_HAS_LONG_LONG to check for "long long" (Jens M.)
  11. // 16 Apr 01 check LONGLONG_MAX when looking for "long long" (Jens Maurer)
  12. // 23 Jan 01 prefer "long" over "int" for int32_t and intmax_t (Jens Maurer)
  13. // 12 Nov 00 Merged <boost/stdint.h> (Jens Maurer)
  14. // 23 Sep 00 Added INTXX_C macro support (John Maddock).
  15. // 22 Sep 00 Better 64-bit support (John Maddock)
  16. // 29 Jun 00 Reimplement to avoid including stdint.h within namespace boost
  17. // 8 Aug 99 Initial version (Beman Dawes)
  18. #ifndef BOOST_CSTDINT_HPP
  19. #define BOOST_CSTDINT_HPP
  20. //
  21. // Since we always define the INT#_C macros as per C++0x,
  22. // define __STDC_CONSTANT_MACROS so that <stdint.h> does the right
  23. // thing if possible, and so that the user knows that the macros
  24. // are actually defined as per C99.
  25. //
  26. #ifndef __STDC_CONSTANT_MACROS
  27. # define __STDC_CONSTANT_MACROS
  28. #endif
  29. #include <boost/config.hpp>
  30. //
  31. // For the following code we get several warnings along the lines of:
  32. //
  33. // boost/cstdint.hpp:428:35: error: use of C99 long long integer constant
  34. //
  35. // So we declare this a system header to suppress these warnings.
  36. // See also https://github.com/boostorg/config/issues/190
  37. //
  38. #if defined(__GNUC__) && (__GNUC__ >= 4)
  39. #pragma GCC system_header
  40. #endif
  41. //
  42. // Note that GLIBC is a bit inconsistent about whether int64_t is defined or not
  43. // depending upon what headers happen to have been included first...
  44. // so we disable use of stdint.h when GLIBC does not define __GLIBC_HAVE_LONG_LONG.
  45. // See https://svn.boost.org/trac/boost/ticket/3548 and http://sources.redhat.com/bugzilla/show_bug.cgi?id=10990
  46. //
  47. #if defined(BOOST_HAS_STDINT_H) \
  48. && (!defined(__GLIBC__) \
  49. || defined(__GLIBC_HAVE_LONG_LONG) \
  50. || (defined(__GLIBC__) && ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 17)))))
  51. // The following #include is an implementation artifact; not part of interface.
  52. # ifdef __hpux
  53. // HP-UX has a vaguely nice <stdint.h> in a non-standard location
  54. # include <inttypes.h>
  55. # ifdef __STDC_32_MODE__
  56. // this is triggered with GCC, because it defines __cplusplus < 199707L
  57. # define BOOST_NO_INT64_T
  58. # endif
  59. # elif defined(__FreeBSD__) || defined(__IBMCPP__) || defined(_AIX)
  60. # include <inttypes.h>
  61. # else
  62. # include <stdint.h>
  63. // There is a bug in Cygwin two _C macros
  64. # if defined(INTMAX_C) && defined(__CYGWIN__)
  65. # undef INTMAX_C
  66. # undef UINTMAX_C
  67. # define INTMAX_C(c) c##LL
  68. # define UINTMAX_C(c) c##ULL
  69. # endif
  70. # endif
  71. #if defined(__QNX__) && defined(__EXT_QNX)
  72. // QNX (Dinkumware stdlib) defines these as non-standard names.
  73. // Reflect to the standard names.
  74. typedef ::intleast8_t int_least8_t;
  75. typedef ::intfast8_t int_fast8_t;
  76. typedef ::uintleast8_t uint_least8_t;
  77. typedef ::uintfast8_t uint_fast8_t;
  78. typedef ::intleast16_t int_least16_t;
  79. typedef ::intfast16_t int_fast16_t;
  80. typedef ::uintleast16_t uint_least16_t;
  81. typedef ::uintfast16_t uint_fast16_t;
  82. typedef ::intleast32_t int_least32_t;
  83. typedef ::intfast32_t int_fast32_t;
  84. typedef ::uintleast32_t uint_least32_t;
  85. typedef ::uintfast32_t uint_fast32_t;
  86. # ifndef BOOST_NO_INT64_T
  87. typedef ::intleast64_t int_least64_t;
  88. typedef ::intfast64_t int_fast64_t;
  89. typedef ::uintleast64_t uint_least64_t;
  90. typedef ::uintfast64_t uint_fast64_t;
  91. # endif
  92. #endif
  93. namespace boost
  94. {
  95. using ::int8_t;
  96. using ::int_least8_t;
  97. using ::int_fast8_t;
  98. using ::uint8_t;
  99. using ::uint_least8_t;
  100. using ::uint_fast8_t;
  101. using ::int16_t;
  102. using ::int_least16_t;
  103. using ::int_fast16_t;
  104. using ::uint16_t;
  105. using ::uint_least16_t;
  106. using ::uint_fast16_t;
  107. using ::int32_t;
  108. using ::int_least32_t;
  109. using ::int_fast32_t;
  110. using ::uint32_t;
  111. using ::uint_least32_t;
  112. using ::uint_fast32_t;
  113. # ifndef BOOST_NO_INT64_T
  114. using ::int64_t;
  115. using ::int_least64_t;
  116. using ::int_fast64_t;
  117. using ::uint64_t;
  118. using ::uint_least64_t;
  119. using ::uint_fast64_t;
  120. # endif
  121. using ::intmax_t;
  122. using ::uintmax_t;
  123. } // namespace boost
  124. #elif defined(__FreeBSD__) && (__FreeBSD__ <= 4) || defined(__osf__) || defined(__VMS) || defined(__SOLARIS9__) || defined(__NetBSD__)
  125. // FreeBSD and Tru64 have an <inttypes.h> that contains much of what we need.
  126. # include <inttypes.h>
  127. namespace boost {
  128. using ::int8_t;
  129. typedef int8_t int_least8_t;
  130. typedef int8_t int_fast8_t;
  131. using ::uint8_t;
  132. typedef uint8_t uint_least8_t;
  133. typedef uint8_t uint_fast8_t;
  134. using ::int16_t;
  135. typedef int16_t int_least16_t;
  136. typedef int16_t int_fast16_t;
  137. using ::uint16_t;
  138. typedef uint16_t uint_least16_t;
  139. typedef uint16_t uint_fast16_t;
  140. using ::int32_t;
  141. typedef int32_t int_least32_t;
  142. typedef int32_t int_fast32_t;
  143. using ::uint32_t;
  144. typedef uint32_t uint_least32_t;
  145. typedef uint32_t uint_fast32_t;
  146. # ifndef BOOST_NO_INT64_T
  147. using ::int64_t;
  148. typedef int64_t int_least64_t;
  149. typedef int64_t int_fast64_t;
  150. using ::uint64_t;
  151. typedef uint64_t uint_least64_t;
  152. typedef uint64_t uint_fast64_t;
  153. typedef int64_t intmax_t;
  154. typedef uint64_t uintmax_t;
  155. # else
  156. typedef int32_t intmax_t;
  157. typedef uint32_t uintmax_t;
  158. # endif
  159. } // namespace boost
  160. #else // BOOST_HAS_STDINT_H
  161. # include <boost/limits.hpp> // implementation artifact; not part of interface
  162. # include <limits.h> // needed for limits macros
  163. namespace boost
  164. {
  165. // These are fairly safe guesses for some 16-bit, and most 32-bit and 64-bit
  166. // platforms. For other systems, they will have to be hand tailored.
  167. //
  168. // Because the fast types are assumed to be the same as the undecorated types,
  169. // it may be possible to hand tailor a more efficient implementation. Such
  170. // an optimization may be illusionary; on the Intel x86-family 386 on, for
  171. // example, byte arithmetic and load/stores are as fast as "int" sized ones.
  172. // 8-bit types ------------------------------------------------------------//
  173. # if UCHAR_MAX == 0xff
  174. typedef signed char int8_t;
  175. typedef signed char int_least8_t;
  176. typedef signed char int_fast8_t;
  177. typedef unsigned char uint8_t;
  178. typedef unsigned char uint_least8_t;
  179. typedef unsigned char uint_fast8_t;
  180. # else
  181. # error defaults not correct; you must hand modify boost/cstdint.hpp
  182. # endif
  183. // 16-bit types -----------------------------------------------------------//
  184. # if USHRT_MAX == 0xffff
  185. # if defined(__crayx1)
  186. // The Cray X1 has a 16-bit short, however it is not recommend
  187. // for use in performance critical code.
  188. typedef short int16_t;
  189. typedef short int_least16_t;
  190. typedef int int_fast16_t;
  191. typedef unsigned short uint16_t;
  192. typedef unsigned short uint_least16_t;
  193. typedef unsigned int uint_fast16_t;
  194. # else
  195. typedef short int16_t;
  196. typedef short int_least16_t;
  197. typedef short int_fast16_t;
  198. typedef unsigned short uint16_t;
  199. typedef unsigned short uint_least16_t;
  200. typedef unsigned short uint_fast16_t;
  201. # endif
  202. # elif (USHRT_MAX == 0xffffffff) && defined(__MTA__)
  203. // On MTA / XMT short is 32 bits unless the -short16 compiler flag is specified
  204. // MTA / XMT does support the following non-standard integer types
  205. typedef __short16 int16_t;
  206. typedef __short16 int_least16_t;
  207. typedef __short16 int_fast16_t;
  208. typedef unsigned __short16 uint16_t;
  209. typedef unsigned __short16 uint_least16_t;
  210. typedef unsigned __short16 uint_fast16_t;
  211. # elif (USHRT_MAX == 0xffffffff) && defined(CRAY)
  212. // no 16-bit types on Cray:
  213. typedef short int_least16_t;
  214. typedef short int_fast16_t;
  215. typedef unsigned short uint_least16_t;
  216. typedef unsigned short uint_fast16_t;
  217. # else
  218. # error defaults not correct; you must hand modify boost/cstdint.hpp
  219. # endif
  220. // 32-bit types -----------------------------------------------------------//
  221. # if UINT_MAX == 0xffffffff
  222. typedef int int32_t;
  223. typedef int int_least32_t;
  224. typedef int int_fast32_t;
  225. typedef unsigned int uint32_t;
  226. typedef unsigned int uint_least32_t;
  227. typedef unsigned int uint_fast32_t;
  228. # elif (USHRT_MAX == 0xffffffff)
  229. typedef short int32_t;
  230. typedef short int_least32_t;
  231. typedef short int_fast32_t;
  232. typedef unsigned short uint32_t;
  233. typedef unsigned short uint_least32_t;
  234. typedef unsigned short uint_fast32_t;
  235. # elif ULONG_MAX == 0xffffffff
  236. typedef long int32_t;
  237. typedef long int_least32_t;
  238. typedef long int_fast32_t;
  239. typedef unsigned long uint32_t;
  240. typedef unsigned long uint_least32_t;
  241. typedef unsigned long uint_fast32_t;
  242. # elif (UINT_MAX == 0xffffffffffffffff) && defined(__MTA__)
  243. // Integers are 64 bits on the MTA / XMT
  244. typedef __int32 int32_t;
  245. typedef __int32 int_least32_t;
  246. typedef __int32 int_fast32_t;
  247. typedef unsigned __int32 uint32_t;
  248. typedef unsigned __int32 uint_least32_t;
  249. typedef unsigned __int32 uint_fast32_t;
  250. # else
  251. # error defaults not correct; you must hand modify boost/cstdint.hpp
  252. # endif
  253. // 64-bit types + intmax_t and uintmax_t ----------------------------------//
  254. # if defined(BOOST_HAS_LONG_LONG) && \
  255. !defined(BOOST_MSVC) && !defined(__BORLANDC__) && \
  256. (!defined(__GLIBCPP__) || defined(_GLIBCPP_USE_LONG_LONG)) && \
  257. (defined(ULLONG_MAX) || defined(ULONG_LONG_MAX) || defined(ULONGLONG_MAX))
  258. # if defined(__hpux)
  259. // HP-UX's value of ULONG_LONG_MAX is unusable in preprocessor expressions
  260. # elif (defined(ULLONG_MAX) && ULLONG_MAX == 18446744073709551615ULL) || (defined(ULONG_LONG_MAX) && ULONG_LONG_MAX == 18446744073709551615ULL) || (defined(ULONGLONG_MAX) && ULONGLONG_MAX == 18446744073709551615ULL)
  261. // 2**64 - 1
  262. # else
  263. # error defaults not correct; you must hand modify boost/cstdint.hpp
  264. # endif
  265. typedef ::boost::long_long_type intmax_t;
  266. typedef ::boost::ulong_long_type uintmax_t;
  267. typedef ::boost::long_long_type int64_t;
  268. typedef ::boost::long_long_type int_least64_t;
  269. typedef ::boost::long_long_type int_fast64_t;
  270. typedef ::boost::ulong_long_type uint64_t;
  271. typedef ::boost::ulong_long_type uint_least64_t;
  272. typedef ::boost::ulong_long_type uint_fast64_t;
  273. # elif ULONG_MAX != 0xffffffff
  274. # if ULONG_MAX == 18446744073709551615 // 2**64 - 1
  275. typedef long intmax_t;
  276. typedef unsigned long uintmax_t;
  277. typedef long int64_t;
  278. typedef long int_least64_t;
  279. typedef long int_fast64_t;
  280. typedef unsigned long uint64_t;
  281. typedef unsigned long uint_least64_t;
  282. typedef unsigned long uint_fast64_t;
  283. # else
  284. # error defaults not correct; you must hand modify boost/cstdint.hpp
  285. # endif
  286. # elif defined(__GNUC__) && defined(BOOST_HAS_LONG_LONG)
  287. __extension__ typedef long long intmax_t;
  288. __extension__ typedef unsigned long long uintmax_t;
  289. __extension__ typedef long long int64_t;
  290. __extension__ typedef long long int_least64_t;
  291. __extension__ typedef long long int_fast64_t;
  292. __extension__ typedef unsigned long long uint64_t;
  293. __extension__ typedef unsigned long long uint_least64_t;
  294. __extension__ typedef unsigned long long uint_fast64_t;
  295. # elif defined(BOOST_HAS_MS_INT64)
  296. //
  297. // we have Borland/Intel/Microsoft __int64:
  298. //
  299. typedef __int64 intmax_t;
  300. typedef unsigned __int64 uintmax_t;
  301. typedef __int64 int64_t;
  302. typedef __int64 int_least64_t;
  303. typedef __int64 int_fast64_t;
  304. typedef unsigned __int64 uint64_t;
  305. typedef unsigned __int64 uint_least64_t;
  306. typedef unsigned __int64 uint_fast64_t;
  307. # else // assume no 64-bit integers
  308. # define BOOST_NO_INT64_T
  309. typedef int32_t intmax_t;
  310. typedef uint32_t uintmax_t;
  311. # endif
  312. } // namespace boost
  313. #endif // BOOST_HAS_STDINT_H
  314. // intptr_t/uintptr_t are defined separately because they are optional and not universally available
  315. #if defined(BOOST_WINDOWS) && !defined(_WIN32_WCE) && !defined(BOOST_HAS_STDINT_H)
  316. // Older MSVC don't have stdint.h and have intptr_t/uintptr_t defined in stddef.h
  317. #include <stddef.h>
  318. #endif
  319. #if (defined(BOOST_WINDOWS) && !defined(_WIN32_WCE)) \
  320. || (defined(_XOPEN_UNIX) && (_XOPEN_UNIX+0 > 0) && !defined(__UCLIBC__)) \
  321. || defined(__CYGWIN__) || defined(__VXWORKS__) \
  322. || defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__) \
  323. || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || (defined(sun) && !defined(BOOST_HAS_STDINT_H)) || defined(INTPTR_MAX)
  324. namespace boost {
  325. using ::intptr_t;
  326. using ::uintptr_t;
  327. }
  328. #define BOOST_HAS_INTPTR_T
  329. // Clang pretends to be GCC, so it'll match this condition
  330. #elif defined(__GNUC__) && defined(__INTPTR_TYPE__) && defined(__UINTPTR_TYPE__)
  331. namespace boost {
  332. typedef __INTPTR_TYPE__ intptr_t;
  333. typedef __UINTPTR_TYPE__ uintptr_t;
  334. }
  335. #define BOOST_HAS_INTPTR_T
  336. #endif
  337. #endif // BOOST_CSTDINT_HPP
  338. /****************************************************
  339. Macro definition section:
  340. Added 23rd September 2000 (John Maddock).
  341. Modified 11th September 2001 to be excluded when
  342. BOOST_HAS_STDINT_H is defined (John Maddock).
  343. Modified 11th Dec 2009 to always define the
  344. INT#_C macros if they're not already defined (John Maddock).
  345. ******************************************************/
  346. #if !defined(BOOST__STDC_CONSTANT_MACROS_DEFINED) && \
  347. (!defined(INT8_C) || !defined(INT16_C) || !defined(INT32_C) || !defined(INT64_C))
  348. //
  349. // Undef the macros as a precaution, since we may get here if <stdint.h> has failed
  350. // to define them all, see https://svn.boost.org/trac/boost/ticket/12786
  351. //
  352. #undef INT8_C
  353. #undef INT16_C
  354. #undef INT32_C
  355. #undef INT64_C
  356. #undef INTMAX_C
  357. #undef UINT8_C
  358. #undef UINT16_C
  359. #undef UINT32_C
  360. #undef UINT64_C
  361. #undef UINTMAX_C
  362. #include <limits.h>
  363. # define BOOST__STDC_CONSTANT_MACROS_DEFINED
  364. # if defined(BOOST_HAS_MS_INT64)
  365. //
  366. // Borland/Intel/Microsoft compilers have width specific suffixes:
  367. //
  368. #ifndef INT8_C
  369. # define INT8_C(value) value##i8
  370. #endif
  371. #ifndef INT16_C
  372. # define INT16_C(value) value##i16
  373. #endif
  374. #ifndef INT32_C
  375. # define INT32_C(value) value##i32
  376. #endif
  377. #ifndef INT64_C
  378. # define INT64_C(value) value##i64
  379. #endif
  380. # ifdef __BORLANDC__
  381. // Borland bug: appending ui8 makes the type a signed char
  382. # define UINT8_C(value) static_cast<unsigned char>(value##u)
  383. # else
  384. # define UINT8_C(value) value##ui8
  385. # endif
  386. #ifndef UINT16_C
  387. # define UINT16_C(value) value##ui16
  388. #endif
  389. #ifndef UINT32_C
  390. # define UINT32_C(value) value##ui32
  391. #endif
  392. #ifndef UINT64_C
  393. # define UINT64_C(value) value##ui64
  394. #endif
  395. #ifndef INTMAX_C
  396. # define INTMAX_C(value) value##i64
  397. # define UINTMAX_C(value) value##ui64
  398. #endif
  399. # else
  400. // do it the old fashioned way:
  401. // 8-bit types ------------------------------------------------------------//
  402. # if (UCHAR_MAX == 0xff) && !defined(INT8_C)
  403. # define INT8_C(value) static_cast<boost::int8_t>(value)
  404. # define UINT8_C(value) static_cast<boost::uint8_t>(value##u)
  405. # endif
  406. // 16-bit types -----------------------------------------------------------//
  407. # if (USHRT_MAX == 0xffff) && !defined(INT16_C)
  408. # define INT16_C(value) static_cast<boost::int16_t>(value)
  409. # define UINT16_C(value) static_cast<boost::uint16_t>(value##u)
  410. # endif
  411. // 32-bit types -----------------------------------------------------------//
  412. #ifndef INT32_C
  413. # if (UINT_MAX == 0xffffffff)
  414. # define INT32_C(value) value
  415. # define UINT32_C(value) value##u
  416. # elif ULONG_MAX == 0xffffffff
  417. # define INT32_C(value) value##L
  418. # define UINT32_C(value) value##uL
  419. # endif
  420. #endif
  421. // 64-bit types + intmax_t and uintmax_t ----------------------------------//
  422. #ifndef INT64_C
  423. # if defined(BOOST_HAS_LONG_LONG) && \
  424. (defined(ULLONG_MAX) || defined(ULONG_LONG_MAX) || defined(ULONGLONG_MAX) || defined(_ULLONG_MAX) || defined(_LLONG_MAX))
  425. # if defined(__hpux)
  426. // HP-UX's value of ULONG_LONG_MAX is unusable in preprocessor expressions
  427. # define INT64_C(value) value##LL
  428. # define UINT64_C(value) value##uLL
  429. # elif (defined(ULLONG_MAX) && ULLONG_MAX == 18446744073709551615ULL) || \
  430. (defined(ULONG_LONG_MAX) && ULONG_LONG_MAX == 18446744073709551615ULL) || \
  431. (defined(ULONGLONG_MAX) && ULONGLONG_MAX == 18446744073709551615ULL) || \
  432. (defined(_ULLONG_MAX) && _ULLONG_MAX == 18446744073709551615ULL) || \
  433. (defined(_LLONG_MAX) && _LLONG_MAX == 9223372036854775807LL)
  434. # define INT64_C(value) value##LL
  435. # define UINT64_C(value) value##uLL
  436. # else
  437. # error defaults not correct; you must hand modify boost/cstdint.hpp
  438. # endif
  439. # elif ULONG_MAX != 0xffffffff
  440. # if ULONG_MAX == 18446744073709551615U // 2**64 - 1
  441. # define INT64_C(value) value##L
  442. # define UINT64_C(value) value##uL
  443. # else
  444. # error defaults not correct; you must hand modify boost/cstdint.hpp
  445. # endif
  446. # elif defined(BOOST_HAS_LONG_LONG)
  447. // Usual macros not defined, work things out for ourselves:
  448. # if(~0uLL == 18446744073709551615ULL)
  449. # define INT64_C(value) value##LL
  450. # define UINT64_C(value) value##uLL
  451. # else
  452. # error defaults not correct; you must hand modify boost/cstdint.hpp
  453. # endif
  454. # else
  455. # error defaults not correct; you must hand modify boost/cstdint.hpp
  456. # endif
  457. # ifdef BOOST_NO_INT64_T
  458. # define INTMAX_C(value) INT32_C(value)
  459. # define UINTMAX_C(value) UINT32_C(value)
  460. # else
  461. # define INTMAX_C(value) INT64_C(value)
  462. # define UINTMAX_C(value) UINT64_C(value)
  463. # endif
  464. #endif
  465. # endif // Borland/Microsoft specific width suffixes
  466. #endif // INT#_C macros.