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