pstdint.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. /* A portable stdint.h
  2. ****************************************************************************
  3. * BSD License:
  4. ****************************************************************************
  5. *
  6. * Copyright (c) 2005-2016 Paul Hsieh
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. The name of the author may not be used to endorse or promote products
  19. * derived from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  22. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  23. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  30. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. ****************************************************************************
  33. *
  34. * Version 0.1.16.0
  35. *
  36. * The ANSI C standard committee, for the C99 standard, specified the
  37. * inclusion of a new standard include file called stdint.h. This is
  38. * a very useful and long desired include file which contains several
  39. * very precise definitions for integer scalar types that is critically
  40. * important for making several classes of applications portable
  41. * including cryptography, hashing, variable length integer libraries
  42. * and so on. But for most developers its likely useful just for
  43. * programming sanity.
  44. *
  45. * The problem is that some compiler vendors chose to ignore the C99
  46. * standard and some older compilers have no opportunity to be updated.
  47. * Because of this situation, simply including stdint.h in your code
  48. * makes it unportable.
  49. *
  50. * So that's what this file is all about. It's an attempt to build a
  51. * single universal include file that works on as many platforms as
  52. * possible to deliver what stdint.h is supposed to. Even compilers
  53. * that already come with stdint.h can use this file instead without
  54. * any loss of functionality. A few things that should be noted about
  55. * this file:
  56. *
  57. * 1) It is not guaranteed to be portable and/or present an identical
  58. * interface on all platforms. The extreme variability of the
  59. * ANSI C standard makes this an impossibility right from the
  60. * very get go. Its really only meant to be useful for the vast
  61. * majority of platforms that possess the capability of
  62. * implementing usefully and precisely defined, standard sized
  63. * integer scalars. Systems which are not intrinsically 2s
  64. * complement may produce invalid constants.
  65. *
  66. * 2) There is an unavoidable use of non-reserved symbols.
  67. *
  68. * 3) Other standard include files are invoked.
  69. *
  70. * 4) This file may come in conflict with future platforms that do
  71. * include stdint.h. The hope is that one or the other can be
  72. * used with no real difference.
  73. *
  74. * 5) In the current version, if your platform can't represent
  75. * int32_t, int16_t and int8_t, it just dumps out with a compiler
  76. * error.
  77. *
  78. * 6) 64 bit integers may or may not be defined. Test for their
  79. * presence with the test: #ifdef INT64_MAX or #ifdef UINT64_MAX.
  80. * Note that this is different from the C99 specification which
  81. * requires the existence of 64 bit support in the compiler. If
  82. * this is not defined for your platform, yet it is capable of
  83. * dealing with 64 bits then it is because this file has not yet
  84. * been extended to cover all of your system's capabilities.
  85. *
  86. * 7) (u)intptr_t may or may not be defined. Test for its presence
  87. * with the test: #ifdef PTRDIFF_MAX. If this is not defined
  88. * for your platform, then it is because this file has not yet
  89. * been extended to cover all of your system's capabilities, not
  90. * because its optional.
  91. *
  92. * 8) The following might not been defined even if your platform is
  93. * capable of defining it:
  94. *
  95. * WCHAR_MIN
  96. * WCHAR_MAX
  97. * (u)int64_t
  98. * PTRDIFF_MIN
  99. * PTRDIFF_MAX
  100. * (u)intptr_t
  101. *
  102. * 9) The following have not been defined:
  103. *
  104. * WINT_MIN
  105. * WINT_MAX
  106. *
  107. * 10) The criteria for defining (u)int_least(*)_t isn't clear,
  108. * except for systems which don't have a type that precisely
  109. * defined 8, 16, or 32 bit types (which this include file does
  110. * not support anyways). Default definitions have been given.
  111. *
  112. * 11) The criteria for defining (u)int_fast(*)_t isn't something I
  113. * would trust to any particular compiler vendor or the ANSI C
  114. * committee. It is well known that "compatible systems" are
  115. * commonly created that have very different performance
  116. * characteristics from the systems they are compatible with,
  117. * especially those whose vendors make both the compiler and the
  118. * system. Default definitions have been given, but its strongly
  119. * recommended that users never use these definitions for any
  120. * reason (they do *NOT* deliver any serious guarantee of
  121. * improved performance -- not in this file, nor any vendor's
  122. * stdint.h).
  123. *
  124. * 12) The following macros:
  125. *
  126. * PRINTF_INTMAX_MODIFIER
  127. * PRINTF_INT64_MODIFIER
  128. * PRINTF_INT32_MODIFIER
  129. * PRINTF_INT16_MODIFIER
  130. * PRINTF_LEAST64_MODIFIER
  131. * PRINTF_LEAST32_MODIFIER
  132. * PRINTF_LEAST16_MODIFIER
  133. * PRINTF_INTPTR_MODIFIER
  134. *
  135. * are strings which have been defined as the modifiers required
  136. * for the "d", "u" and "x" printf formats to correctly output
  137. * (u)intmax_t, (u)int64_t, (u)int32_t, (u)int16_t, (u)least64_t,
  138. * (u)least32_t, (u)least16_t and (u)intptr_t types respectively.
  139. * PRINTF_INTPTR_MODIFIER is not defined for some systems which
  140. * provide their own stdint.h. PRINTF_INT64_MODIFIER is not
  141. * defined if INT64_MAX is not defined. These are an extension
  142. * beyond what C99 specifies must be in stdint.h.
  143. *
  144. * In addition, the following macros are defined:
  145. *
  146. * PRINTF_INTMAX_HEX_WIDTH
  147. * PRINTF_INT64_HEX_WIDTH
  148. * PRINTF_INT32_HEX_WIDTH
  149. * PRINTF_INT16_HEX_WIDTH
  150. * PRINTF_INT8_HEX_WIDTH
  151. * PRINTF_INTMAX_DEC_WIDTH
  152. * PRINTF_INT64_DEC_WIDTH
  153. * PRINTF_INT32_DEC_WIDTH
  154. * PRINTF_INT16_DEC_WIDTH
  155. * PRINTF_UINT8_DEC_WIDTH
  156. * PRINTF_UINTMAX_DEC_WIDTH
  157. * PRINTF_UINT64_DEC_WIDTH
  158. * PRINTF_UINT32_DEC_WIDTH
  159. * PRINTF_UINT16_DEC_WIDTH
  160. * PRINTF_UINT8_DEC_WIDTH
  161. *
  162. * Which specifies the maximum number of characters required to
  163. * print the number of that type in either hexadecimal or decimal.
  164. * These are an extension beyond what C99 specifies must be in
  165. * stdint.h.
  166. *
  167. * Compilers tested (all with 0 warnings at their highest respective
  168. * settings): Borland Turbo C 2.0, WATCOM C/C++ 11.0 (16 bits and 32
  169. * bits), Microsoft Visual C++ 6.0 (32 bit), Microsoft Visual Studio
  170. * .net (VC7), Intel C++ 4.0, GNU gcc v3.3.3
  171. *
  172. * This file should be considered a work in progress. Suggestions for
  173. * improvements, especially those which increase coverage are strongly
  174. * encouraged.
  175. *
  176. * Acknowledgements
  177. *
  178. * The following people have made significant contributions to the
  179. * development and testing of this file:
  180. *
  181. * Chris Howie
  182. * John Steele Scott
  183. * Dave Thorup
  184. * John Dill
  185. * Florian Wobbe
  186. * Christopher Sean Morrison
  187. * Mikkel Fahnoe Jorgensen
  188. *
  189. */
  190. #include <stddef.h>
  191. #include <limits.h>
  192. #include <signal.h>
  193. /*
  194. * For gcc with _STDINT_H, fill in the PRINTF_INT*_MODIFIER macros, and
  195. * do nothing else. On the Mac OS X version of gcc this is _STDINT_H_.
  196. */
  197. #if ((defined(__SUNPRO_C) && __SUNPRO_C >= 0x570) || (defined(_MSC_VER) && _MSC_VER >= 1600) || (defined(__STDC__) && __STDC__ && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || (defined (__WATCOMC__) && (defined (_STDINT_H_INCLUDED) || __WATCOMC__ >= 1250)) || (defined(__GNUC__) && (__GNUC__ > 3 || defined(_STDINT_H) || defined(_STDINT_H_) || defined (__UINT_FAST64_TYPE__)) )) && !defined (_PSTDINT_H_INCLUDED)
  198. #include <stdint.h>
  199. #define _PSTDINT_H_INCLUDED
  200. # if defined(__GNUC__) && (defined(__x86_64__) || defined(__ppc64__)) && !(defined(__APPLE__) && defined(__MACH__))
  201. # ifndef PRINTF_INT64_MODIFIER
  202. # define PRINTF_INT64_MODIFIER "l"
  203. # endif
  204. # ifndef PRINTF_INT32_MODIFIER
  205. # define PRINTF_INT32_MODIFIER ""
  206. # endif
  207. # else
  208. # ifndef PRINTF_INT64_MODIFIER
  209. # define PRINTF_INT64_MODIFIER "ll"
  210. # endif
  211. # ifndef PRINTF_INT32_MODIFIER
  212. # if (UINT_MAX == UINT32_MAX)
  213. # define PRINTF_INT32_MODIFIER ""
  214. # else
  215. # define PRINTF_INT32_MODIFIER "l"
  216. # endif
  217. # endif
  218. # endif
  219. # ifndef PRINTF_INT16_MODIFIER
  220. # define PRINTF_INT16_MODIFIER "h"
  221. # endif
  222. # ifndef PRINTF_INTMAX_MODIFIER
  223. # define PRINTF_INTMAX_MODIFIER PRINTF_INT64_MODIFIER
  224. # endif
  225. # ifndef PRINTF_INT64_HEX_WIDTH
  226. # define PRINTF_INT64_HEX_WIDTH "16"
  227. # endif
  228. # ifndef PRINTF_UINT64_HEX_WIDTH
  229. # define PRINTF_UINT64_HEX_WIDTH "16"
  230. # endif
  231. # ifndef PRINTF_INT32_HEX_WIDTH
  232. # define PRINTF_INT32_HEX_WIDTH "8"
  233. # endif
  234. # ifndef PRINTF_UINT32_HEX_WIDTH
  235. # define PRINTF_UINT32_HEX_WIDTH "8"
  236. # endif
  237. # ifndef PRINTF_INT16_HEX_WIDTH
  238. # define PRINTF_INT16_HEX_WIDTH "4"
  239. # endif
  240. # ifndef PRINTF_UINT16_HEX_WIDTH
  241. # define PRINTF_UINT16_HEX_WIDTH "4"
  242. # endif
  243. # ifndef PRINTF_INT8_HEX_WIDTH
  244. # define PRINTF_INT8_HEX_WIDTH "2"
  245. # endif
  246. # ifndef PRINTF_UINT8_HEX_WIDTH
  247. # define PRINTF_UINT8_HEX_WIDTH "2"
  248. # endif
  249. # ifndef PRINTF_INT64_DEC_WIDTH
  250. # define PRINTF_INT64_DEC_WIDTH "19"
  251. # endif
  252. # ifndef PRINTF_UINT64_DEC_WIDTH
  253. # define PRINTF_UINT64_DEC_WIDTH "20"
  254. # endif
  255. # ifndef PRINTF_INT32_DEC_WIDTH
  256. # define PRINTF_INT32_DEC_WIDTH "10"
  257. # endif
  258. # ifndef PRINTF_UINT32_DEC_WIDTH
  259. # define PRINTF_UINT32_DEC_WIDTH "10"
  260. # endif
  261. # ifndef PRINTF_INT16_DEC_WIDTH
  262. # define PRINTF_INT16_DEC_WIDTH "5"
  263. # endif
  264. # ifndef PRINTF_UINT16_DEC_WIDTH
  265. # define PRINTF_UINT16_DEC_WIDTH "5"
  266. # endif
  267. # ifndef PRINTF_INT8_DEC_WIDTH
  268. # define PRINTF_INT8_DEC_WIDTH "3"
  269. # endif
  270. # ifndef PRINTF_UINT8_DEC_WIDTH
  271. # define PRINTF_UINT8_DEC_WIDTH "3"
  272. # endif
  273. # ifndef PRINTF_INTMAX_HEX_WIDTH
  274. # define PRINTF_INTMAX_HEX_WIDTH PRINTF_UINT64_HEX_WIDTH
  275. # endif
  276. # ifndef PRINTF_UINTMAX_HEX_WIDTH
  277. # define PRINTF_UINTMAX_HEX_WIDTH PRINTF_UINT64_HEX_WIDTH
  278. # endif
  279. # ifndef PRINTF_INTMAX_DEC_WIDTH
  280. # define PRINTF_INTMAX_DEC_WIDTH PRINTF_UINT64_DEC_WIDTH
  281. # endif
  282. # ifndef PRINTF_UINTMAX_DEC_WIDTH
  283. # define PRINTF_UINTMAX_DEC_WIDTH PRINTF_UINT64_DEC_WIDTH
  284. # endif
  285. /*
  286. * Something really weird is going on with Open Watcom. Just pull some of
  287. * these duplicated definitions from Open Watcom's stdint.h file for now.
  288. */
  289. # if defined (__WATCOMC__) && __WATCOMC__ >= 1250
  290. # if !defined (INT64_C)
  291. # define INT64_C(x) (x + (INT64_MAX - INT64_MAX))
  292. # endif
  293. # if !defined (UINT64_C)
  294. # define UINT64_C(x) (x + (UINT64_MAX - UINT64_MAX))
  295. # endif
  296. # if !defined (INT32_C)
  297. # define INT32_C(x) (x + (INT32_MAX - INT32_MAX))
  298. # endif
  299. # if !defined (UINT32_C)
  300. # define UINT32_C(x) (x + (UINT32_MAX - UINT32_MAX))
  301. # endif
  302. # if !defined (INT16_C)
  303. # define INT16_C(x) (x)
  304. # endif
  305. # if !defined (UINT16_C)
  306. # define UINT16_C(x) (x)
  307. # endif
  308. # if !defined (INT8_C)
  309. # define INT8_C(x) (x)
  310. # endif
  311. # if !defined (UINT8_C)
  312. # define UINT8_C(x) (x)
  313. # endif
  314. # if !defined (UINT64_MAX)
  315. # define UINT64_MAX 18446744073709551615ULL
  316. # endif
  317. # if !defined (INT64_MAX)
  318. # define INT64_MAX 9223372036854775807LL
  319. # endif
  320. # if !defined (UINT32_MAX)
  321. # define UINT32_MAX 4294967295UL
  322. # endif
  323. # if !defined (INT32_MAX)
  324. # define INT32_MAX 2147483647L
  325. # endif
  326. # if !defined (INTMAX_MAX)
  327. # define INTMAX_MAX INT64_MAX
  328. # endif
  329. # if !defined (INTMAX_MIN)
  330. # define INTMAX_MIN INT64_MIN
  331. # endif
  332. # endif
  333. #endif
  334. /*
  335. * I have no idea what is the truly correct thing to do on older Solaris.
  336. * From some online discussions, this seems to be what is being
  337. * recommended. For people who actually are developing on older Solaris,
  338. * what I would like to know is, does this define all of the relevant
  339. * macros of a complete stdint.h? Remember, in pstdint.h 64 bit is
  340. * considered optional.
  341. */
  342. #if (defined(__SUNPRO_C) && __SUNPRO_C >= 0x420) && !defined(_PSTDINT_H_INCLUDED)
  343. #include <sys/inttypes.h>
  344. #define _PSTDINT_H_INCLUDED
  345. #endif
  346. #ifndef _PSTDINT_H_INCLUDED
  347. #define _PSTDINT_H_INCLUDED
  348. #ifndef SIZE_MAX
  349. # define SIZE_MAX ((size_t)-1)
  350. #endif
  351. /*
  352. * Deduce the type assignments from limits.h under the assumption that
  353. * integer sizes in bits are powers of 2, and follow the ANSI
  354. * definitions.
  355. */
  356. #ifndef UINT8_MAX
  357. # define UINT8_MAX 0xff
  358. #endif
  359. #if !defined(uint8_t) && !defined(_UINT8_T) && !defined(vxWorks)
  360. # if (UCHAR_MAX == UINT8_MAX) || defined (S_SPLINT_S)
  361. typedef unsigned char uint8_t;
  362. # define UINT8_C(v) ((uint8_t) v)
  363. # else
  364. # error "Platform not supported"
  365. # endif
  366. #endif
  367. #ifndef INT8_MAX
  368. # define INT8_MAX 0x7f
  369. #endif
  370. #ifndef INT8_MIN
  371. # define INT8_MIN INT8_C(0x80)
  372. #endif
  373. #if !defined(int8_t) && !defined(_INT8_T) && !defined(vxWorks)
  374. # if (SCHAR_MAX == INT8_MAX) || defined (S_SPLINT_S)
  375. typedef signed char int8_t;
  376. # define INT8_C(v) ((int8_t) v)
  377. # else
  378. # error "Platform not supported"
  379. # endif
  380. #endif
  381. #ifndef UINT16_MAX
  382. # define UINT16_MAX 0xffff
  383. #endif
  384. #if !defined(uint16_t) && !defined(_UINT16_T) && !defined(vxWorks)
  385. #if (UINT_MAX == UINT16_MAX) || defined (S_SPLINT_S)
  386. typedef unsigned int uint16_t;
  387. # ifndef PRINTF_INT16_MODIFIER
  388. # define PRINTF_INT16_MODIFIER ""
  389. # endif
  390. # define UINT16_C(v) ((uint16_t) (v))
  391. #elif (USHRT_MAX == UINT16_MAX)
  392. typedef unsigned short uint16_t;
  393. # define UINT16_C(v) ((uint16_t) (v))
  394. # ifndef PRINTF_INT16_MODIFIER
  395. # define PRINTF_INT16_MODIFIER "h"
  396. # endif
  397. #else
  398. #error "Platform not supported"
  399. #endif
  400. #endif
  401. #ifndef INT16_MAX
  402. # define INT16_MAX 0x7fff
  403. #endif
  404. #ifndef INT16_MIN
  405. # define INT16_MIN INT16_C(0x8000)
  406. #endif
  407. #if !defined(int16_t) && !defined(_INT16_T) && !defined(vxWorks)
  408. #if (INT_MAX == INT16_MAX) || defined (S_SPLINT_S)
  409. typedef signed int int16_t;
  410. # define INT16_C(v) ((int16_t) (v))
  411. # ifndef PRINTF_INT16_MODIFIER
  412. # define PRINTF_INT16_MODIFIER ""
  413. # endif
  414. #elif (SHRT_MAX == INT16_MAX)
  415. typedef signed short int16_t;
  416. # define INT16_C(v) ((int16_t) (v))
  417. # ifndef PRINTF_INT16_MODIFIER
  418. # define PRINTF_INT16_MODIFIER "h"
  419. # endif
  420. #else
  421. #error "Platform not supported"
  422. #endif
  423. #endif
  424. #ifndef UINT32_MAX
  425. # define UINT32_MAX (0xffffffffUL)
  426. #endif
  427. #if !defined(uint32_t) && !defined(_UINT32_T) && !defined(vxWorks)
  428. #if (ULONG_MAX == UINT32_MAX) || defined (S_SPLINT_S)
  429. typedef unsigned long uint32_t;
  430. # define UINT32_C(v) v ## UL
  431. # ifndef PRINTF_INT32_MODIFIER
  432. # define PRINTF_INT32_MODIFIER "l"
  433. # endif
  434. #elif (UINT_MAX == UINT32_MAX)
  435. typedef unsigned int uint32_t;
  436. # ifndef PRINTF_INT32_MODIFIER
  437. # define PRINTF_INT32_MODIFIER ""
  438. # endif
  439. # define UINT32_C(v) v ## U
  440. #elif (USHRT_MAX == UINT32_MAX)
  441. typedef unsigned short uint32_t;
  442. # define UINT32_C(v) ((unsigned short) (v))
  443. # ifndef PRINTF_INT32_MODIFIER
  444. # define PRINTF_INT32_MODIFIER ""
  445. # endif
  446. #else
  447. #error "Platform not supported"
  448. #endif
  449. #endif
  450. #ifndef INT32_MAX
  451. # define INT32_MAX (0x7fffffffL)
  452. #endif
  453. #ifndef INT32_MIN
  454. # define INT32_MIN INT32_C(0x80000000)
  455. #endif
  456. #if !defined(int32_t) && !defined(_INT32_T) && !defined(vxWorks)
  457. #if (LONG_MAX == INT32_MAX) || defined (S_SPLINT_S)
  458. typedef signed long int32_t;
  459. # define INT32_C(v) v ## L
  460. # ifndef PRINTF_INT32_MODIFIER
  461. # define PRINTF_INT32_MODIFIER "l"
  462. # endif
  463. #elif (INT_MAX == INT32_MAX)
  464. typedef signed int int32_t;
  465. # define INT32_C(v) v
  466. # ifndef PRINTF_INT32_MODIFIER
  467. # define PRINTF_INT32_MODIFIER ""
  468. # endif
  469. #elif (SHRT_MAX == INT32_MAX)
  470. typedef signed short int32_t;
  471. # define INT32_C(v) ((short) (v))
  472. # ifndef PRINTF_INT32_MODIFIER
  473. # define PRINTF_INT32_MODIFIER ""
  474. # endif
  475. #else
  476. #error "Platform not supported"
  477. #endif
  478. #endif
  479. /*
  480. * The macro stdint_int64_defined is temporarily used to record
  481. * whether or not 64 integer support is available. It must be
  482. * defined for any 64 integer extensions for new platforms that are
  483. * added.
  484. */
  485. #undef stdint_int64_defined
  486. #if (defined(__STDC__) && defined(__STDC_VERSION__)) || defined (S_SPLINT_S)
  487. # if (__STDC__ && __STDC_VERSION__ >= 199901L) || defined (S_SPLINT_S)
  488. # define stdint_int64_defined
  489. typedef long long int64_t;
  490. typedef unsigned long long uint64_t;
  491. # define UINT64_C(v) v ## ULL
  492. # define INT64_C(v) v ## LL
  493. # ifndef PRINTF_INT64_MODIFIER
  494. # define PRINTF_INT64_MODIFIER "ll"
  495. # endif
  496. # endif
  497. #endif
  498. #if !defined (stdint_int64_defined)
  499. # if defined(__GNUC__) && !defined(vxWorks)
  500. # define stdint_int64_defined
  501. __extension__ typedef long long int64_t;
  502. __extension__ typedef unsigned long long uint64_t;
  503. # define UINT64_C(v) v ## ULL
  504. # define INT64_C(v) v ## LL
  505. # ifndef PRINTF_INT64_MODIFIER
  506. # define PRINTF_INT64_MODIFIER "ll"
  507. # endif
  508. # elif defined(__MWERKS__) || defined (__SUNPRO_C) || defined (__SUNPRO_CC) || defined (__APPLE_CC__) || defined (_LONG_LONG) || defined (_CRAYC) || defined (S_SPLINT_S)
  509. # define stdint_int64_defined
  510. typedef long long int64_t;
  511. typedef unsigned long long uint64_t;
  512. # define UINT64_C(v) v ## ULL
  513. # define INT64_C(v) v ## LL
  514. # ifndef PRINTF_INT64_MODIFIER
  515. # define PRINTF_INT64_MODIFIER "ll"
  516. # endif
  517. # elif (defined(__WATCOMC__) && defined(__WATCOM_INT64__)) || (defined(_MSC_VER) && _INTEGRAL_MAX_BITS >= 64) || (defined (__BORLANDC__) && __BORLANDC__ > 0x460) || defined (__alpha) || defined (__DECC)
  518. # define stdint_int64_defined
  519. typedef __int64 int64_t;
  520. typedef unsigned __int64 uint64_t;
  521. # define UINT64_C(v) v ## UI64
  522. # define INT64_C(v) v ## I64
  523. # ifndef PRINTF_INT64_MODIFIER
  524. # define PRINTF_INT64_MODIFIER "I64"
  525. # endif
  526. # endif
  527. #endif
  528. #if !defined (LONG_LONG_MAX) && defined (INT64_C)
  529. # define LONG_LONG_MAX INT64_C (9223372036854775807)
  530. #endif
  531. #ifndef ULONG_LONG_MAX
  532. # define ULONG_LONG_MAX UINT64_C (18446744073709551615)
  533. #endif
  534. #if !defined (INT64_MAX) && defined (INT64_C)
  535. # define INT64_MAX INT64_C (9223372036854775807)
  536. #endif
  537. #if !defined (INT64_MIN) && defined (INT64_C)
  538. # define INT64_MIN INT64_C (-9223372036854775808)
  539. #endif
  540. #if !defined (UINT64_MAX) && defined (INT64_C)
  541. # define UINT64_MAX UINT64_C (18446744073709551615)
  542. #endif
  543. /*
  544. * Width of hexadecimal for number field.
  545. */
  546. #ifndef PRINTF_INT64_HEX_WIDTH
  547. # define PRINTF_INT64_HEX_WIDTH "16"
  548. #endif
  549. #ifndef PRINTF_INT32_HEX_WIDTH
  550. # define PRINTF_INT32_HEX_WIDTH "8"
  551. #endif
  552. #ifndef PRINTF_INT16_HEX_WIDTH
  553. # define PRINTF_INT16_HEX_WIDTH "4"
  554. #endif
  555. #ifndef PRINTF_INT8_HEX_WIDTH
  556. # define PRINTF_INT8_HEX_WIDTH "2"
  557. #endif
  558. #ifndef PRINTF_INT64_DEC_WIDTH
  559. # define PRINTF_INT64_DEC_WIDTH "19"
  560. #endif
  561. #ifndef PRINTF_INT32_DEC_WIDTH
  562. # define PRINTF_INT32_DEC_WIDTH "10"
  563. #endif
  564. #ifndef PRINTF_INT16_DEC_WIDTH
  565. # define PRINTF_INT16_DEC_WIDTH "5"
  566. #endif
  567. #ifndef PRINTF_INT8_DEC_WIDTH
  568. # define PRINTF_INT8_DEC_WIDTH "3"
  569. #endif
  570. #ifndef PRINTF_UINT64_DEC_WIDTH
  571. # define PRINTF_UINT64_DEC_WIDTH "20"
  572. #endif
  573. #ifndef PRINTF_UINT32_DEC_WIDTH
  574. # define PRINTF_UINT32_DEC_WIDTH "10"
  575. #endif
  576. #ifndef PRINTF_UINT16_DEC_WIDTH
  577. # define PRINTF_UINT16_DEC_WIDTH "5"
  578. #endif
  579. #ifndef PRINTF_UINT8_DEC_WIDTH
  580. # define PRINTF_UINT8_DEC_WIDTH "3"
  581. #endif
  582. /*
  583. * Ok, lets not worry about 128 bit integers for now. Moore's law says
  584. * we don't need to worry about that until about 2040 at which point
  585. * we'll have bigger things to worry about.
  586. */
  587. #ifdef stdint_int64_defined
  588. typedef int64_t intmax_t;
  589. typedef uint64_t uintmax_t;
  590. # define INTMAX_MAX INT64_MAX
  591. # define INTMAX_MIN INT64_MIN
  592. # define UINTMAX_MAX UINT64_MAX
  593. # define UINTMAX_C(v) UINT64_C(v)
  594. # define INTMAX_C(v) INT64_C(v)
  595. # ifndef PRINTF_INTMAX_MODIFIER
  596. # define PRINTF_INTMAX_MODIFIER PRINTF_INT64_MODIFIER
  597. # endif
  598. # ifndef PRINTF_INTMAX_HEX_WIDTH
  599. # define PRINTF_INTMAX_HEX_WIDTH PRINTF_INT64_HEX_WIDTH
  600. # endif
  601. # ifndef PRINTF_INTMAX_DEC_WIDTH
  602. # define PRINTF_INTMAX_DEC_WIDTH PRINTF_INT64_DEC_WIDTH
  603. # endif
  604. #else
  605. typedef int32_t intmax_t;
  606. typedef uint32_t uintmax_t;
  607. # define INTMAX_MAX INT32_MAX
  608. # define UINTMAX_MAX UINT32_MAX
  609. # define UINTMAX_C(v) UINT32_C(v)
  610. # define INTMAX_C(v) INT32_C(v)
  611. # ifndef PRINTF_INTMAX_MODIFIER
  612. # define PRINTF_INTMAX_MODIFIER PRINTF_INT32_MODIFIER
  613. # endif
  614. # ifndef PRINTF_INTMAX_HEX_WIDTH
  615. # define PRINTF_INTMAX_HEX_WIDTH PRINTF_INT32_HEX_WIDTH
  616. # endif
  617. # ifndef PRINTF_INTMAX_DEC_WIDTH
  618. # define PRINTF_INTMAX_DEC_WIDTH PRINTF_INT32_DEC_WIDTH
  619. # endif
  620. #endif
  621. /*
  622. * Because this file currently only supports platforms which have
  623. * precise powers of 2 as bit sizes for the default integers, the
  624. * least definitions are all trivial. Its possible that a future
  625. * version of this file could have different definitions.
  626. */
  627. #ifndef stdint_least_defined
  628. typedef int8_t int_least8_t;
  629. typedef uint8_t uint_least8_t;
  630. typedef int16_t int_least16_t;
  631. typedef uint16_t uint_least16_t;
  632. typedef int32_t int_least32_t;
  633. typedef uint32_t uint_least32_t;
  634. # define PRINTF_LEAST32_MODIFIER PRINTF_INT32_MODIFIER
  635. # define PRINTF_LEAST16_MODIFIER PRINTF_INT16_MODIFIER
  636. # define UINT_LEAST8_MAX UINT8_MAX
  637. # define INT_LEAST8_MAX INT8_MAX
  638. # define UINT_LEAST16_MAX UINT16_MAX
  639. # define INT_LEAST16_MAX INT16_MAX
  640. # define UINT_LEAST32_MAX UINT32_MAX
  641. # define INT_LEAST32_MAX INT32_MAX
  642. # define INT_LEAST8_MIN INT8_MIN
  643. # define INT_LEAST16_MIN INT16_MIN
  644. # define INT_LEAST32_MIN INT32_MIN
  645. # ifdef stdint_int64_defined
  646. typedef int64_t int_least64_t;
  647. typedef uint64_t uint_least64_t;
  648. # define PRINTF_LEAST64_MODIFIER PRINTF_INT64_MODIFIER
  649. # define UINT_LEAST64_MAX UINT64_MAX
  650. # define INT_LEAST64_MAX INT64_MAX
  651. # define INT_LEAST64_MIN INT64_MIN
  652. # endif
  653. #endif
  654. #undef stdint_least_defined
  655. /*
  656. * The ANSI C committee has defined *int*_fast*_t types as well. This,
  657. * of course, defies rationality -- you can't know what will be fast
  658. * just from the type itself. Even for a given architecture, compatible
  659. * implementations might have different performance characteristics.
  660. * Developers are warned to stay away from these types when using this
  661. * or any other stdint.h.
  662. */
  663. typedef int_least8_t int_fast8_t;
  664. typedef uint_least8_t uint_fast8_t;
  665. typedef int_least16_t int_fast16_t;
  666. typedef uint_least16_t uint_fast16_t;
  667. typedef int_least32_t int_fast32_t;
  668. typedef uint_least32_t uint_fast32_t;
  669. #define UINT_FAST8_MAX UINT_LEAST8_MAX
  670. #define INT_FAST8_MAX INT_LEAST8_MAX
  671. #define UINT_FAST16_MAX UINT_LEAST16_MAX
  672. #define INT_FAST16_MAX INT_LEAST16_MAX
  673. #define UINT_FAST32_MAX UINT_LEAST32_MAX
  674. #define INT_FAST32_MAX INT_LEAST32_MAX
  675. #define INT_FAST8_MIN INT_LEAST8_MIN
  676. #define INT_FAST16_MIN INT_LEAST16_MIN
  677. #define INT_FAST32_MIN INT_LEAST32_MIN
  678. #ifdef stdint_int64_defined
  679. typedef int_least64_t int_fast64_t;
  680. typedef uint_least64_t uint_fast64_t;
  681. # define UINT_FAST64_MAX UINT_LEAST64_MAX
  682. # define INT_FAST64_MAX INT_LEAST64_MAX
  683. # define INT_FAST64_MIN INT_LEAST64_MIN
  684. #endif
  685. #undef stdint_int64_defined
  686. /*
  687. * Whatever piecemeal, per compiler thing we can do about the wchar_t
  688. * type limits.
  689. */
  690. #if defined(__WATCOMC__) || defined(_MSC_VER) || defined (__GNUC__) && !defined(vxWorks)
  691. # include <wchar.h>
  692. # ifndef WCHAR_MIN
  693. # define WCHAR_MIN 0
  694. # endif
  695. # ifndef WCHAR_MAX
  696. # define WCHAR_MAX ((wchar_t)-1)
  697. # endif
  698. #endif
  699. /*
  700. * Whatever piecemeal, per compiler/platform thing we can do about the
  701. * (u)intptr_t types and limits.
  702. */
  703. #if (defined (_MSC_VER) && defined (_UINTPTR_T_DEFINED)) || defined (_UINTPTR_T)
  704. # define STDINT_H_UINTPTR_T_DEFINED
  705. #endif
  706. #ifndef STDINT_H_UINTPTR_T_DEFINED
  707. # if defined (__alpha__) || defined (__ia64__) || defined (__x86_64__) || defined (_WIN64) || defined (__ppc64__)
  708. # define stdint_intptr_bits 64
  709. # elif defined (__WATCOMC__) || defined (__TURBOC__)
  710. # if defined(__TINY__) || defined(__SMALL__) || defined(__MEDIUM__)
  711. # define stdint_intptr_bits 16
  712. # else
  713. # define stdint_intptr_bits 32
  714. # endif
  715. # elif defined (__i386__) || defined (_WIN32) || defined (WIN32) || defined (__ppc64__)
  716. # define stdint_intptr_bits 32
  717. # elif defined (__INTEL_COMPILER)
  718. /* TODO -- what did Intel do about x86-64? */
  719. # else
  720. /* #error "This platform might not be supported yet" */
  721. # endif
  722. # ifdef stdint_intptr_bits
  723. # define stdint_intptr_glue3_i(a,b,c) a##b##c
  724. # define stdint_intptr_glue3(a,b,c) stdint_intptr_glue3_i(a,b,c)
  725. # ifndef PRINTF_INTPTR_MODIFIER
  726. # define PRINTF_INTPTR_MODIFIER stdint_intptr_glue3(PRINTF_INT,stdint_intptr_bits,_MODIFIER)
  727. # endif
  728. # ifndef PTRDIFF_MAX
  729. # define PTRDIFF_MAX stdint_intptr_glue3(INT,stdint_intptr_bits,_MAX)
  730. # endif
  731. # ifndef PTRDIFF_MIN
  732. # define PTRDIFF_MIN stdint_intptr_glue3(INT,stdint_intptr_bits,_MIN)
  733. # endif
  734. # ifndef UINTPTR_MAX
  735. # define UINTPTR_MAX stdint_intptr_glue3(UINT,stdint_intptr_bits,_MAX)
  736. # endif
  737. # ifndef INTPTR_MAX
  738. # define INTPTR_MAX stdint_intptr_glue3(INT,stdint_intptr_bits,_MAX)
  739. # endif
  740. # ifndef INTPTR_MIN
  741. # define INTPTR_MIN stdint_intptr_glue3(INT,stdint_intptr_bits,_MIN)
  742. # endif
  743. # ifndef INTPTR_C
  744. # define INTPTR_C(x) stdint_intptr_glue3(INT,stdint_intptr_bits,_C)(x)
  745. # endif
  746. # ifndef UINTPTR_C
  747. # define UINTPTR_C(x) stdint_intptr_glue3(UINT,stdint_intptr_bits,_C)(x)
  748. # endif
  749. typedef stdint_intptr_glue3(uint,stdint_intptr_bits,_t) uintptr_t;
  750. typedef stdint_intptr_glue3( int,stdint_intptr_bits,_t) intptr_t;
  751. # else
  752. /* TODO -- This following is likely wrong for some platforms, and does
  753. nothing for the definition of uintptr_t. */
  754. typedef ptrdiff_t intptr_t;
  755. # endif
  756. # define STDINT_H_UINTPTR_T_DEFINED
  757. #endif
  758. /*
  759. * Assumes sig_atomic_t is signed and we have a 2s complement machine.
  760. */
  761. #ifndef SIG_ATOMIC_MAX
  762. # define SIG_ATOMIC_MAX ((((sig_atomic_t) 1) << (sizeof (sig_atomic_t)*CHAR_BIT-1)) - 1)
  763. #endif
  764. #endif
  765. #if defined (__TEST_PSTDINT_FOR_CORRECTNESS)
  766. /*
  767. * Please compile with the maximum warning settings to make sure macros are
  768. * not defined more than once.
  769. */
  770. #include <stdlib.h>
  771. #include <stdio.h>
  772. #include <string.h>
  773. #define glue3_aux(x,y,z) x ## y ## z
  774. #define glue3(x,y,z) glue3_aux(x,y,z)
  775. #define DECLU(bits) glue3(uint,bits,_t) glue3(u,bits,) = glue3(UINT,bits,_C) (0);
  776. #define DECLI(bits) glue3(int,bits,_t) glue3(i,bits,) = glue3(INT,bits,_C) (0);
  777. #define DECL(us,bits) glue3(DECL,us,) (bits)
  778. #define TESTUMAX(bits) glue3(u,bits,) = ~glue3(u,bits,); if (glue3(UINT,bits,_MAX) != glue3(u,bits,)) printf ("Something wrong with UINT%d_MAX\n", bits)
  779. #define REPORTERROR(msg) { err_n++; if (err_first <= 0) err_first = __LINE__; printf msg; }
  780. #define X_SIZE_MAX ((size_t)-1)
  781. int main () {
  782. int err_n = 0;
  783. int err_first = 0;
  784. DECL(I,8)
  785. DECL(U,8)
  786. DECL(I,16)
  787. DECL(U,16)
  788. DECL(I,32)
  789. DECL(U,32)
  790. #ifdef INT64_MAX
  791. DECL(I,64)
  792. DECL(U,64)
  793. #endif
  794. intmax_t imax = INTMAX_C(0);
  795. uintmax_t umax = UINTMAX_C(0);
  796. char str0[256], str1[256];
  797. sprintf (str0, "%" PRINTF_INT32_MODIFIER "d", INT32_C(2147483647));
  798. if (0 != strcmp (str0, "2147483647")) REPORTERROR (("Something wrong with PRINTF_INT32_MODIFIER : %s\n", str0));
  799. if (atoi(PRINTF_INT32_DEC_WIDTH) != (int) strlen(str0)) REPORTERROR (("Something wrong with PRINTF_INT32_DEC_WIDTH : %s\n", PRINTF_INT32_DEC_WIDTH));
  800. sprintf (str0, "%" PRINTF_INT32_MODIFIER "u", UINT32_C(4294967295));
  801. if (0 != strcmp (str0, "4294967295")) REPORTERROR (("Something wrong with PRINTF_INT32_MODIFIER : %s\n", str0));
  802. if (atoi(PRINTF_UINT32_DEC_WIDTH) != (int) strlen(str0)) REPORTERROR (("Something wrong with PRINTF_UINT32_DEC_WIDTH : %s\n", PRINTF_UINT32_DEC_WIDTH));
  803. #ifdef INT64_MAX
  804. sprintf (str1, "%" PRINTF_INT64_MODIFIER "d", INT64_C(9223372036854775807));
  805. if (0 != strcmp (str1, "9223372036854775807")) REPORTERROR (("Something wrong with PRINTF_INT32_MODIFIER : %s\n", str1));
  806. if (atoi(PRINTF_INT64_DEC_WIDTH) != (int) strlen(str1)) REPORTERROR (("Something wrong with PRINTF_INT64_DEC_WIDTH : %s, %d\n", PRINTF_INT64_DEC_WIDTH, (int) strlen(str1)));
  807. sprintf (str1, "%" PRINTF_INT64_MODIFIER "u", UINT64_C(18446744073709550591));
  808. if (0 != strcmp (str1, "18446744073709550591")) REPORTERROR (("Something wrong with PRINTF_INT32_MODIFIER : %s\n", str1));
  809. if (atoi(PRINTF_UINT64_DEC_WIDTH) != (int) strlen(str1)) REPORTERROR (("Something wrong with PRINTF_UINT64_DEC_WIDTH : %s, %d\n", PRINTF_UINT64_DEC_WIDTH, (int) strlen(str1)));
  810. #endif
  811. sprintf (str0, "%d %x\n", 0, ~0);
  812. sprintf (str1, "%d %x\n", i8, ~0);
  813. if (0 != strcmp (str0, str1)) REPORTERROR (("Something wrong with i8 : %s\n", str1));
  814. sprintf (str1, "%u %x\n", u8, ~0);
  815. if (0 != strcmp (str0, str1)) REPORTERROR (("Something wrong with u8 : %s\n", str1));
  816. sprintf (str1, "%d %x\n", i16, ~0);
  817. if (0 != strcmp (str0, str1)) REPORTERROR (("Something wrong with i16 : %s\n", str1));
  818. sprintf (str1, "%u %x\n", u16, ~0);
  819. if (0 != strcmp (str0, str1)) REPORTERROR (("Something wrong with u16 : %s\n", str1));
  820. sprintf (str1, "%" PRINTF_INT32_MODIFIER "d %x\n", i32, ~0);
  821. if (0 != strcmp (str0, str1)) REPORTERROR (("Something wrong with i32 : %s\n", str1));
  822. sprintf (str1, "%" PRINTF_INT32_MODIFIER "u %x\n", u32, ~0);
  823. if (0 != strcmp (str0, str1)) REPORTERROR (("Something wrong with u32 : %s\n", str1));
  824. #ifdef INT64_MAX
  825. sprintf (str1, "%" PRINTF_INT64_MODIFIER "d %x\n", i64, ~0);
  826. if (0 != strcmp (str0, str1)) REPORTERROR (("Something wrong with i64 : %s\n", str1));
  827. #endif
  828. sprintf (str1, "%" PRINTF_INTMAX_MODIFIER "d %x\n", imax, ~0);
  829. if (0 != strcmp (str0, str1)) REPORTERROR (("Something wrong with imax : %s\n", str1));
  830. sprintf (str1, "%" PRINTF_INTMAX_MODIFIER "u %x\n", umax, ~0);
  831. if (0 != strcmp (str0, str1)) REPORTERROR (("Something wrong with umax : %s\n", str1));
  832. TESTUMAX(8);
  833. TESTUMAX(16);
  834. TESTUMAX(32);
  835. #ifdef INT64_MAX
  836. TESTUMAX(64);
  837. #endif
  838. #define STR(v) #v
  839. #define Q(v) printf ("sizeof " STR(v) " = %u\n", (unsigned) sizeof (v));
  840. if (err_n) {
  841. printf ("pstdint.h is not correct. Please use sizes below to correct it:\n");
  842. }
  843. Q(int)
  844. Q(unsigned)
  845. Q(long int)
  846. Q(short int)
  847. Q(int8_t)
  848. Q(int16_t)
  849. Q(int32_t)
  850. #ifdef INT64_MAX
  851. Q(int64_t)
  852. #endif
  853. #if UINT_MAX < X_SIZE_MAX
  854. printf ("UINT_MAX < X_SIZE_MAX\n");
  855. #else
  856. printf ("UINT_MAX >= X_SIZE_MAX\n");
  857. #endif
  858. printf ("%" PRINTF_INT64_MODIFIER "u vs %" PRINTF_INT64_MODIFIER "u\n", UINT_MAX, X_SIZE_MAX);
  859. return EXIT_SUCCESS;
  860. }
  861. #endif