stdint.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. * Copyright (c) 2004, 2005 by
  3. * Ralf Corsepius, Ulm/Germany. All rights reserved.
  4. *
  5. * Permission to use, copy, modify, and distribute this software
  6. * is freely granted, provided that this notice is preserved.
  7. */
  8. #ifndef _STDINT_H
  9. #define _STDINT_H
  10. #include <machine/_default_types.h>
  11. #include <sys/_intsup.h>
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. #ifdef ___int8_t_defined
  16. typedef __int8_t int8_t ;
  17. typedef __uint8_t uint8_t ;
  18. #define __int8_t_defined 1
  19. #endif
  20. #ifdef ___int_least8_t_defined
  21. typedef __int_least8_t int_least8_t;
  22. typedef __uint_least8_t uint_least8_t;
  23. #define __int_least8_t_defined 1
  24. #endif
  25. #ifdef ___int16_t_defined
  26. typedef __int16_t int16_t ;
  27. typedef __uint16_t uint16_t ;
  28. #define __int16_t_defined 1
  29. #endif
  30. #ifdef ___int_least16_t_defined
  31. typedef __int_least16_t int_least16_t;
  32. typedef __uint_least16_t uint_least16_t;
  33. #define __int_least16_t_defined 1
  34. #endif
  35. #ifdef ___int32_t_defined
  36. typedef __int32_t int32_t ;
  37. typedef __uint32_t uint32_t ;
  38. #define __int32_t_defined 1
  39. #endif
  40. #ifdef ___int_least32_t_defined
  41. typedef __int_least32_t int_least32_t;
  42. typedef __uint_least32_t uint_least32_t;
  43. #define __int_least32_t_defined 1
  44. #endif
  45. #ifdef ___int64_t_defined
  46. typedef __int64_t int64_t ;
  47. typedef __uint64_t uint64_t ;
  48. #define __int64_t_defined 1
  49. #endif
  50. #ifdef ___int_least64_t_defined
  51. typedef __int_least64_t int_least64_t;
  52. typedef __uint_least64_t uint_least64_t;
  53. #define __int_least64_t_defined 1
  54. #endif
  55. /*
  56. * Fastest minimum-width integer types
  57. *
  58. * Assume int to be the fastest type for all types with a width
  59. * less than __INT_MAX__ rsp. INT_MAX
  60. */
  61. #ifdef __INT_FAST8_TYPE__
  62. typedef __INT_FAST8_TYPE__ int_fast8_t;
  63. typedef __UINT_FAST8_TYPE__ uint_fast8_t;
  64. #define __int_fast8_t_defined 1
  65. #elif __STDINT_EXP(INT_MAX) >= 0x7f
  66. typedef signed int int_fast8_t;
  67. typedef unsigned int uint_fast8_t;
  68. #define __int_fast8_t_defined 1
  69. #endif
  70. #ifdef __INT_FAST16_TYPE__
  71. typedef __INT_FAST16_TYPE__ int_fast16_t;
  72. typedef __UINT_FAST16_TYPE__ uint_fast16_t;
  73. #define __int_fast16_t_defined 1
  74. #elif __STDINT_EXP(INT_MAX) >= 0x7fff
  75. typedef signed int int_fast16_t;
  76. typedef unsigned int uint_fast16_t;
  77. #define __int_fast16_t_defined 1
  78. #endif
  79. #ifdef __INT_FAST32_TYPE__
  80. typedef __INT_FAST32_TYPE__ int_fast32_t;
  81. typedef __UINT_FAST32_TYPE__ uint_fast32_t;
  82. #define __int_fast32_t_defined 1
  83. #elif __STDINT_EXP(INT_MAX) >= 0x7fffffff
  84. typedef signed int int_fast32_t;
  85. typedef unsigned int uint_fast32_t;
  86. #define __int_fast32_t_defined 1
  87. #endif
  88. #ifdef __INT_FAST64_TYPE__
  89. typedef __INT_FAST64_TYPE__ int_fast64_t;
  90. typedef __UINT_FAST64_TYPE__ uint_fast64_t;
  91. #define __int_fast64_t_defined 1
  92. #elif __STDINT_EXP(INT_MAX) > 0x7fffffff
  93. typedef signed int int_fast64_t;
  94. typedef unsigned int uint_fast64_t;
  95. #define __int_fast64_t_defined 1
  96. #endif
  97. /*
  98. * Fall back to [u]int_least<N>_t for [u]int_fast<N>_t types
  99. * not having been defined, yet.
  100. * Leave undefined, if [u]int_least<N>_t should not be available.
  101. */
  102. #if !__int_fast8_t_defined
  103. #if __int_least8_t_defined
  104. typedef int_least8_t int_fast8_t;
  105. typedef uint_least8_t uint_fast8_t;
  106. #define __int_fast8_t_defined 1
  107. #endif
  108. #endif
  109. #if !__int_fast16_t_defined
  110. #if __int_least16_t_defined
  111. typedef int_least16_t int_fast16_t;
  112. typedef uint_least16_t uint_fast16_t;
  113. #define __int_fast16_t_defined 1
  114. #endif
  115. #endif
  116. #if !__int_fast32_t_defined
  117. #if __int_least32_t_defined
  118. typedef int_least32_t int_fast32_t;
  119. typedef uint_least32_t uint_fast32_t;
  120. #define __int_fast32_t_defined 1
  121. #endif
  122. #endif
  123. #if !__int_fast64_t_defined
  124. #if __int_least64_t_defined
  125. typedef int_least64_t int_fast64_t;
  126. typedef uint_least64_t uint_fast64_t;
  127. #define __int_fast64_t_defined 1
  128. #endif
  129. #endif
  130. /* Greatest-width integer types */
  131. /* Modern GCCs provide __INTMAX_TYPE__ */
  132. #if defined(__INTMAX_TYPE__)
  133. typedef __INTMAX_TYPE__ intmax_t;
  134. #elif __have_longlong64
  135. typedef signed long long intmax_t;
  136. #else
  137. typedef signed long intmax_t;
  138. #endif
  139. /* Modern GCCs provide __UINTMAX_TYPE__ */
  140. #if defined(__UINTMAX_TYPE__)
  141. typedef __UINTMAX_TYPE__ uintmax_t;
  142. #elif __have_longlong64
  143. typedef unsigned long long uintmax_t;
  144. #else
  145. typedef unsigned long uintmax_t;
  146. #endif
  147. typedef __intptr_t intptr_t;
  148. typedef __uintptr_t uintptr_t;
  149. #ifdef __INTPTR_TYPE__
  150. #define INTPTR_MIN (-__INTPTR_MAX__ - 1)
  151. #define INTPTR_MAX __INTPTR_MAX__
  152. #define UINTPTR_MAX __UINTPTR_MAX__
  153. #elif defined(__PTRDIFF_TYPE__)
  154. #define INTPTR_MAX PTRDIFF_MAX
  155. #define INTPTR_MIN PTRDIFF_MIN
  156. #ifdef __UINTPTR_MAX__
  157. #define UINTPTR_MAX __UINTPTR_MAX__
  158. #else
  159. #define UINTPTR_MAX (2UL * PTRDIFF_MAX + 1)
  160. #endif
  161. #else
  162. /*
  163. * Fallback to hardcoded values,
  164. * should be valid on cpu's with 32bit int/32bit void*
  165. */
  166. #define INTPTR_MAX __STDINT_EXP(LONG_MAX)
  167. #define INTPTR_MIN (-__STDINT_EXP(LONG_MAX) - 1)
  168. #define UINTPTR_MAX (__STDINT_EXP(LONG_MAX) * 2UL + 1)
  169. #endif
  170. /* Limits of Specified-Width Integer Types */
  171. #ifdef __INT8_MAX__
  172. #define INT8_MIN (-__INT8_MAX__ - 1)
  173. #define INT8_MAX __INT8_MAX__
  174. #define UINT8_MAX __UINT8_MAX__
  175. #elif defined(__int8_t_defined)
  176. #define INT8_MIN -128
  177. #define INT8_MAX 127
  178. #define UINT8_MAX 255
  179. #endif
  180. #ifdef __INT_LEAST8_MAX__
  181. #define INT_LEAST8_MIN (-__INT_LEAST8_MAX__ - 1)
  182. #define INT_LEAST8_MAX __INT_LEAST8_MAX__
  183. #define UINT_LEAST8_MAX __UINT_LEAST8_MAX__
  184. #elif defined(__int_least8_t_defined)
  185. #define INT_LEAST8_MIN -128
  186. #define INT_LEAST8_MAX 127
  187. #define UINT_LEAST8_MAX 255
  188. #else
  189. #error required type int_least8_t missing
  190. #endif
  191. #ifdef __INT16_MAX__
  192. #define INT16_MIN (-__INT16_MAX__ - 1)
  193. #define INT16_MAX __INT16_MAX__
  194. #define UINT16_MAX __UINT16_MAX__
  195. #elif defined(__int16_t_defined)
  196. #define INT16_MIN -32768
  197. #define INT16_MAX 32767
  198. #define UINT16_MAX 65535
  199. #endif
  200. #ifdef __INT_LEAST16_MAX__
  201. #define INT_LEAST16_MIN (-__INT_LEAST16_MAX__ - 1)
  202. #define INT_LEAST16_MAX __INT_LEAST16_MAX__
  203. #define UINT_LEAST16_MAX __UINT_LEAST16_MAX__
  204. #elif defined(__int_least16_t_defined)
  205. #define INT_LEAST16_MIN -32768
  206. #define INT_LEAST16_MAX 32767
  207. #define UINT_LEAST16_MAX 65535
  208. #else
  209. #error required type int_least16_t missing
  210. #endif
  211. #ifdef __INT32_MAX__
  212. #define INT32_MIN (-__INT32_MAX__ - 1)
  213. #define INT32_MAX __INT32_MAX__
  214. #define UINT32_MAX __UINT32_MAX__
  215. #elif defined(__int32_t_defined)
  216. #if __have_long32
  217. #define INT32_MIN (-2147483647L-1)
  218. #define INT32_MAX 2147483647L
  219. #define UINT32_MAX 4294967295UL
  220. #else
  221. #define INT32_MIN (-2147483647-1)
  222. #define INT32_MAX 2147483647
  223. #define UINT32_MAX 4294967295U
  224. #endif
  225. #endif
  226. #ifdef __INT_LEAST32_MAX__
  227. #define INT_LEAST32_MIN (-__INT_LEAST32_MAX__ - 1)
  228. #define INT_LEAST32_MAX __INT_LEAST32_MAX__
  229. #define UINT_LEAST32_MAX __UINT_LEAST32_MAX__
  230. #elif defined(__int_least32_t_defined)
  231. #if __have_long32
  232. #define INT_LEAST32_MIN (-2147483647L-1)
  233. #define INT_LEAST32_MAX 2147483647L
  234. #define UINT_LEAST32_MAX 4294967295UL
  235. #else
  236. #define INT_LEAST32_MIN (-2147483647-1)
  237. #define INT_LEAST32_MAX 2147483647
  238. #define UINT_LEAST32_MAX 4294967295U
  239. #endif
  240. #else
  241. #error required type int_least32_t missing
  242. #endif
  243. #ifdef __INT64_MAX__
  244. #define INT64_MIN (-__INT64_MAX__ - 1)
  245. #define INT64_MAX __INT64_MAX__
  246. #define UINT64_MAX __UINT64_MAX__
  247. #elif defined(__int64_t_defined)
  248. #if __have_long64
  249. #define INT64_MIN (-9223372036854775807L-1L)
  250. #define INT64_MAX 9223372036854775807L
  251. #define UINT64_MAX 18446744073709551615U
  252. #elif __have_longlong64
  253. #define INT64_MIN (-9223372036854775807LL-1LL)
  254. #define INT64_MAX 9223372036854775807LL
  255. #define UINT64_MAX 18446744073709551615ULL
  256. #endif
  257. #endif
  258. #ifdef __INT_LEAST64_MAX__
  259. #define INT_LEAST64_MIN (-__INT_LEAST64_MAX__ - 1)
  260. #define INT_LEAST64_MAX __INT_LEAST64_MAX__
  261. #define UINT_LEAST64_MAX __UINT_LEAST64_MAX__
  262. #elif defined(__int_least64_t_defined)
  263. #if __have_long64
  264. #define INT_LEAST64_MIN (-9223372036854775807L-1L)
  265. #define INT_LEAST64_MAX 9223372036854775807L
  266. #define UINT_LEAST64_MAX 18446744073709551615U
  267. #elif __have_longlong64
  268. #define INT_LEAST64_MIN (-9223372036854775807LL-1LL)
  269. #define INT_LEAST64_MAX 9223372036854775807LL
  270. #define UINT_LEAST64_MAX 18446744073709551615ULL
  271. #endif
  272. #endif
  273. #ifdef __INT_FAST8_MAX__
  274. #define INT_FAST8_MIN (-__INT_FAST8_MAX__ - 1)
  275. #define INT_FAST8_MAX __INT_FAST8_MAX__
  276. #define UINT_FAST8_MAX __UINT_FAST8_MAX__
  277. #elif defined(__int_fast8_t_defined)
  278. #if __STDINT_EXP(INT_MAX) >= 0x7f
  279. #define INT_FAST8_MIN (-__STDINT_EXP(INT_MAX)-1)
  280. #define INT_FAST8_MAX __STDINT_EXP(INT_MAX)
  281. #define UINT_FAST8_MAX (__STDINT_EXP(INT_MAX)*2U+1U)
  282. #else
  283. #define INT_FAST8_MIN INT_LEAST8_MIN
  284. #define INT_FAST8_MAX INT_LEAST8_MAX
  285. #define UINT_FAST8_MAX UINT_LEAST8_MAX
  286. #endif
  287. #endif
  288. #ifdef __INT_FAST16_MAX__
  289. #define INT_FAST16_MIN (-__INT_FAST16_MAX__ - 1)
  290. #define INT_FAST16_MAX __INT_FAST16_MAX__
  291. #define UINT_FAST16_MAX __UINT_FAST16_MAX__
  292. #elif defined(__int_fast16_t_defined)
  293. #if __STDINT_EXP(INT_MAX) >= 0x7fff
  294. #define INT_FAST16_MIN (-__STDINT_EXP(INT_MAX)-1)
  295. #define INT_FAST16_MAX __STDINT_EXP(INT_MAX)
  296. #define UINT_FAST16_MAX (__STDINT_EXP(INT_MAX)*2U+1U)
  297. #else
  298. #define INT_FAST16_MIN INT_LEAST16_MIN
  299. #define INT_FAST16_MAX INT_LEAST16_MAX
  300. #define UINT_FAST16_MAX UINT_LEAST16_MAX
  301. #endif
  302. #endif
  303. #ifdef __INT_FAST32_MAX__
  304. #define INT_FAST32_MIN (-__INT_FAST32_MAX__ - 1)
  305. #define INT_FAST32_MAX __INT_FAST32_MAX__
  306. #define UINT_FAST32_MAX __UINT_FAST32_MAX__
  307. #elif defined(__int_fast32_t_defined)
  308. #if __STDINT_EXP(INT_MAX) >= 0x7fffffff
  309. #define INT_FAST32_MIN (-__STDINT_EXP(INT_MAX)-1)
  310. #define INT_FAST32_MAX __STDINT_EXP(INT_MAX)
  311. #define UINT_FAST32_MAX (__STDINT_EXP(INT_MAX)*2U+1U)
  312. #else
  313. #define INT_FAST32_MIN INT_LEAST32_MIN
  314. #define INT_FAST32_MAX INT_LEAST32_MAX
  315. #define UINT_FAST32_MAX UINT_LEAST32_MAX
  316. #endif
  317. #endif
  318. #ifdef __INT_FAST64_MAX__
  319. #define INT_FAST64_MIN (-__INT_FAST64_MAX__ - 1)
  320. #define INT_FAST64_MAX __INT_FAST64_MAX__
  321. #define UINT_FAST64_MAX __UINT_FAST64_MAX__
  322. #elif defined(__int_fast64_t_defined)
  323. #if __STDINT_EXP(INT_MAX) > 0x7fffffff
  324. #define INT_FAST64_MIN (-__STDINT_EXP(INT_MAX)-1)
  325. #define INT_FAST64_MAX __STDINT_EXP(INT_MAX)
  326. #define UINT_FAST64_MAX (__STDINT_EXP(INT_MAX)*2U+1U)
  327. #else
  328. #define INT_FAST64_MIN INT_LEAST64_MIN
  329. #define INT_FAST64_MAX INT_LEAST64_MAX
  330. #define UINT_FAST64_MAX UINT_LEAST64_MAX
  331. #endif
  332. #endif
  333. #ifdef __INTMAX_MAX__
  334. #define INTMAX_MAX __INTMAX_MAX__
  335. #define INTMAX_MIN (-INTMAX_MAX - 1)
  336. #elif defined(__INTMAX_TYPE__)
  337. /* All relevant GCC versions prefer long to long long for intmax_t. */
  338. #define INTMAX_MAX INT64_MAX
  339. #define INTMAX_MIN INT64_MIN
  340. #endif
  341. #ifdef __UINTMAX_MAX__
  342. #define UINTMAX_MAX __UINTMAX_MAX__
  343. #elif defined(__UINTMAX_TYPE__)
  344. /* All relevant GCC versions prefer long to long long for intmax_t. */
  345. #define UINTMAX_MAX UINT64_MAX
  346. #endif
  347. /* This must match size_t in stddef.h, currently long unsigned int */
  348. #ifdef __SIZE_MAX__
  349. #define SIZE_MAX __SIZE_MAX__
  350. #else
  351. #define SIZE_MAX (__STDINT_EXP(LONG_MAX) * 2UL + 1)
  352. #endif
  353. /* This must match sig_atomic_t in <signal.h> (currently int) */
  354. #define SIG_ATOMIC_MIN (-__STDINT_EXP(INT_MAX) - 1)
  355. #define SIG_ATOMIC_MAX __STDINT_EXP(INT_MAX)
  356. /* This must match ptrdiff_t in <stddef.h> (currently long int) */
  357. #ifdef __PTRDIFF_MAX__
  358. #define PTRDIFF_MAX __PTRDIFF_MAX__
  359. #else
  360. #define PTRDIFF_MAX __STDINT_EXP(LONG_MAX)
  361. #endif
  362. #define PTRDIFF_MIN (-PTRDIFF_MAX - 1)
  363. /* This must match definition in <wchar.h> */
  364. #ifndef WCHAR_MIN
  365. #ifdef __WCHAR_MIN__
  366. #define WCHAR_MIN __WCHAR_MIN__
  367. #elif defined(__WCHAR_UNSIGNED__) || (L'\0' - 1 > 0)
  368. #define WCHAR_MIN (0 + L'\0')
  369. #else
  370. #define WCHAR_MIN (-0x7fffffff - 1 + L'\0')
  371. #endif
  372. #endif
  373. /* This must match definition in <wchar.h> */
  374. #ifndef WCHAR_MAX
  375. #ifdef __WCHAR_MAX__
  376. #define WCHAR_MAX __WCHAR_MAX__
  377. #elif defined(__WCHAR_UNSIGNED__) || (L'\0' - 1 > 0)
  378. #define WCHAR_MAX (0xffffffffu + L'\0')
  379. #else
  380. #define WCHAR_MAX (0x7fffffff + L'\0')
  381. #endif
  382. #endif
  383. /* wint_t is unsigned int on almost all GCC targets. */
  384. #ifdef __WINT_MAX__
  385. #define WINT_MAX __WINT_MAX__
  386. #else
  387. #define WINT_MAX (__STDINT_EXP(INT_MAX) * 2U + 1U)
  388. #endif
  389. #ifdef __WINT_MIN__
  390. #define WINT_MIN __WINT_MIN__
  391. #else
  392. #define WINT_MIN 0U
  393. #endif
  394. /** Macros for minimum-width integer constant expressions */
  395. #ifdef __INT8_C
  396. #define INT8_C(x) __INT8_C(x)
  397. #define UINT8_C(x) __UINT8_C(x)
  398. #else
  399. #define INT8_C(x) x
  400. #if __STDINT_EXP(INT_MAX) > 0x7f
  401. #define UINT8_C(x) x
  402. #else
  403. #define UINT8_C(x) x##U
  404. #endif
  405. #endif
  406. #ifdef __INT16_C
  407. #define INT16_C(x) __INT16_C(x)
  408. #define UINT16_C(x) __UINT16_C(x)
  409. #else
  410. #define INT16_C(x) x
  411. #if __STDINT_EXP(INT_MAX) > 0x7fff
  412. #define UINT16_C(x) x
  413. #else
  414. #define UINT16_C(x) x##U
  415. #endif
  416. #endif
  417. #ifdef __INT32_C
  418. #define INT32_C(x) __INT32_C(x)
  419. #define UINT32_C(x) __UINT32_C(x)
  420. #else
  421. #if __have_long32
  422. #define INT32_C(x) x##L
  423. #define UINT32_C(x) x##UL
  424. #else
  425. #define INT32_C(x) x
  426. #define UINT32_C(x) x##U
  427. #endif
  428. #endif
  429. #ifdef __INT64_C
  430. #define INT64_C(x) __INT64_C(x)
  431. #define UINT64_C(x) __UINT64_C(x)
  432. #else
  433. #if __int64_t_defined
  434. #if __have_long64
  435. #define INT64_C(x) x##L
  436. #define UINT64_C(x) x##UL
  437. #else
  438. #define INT64_C(x) x##LL
  439. #define UINT64_C(x) x##ULL
  440. #endif
  441. #endif
  442. #endif
  443. /** Macros for greatest-width integer constant expression */
  444. #ifdef __INTMAX_C
  445. #define INTMAX_C(x) __INTMAX_C(x)
  446. #define UINTMAX_C(x) __UINTMAX_C(x)
  447. #else
  448. #if __have_long64
  449. #define INTMAX_C(x) x##L
  450. #define UINTMAX_C(x) x##UL
  451. #else
  452. #define INTMAX_C(x) x##LL
  453. #define UINTMAX_C(x) x##ULL
  454. #endif
  455. #endif
  456. #ifdef __cplusplus
  457. }
  458. #endif
  459. #endif /* _STDINT_H */