stdint.h revision 198893
1/*===---- 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
25#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    defined(__has_include_next) && __has_include_next(<stdint.h>)
33# include_next <stdint.h>
34#else
35
36/* We currently only support targets with power of two, 2s complement integers.
37 */
38
39/* C99 7.18.1.1 Exact-width integer types.
40 * C99 7.18.1.2 Minimum-width integer types.
41 * C99 7.18.1.3 Fastest minimum-width integer types.
42 * Since we only support pow-2 targets, these map directly to exact width types.
43 */
44
45#ifndef __int8_t_defined  /* glibc does weird things with sys/types.h */
46#define __int8_t_defined
47typedef signed __INT8_TYPE__ int8_t;
48typedef __INT16_TYPE__ int16_t;
49typedef __INT32_TYPE__ int32_t;
50#ifdef __INT64_TYPE__
51typedef __INT64_TYPE__ int64_t;
52#endif
53#endif
54
55typedef unsigned __INT8_TYPE__ uint8_t;
56typedef int8_t     int_least8_t;
57typedef uint8_t   uint_least8_t;
58typedef int8_t     int_fast8_t;
59typedef uint8_t   uint_fast8_t;
60
61typedef unsigned __INT16_TYPE__ uint16_t;
62typedef int16_t   int_least16_t;
63typedef uint16_t uint_least16_t;
64typedef int16_t   int_fast16_t;
65typedef uint16_t uint_fast16_t;
66
67#ifndef __uint32_t_defined  /* more glibc compatibility */
68#define __uint32_t_defined
69typedef unsigned __INT32_TYPE__ uint32_t;
70#endif
71typedef int32_t   int_least32_t;
72typedef uint32_t uint_least32_t;
73typedef int32_t   int_fast32_t;
74typedef uint32_t uint_fast32_t;
75
76/* Some 16-bit targets do not have a 64-bit datatype.  Only define the 64-bit
77 * typedefs if there is something to typedef them to.
78 */
79#ifdef __INT64_TYPE__
80typedef unsigned __INT64_TYPE__ uint64_t;
81typedef int64_t   int_least64_t;
82typedef uint64_t uint_least64_t;
83typedef int64_t   int_fast64_t;
84typedef uint64_t uint_fast64_t;
85#endif
86
87
88/* C99 7.18.1.4 Integer types capable of holding object pointers.
89 */
90#ifndef __intptr_t_defined
91typedef __INTPTR_TYPE__          intptr_t;
92#define __intptr_t_defined
93#endif
94typedef unsigned __INTPTR_TYPE__ uintptr_t;
95
96/* C99 7.18.1.5 Greatest-width integer types.
97 */
98typedef __INTMAX_TYPE__   intmax_t;
99typedef __UINTMAX_TYPE__ uintmax_t;
100
101/* C99 7.18.2.1 Limits of exact-width integer types.
102 * Fixed sized values have fixed size max/min.
103 * C99 7.18.2.2 Limits of minimum-width integer types.
104 * Since we map these directly onto fixed-sized types, these values the same.
105 * C99 7.18.2.3 Limits of fastest minimum-width integer types.
106 *
107 * Note that C++ should not check __STDC_LIMIT_MACROS here, contrary to the
108 * claims of the C standard (see C++ 18.3.1p2, [cstdint.syn]).
109 */
110
111#define INT8_MAX    127
112#define INT8_MIN  (-128)
113#define UINT8_MAX   255
114#define INT_LEAST8_MIN   INT8_MIN
115#define INT_LEAST8_MAX   INT8_MAX
116#define UINT_LEAST8_MAX UINT8_MAX
117#define INT_FAST8_MIN    INT8_MIN
118#define INT_FAST8_MAX    INT8_MAX
119#define UINT_FAST8_MAX  UINT8_MAX
120
121#define INT16_MAX    32767
122#define INT16_MIN  (-32768)
123#define UINT16_MAX   65535
124#define INT_LEAST16_MIN   INT16_MIN
125#define INT_LEAST16_MAX   INT16_MAX
126#define UINT_LEAST16_MAX UINT16_MAX
127#define INT_FAST16_MIN    INT16_MIN
128#define INT_FAST16_MAX    INT16_MAX
129#define UINT_FAST16_MAX  UINT16_MAX
130
131#define INT32_MAX         2147483647
132#define INT32_MIN        (-2147483647-1)
133#define UINT32_MAX        4294967295U
134#define INT_LEAST32_MIN  INT32_MIN
135#define INT_LEAST32_MAX  INT32_MAX
136#define UINT_LEAST32_MAX UINT32_MAX
137#define INT_FAST32_MIN   INT32_MIN
138#define INT_FAST32_MAX   INT32_MAX
139#define UINT_FAST32_MAX  UINT32_MAX
140
141/* If we do not have 64-bit support, don't define the 64-bit size macros. */
142#ifdef __INT64_TYPE__
143#define INT64_MAX      9223372036854775807LL
144#define INT64_MIN    (-9223372036854775807LL-1)
145#define UINT64_MAX    18446744073709551615ULL
146#define INT_LEAST64_MIN  INT64_MIN
147#define INT_LEAST64_MAX  INT64_MAX
148#define UINT_LEAST64_MAX UINT64_MAX
149#define INT_FAST64_MIN    INT64_MIN
150#define INT_FAST64_MAX    INT64_MAX
151#define UINT_FAST64_MAX  UINT64_MAX
152#endif
153
154/* C99 7.18.2.4 Limits of integer types capable of holding object pointers. */
155/* C99 7.18.3 Limits of other integer types. */
156
157#if __POINTER_WIDTH__ == 64
158
159#define  INTPTR_MIN  INT64_MIN
160#define  INTPTR_MAX  INT64_MAX
161#define UINTPTR_MAX UINT64_MAX
162#define PTRDIFF_MIN  INT64_MIN
163#define PTRDIFF_MAX  INT64_MAX
164#define SIZE_MAX    UINT64_MAX
165
166#elif __POINTER_WIDTH__ == 32
167
168#define  INTPTR_MIN  INT32_MIN
169#define  INTPTR_MAX  INT32_MAX
170#define UINTPTR_MAX UINT32_MAX
171#define PTRDIFF_MIN  INT32_MIN
172#define PTRDIFF_MAX  INT32_MAX
173#define SIZE_MAX    UINT32_MAX
174
175#elif __POINTER_WIDTH__ == 16
176
177#define  INTPTR_MIN  INT16_MIN
178#define  INTPTR_MAX  INT16_MAX
179#define UINTPTR_MAX UINT16_MAX
180#define PTRDIFF_MIN  INT16_MIN
181#define PTRDIFF_MAX  INT16_MAX
182#define SIZE_MAX    UINT16_MAX
183
184#else
185#error "unknown or unset pointer width!"
186#endif
187
188/* C99 7.18.2.5 Limits of greatest-width integer types. */
189#define INTMAX_MIN  (-__INTMAX_MAX__-1)
190#define INTMAX_MAX   __INTMAX_MAX__
191#define UINTMAX_MAX (__INTMAX_MAX__*2ULL+1ULL)
192
193/* C99 7.18.3 Limits of other integer types. */
194#define SIG_ATOMIC_MIN INT32_MIN
195#define SIG_ATOMIC_MAX INT32_MAX
196#define WINT_MIN       INT32_MIN
197#define WINT_MAX       INT32_MAX
198
199/* FIXME: if we ever support a target with unsigned wchar_t, this should be
200 * 0 .. Max.
201 */
202#ifndef WCHAR_MAX
203#define WCHAR_MAX __WCHAR_MAX__
204#endif
205#ifndef WCHAR_MIN
206#define WCHAR_MIN (-__WCHAR_MAX__-1)
207#endif
208
209/* C99 7.18.4 Macros for minimum-width integer constants.
210 *
211 * Note that C++ should not check __STDC_CONSTANT_MACROS here, contrary to the
212 * claims of the C standard (see C++ 18.3.1p2, [cstdint.syn]).
213 */
214
215#define INT8_C(v)   (v)
216#define UINT8_C(v)  (v##U)
217#define INT16_C(v)  (v)
218#define UINT16_C(v) (v##U)
219#define INT32_C(v)  (v)
220#define UINT32_C(v) (v##U)
221
222/* Only define the 64-bit size macros if we have 64-bit support. */
223#ifdef __INT64_TYPE__
224#define INT64_C(v)  (v##LL)
225#define UINT64_C(v) (v##ULL)
226#endif
227
228/* 7.18.4.2 Macros for greatest-width integer constants. */
229#define INTMAX_C(v)  (v##LL)
230#define UINTMAX_C(v) (v##ULL)
231
232#endif /* __STDC_HOSTED__ */
233#endif /* __CLANG_STDINT_H */
234