stdint_.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /* Copyright (C) 2001-2002, 2004-2007 Free Software Foundation, Inc.
  2. Written by Paul Eggert, Bruno Haible, Sam Steingold, Peter Burwood.
  3. This file is part of gnulib.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  15. #ifndef _GL_STDINT_H
  16. #define _GL_STDINT_H
  17. /*
  18. * ISO C 99 <stdint.h> for platforms that lack it.
  19. * <http://www.opengroup.org/susv3xbd/stdint.h.html>
  20. */
  21. /* Get those types that are already defined in other system include
  22. files, so that we can "#define int8_t signed char" below without
  23. worrying about a later system include file containing a "typedef
  24. signed char int8_t;" that will get messed up by our macro. Our
  25. macros should all be consistent with the system versions, except
  26. for the "fast" types and macros, which we recommend against using
  27. in public interfaces due to compiler differences. */
  28. #if @HAVE_STDINT_H@
  29. # if defined __sgi && ! defined __c99
  30. /* Bypass IRIX's <stdint.h> if in C89 mode, since it merely annoys users
  31. with "This header file is to be used only for c99 mode compilations"
  32. diagnostics. */
  33. # define __STDINT_H__
  34. # endif
  35. /* Other systems may have an incomplete or buggy <stdint.h>.
  36. Include it before <inttypes.h>, since any "#include <stdint.h>"
  37. in <inttypes.h> would reinclude us, skipping our contents because
  38. _GL_STDINT_H is defined. */
  39. # include @ABSOLUTE_STDINT_H@
  40. #endif
  41. /* <sys/types.h> defines some of the stdint.h types as well, on glibc,
  42. IRIX 6.5, and OpenBSD 3.8 (via <machine/types.h>).
  43. AIX 5.2 <sys/types.h> isn't needed and causes troubles.
  44. MacOS X 10.4.6 <sys/types.h> includes <stdint.h> (which is us), but
  45. relies on the system <stdint.h> definitions, so include
  46. <sys/types.h> after @ABSOLUTE_STDINT_H@. */
  47. #if @HAVE_SYS_TYPES_H@ && ! defined _AIX
  48. # include <sys/types.h>
  49. #endif
  50. /* Get LONG_MIN, LONG_MAX, ULONG_MAX. */
  51. #include <limits.h>
  52. #if @HAVE_INTTYPES_H@
  53. /* In OpenBSD 3.8, <inttypes.h> includes <machine/types.h>, which defines
  54. int{8,16,32,64}_t, uint{8,16,32,64}_t and __BIT_TYPES_DEFINED__.
  55. <inttypes.h> also defines intptr_t and uintptr_t. */
  56. # define _GL_JUST_INCLUDE_ABSOLUTE_INTTYPES_H
  57. # include <inttypes.h>
  58. # undef _GL_JUST_INCLUDE_ABSOLUTE_INTTYPES_H
  59. #elif @HAVE_SYS_INTTYPES_H@
  60. /* Solaris 7 <sys/inttypes.h> has the types except the *_fast*_t types, and
  61. the macros except for *_FAST*_*, INTPTR_MIN, PTRDIFF_MIN, PTRDIFF_MAX. */
  62. # include <sys/inttypes.h>
  63. #endif
  64. #if @HAVE_SYS_BITYPES_H@ && ! defined __BIT_TYPES_DEFINED__
  65. /* Linux libc4 >= 4.6.7 and libc5 have a <sys/bitypes.h> that defines
  66. int{8,16,32,64}_t and __BIT_TYPES_DEFINED__. In libc5 >= 5.2.2 it is
  67. included by <sys/types.h>. */
  68. # include <sys/bitypes.h>
  69. #endif
  70. #if ! defined __cplusplus || defined __STDC_CONSTANT_MACROS
  71. /* Get WCHAR_MIN, WCHAR_MAX. */
  72. # if ! (defined WCHAR_MIN && defined WCHAR_MAX)
  73. # include <wchar.h>
  74. # endif
  75. #endif
  76. /* Minimum and maximum values for a integer type under the usual assumption.
  77. Return an unspecified value if BITS == 0, adding a check to pacify
  78. picky compilers. */
  79. #define _STDINT_MIN(signed, bits, zero) \
  80. ((signed) ? (- ((zero) + 1) << ((bits) ? (bits) - 1 : 0)) : (zero))
  81. #define _STDINT_MAX(signed, bits, zero) \
  82. ((signed) \
  83. ? ~ _STDINT_MIN (signed, bits, zero) \
  84. : ((((zero) + 1) << ((bits) ? (bits) - 1 : 0)) - 1) * 2 + 1)
  85. /* 7.18.1.1. Exact-width integer types */
  86. /* Here we assume a standard architecture where the hardware integer
  87. types have 8, 16, 32, optionally 64 bits. */
  88. #undef int8_t
  89. #undef uint8_t
  90. #define int8_t signed char
  91. #define uint8_t unsigned char
  92. #undef int16_t
  93. #undef uint16_t
  94. #define int16_t short int
  95. #define uint16_t unsigned short int
  96. #undef int32_t
  97. #undef uint32_t
  98. #define int32_t int
  99. #define uint32_t unsigned int
  100. #undef int64_t
  101. #if LONG_MAX >> 31 >> 31 == 1
  102. # define int64_t long int
  103. #elif defined _MSC_VER
  104. # define int64_t __int64
  105. #elif @HAVE_LONG_LONG_INT@
  106. # define int64_t long long int
  107. #endif
  108. #undef uint64_t
  109. #if ULONG_MAX >> 31 >> 31 >> 1 == 1
  110. # define uint64_t unsigned long int
  111. #elif defined _MSC_VER
  112. # define uint64_t unsigned __int64
  113. #elif @HAVE_UNSIGNED_LONG_LONG_INT@
  114. # define uint64_t unsigned long long int
  115. #endif
  116. /* Avoid collision with Solaris 2.5.1 <pthread.h> etc. */
  117. #define _UINT8_T
  118. #define _UINT32_T
  119. #define _UINT64_T
  120. /* 7.18.1.2. Minimum-width integer types */
  121. /* Here we assume a standard architecture where the hardware integer
  122. types have 8, 16, 32, optionally 64 bits. Therefore the leastN_t types
  123. are the same as the corresponding N_t types. */
  124. #undef int_least8_t
  125. #undef uint_least8_t
  126. #undef int_least16_t
  127. #undef uint_least16_t
  128. #undef int_least32_t
  129. #undef uint_least32_t
  130. #undef int_least64_t
  131. #undef uint_least64_t
  132. #define int_least8_t int8_t
  133. #define uint_least8_t uint8_t
  134. #define int_least16_t int16_t
  135. #define uint_least16_t uint16_t
  136. #define int_least32_t int32_t
  137. #define uint_least32_t uint32_t
  138. #ifdef int64_t
  139. # define int_least64_t int64_t
  140. #endif
  141. #ifdef uint64_t
  142. # define uint_least64_t uint64_t
  143. #endif
  144. /* 7.18.1.3. Fastest minimum-width integer types */
  145. /* Note: Other <stdint.h> substitutes may define these types differently.
  146. It is not recommended to use these types in public header files. */
  147. /* Here we assume a standard architecture where the hardware integer
  148. types have 8, 16, 32, optionally 64 bits. Therefore the fastN_t types
  149. are taken from the same list of types. Assume that 'long int'
  150. is fast enough for all narrower integers. */
  151. #undef int_fast8_t
  152. #undef uint_fast8_t
  153. #undef int_fast16_t
  154. #undef uint_fast16_t
  155. #undef int_fast32_t
  156. #undef uint_fast32_t
  157. #undef int_fast64_t
  158. #undef uint_fast64_t
  159. #define int_fast8_t long int
  160. #define uint_fast8_t unsigned int_fast8_t
  161. #define int_fast16_t long int
  162. #define uint_fast16_t unsigned int_fast16_t
  163. #define int_fast32_t long int
  164. #define uint_fast32_t unsigned int_fast32_t
  165. #ifdef int64_t
  166. # define int_fast64_t int64_t
  167. #endif
  168. #ifdef uint64_t
  169. # define uint_fast64_t uint64_t
  170. #endif
  171. /* 7.18.1.4. Integer types capable of holding object pointers */
  172. #undef intptr_t
  173. #undef uintptr_t
  174. #define intptr_t long int
  175. #define uintptr_t unsigned long int
  176. /* 7.18.1.5. Greatest-width integer types */
  177. /* Note: These types are compiler dependent. It may be unwise to use them in
  178. public header files. */
  179. #undef intmax_t
  180. #if @HAVE_LONG_LONG_INT@ && LONG_MAX >> 30 == 1
  181. # define intmax_t long long int
  182. #elif defined int64_t
  183. # define intmax_t int64_t
  184. #else
  185. # define intmax_t long int
  186. #endif
  187. #undef uintmax_t
  188. #if @HAVE_UNSIGNED_LONG_LONG_INT@ && ULONG_MAX >> 31 == 1
  189. # define uintmax_t unsigned long long int
  190. #elif defined uint64_t
  191. # define uintmax_t uint64_t
  192. #else
  193. # define uintmax_t unsigned long int
  194. #endif
  195. /* 7.18.2. Limits of specified-width integer types */
  196. #if ! defined __cplusplus || defined __STDC_LIMIT_MACROS
  197. /* 7.18.2.1. Limits of exact-width integer types */
  198. /* Here we assume a standard architecture where the hardware integer
  199. types have 8, 16, 32, optionally 64 bits. */
  200. #undef INT8_MIN
  201. #undef INT8_MAX
  202. #undef UINT8_MAX
  203. #define INT8_MIN (~ INT8_MAX)
  204. #define INT8_MAX 127
  205. #define UINT8_MAX 255
  206. #undef INT16_MIN
  207. #undef INT16_MAX
  208. #undef UINT16_MAX
  209. #define INT16_MIN (~ INT16_MAX)
  210. #define INT16_MAX 32767
  211. #define UINT16_MAX 65535
  212. #undef INT32_MIN
  213. #undef INT32_MAX
  214. #undef UINT32_MAX
  215. #define INT32_MIN (~ INT32_MAX)
  216. #define INT32_MAX 2147483647
  217. #define UINT32_MAX 4294967295U
  218. #undef INT64_MIN
  219. #undef INT64_MAX
  220. #ifdef int64_t
  221. # define INT64_MIN (~ INT64_MAX)
  222. # define INT64_MAX INTMAX_C (9223372036854775807)
  223. #endif
  224. #undef UINT64_MAX
  225. #ifdef uint64_t
  226. # define UINT64_MAX UINTMAX_C (18446744073709551615)
  227. #endif
  228. /* 7.18.2.2. Limits of minimum-width integer types */
  229. /* Here we assume a standard architecture where the hardware integer
  230. types have 8, 16, 32, optionally 64 bits. Therefore the leastN_t types
  231. are the same as the corresponding N_t types. */
  232. #undef INT_LEAST8_MIN
  233. #undef INT_LEAST8_MAX
  234. #undef UINT_LEAST8_MAX
  235. #define INT_LEAST8_MIN INT8_MIN
  236. #define INT_LEAST8_MAX INT8_MAX
  237. #define UINT_LEAST8_MAX UINT8_MAX
  238. #undef INT_LEAST16_MIN
  239. #undef INT_LEAST16_MAX
  240. #undef UINT_LEAST16_MAX
  241. #define INT_LEAST16_MIN INT16_MIN
  242. #define INT_LEAST16_MAX INT16_MAX
  243. #define UINT_LEAST16_MAX UINT16_MAX
  244. #undef INT_LEAST32_MIN
  245. #undef INT_LEAST32_MAX
  246. #undef UINT_LEAST32_MAX
  247. #define INT_LEAST32_MIN INT32_MIN
  248. #define INT_LEAST32_MAX INT32_MAX
  249. #define UINT_LEAST32_MAX UINT32_MAX
  250. #undef INT_LEAST64_MIN
  251. #undef INT_LEAST64_MAX
  252. #ifdef int64_t
  253. # define INT_LEAST64_MIN INT64_MIN
  254. # define INT_LEAST64_MAX INT64_MAX
  255. #endif
  256. #undef UINT_LEAST64_MAX
  257. #ifdef uint64_t
  258. # define UINT_LEAST64_MAX UINT64_MAX
  259. #endif
  260. /* 7.18.2.3. Limits of fastest minimum-width integer types */
  261. /* Here we assume a standard architecture where the hardware integer
  262. types have 8, 16, 32, optionally 64 bits. Therefore the fastN_t types
  263. are taken from the same list of types. */
  264. #undef INT_FAST8_MIN
  265. #undef INT_FAST8_MAX
  266. #undef UINT_FAST8_MAX
  267. #define INT_FAST8_MIN LONG_MIN
  268. #define INT_FAST8_MAX LONG_MAX
  269. #define UINT_FAST8_MAX ULONG_MAX
  270. #undef INT_FAST16_MIN
  271. #undef INT_FAST16_MAX
  272. #undef UINT_FAST16_MAX
  273. #define INT_FAST16_MIN LONG_MIN
  274. #define INT_FAST16_MAX LONG_MAX
  275. #define UINT_FAST16_MAX ULONG_MAX
  276. #undef INT_FAST32_MIN
  277. #undef INT_FAST32_MAX
  278. #undef UINT_FAST32_MAX
  279. #define INT_FAST32_MIN LONG_MIN
  280. #define INT_FAST32_MAX LONG_MAX
  281. #define UINT_FAST32_MAX ULONG_MAX
  282. #undef INT_FAST64_MIN
  283. #undef INT_FAST64_MAX
  284. #ifdef int64_t
  285. # define INT_FAST64_MIN INT64_MIN
  286. # define INT_FAST64_MAX INT64_MAX
  287. #endif
  288. #undef UINT_FAST64_MAX
  289. #ifdef uint64_t
  290. # define UINT_FAST64_MAX UINT64_MAX
  291. #endif
  292. /* 7.18.2.4. Limits of integer types capable of holding object pointers */
  293. #undef INTPTR_MIN
  294. #undef INTPTR_MAX
  295. #undef UINTPTR_MAX
  296. #define INTPTR_MIN LONG_MIN
  297. #define INTPTR_MAX LONG_MAX
  298. #define UINTPTR_MAX ULONG_MAX
  299. /* 7.18.2.5. Limits of greatest-width integer types */
  300. #undef INTMAX_MIN
  301. #undef INTMAX_MAX
  302. #define INTMAX_MIN (~ INTMAX_MAX)
  303. #ifdef INT64_MAX
  304. # define INTMAX_MAX INT64_MAX
  305. #else
  306. # define INTMAX_MAX INT32_MAX
  307. #endif
  308. #undef UINTMAX_MAX
  309. #ifdef UINT64_MAX
  310. # define UINTMAX_MAX UINT64_MAX
  311. #else
  312. # define UINTMAX_MAX UINT32_MAX
  313. #endif
  314. /* 7.18.3. Limits of other integer types */
  315. /* ptrdiff_t limits */
  316. #undef PTRDIFF_MIN
  317. #undef PTRDIFF_MAX
  318. #define PTRDIFF_MIN \
  319. _STDINT_MIN (1, @BITSIZEOF_PTRDIFF_T@, 0@PTRDIFF_T_SUFFIX@)
  320. #define PTRDIFF_MAX \
  321. _STDINT_MAX (1, @BITSIZEOF_PTRDIFF_T@, 0@PTRDIFF_T_SUFFIX@)
  322. /* sig_atomic_t limits */
  323. #undef SIG_ATOMIC_MIN
  324. #undef SIG_ATOMIC_MAX
  325. #define SIG_ATOMIC_MIN \
  326. _STDINT_MIN (@HAVE_SIGNED_SIG_ATOMIC_T@, @BITSIZEOF_SIG_ATOMIC_T@, \
  327. 0@SIG_ATOMIC_T_SUFFIX@)
  328. #define SIG_ATOMIC_MAX \
  329. _STDINT_MAX (@HAVE_SIGNED_SIG_ATOMIC_T@, @BITSIZEOF_SIG_ATOMIC_T@, \
  330. 0@SIG_ATOMIC_T_SUFFIX@)
  331. /* size_t limit */
  332. #undef SIZE_MAX
  333. #define SIZE_MAX _STDINT_MAX (0, @BITSIZEOF_SIZE_T@, 0@SIZE_T_SUFFIX@)
  334. /* wchar_t limits */
  335. #undef WCHAR_MIN
  336. #undef WCHAR_MAX
  337. #define WCHAR_MIN \
  338. _STDINT_MIN (@HAVE_SIGNED_WCHAR_T@, @BITSIZEOF_WCHAR_T@, 0@WCHAR_T_SUFFIX@)
  339. #define WCHAR_MAX \
  340. _STDINT_MAX (@HAVE_SIGNED_WCHAR_T@, @BITSIZEOF_WCHAR_T@, 0@WCHAR_T_SUFFIX@)
  341. /* wint_t limits */
  342. #undef WINT_MIN
  343. #undef WINT_MAX
  344. #define WINT_MIN \
  345. _STDINT_MIN (@HAVE_SIGNED_WINT_T@, @BITSIZEOF_WINT_T@, 0@WINT_T_SUFFIX@)
  346. #define WINT_MAX \
  347. _STDINT_MAX (@HAVE_SIGNED_WINT_T@, @BITSIZEOF_WINT_T@, 0@WINT_T_SUFFIX@)
  348. #endif /* !defined __cplusplus || defined __STDC_LIMIT_MACROS */
  349. /* 7.18.4. Macros for integer constants */
  350. #if ! defined __cplusplus || defined __STDC_CONSTANT_MACROS
  351. /* 7.18.4.1. Macros for minimum-width integer constants */
  352. /* According to ISO C 99 Technical Corrigendum 1 */
  353. /* Here we assume a standard architecture where the hardware integer
  354. types have 8, 16, 32, optionally 64 bits, and int is 32 bits. */
  355. #undef INT8_C
  356. #undef UINT8_C
  357. #define INT8_C(x) x
  358. #define UINT8_C(x) x
  359. #undef INT16_C
  360. #undef UINT16_C
  361. #define INT16_C(x) x
  362. #define UINT16_C(x) x
  363. #undef INT32_C
  364. #undef UINT32_C
  365. #define INT32_C(x) x
  366. #define UINT32_C(x) x ## U
  367. #undef INT64_C
  368. #undef UINT64_C
  369. #if LONG_MAX >> 31 >> 31 == 1
  370. # define INT64_C(x) x##L
  371. #elif defined _MSC_VER
  372. # define INT64_C(x) x##i64
  373. #elif @HAVE_LONG_LONG_INT@
  374. # define INT64_C(x) x##LL
  375. #endif
  376. #if ULONG_MAX >> 31 >> 31 >> 1 == 1
  377. # define UINT64_C(x) x##UL
  378. #elif defined _MSC_VER
  379. # define UINT64_C(x) x##ui64
  380. #elif @HAVE_UNSIGNED_LONG_LONG_INT@
  381. # define UINT64_C(x) x##ULL
  382. #endif
  383. /* 7.18.4.2. Macros for greatest-width integer constants */
  384. #undef INTMAX_C
  385. #if @HAVE_LONG_LONG_INT@ && LONG_MAX >> 30 == 1
  386. # define INTMAX_C(x) x##LL
  387. #elif defined int64_t
  388. # define INTMAX_C(x) INT64_C(x)
  389. #else
  390. # define INTMAX_C(x) x##L
  391. #endif
  392. #undef UINTMAX_C
  393. #if @HAVE_UNSIGNED_LONG_LONG_INT@ && ULONG_MAX >> 31 == 1
  394. # define UINTMAX_C(x) x##ULL
  395. #elif defined uint64_t
  396. # define UINTMAX_C(x) UINT64_C(x)
  397. #else
  398. # define UINTMAX_C(x) x##UL
  399. #endif
  400. #endif /* !defined __cplusplus || defined __STDC_CONSTANT_MACROS */
  401. #endif /* _GL_STDINT_H */