pstdint.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. /* A portable stdint.h
  2. ****************************************************************************
  3. * BSD License:
  4. ****************************************************************************
  5. *
  6. * Copyright (c) 2005-2007 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.11
  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
  40. * critically important for making portable several classes of
  41. * applications including cryptography, hashing, variable length
  42. * integer libraries and so on. But for most developers its likely
  43. * useful just for programming sanity.
  44. *
  45. * The problem is that most compiler vendors have decided not to
  46. * implement the C99 standard, and the next C++ language standard
  47. * (which has a lot more mindshare these days) will be a long time in
  48. * coming and its unknown whether or not it will include stdint.h or
  49. * how much adoption it will have. Either way, it will be a long time
  50. * before all compilers come with a stdint.h and it also does nothing
  51. * for the extremely large number of compilers available today which
  52. * do not include this file, or anything comparable to it.
  53. *
  54. * So that's what this file is all about. Its an attempt to build a
  55. * single universal include file that works on as many platforms as
  56. * possible to deliver what stdint.h is supposed to. A few things
  57. * that should be noted about this file:
  58. *
  59. * 1) It is not guaranteed to be portable and/or present an identical
  60. * interface on all platforms. The extreme variability of the
  61. * ANSI C standard makes this an impossibility right from the
  62. * very get go. Its really only meant to be useful for the vast
  63. * majority of platforms that possess the capability of
  64. * implementing usefully and precisely defined, standard sized
  65. * integer scalars. Systems which are not intrinsically 2s
  66. * complement may produce invalid constants.
  67. *
  68. * 2) There is an unavoidable use of non-reserved symbols.
  69. *
  70. * 3) Other standard include files are invoked.
  71. *
  72. * 4) This file may come in conflict with future platforms that do
  73. * include stdint.h. The hope is that one or the other can be
  74. * used with no real difference.
  75. *
  76. * 5) In the current verison, if your platform can't represent
  77. * int32_t, int16_t and int8_t, it just dumps out with a compiler
  78. * error.
  79. *
  80. * 6) 64 bit integers may or may not be defined. Test for their
  81. * presence with the test: #ifdef INT64_MAX or #ifdef UINT64_MAX.
  82. * Note that this is different from the C99 specification which
  83. * requires the existence of 64 bit support in the compiler. If
  84. * this is not defined for your platform, yet it is capable of
  85. * dealing with 64 bits then it is because this file has not yet
  86. * been extended to cover all of your system's capabilities.
  87. *
  88. * 7) (u)intptr_t may or may not be defined. Test for its presence
  89. * with the test: #ifdef PTRDIFF_MAX. If this is not defined
  90. * for your platform, then it is because this file has not yet
  91. * been extended to cover all of your system's capabilities, not
  92. * because its optional.
  93. *
  94. * 8) The following might not been defined even if your platform is
  95. * capable of defining it:
  96. *
  97. * WCHAR_MIN
  98. * WCHAR_MAX
  99. * (u)int64_t
  100. * PTRDIFF_MIN
  101. * PTRDIFF_MAX
  102. * (u)intptr_t
  103. *
  104. * 9) The following have not been defined:
  105. *
  106. * WINT_MIN
  107. * WINT_MAX
  108. *
  109. * 10) The criteria for defining (u)int_least(*)_t isn't clear,
  110. * except for systems which don't have a type that precisely
  111. * defined 8, 16, or 32 bit types (which this include file does
  112. * not support anyways). Default definitions have been given.
  113. *
  114. * 11) The criteria for defining (u)int_fast(*)_t isn't something I
  115. * would trust to any particular compiler vendor or the ANSI C
  116. * committee. It is well known that "compatible systems" are
  117. * commonly created that have very different performance
  118. * characteristics from the systems they are compatible with,
  119. * especially those whose vendors make both the compiler and the
  120. * system. Default definitions have been given, but its strongly
  121. * recommended that users never use these definitions for any
  122. * reason (they do *NOT* deliver any serious guarantee of
  123. * improved performance -- not in this file, nor any vendor's
  124. * stdint.h).
  125. *
  126. * 12) The following macros:
  127. *
  128. * PRINTF_INTMAX_MODIFIER
  129. * PRINTF_INT64_MODIFIER
  130. * PRINTF_INT32_MODIFIER
  131. * PRINTF_INT16_MODIFIER
  132. * PRINTF_LEAST64_MODIFIER
  133. * PRINTF_LEAST32_MODIFIER
  134. * PRINTF_LEAST16_MODIFIER
  135. * PRINTF_INTPTR_MODIFIER
  136. *
  137. * are strings which have been defined as the modifiers required
  138. * for the "d", "u" and "x" printf formats to correctly output
  139. * (u)intmax_t, (u)int64_t, (u)int32_t, (u)int16_t, (u)least64_t,
  140. * (u)least32_t, (u)least16_t and (u)intptr_t types respectively.
  141. * PRINTF_INTPTR_MODIFIER is not defined for some systems which
  142. * provide their own stdint.h. PRINTF_INT64_MODIFIER is not
  143. * defined if INT64_MAX is not defined. These are an extension
  144. * beyond what C99 specifies must be in stdint.h.
  145. *
  146. * In addition, the following macros are defined:
  147. *
  148. * PRINTF_INTMAX_HEX_WIDTH
  149. * PRINTF_INT64_HEX_WIDTH
  150. * PRINTF_INT32_HEX_WIDTH
  151. * PRINTF_INT16_HEX_WIDTH
  152. * PRINTF_INT8_HEX_WIDTH
  153. * PRINTF_INTMAX_DEC_WIDTH
  154. * PRINTF_INT64_DEC_WIDTH
  155. * PRINTF_INT32_DEC_WIDTH
  156. * PRINTF_INT16_DEC_WIDTH
  157. * PRINTF_INT8_DEC_WIDTH
  158. *
  159. * Which specifies the maximum number of characters required to
  160. * print the number of that type in either hexadecimal or decimal.
  161. * These are an extension beyond what C99 specifies must be in
  162. * stdint.h.
  163. *
  164. * Compilers tested (all with 0 warnings at their highest respective
  165. * settings): Borland Turbo C 2.0, WATCOM C/C++ 11.0 (16 bits and 32
  166. * bits), Microsoft Visual C++ 6.0 (32 bit), Microsoft Visual Studio
  167. * .net (VC7), Intel C++ 4.0, GNU gcc v3.3.3
  168. *
  169. * This file should be considered a work in progress. Suggestions for
  170. * improvements, especially those which increase coverage are strongly
  171. * encouraged.
  172. *
  173. * Acknowledgements
  174. *
  175. * The following people have made significant contributions to the
  176. * development and testing of this file:
  177. *
  178. * Chris Howie
  179. * John Steele Scott
  180. * Dave Thorup
  181. *
  182. */
  183. #include <stddef.h>
  184. #include <limits.h>
  185. #include <signal.h>
  186. /*
  187. * For gcc with _STDINT_H, fill in the PRINTF_INT*_MODIFIER macros, and
  188. * do nothing else. On the Mac OS X version of gcc this is _STDINT_H_.
  189. */
  190. #if ((defined(__STDC__) && __STDC__ && __STDC_VERSION__ >= 199901L) || (defined (__WATCOMC__) && (defined (_STDINT_H_INCLUDED) || __WATCOMC__ >= 1250)) || (defined(__GNUC__) && (defined(_STDINT_H) || defined(_STDINT_H_)) )) && !defined (_PSTDINT_H_INCLUDED)
  191. #include <stdint.h>
  192. #define _PSTDINT_H_INCLUDED
  193. # ifndef PRINTF_INT64_MODIFIER
  194. # define PRINTF_INT64_MODIFIER "ll"
  195. # endif
  196. # ifndef PRINTF_INT32_MODIFIER
  197. # define PRINTF_INT32_MODIFIER "l"
  198. # endif
  199. # ifndef PRINTF_INT16_MODIFIER
  200. # define PRINTF_INT16_MODIFIER "h"
  201. # endif
  202. # ifndef PRINTF_INTMAX_MODIFIER
  203. # define PRINTF_INTMAX_MODIFIER PRINTF_INT64_MODIFIER
  204. # endif
  205. # ifndef PRINTF_INT64_HEX_WIDTH
  206. # define PRINTF_INT64_HEX_WIDTH "16"
  207. # endif
  208. # ifndef PRINTF_INT32_HEX_WIDTH
  209. # define PRINTF_INT32_HEX_WIDTH "8"
  210. # endif
  211. # ifndef PRINTF_INT16_HEX_WIDTH
  212. # define PRINTF_INT16_HEX_WIDTH "4"
  213. # endif
  214. # ifndef PRINTF_INT8_HEX_WIDTH
  215. # define PRINTF_INT8_HEX_WIDTH "2"
  216. # endif
  217. # ifndef PRINTF_INT64_DEC_WIDTH
  218. # define PRINTF_INT64_DEC_WIDTH "20"
  219. # endif
  220. # ifndef PRINTF_INT32_DEC_WIDTH
  221. # define PRINTF_INT32_DEC_WIDTH "10"
  222. # endif
  223. # ifndef PRINTF_INT16_DEC_WIDTH
  224. # define PRINTF_INT16_DEC_WIDTH "5"
  225. # endif
  226. # ifndef PRINTF_INT8_DEC_WIDTH
  227. # define PRINTF_INT8_DEC_WIDTH "3"
  228. # endif
  229. # ifndef PRINTF_INTMAX_HEX_WIDTH
  230. # define PRINTF_INTMAX_HEX_WIDTH PRINTF_INT64_HEX_WIDTH
  231. # endif
  232. # ifndef PRINTF_INTMAX_DEC_WIDTH
  233. # define PRINTF_INTMAX_DEC_WIDTH PRINTF_INT64_DEC_WIDTH
  234. # endif
  235. /*
  236. * Something really weird is going on with Open Watcom. Just pull some of
  237. * these duplicated definitions from Open Watcom's stdint.h file for now.
  238. */
  239. # if defined (__WATCOMC__) && __WATCOMC__ >= 1250
  240. # if !defined (INT64_C)
  241. # define INT64_C(x) (x + (INT64_MAX - INT64_MAX))
  242. # endif
  243. # if !defined (UINT64_C)
  244. # define UINT64_C(x) (x + (UINT64_MAX - UINT64_MAX))
  245. # endif
  246. # if !defined (INT32_C)
  247. # define INT32_C(x) (x + (INT32_MAX - INT32_MAX))
  248. # endif
  249. # if !defined (UINT32_C)
  250. # define UINT32_C(x) (x + (UINT32_MAX - UINT32_MAX))
  251. # endif
  252. # if !defined (INT16_C)
  253. # define INT16_C(x) (x)
  254. # endif
  255. # if !defined (UINT16_C)
  256. # define UINT16_C(x) (x)
  257. # endif
  258. # if !defined (INT8_C)
  259. # define INT8_C(x) (x)
  260. # endif
  261. # if !defined (UINT8_C)
  262. # define UINT8_C(x) (x)
  263. # endif
  264. # if !defined (UINT64_MAX)
  265. # define UINT64_MAX 18446744073709551615ULL
  266. # endif
  267. # if !defined (INT64_MAX)
  268. # define INT64_MAX 9223372036854775807LL
  269. # endif
  270. # if !defined (UINT32_MAX)
  271. # define UINT32_MAX 4294967295UL
  272. # endif
  273. # if !defined (INT32_MAX)
  274. # define INT32_MAX 2147483647L
  275. # endif
  276. # if !defined (INTMAX_MAX)
  277. # define INTMAX_MAX INT64_MAX
  278. # endif
  279. # if !defined (INTMAX_MIN)
  280. # define INTMAX_MIN INT64_MIN
  281. # endif
  282. # endif
  283. #endif
  284. #ifndef _PSTDINT_H_INCLUDED
  285. #define _PSTDINT_H_INCLUDED
  286. #ifndef SIZE_MAX
  287. # define SIZE_MAX (~(size_t)0)
  288. #endif
  289. /*
  290. * Deduce the type assignments from limits.h under the assumption that
  291. * integer sizes in bits are powers of 2, and follow the ANSI
  292. * definitions.
  293. */
  294. #ifndef UINT8_MAX
  295. # define UINT8_MAX 0xff
  296. #endif
  297. #ifndef uint8_t
  298. # if (UCHAR_MAX == UINT8_MAX) || defined (S_SPLINT_S)
  299. typedef unsigned char uint8_t;
  300. # define UINT8_C(v) ((uint8_t) v)
  301. # else
  302. # error "Platform not supported"
  303. # endif
  304. #endif
  305. #ifndef INT8_MAX
  306. # define INT8_MAX 0x7f
  307. #endif
  308. #ifndef INT8_MIN
  309. # define INT8_MIN INT8_C(0x80)
  310. #endif
  311. #ifndef int8_t
  312. # if (SCHAR_MAX == INT8_MAX) || defined (S_SPLINT_S)
  313. typedef signed char int8_t;
  314. # define INT8_C(v) ((int8_t) v)
  315. # else
  316. # error "Platform not supported"
  317. # endif
  318. #endif
  319. #ifndef UINT16_MAX
  320. # define UINT16_MAX 0xffff
  321. #endif
  322. #ifndef uint16_t
  323. #if (UINT_MAX == UINT16_MAX) || defined (S_SPLINT_S)
  324. typedef unsigned int uint16_t;
  325. # ifndef PRINTF_INT16_MODIFIER
  326. # define PRINTF_INT16_MODIFIER ""
  327. # endif
  328. # define UINT16_C(v) ((uint16_t) (v))
  329. #elif (USHRT_MAX == UINT16_MAX)
  330. typedef unsigned short uint16_t;
  331. # define UINT16_C(v) ((uint16_t) (v))
  332. # ifndef PRINTF_INT16_MODIFIER
  333. # define PRINTF_INT16_MODIFIER "h"
  334. # endif
  335. #else
  336. #error "Platform not supported"
  337. #endif
  338. #endif
  339. #ifndef INT16_MAX
  340. # define INT16_MAX 0x7fff
  341. #endif
  342. #ifndef INT16_MIN
  343. # define INT16_MIN INT16_C(0x8000)
  344. #endif
  345. #ifndef int16_t
  346. #if (INT_MAX == INT16_MAX) || defined (S_SPLINT_S)
  347. typedef signed int int16_t;
  348. # define INT16_C(v) ((int16_t) (v))
  349. # ifndef PRINTF_INT16_MODIFIER
  350. # define PRINTF_INT16_MODIFIER ""
  351. # endif
  352. #elif (SHRT_MAX == INT16_MAX)
  353. typedef signed short int16_t;
  354. # define INT16_C(v) ((int16_t) (v))
  355. # ifndef PRINTF_INT16_MODIFIER
  356. # define PRINTF_INT16_MODIFIER "h"
  357. # endif
  358. #else
  359. #error "Platform not supported"
  360. #endif
  361. #endif
  362. #ifndef UINT32_MAX
  363. # define UINT32_MAX (0xffffffffUL)
  364. #endif
  365. #ifndef uint32_t
  366. #if (ULONG_MAX == UINT32_MAX) || defined (S_SPLINT_S)
  367. typedef unsigned long uint32_t;
  368. # define UINT32_C(v) v ## UL
  369. # ifndef PRINTF_INT32_MODIFIER
  370. # define PRINTF_INT32_MODIFIER "l"
  371. # endif
  372. #elif (UINT_MAX == UINT32_MAX)
  373. typedef unsigned int uint32_t;
  374. # ifndef PRINTF_INT32_MODIFIER
  375. # define PRINTF_INT32_MODIFIER ""
  376. # endif
  377. # define UINT32_C(v) v ## U
  378. #elif (USHRT_MAX == UINT32_MAX)
  379. typedef unsigned short uint32_t;
  380. # define UINT32_C(v) ((unsigned short) (v))
  381. # ifndef PRINTF_INT32_MODIFIER
  382. # define PRINTF_INT32_MODIFIER ""
  383. # endif
  384. #else
  385. #error "Platform not supported"
  386. #endif
  387. #endif
  388. #ifndef INT32_MAX
  389. # define INT32_MAX (0x7fffffffL)
  390. #endif
  391. #ifndef INT32_MIN
  392. # define INT32_MIN INT32_C(0x80000000)
  393. #endif
  394. #ifndef int32_t
  395. #if (LONG_MAX == INT32_MAX) || defined (S_SPLINT_S)
  396. typedef signed long int32_t;
  397. # define INT32_C(v) v ## L
  398. # ifndef PRINTF_INT32_MODIFIER
  399. # define PRINTF_INT32_MODIFIER "l"
  400. # endif
  401. #elif (INT_MAX == INT32_MAX)
  402. typedef signed int int32_t;
  403. # define INT32_C(v) v
  404. # ifndef PRINTF_INT32_MODIFIER
  405. # define PRINTF_INT32_MODIFIER ""
  406. # endif
  407. #elif (SHRT_MAX == INT32_MAX)
  408. typedef signed short int32_t;
  409. # define INT32_C(v) ((short) (v))
  410. # ifndef PRINTF_INT32_MODIFIER
  411. # define PRINTF_INT32_MODIFIER ""
  412. # endif
  413. #else
  414. #error "Platform not supported"
  415. #endif
  416. #endif
  417. /*
  418. * The macro stdint_int64_defined is temporarily used to record
  419. * whether or not 64 integer support is available. It must be
  420. * defined for any 64 integer extensions for new platforms that are
  421. * added.
  422. */
  423. #undef stdint_int64_defined
  424. #if (defined(__STDC__) && defined(__STDC_VERSION__)) || defined (S_SPLINT_S)
  425. # if (__STDC__ && __STDC_VERSION >= 199901L) || defined (S_SPLINT_S)
  426. # define stdint_int64_defined
  427. typedef long long int64_t;
  428. typedef unsigned long long uint64_t;
  429. # define UINT64_C(v) v ## ULL
  430. # define INT64_C(v) v ## LL
  431. # ifndef PRINTF_INT64_MODIFIER
  432. # define PRINTF_INT64_MODIFIER "ll"
  433. # endif
  434. # endif
  435. #endif
  436. #if !defined (stdint_int64_defined)
  437. # if defined(__GNUC__)
  438. # define stdint_int64_defined
  439. __extension__ typedef long long int64_t;
  440. __extension__ typedef unsigned long long uint64_t;
  441. # define UINT64_C(v) v ## ULL
  442. # define INT64_C(v) v ## LL
  443. # ifndef PRINTF_INT64_MODIFIER
  444. # define PRINTF_INT64_MODIFIER "ll"
  445. # endif
  446. # elif defined(__MWERKS__) || defined (__SUNPRO_C) || defined (__SUNPRO_CC) || defined (__APPLE_CC__) || defined (_LONG_LONG) || defined (_CRAYC) || defined (S_SPLINT_S)
  447. # define stdint_int64_defined
  448. typedef long long int64_t;
  449. typedef unsigned long long uint64_t;
  450. # define UINT64_C(v) v ## ULL
  451. # define INT64_C(v) v ## LL
  452. # ifndef PRINTF_INT64_MODIFIER
  453. # define PRINTF_INT64_MODIFIER "ll"
  454. # endif
  455. # elif (defined(__WATCOMC__) && defined(__WATCOM_INT64__)) || (defined(_MSC_VER) && _INTEGRAL_MAX_BITS >= 64) || (defined (__BORLANDC__) && __BORLANDC__ > 0x460) || defined (__alpha) || defined (__DECC)
  456. # define stdint_int64_defined
  457. typedef __int64 int64_t;
  458. typedef unsigned __int64 uint64_t;
  459. # define UINT64_C(v) v ## UI64
  460. # define INT64_C(v) v ## I64
  461. # ifndef PRINTF_INT64_MODIFIER
  462. # define PRINTF_INT64_MODIFIER "I64"
  463. # endif
  464. # endif
  465. #endif
  466. #if !defined (LONG_LONG_MAX) && defined (INT64_C)
  467. # define LONG_LONG_MAX INT64_C (9223372036854775807)
  468. #endif
  469. #ifndef ULONG_LONG_MAX
  470. # define ULONG_LONG_MAX UINT64_C (18446744073709551615)
  471. #endif
  472. #if !defined (INT64_MAX) && defined (INT64_C)
  473. # define INT64_MAX INT64_C (9223372036854775807)
  474. #endif
  475. #if !defined (INT64_MIN) && defined (INT64_C)
  476. # define INT64_MIN INT64_C (-9223372036854775808)
  477. #endif
  478. #if !defined (UINT64_MAX) && defined (INT64_C)
  479. # define UINT64_MAX UINT64_C (18446744073709551615)
  480. #endif
  481. /*
  482. * Width of hexadecimal for number field.
  483. */
  484. #ifndef PRINTF_INT64_HEX_WIDTH
  485. # define PRINTF_INT64_HEX_WIDTH "16"
  486. #endif
  487. #ifndef PRINTF_INT32_HEX_WIDTH
  488. # define PRINTF_INT32_HEX_WIDTH "8"
  489. #endif
  490. #ifndef PRINTF_INT16_HEX_WIDTH
  491. # define PRINTF_INT16_HEX_WIDTH "4"
  492. #endif
  493. #ifndef PRINTF_INT8_HEX_WIDTH
  494. # define PRINTF_INT8_HEX_WIDTH "2"
  495. #endif
  496. #ifndef PRINTF_INT64_DEC_WIDTH
  497. # define PRINTF_INT64_DEC_WIDTH "20"
  498. #endif
  499. #ifndef PRINTF_INT32_DEC_WIDTH
  500. # define PRINTF_INT32_DEC_WIDTH "10"
  501. #endif
  502. #ifndef PRINTF_INT16_DEC_WIDTH
  503. # define PRINTF_INT16_DEC_WIDTH "5"
  504. #endif
  505. #ifndef PRINTF_INT8_DEC_WIDTH
  506. # define PRINTF_INT8_DEC_WIDTH "3"
  507. #endif
  508. /*
  509. * Ok, lets not worry about 128 bit integers for now. Moore's law says
  510. * we don't need to worry about that until about 2040 at which point
  511. * we'll have bigger things to worry about.
  512. */
  513. #ifdef stdint_int64_defined
  514. typedef int64_t intmax_t;
  515. typedef uint64_t uintmax_t;
  516. # define INTMAX_MAX INT64_MAX
  517. # define INTMAX_MIN INT64_MIN
  518. # define UINTMAX_MAX UINT64_MAX
  519. # define UINTMAX_C(v) UINT64_C(v)
  520. # define INTMAX_C(v) INT64_C(v)
  521. # ifndef PRINTF_INTMAX_MODIFIER
  522. # define PRINTF_INTMAX_MODIFIER PRINTF_INT64_MODIFIER
  523. # endif
  524. # ifndef PRINTF_INTMAX_HEX_WIDTH
  525. # define PRINTF_INTMAX_HEX_WIDTH PRINTF_INT64_HEX_WIDTH
  526. # endif
  527. # ifndef PRINTF_INTMAX_DEC_WIDTH
  528. # define PRINTF_INTMAX_DEC_WIDTH PRINTF_INT64_DEC_WIDTH
  529. # endif
  530. #else
  531. typedef int32_t intmax_t;
  532. typedef uint32_t uintmax_t;
  533. # define INTMAX_MAX INT32_MAX
  534. # define UINTMAX_MAX UINT32_MAX
  535. # define UINTMAX_C(v) UINT32_C(v)
  536. # define INTMAX_C(v) INT32_C(v)
  537. # ifndef PRINTF_INTMAX_MODIFIER
  538. # define PRINTF_INTMAX_MODIFIER PRINTF_INT32_MODIFIER
  539. # endif
  540. # ifndef PRINTF_INTMAX_HEX_WIDTH
  541. # define PRINTF_INTMAX_HEX_WIDTH PRINTF_INT32_HEX_WIDTH
  542. # endif
  543. # ifndef PRINTF_INTMAX_DEC_WIDTH
  544. # define PRINTF_INTMAX_DEC_WIDTH PRINTF_INT32_DEC_WIDTH
  545. # endif
  546. #endif
  547. /*
  548. * Because this file currently only supports platforms which have
  549. * precise powers of 2 as bit sizes for the default integers, the
  550. * least definitions are all trivial. Its possible that a future
  551. * version of this file could have different definitions.
  552. */
  553. #ifndef stdint_least_defined
  554. typedef int8_t int_least8_t;
  555. typedef uint8_t uint_least8_t;
  556. typedef int16_t int_least16_t;
  557. typedef uint16_t uint_least16_t;
  558. typedef int32_t int_least32_t;
  559. typedef uint32_t uint_least32_t;
  560. # define PRINTF_LEAST32_MODIFIER PRINTF_INT32_MODIFIER
  561. # define PRINTF_LEAST16_MODIFIER PRINTF_INT16_MODIFIER
  562. # define UINT_LEAST8_MAX UINT8_MAX
  563. # define INT_LEAST8_MAX INT8_MAX
  564. # define UINT_LEAST16_MAX UINT16_MAX
  565. # define INT_LEAST16_MAX INT16_MAX
  566. # define UINT_LEAST32_MAX UINT32_MAX
  567. # define INT_LEAST32_MAX INT32_MAX
  568. # define INT_LEAST8_MIN INT8_MIN
  569. # define INT_LEAST16_MIN INT16_MIN
  570. # define INT_LEAST32_MIN INT32_MIN
  571. # ifdef stdint_int64_defined
  572. typedef int64_t int_least64_t;
  573. typedef uint64_t uint_least64_t;
  574. # define PRINTF_LEAST64_MODIFIER PRINTF_INT64_MODIFIER
  575. # define UINT_LEAST64_MAX UINT64_MAX
  576. # define INT_LEAST64_MAX INT64_MAX
  577. # define INT_LEAST64_MIN INT64_MIN
  578. # endif
  579. #endif
  580. #undef stdint_least_defined
  581. /*
  582. * The ANSI C committee pretending to know or specify anything about
  583. * performance is the epitome of misguided arrogance. The mandate of
  584. * this file is to *ONLY* ever support that absolute minimum
  585. * definition of the fast integer types, for compatibility purposes.
  586. * No extensions, and no attempt to suggest what may or may not be a
  587. * faster integer type will ever be made in this file. Developers are
  588. * warned to stay away from these types when using this or any other
  589. * stdint.h.
  590. */
  591. typedef int_least8_t int_fast8_t;
  592. typedef uint_least8_t uint_fast8_t;
  593. typedef int_least16_t int_fast16_t;
  594. typedef uint_least16_t uint_fast16_t;
  595. typedef int_least32_t int_fast32_t;
  596. typedef uint_least32_t uint_fast32_t;
  597. #define UINT_FAST8_MAX UINT_LEAST8_MAX
  598. #define INT_FAST8_MAX INT_LEAST8_MAX
  599. #define UINT_FAST16_MAX UINT_LEAST16_MAX
  600. #define INT_FAST16_MAX INT_LEAST16_MAX
  601. #define UINT_FAST32_MAX UINT_LEAST32_MAX
  602. #define INT_FAST32_MAX INT_LEAST32_MAX
  603. #define INT_FAST8_MIN INT_LEAST8_MIN
  604. #define INT_FAST16_MIN INT_LEAST16_MIN
  605. #define INT_FAST32_MIN INT_LEAST32_MIN
  606. #ifdef stdint_int64_defined
  607. typedef int_least64_t int_fast64_t;
  608. typedef uint_least64_t uint_fast64_t;
  609. # define UINT_FAST64_MAX UINT_LEAST64_MAX
  610. # define INT_FAST64_MAX INT_LEAST64_MAX
  611. # define INT_FAST64_MIN INT_LEAST64_MIN
  612. #endif
  613. #undef stdint_int64_defined
  614. /*
  615. * Whatever piecemeal, per compiler thing we can do about the wchar_t
  616. * type limits.
  617. */
  618. #if defined(__WATCOMC__) || defined(_MSC_VER) || defined (__GNUC__)
  619. # include <wchar.h>
  620. # ifndef WCHAR_MIN
  621. # define WCHAR_MIN 0
  622. # endif
  623. # ifndef WCHAR_MAX
  624. # define WCHAR_MAX ((wchar_t)-1)
  625. # endif
  626. #endif
  627. /*
  628. * Whatever piecemeal, per compiler/platform thing we can do about the
  629. * (u)intptr_t types and limits.
  630. */
  631. #if defined (_MSC_VER) && defined (_UINTPTR_T_DEFINED)
  632. # define STDINT_H_UINTPTR_T_DEFINED
  633. #endif
  634. #ifndef STDINT_H_UINTPTR_T_DEFINED
  635. # if defined (__alpha__) || defined (__ia64__) || defined (__x86_64__) || defined (_WIN64)
  636. # define stdint_intptr_bits 64
  637. # elif defined (__WATCOMC__) || defined (__TURBOC__)
  638. # if defined(__TINY__) || defined(__SMALL__) || defined(__MEDIUM__)
  639. # define stdint_intptr_bits 16
  640. # else
  641. # define stdint_intptr_bits 32
  642. # endif
  643. # elif defined (__i386__) || defined (_WIN32) || defined (WIN32)
  644. # define stdint_intptr_bits 32
  645. # elif defined (__INTEL_COMPILER)
  646. /* TODO -- what will Intel do about x86-64? */
  647. # endif
  648. # ifdef stdint_intptr_bits
  649. # define stdint_intptr_glue3_i(a,b,c) a##b##c
  650. # define stdint_intptr_glue3(a,b,c) stdint_intptr_glue3_i(a,b,c)
  651. # ifndef PRINTF_INTPTR_MODIFIER
  652. # define PRINTF_INTPTR_MODIFIER stdint_intptr_glue3(PRINTF_INT,stdint_intptr_bits,_MODIFIER)
  653. # endif
  654. # ifndef PTRDIFF_MAX
  655. # define PTRDIFF_MAX stdint_intptr_glue3(INT,stdint_intptr_bits,_MAX)
  656. # endif
  657. # ifndef PTRDIFF_MIN
  658. # define PTRDIFF_MIN stdint_intptr_glue3(INT,stdint_intptr_bits,_MIN)
  659. # endif
  660. # ifndef UINTPTR_MAX
  661. # define UINTPTR_MAX stdint_intptr_glue3(UINT,stdint_intptr_bits,_MAX)
  662. # endif
  663. # ifndef INTPTR_MAX
  664. # define INTPTR_MAX stdint_intptr_glue3(INT,stdint_intptr_bits,_MAX)
  665. # endif
  666. # ifndef INTPTR_MIN
  667. # define INTPTR_MIN stdint_intptr_glue3(INT,stdint_intptr_bits,_MIN)
  668. # endif
  669. # ifndef INTPTR_C
  670. # define INTPTR_C(x) stdint_intptr_glue3(INT,stdint_intptr_bits,_C)(x)
  671. # endif
  672. # ifndef UINTPTR_C
  673. # define UINTPTR_C(x) stdint_intptr_glue3(UINT,stdint_intptr_bits,_C)(x)
  674. # endif
  675. typedef stdint_intptr_glue3(uint,stdint_intptr_bits,_t) uintptr_t;
  676. typedef stdint_intptr_glue3( int,stdint_intptr_bits,_t) intptr_t;
  677. # else
  678. /* TODO -- This following is likely wrong for some platforms, and does
  679. nothing for the definition of uintptr_t. */
  680. typedef ptrdiff_t intptr_t;
  681. # endif
  682. # define STDINT_H_UINTPTR_T_DEFINED
  683. #endif
  684. /*
  685. * Assumes sig_atomic_t is signed and we have a 2s complement machine.
  686. */
  687. #ifndef SIG_ATOMIC_MAX
  688. # define SIG_ATOMIC_MAX ((((sig_atomic_t) 1) << (sizeof (sig_atomic_t)*CHAR_BIT-1)) - 1)
  689. #endif
  690. #endif
  691. #if defined (__TEST_PSTDINT_FOR_CORRECTNESS)
  692. /*
  693. * Please compile with the maximum warning settings to make sure macros are not
  694. * defined more than once.
  695. */
  696. #include <stdlib.h>
  697. #include <stdio.h>
  698. #include <string.h>
  699. #define glue3_aux(x,y,z) x ## y ## z
  700. #define glue3(x,y,z) glue3_aux(x,y,z)
  701. #define DECLU(bits) glue3(uint,bits,_t) glue3(u,bits,=) glue3(UINT,bits,_C) (0);
  702. #define DECLI(bits) glue3(int,bits,_t) glue3(i,bits,=) glue3(INT,bits,_C) (0);
  703. #define DECL(us,bits) glue3(DECL,us,) (bits)
  704. #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)
  705. int main () {
  706. DECL(I,8)
  707. DECL(U,8)
  708. DECL(I,16)
  709. DECL(U,16)
  710. DECL(I,32)
  711. DECL(U,32)
  712. #ifdef INT64_MAX
  713. DECL(I,64)
  714. DECL(U,64)
  715. #endif
  716. intmax_t imax = INTMAX_C(0);
  717. uintmax_t umax = UINTMAX_C(0);
  718. char str0[256], str1[256];
  719. sprintf (str0, "%d %x\n", 0, ~0);
  720. sprintf (str1, "%d %x\n", i8, ~0);
  721. if (0 != strcmp (str0, str1)) printf ("Something wrong with i8 : %s\n", str1);
  722. sprintf (str1, "%u %x\n", u8, ~0);
  723. if (0 != strcmp (str0, str1)) printf ("Something wrong with u8 : %s\n", str1);
  724. sprintf (str1, "%d %x\n", i16, ~0);
  725. if (0 != strcmp (str0, str1)) printf ("Something wrong with i16 : %s\n", str1);
  726. sprintf (str1, "%u %x\n", u16, ~0);
  727. if (0 != strcmp (str0, str1)) printf ("Something wrong with u16 : %s\n", str1);
  728. sprintf (str1, "%" PRINTF_INT32_MODIFIER "d %x\n", i32, ~0);
  729. if (0 != strcmp (str0, str1)) printf ("Something wrong with i32 : %s\n", str1);
  730. sprintf (str1, "%" PRINTF_INT32_MODIFIER "u %x\n", u32, ~0);
  731. if (0 != strcmp (str0, str1)) printf ("Something wrong with u32 : %s\n", str1);
  732. #ifdef INT64_MAX
  733. sprintf (str1, "%" PRINTF_INT64_MODIFIER "d %x\n", i64, ~0);
  734. if (0 != strcmp (str0, str1)) printf ("Something wrong with i64 : %s\n", str1);
  735. #endif
  736. sprintf (str1, "%" PRINTF_INTMAX_MODIFIER "d %x\n", imax, ~0);
  737. if (0 != strcmp (str0, str1)) printf ("Something wrong with imax : %s\n", str1);
  738. sprintf (str1, "%" PRINTF_INTMAX_MODIFIER "u %x\n", umax, ~0);
  739. if (0 != strcmp (str0, str1)) printf ("Something wrong with umax : %s\n", str1);
  740. TESTUMAX(8);
  741. TESTUMAX(16);
  742. TESTUMAX(32);
  743. #ifdef INT64_MAX
  744. TESTUMAX(64);
  745. #endif
  746. return EXIT_SUCCESS;
  747. }
  748. #endif