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 9 /* 10 * @todo - Add support for wint_t types. 11 */ 12 13 #ifndef _STDINT_H 14 #define _STDINT_H 15 16 #ifdef __cplusplus 17 extern "C" { 18 #endif 19 20 #if defined(__GNUC__) && \ 21 ( (__GNUC__ >= 4) || \ 22 ( (__GNUC__ >= 3) && defined(__GNUC_MINOR__) && (__GNUC_MINOR__ > 2) ) ) 23 /* gcc > 3.2 implicitly defines the values we are interested */ 24 #define __STDINT_EXP(x) __##x##__ 25 #else 26 #define __STDINT_EXP(x) x 27 #include <limits.h> 28 #endif 29 30 /* Check if "long long" is 64bit wide */ 31 /* Modern GCCs provide __LONG_LONG_MAX__, SUSv3 wants LLONG_MAX */ 32 #if ( defined(__LONG_LONG_MAX__) && (__LONG_LONG_MAX__ > 0x7fffffff) ) \ 33 || ( defined(LLONG_MAX) && (LLONG_MAX > 0x7fffffff) ) 34 #define __have_longlong64 1 35 #endif 36 37 /* Check if "long" is 64bit or 32bit wide */ 38 #if __STDINT_EXP(LONG_MAX) > 0x7fffffff 39 #define __have_long64 1 40 #elif __STDINT_EXP(LONG_MAX) == 0x7fffffff 41 #define __have_long32 1 42 #endif 43 44 #if __STDINT_EXP(SCHAR_MAX) == 0x7f 45 typedef signed char int8_t ; 46 typedef unsigned char uint8_t ; 47 #define __int8_t_defined 1 48 #endif 49 50 #if __int8_t_defined 51 typedef signed char int_least8_t; 52 typedef unsigned char uint_least8_t; 53 #define __int_least8_t_defined 1 54 #endif 55 56 #if __STDINT_EXP(SHRT_MAX) == 0x7fff 57 typedef signed short int16_t; 58 typedef unsigned short uint16_t; 59 #define __int16_t_defined 1 60 #elif __STDINT_EXP(INT_MAX) == 0x7fff 61 typedef signed int int16_t; 62 typedef unsigned int uint16_t; 63 #define __int16_t_defined 1 64 #elif __STDINT_EXP(SCHAR_MAX) == 0x7fff 65 typedef signed char int16_t; 66 typedef unsigned char uint16_t; 67 #define __int16_t_defined 1 68 #endif 69 70 #if __int16_t_defined 71 typedef int16_t int_least16_t; 72 typedef uint16_t uint_least16_t; 73 #define __int_least16_t_defined 1 74 75 #if !__int_least8_t_defined 76 typedef int16_t int_least8_t; 77 typedef uint16_t uint_least8_t; 78 #define __int_least8_t_defined 1 79 #endif 80 #endif 81 82 #if __have_long32 83 typedef signed long int32_t; 84 typedef unsigned long uint32_t; 85 #define __int32_t_defined 1 86 #elif __STDINT_EXP(INT_MAX) == 0x7fffffffL 87 typedef signed int int32_t; 88 typedef unsigned int uint32_t; 89 #define __int32_t_defined 1 90 #elif __STDINT_EXP(SHRT_MAX) == 0x7fffffffL 91 typedef signed short int32_t; 92 typedef unsigned short uint32_t; 93 #define __int32_t_defined 1 94 #elif __STDINT_EXP(SCHAR_MAX) == 0x7fffffffL 95 typedef signed char int32_t; 96 typedef unsigned char uint32_t; 97 #define __int32_t_defined 1 98 #endif 99 100 #if __int32_t_defined 101 typedef int32_t int_least32_t; 102 typedef uint32_t uint_least32_t; 103 #define __int_least32_t_defined 1 104 105 #if !__int_least8_t_defined 106 typedef int32_t int_least8_t; 107 typedef uint32_t uint_least8_t; 108 #define __int_least8_t_defined 1 109 #endif 110 111 #if !__int_least16_t_defined 112 typedef int32_t int_least16_t; 113 typedef uint32_t uint_least16_t; 114 #define __int_least16_t_defined 1 115 #endif 116 #endif 117 118 #if __have_long64 119 typedef signed long int64_t; 120 typedef unsigned long uint64_t; 121 #define __int64_t_defined 1 122 #elif __have_longlong64 123 typedef signed long long int64_t; 124 typedef unsigned long long uint64_t; 125 #define __int64_t_defined 1 126 #elif __STDINT_EXP(INT_MAX) > 0x7fffffff 127 typedef signed int int64_t; 128 typedef unsigned int uint64_t; 129 #define __int64_t_defined 1 130 #endif 131 132 #if __int64_t_defined 133 typedef int64_t int_least64_t; 134 typedef uint64_t uint_least64_t; 135 #define __int_least64_t_defined 1 136 137 #if !__int_least8_t_defined 138 typedef int64_t int_least8_t; 139 typedef uint64_t uint_least8_t; 140 #define __int_least8_t_defined 1 141 #endif 142 143 #if !__int_least16_t_defined 144 typedef int64_t int_least16_t; 145 typedef uint64_t uint_least16_t; 146 #define __int_least16_t_defined 1 147 #endif 148 149 #if !__int_least32_t_defined 150 typedef int64_t int_least32_t; 151 typedef uint64_t uint_least32_t; 152 #define __int_least32_t_defined 1 153 #endif 154 #endif 155 156 /* 157 * Fastest minimum-width integer types 158 * 159 * Assume int to be the fastest type for all types with a width 160 * less than __INT_MAX__ rsp. INT_MAX 161 */ 162 #if __STDINT_EXP(INT_MAX) >= 0x7f 163 typedef signed int int_fast8_t; 164 typedef unsigned int uint_fast8_t; 165 #define __int_fast8_t_defined 1 166 #endif 167 168 #if __STDINT_EXP(INT_MAX) >= 0x7fff 169 typedef signed int int_fast16_t; 170 typedef unsigned int uint_fast16_t; 171 #define __int_fast16_t_defined 1 172 #endif 173 174 #if __STDINT_EXP(INT_MAX) >= 0x7fffffff 175 typedef signed int int_fast32_t; 176 typedef unsigned int uint_fast32_t; 177 #define __int_fast32_t_defined 1 178 #endif 179 180 #if __STDINT_EXP(INT_MAX) > 0x7fffffff 181 typedef signed int int_fast64_t; 182 typedef unsigned int uint_fast64_t; 183 #define __int_fast64_t_defined 1 184 #endif 185 186 /* 187 * Fall back to [u]int_least<N>_t for [u]int_fast<N>_t types 188 * not having been defined, yet. 189 * Leave undefined, if [u]int_least<N>_t should not be available. 190 */ 191 #if !__int_fast8_t_defined 192 #if __int_least8_t_defined 193 typedef int_least8_t int_fast8_t; 194 typedef uint_least8_t uint_fast8_t; 195 #define __int_fast8_t_defined 1 196 #endif 197 #endif 198 199 #if !__int_fast16_t_defined 200 #if __int_least16_t_defined 201 typedef int_least16_t int_fast16_t; 202 typedef uint_least16_t uint_fast16_t; 203 #define __int_fast16_t_defined 1 204 #endif 205 #endif 206 207 #if !__int_fast32_t_defined 208 #if __int_least32_t_defined 209 typedef int_least32_t int_fast32_t; 210 typedef uint_least32_t uint_fast32_t; 211 #define __int_fast32_t_defined 1 212 #endif 213 #endif 214 215 #if !__int_fast64_t_defined 216 #if __int_least64_t_defined 217 typedef int_least64_t int_fast64_t; 218 typedef uint_least64_t uint_fast64_t; 219 #define __int_fast64_t_defined 1 220 #endif 221 #endif 222 223 /* Greatest-width integer types */ 224 /* Modern GCCs provide __INTMAX_TYPE__ */ 225 #if defined(__INTMAX_TYPE__) 226 typedef __INTMAX_TYPE__ intmax_t; 227 #elif __have_longlong64 228 typedef signed long long intmax_t; 229 #else 230 typedef signed long intmax_t; 231 #endif 232 233 /* Modern GCCs provide __UINTMAX_TYPE__ */ 234 #if defined(__UINTMAX_TYPE__) 235 typedef __UINTMAX_TYPE__ uintmax_t; 236 #elif __have_longlong64 237 typedef unsigned long long uintmax_t; 238 #else 239 typedef unsigned long uintmax_t; 240 #endif 241 242 /* 243 * GCC doesn't provide an appropriate macro for [u]intptr_t 244 * For now, use __PTRDIFF_TYPE__ 245 */ 246 #if defined(__PTRDIFF_TYPE__) 247 typedef signed __PTRDIFF_TYPE__ intptr_t; 248 typedef unsigned __PTRDIFF_TYPE__ uintptr_t; 249 #else 250 /* 251 * Fallback to hardcoded values, 252 * should be valid on cpu's with 32bit int/32bit void* 253 */ 254 typedef signed long intptr_t; 255 typedef unsigned long uintptr_t; 256 #endif 257 258 /* Limits of Specified-Width Integer Types */ 259 260 #if __int8_t_defined 261 #define INT8_MIN -128 262 #define INT8_MAX 127 263 #define UINT8_MAX 255 264 #endif 265 266 #if __int_least8_t_defined 267 #define INT_LEAST8_MIN -128 268 #define INT_LEAST8_MAX 127 269 #define UINT_LEAST8_MAX 255 270 #else 271 #error required type int_least8_t missing 272 #endif 273 274 #if __int16_t_defined 275 #define INT16_MIN -32768 276 #define INT16_MAX 32767 277 #define UINT16_MAX 65535 278 #endif 279 280 #if __int_least16_t_defined 281 #define INT_LEAST16_MIN -32768 282 #define INT_LEAST16_MAX 32767 283 #define UINT_LEAST16_MAX 65535 284 #else 285 #error required type int_least16_t missing 286 #endif 287 288 #if __int32_t_defined 289 #define INT32_MIN (-2147483647-1) 290 #define INT32_MAX 2147483647 291 #define UINT32_MAX 4294967295U 292 #endif 293 294 #if __int_least32_t_defined 295 #define INT_LEAST32_MIN (-2147483647-1) 296 #define INT_LEAST32_MAX 2147483647 297 #define UINT_LEAST32_MAX 4294967295U 298 #else 299 #error required type int_least32_t missing 300 #endif 301 302 #if __int64_t_defined 303 #if __have_long64 304 #define INT64_MIN (-9223372036854775807L-1L) 305 #define INT64_MAX 9223372036854775807L 306 #define UINT64_MAX 18446744073709551615U 307 #elif __have_longlong64 308 #define INT64_MIN (-9223372036854775807LL-1LL) 309 #define INT64_MAX 9223372036854775807LL 310 #define UINT64_MAX 18446744073709551615ULL 311 #endif 312 #endif 313 314 #if __int_least64_t_defined 315 #if __have_long64 316 #define INT_LEAST64_MIN (-9223372036854775807L-1L) 317 #define INT_LEAST64_MAX 9223372036854775807L 318 #define UINT_LEAST64_MAX 18446744073709551615U 319 #elif __have_longlong64 320 #define INT_LEAST64_MIN (-9223372036854775807LL-1LL) 321 #define INT_LEAST64_MAX 9223372036854775807LL 322 #define UINT_LEAST64_MAX 18446744073709551615ULL 323 #endif 324 #endif 325 326 #if __int_fast8_t_defined 327 #define INT_FAST8_MIN INT8_MIN 328 #define INT_FAST8_MAX INT8_MAX 329 #define UINT_FAST8_MAX UINT8_MAX 330 #endif 331 332 #if __int_fast16_t_defined 333 #define INT_FAST16_MIN INT16_MIN 334 #define INT_FAST16_MAX INT16_MAX 335 #define UINT_FAST16_MAX UINT16_MAX 336 #endif 337 338 #if __int_fast32_t_defined 339 #define INT_FAST32_MIN INT32_MIN 340 #define INT_FAST32_MAX INT32_MAX 341 #define UINT_FAST32_MAX UINT32_MAX 342 #endif 343 344 #if __int_fast64_t_defined 345 #define INT_FAST64_MIN INT64_MIN 346 #define INT_FAST64_MAX INT64_MAX 347 #define UINT_FAST64_MAX UINT64_MAX 348 #endif 349 350 /* This must match size_t in stddef.h, currently long unsigned int */ 351 #define SIZE_MIN (-__STDINT_EXP(LONG_MAX) - 1L) 352 #define SIZE_MAX __STDINT_EXP(LONG_MAX) 353 354 /* This must match sig_atomic_t in <signal.h> (currently int) */ 355 #define SIG_ATOMIC_MIN (-__STDINT_EXP(INT_MAX) - 1) 356 #define SIG_ATOMIC_MAX __STDINT_EXP(INT_MAX) 357 358 /* This must match ptrdiff_t in <stddef.h> (currently long int) */ 359 #define PTRDIFF_MIN (-__STDINT_EXP(LONG_MAX) - 1L) 360 #define PTRDIFF_MAX __STDINT_EXP(LONG_MAX) 361 362 /** Macros for minimum-width integer constant expressions */ 363 #define INT8_C(x) x 364 #define UINT8_C(x) x##U 365 366 #define INT16_C(x) x 367 #define UINT16_C(x) x##U 368 369 #if __have_long32 370 #define INT32_C(x) x##L 371 #define UINT32_C(x) x##UL 372 #else 373 #define INT32_C(x) x 374 #define UINT32_C(x) x##U 375 #endif 376 377 #if __int64_t_defined 378 #if __have_longlong64 379 #define INT64_C(x) x##LL 380 #define UINT64_C(x) x##ULL 381 #else 382 #define INT64_C(x) x##L 383 #define UINT64_C(x) x##UL 384 #endif 385 #endif 386 387 /** Macros for greatest-width integer constant expression */ 388 #if __have_longlong64 389 #define INTMAX_C(x) x##LL 390 #define UINTMAX_C(x) x##ULL 391 #else 392 #define INTMAX_C(x) x##L 393 #define UINTMAX_C(x) x##UL 394 #endif 395 396 397 #ifdef __cplusplus 398 } 399 #endif 400 401 #endif /* _STDINT_H */ 402