blob: a7020d838ec96558bed6390bb9a6def96042397f [file] [log] [blame]
Chris Lattner2a67f7b2009-02-06 22:59:47 +00001/*===---- stdint.h - Standard header for sized integer types --------------===*\
2 *
3 * Copyright (c) 2009 Chris Lattner
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 *
23\*===----------------------------------------------------------------------===*/
24
Eli Friedman6fdca0e2009-05-03 23:00:48 +000025#ifndef __CLANG_STDINT_H
26#define __CLANG_STDINT_H
27
28/* If we're hosted, fall back to the system's stdint.h, which might have
29 * additional definitions.
30 */
31#if __STDC_HOSTED__
32# include_next <stdint.h>
33#else
Chris Lattner2a67f7b2009-02-06 22:59:47 +000034
35/* We currently only support targets with power of two, 2s complement integers.
36 */
37
38/* C99 7.18.1.1 Exact-width integer types.
39 * C99 7.18.1.2 Minimum-width integer types.
40 * C99 7.18.1.3 Fastest minimum-width integer types.
41 * Since we only support pow-2 targets, these map directly to exact width types.
42 */
43
Chris Lattnerca931952009-04-18 19:11:11 +000044#ifndef __int8_t_defined /* glibc does weird things with sys/types.h */
45#define __int8_t_defined
Chris Lattner2a67f7b2009-02-06 22:59:47 +000046typedef signed __INT8_TYPE__ int8_t;
Chris Lattnerca931952009-04-18 19:11:11 +000047typedef __INT16_TYPE__ int16_t;
48typedef __INT32_TYPE__ int32_t;
49#ifdef __INT64_TYPE__
50typedef __INT64_TYPE__ int64_t;
51#endif
52#endif
53
Sebastian Redl63b4fe62009-02-06 23:57:52 +000054typedef unsigned __INT8_TYPE__ uint8_t;
Chris Lattner2a67f7b2009-02-06 22:59:47 +000055typedef int8_t int_least8_t;
56typedef uint8_t uint_least8_t;
57typedef int8_t int_fast8_t;
58typedef uint8_t uint_fast8_t;
59
Chris Lattner2a67f7b2009-02-06 22:59:47 +000060typedef unsigned __INT16_TYPE__ uint16_t;
61typedef int16_t int_least16_t;
62typedef uint16_t uint_least16_t;
63typedef int16_t int_fast16_t;
64typedef uint16_t uint_fast16_t;
65
Chris Lattnerca931952009-04-18 19:11:11 +000066#ifndef __uint32_t_defined /* more glibc compatibility */
67#define __uint32_t_defined
Chris Lattner2a67f7b2009-02-06 22:59:47 +000068typedef unsigned __INT32_TYPE__ uint32_t;
Chris Lattnerca931952009-04-18 19:11:11 +000069#endif
Chris Lattner2a67f7b2009-02-06 22:59:47 +000070typedef int32_t int_least32_t;
71typedef uint32_t uint_least32_t;
72typedef int32_t int_fast32_t;
73typedef uint32_t uint_fast32_t;
74
75/* Some 16-bit targets do not have a 64-bit datatype. Only define the 64-bit
76 * typedefs if there is something to typedef them to.
77 */
78#ifdef __INT64_TYPE__
Chris Lattner2a67f7b2009-02-06 22:59:47 +000079typedef unsigned __INT64_TYPE__ uint64_t;
80typedef int64_t int_least64_t;
81typedef uint64_t uint_least64_t;
82typedef int64_t int_fast64_t;
83typedef uint64_t uint_fast64_t;
84#endif
85
86
87/* C99 7.18.1.4 Integer types capable of holding object pointers.
88 */
Chris Lattner40e60c22009-02-13 22:43:13 +000089#ifndef __intptr_t_defined
Chris Lattner6ad474f2009-02-13 22:28:55 +000090typedef __INTPTR_TYPE__ intptr_t;
Chris Lattner40e60c22009-02-13 22:43:13 +000091#define __intptr_t_defined
92#endif
Daniel Dunbarcf117d32009-03-15 03:16:47 +000093typedef unsigned __INTPTR_TYPE__ uintptr_t;
Chris Lattner2a67f7b2009-02-06 22:59:47 +000094
95/* C99 7.18.1.5 Greatest-width integer types.
96 */
97typedef __INTMAX_TYPE__ intmax_t;
98typedef __UINTMAX_TYPE__ uintmax_t;
99
100/* C99 7.18.2.1 Limits of exact-width integer types.
101 * Fixed sized values have fixed size max/min.
102 * C99 7.18.2.2 Limits of minimum-width integer types.
103 * Since we map these directly onto fixed-sized types, these values the same.
104 * C99 7.18.2.3 Limits of fastest minimum-width integer types.
Chris Lattner61484282009-02-07 22:21:31 +0000105 *
106 * Note that C++ should not check __STDC_LIMIT_MACROS here, contrary to the
107 * claims of the C standard (see C++ 18.3.1p2, [cstdint.syn]).
Chris Lattner2a67f7b2009-02-06 22:59:47 +0000108 */
109
Chris Lattnerf894bd02009-02-07 06:33:44 +0000110#define INT8_MAX 127
Chris Lattner7ed4f392009-02-07 06:42:04 +0000111#define INT8_MIN (-128)
Chris Lattnerdfd556e2009-02-07 08:49:37 +0000112#define UINT8_MAX 255
Chris Lattner2a67f7b2009-02-06 22:59:47 +0000113#define INT_LEAST8_MIN INT8_MIN
114#define INT_LEAST8_MAX INT8_MAX
115#define UINT_LEAST8_MAX UINT8_MAX
116#define INT_FAST8_MIN INT8_MIN
117#define INT_FAST8_MAX INT8_MAX
118#define UINT_FAST8_MAX UINT8_MAX
119
Chris Lattnerf894bd02009-02-07 06:33:44 +0000120#define INT16_MAX 32767
Chris Lattner7ed4f392009-02-07 06:42:04 +0000121#define INT16_MIN (-32768)
Chris Lattnerdfd556e2009-02-07 08:49:37 +0000122#define UINT16_MAX 65535
Chris Lattner2a67f7b2009-02-06 22:59:47 +0000123#define INT_LEAST16_MIN INT16_MIN
124#define INT_LEAST16_MAX INT16_MAX
125#define UINT_LEAST16_MAX UINT16_MAX
126#define INT_FAST16_MIN INT16_MIN
127#define INT_FAST16_MAX INT16_MAX
128#define UINT_FAST16_MAX UINT16_MAX
129
Chris Lattnerf894bd02009-02-07 06:33:44 +0000130#define INT32_MAX 2147483647
Chris Lattner7ed4f392009-02-07 06:42:04 +0000131#define INT32_MIN (-2147483647-1)
Chris Lattnerf894bd02009-02-07 06:33:44 +0000132#define UINT32_MAX 4294967295U
Chris Lattner2a67f7b2009-02-06 22:59:47 +0000133#define INT_LEAST32_MIN INT32_MIN
134#define INT_LEAST32_MAX INT32_MAX
135#define UINT_LEAST32_MAX UINT32_MAX
136#define INT_FAST32_MIN INT32_MIN
137#define INT_FAST32_MAX INT32_MAX
138#define UINT_FAST32_MAX UINT32_MAX
139
140/* If we do not have 64-bit support, don't define the 64-bit size macros. */
Chris Lattnerab775f62009-02-28 18:53:33 +0000141#ifdef __INT64_TYPE__
Chris Lattnerf894bd02009-02-07 06:33:44 +0000142#define INT64_MAX 9223372036854775807LL
143#define INT64_MIN (-9223372036854775807LL-1)
Chris Lattner7ed4f392009-02-07 06:42:04 +0000144#define UINT64_MAX 18446744073709551615ULL
Chris Lattner2a67f7b2009-02-06 22:59:47 +0000145#define INT_LEAST64_MIN INT64_MIN
146#define INT_LEAST64_MAX INT64_MAX
147#define UINT_LEAST64_MAX UINT64_MAX
148#define INT_FAST64_MIN INT64_MIN
149#define INT_FAST64_MAX INT64_MAX
150#define UINT_FAST64_MAX UINT64_MAX
151#endif
152
153/* C99 7.18.2.4 Limits of integer types capable of holding object pointers. */
154/* C99 7.18.3 Limits of other integer types. */
155
156#if __POINTER_WIDTH__ == 64
157
158#define INTPTR_MIN INT64_MIN
159#define INTPTR_MAX INT64_MAX
160#define UINTPTR_MAX UINT64_MAX
161#define PTRDIFF_MIN INT64_MIN
162#define PTRDIFF_MAX INT64_MAX
163#define SIZE_MAX UINT64_MAX
164
Sebastian Redl63b4fe62009-02-06 23:57:52 +0000165#elif __POINTER_WIDTH__ == 32
Chris Lattner2a67f7b2009-02-06 22:59:47 +0000166
167#define INTPTR_MIN INT32_MIN
168#define INTPTR_MAX INT32_MAX
169#define UINTPTR_MAX UINT32_MAX
170#define PTRDIFF_MIN INT32_MIN
171#define PTRDIFF_MAX INT32_MAX
172#define SIZE_MAX UINT32_MAX
173
Sebastian Redl63b4fe62009-02-06 23:57:52 +0000174#elif __POINTER_WIDTH__ == 16
Chris Lattner2a67f7b2009-02-06 22:59:47 +0000175
176#define INTPTR_MIN INT16_MIN
177#define INTPTR_MAX INT16_MAX
178#define UINTPTR_MAX UINT16_MAX
179#define PTRDIFF_MIN INT16_MIN
180#define PTRDIFF_MAX INT16_MAX
181#define SIZE_MAX UINT16_MAX
182
183#else
184#error "unknown or unset pointer width!"
185#endif
186
187/* C99 7.18.2.5 Limits of greatest-width integer types. */
188#define INTMAX_MIN (-__INTMAX_MAX__-1)
189#define INTMAX_MAX __INTMAX_MAX__
Chris Lattnerc865f7a2009-02-07 06:38:06 +0000190#define UINTMAX_MAX (__INTMAX_MAX__*2ULL+1ULL)
Chris Lattner2a67f7b2009-02-06 22:59:47 +0000191
192/* C99 7.18.3 Limits of other integer types. */
193#define SIG_ATOMIC_MIN INT32_MIN
194#define SIG_ATOMIC_MAX INT32_MAX
195#define WINT_MIN INT32_MIN
196#define WINT_MAX INT32_MAX
197
198/* FIXME: if we ever support a target with unsigned wchar_t, this should be
199 * 0 .. Max.
200 */
201#ifndef WCHAR_MAX
202#define WCHAR_MAX __WCHAR_MAX__
203#endif
204#ifndef WCHAR_MIN
205#define WCHAR_MIN (-__WCHAR_MAX__-1)
206#endif
207
Chris Lattner61484282009-02-07 22:21:31 +0000208/* C99 7.18.4 Macros for minimum-width integer constants.
209 *
210 * Note that C++ should not check __STDC_CONSTANT_MACROS here, contrary to the
211 * claims of the C standard (see C++ 18.3.1p2, [cstdint.syn]).
212 */
Chris Lattner2a67f7b2009-02-06 22:59:47 +0000213
214#define INT8_C(v) (v)
215#define UINT8_C(v) (v##U)
216#define INT16_C(v) (v)
217#define UINT16_C(v) (v##U)
218#define INT32_C(v) (v)
219#define UINT32_C(v) (v##U)
220
Anders Carlsson6c909192009-02-10 06:18:19 +0000221/* Only define the 64-bit size macros if we have 64-bit support. */
222#ifdef __INT64_TYPE__
Chris Lattner2a67f7b2009-02-06 22:59:47 +0000223#define INT64_C(v) (v##LL)
224#define UINT64_C(v) (v##ULL)
225#endif
226
227/* 7.18.4.2 Macros for greatest-width integer constants. */
Chris Lattner7ed4f392009-02-07 06:42:04 +0000228#define INTMAX_C(v) (v##LL)
229#define UINTMAX_C(v) (v##ULL)
Chris Lattner2a67f7b2009-02-06 22:59:47 +0000230
Eli Friedman6fdca0e2009-05-03 23:00:48 +0000231#endif /* __STDC_HOSTED__ */
232#endif /* __CLANG_STDINT_H */