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