| 1 | // Copyright 2008 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Basic integer type definitions for various platforms |
| 4 | // |
| 5 | // This code is compiled directly on many platforms, including client |
| 6 | // platforms like Windows, Mac, and embedded systems. Before making |
| 7 | // any changes here, make sure that you're not breaking any platforms. |
| 8 | // |
| 9 | |
| 10 | #ifndef BASE_INT_TYPES_H_ |
| 11 | #define BASE_INT_TYPES_H_ |
| 12 | |
| 13 | // These typedefs are also defined in base/google.swig. In the |
| 14 | // SWIG environment, we use those definitions and avoid duplicate |
| 15 | // definitions here with an ifdef. The definitions should be the |
| 16 | // same in both files, and ideally be only defined in this file. |
| 17 | #ifndef SWIG |
| 18 | // Standard typedefs |
| 19 | // All Google2 code is compiled with -funsigned-char to make "char" |
| 20 | // unsigned. Google2 code therefore doesn't need a "uchar" type. |
| 21 | typedef signed char schar; |
| 22 | typedef signed char int8; |
| 23 | typedef short int16; |
| 24 | typedef int int32; |
| 25 | #ifdef COMPILER_MSVC |
| 26 | typedef __int64 int64; |
| 27 | #else |
| 28 | typedef long long int64; |
| 29 | #endif /* COMPILER_MSVC */ |
| 30 | |
| 31 | // NOTE: unsigned types are DANGEROUS in loops and other arithmetical |
| 32 | // places. Use the signed types unless your variable represents a bit |
| 33 | // pattern (eg a hash value) or you really need the extra bit. Do NOT |
| 34 | // use 'unsigned' to express "this value should always be positive"; |
| 35 | // use assertions for this. |
| 36 | |
| 37 | typedef unsigned char uint8; |
| 38 | typedef unsigned short uint16; |
| 39 | typedef unsigned int uint32; |
| 40 | #ifdef COMPILER_MSVC |
| 41 | typedef unsigned __int64 uint64; |
| 42 | #else |
| 43 | typedef unsigned long long uint64; |
| 44 | #endif /* COMPILER_MSVC */ |
| 45 | |
| 46 | // A type to represent a Unicode code-point value. As of Unicode 4.0, |
| 47 | // such values require up to 21 bits. |
| 48 | // (For type-checking on pointers, make this explicitly signed, |
| 49 | // and it should always be the signed version of whatever int32 is.) |
| 50 | typedef signed int char32; |
| 51 | |
| 52 | // A type to represent a natural machine word (for e.g. efficiently |
| 53 | // scanning through memory for checksums or index searching). Don't use |
| 54 | // this for storing normal integers. Ideally this would be just |
| 55 | // unsigned int, but our 64-bit architectures use the LP64 model |
| 56 | // (http://www.opengroup.org/public/tech/aspen/lp64_wp.htm), hence |
| 57 | // their ints are only 32 bits. We want to use the same fundamental |
| 58 | // type on all archs if possible to preserve *printf() compatability. |
| 59 | typedef unsigned long uword_t; |
| 60 | |
| 61 | // A signed natural machine word. In general you want to use "int" |
| 62 | // rather than "sword_t" |
| 63 | typedef long sword_t; |
| 64 | |
| 65 | #endif /* SWIG */ |
| 66 | |
| 67 | // long long macros to be used because gcc and vc++ use different suffixes, |
| 68 | // and different size specifiers in format strings |
| 69 | #undef GG_LONGLONG |
| 70 | #undef GG_ULONGLONG |
| 71 | #undef GG_LL_FORMAT |
| 72 | |
| 73 | #ifdef COMPILER_MSVC /* if Visual C++ */ |
| 74 | |
| 75 | // VC++ long long suffixes |
| 76 | #define GG_LONGLONG(x) x##I64 |
| 77 | #define GG_ULONGLONG(x) x##UI64 |
| 78 | |
| 79 | // Length modifier in printf format string for int64's (e.g. within %d) |
| 80 | #define GG_LL_FORMAT "I64" // As in printf("%I64d", ...) |
| 81 | #define GG_LL_FORMAT_W L"I64" |
| 82 | |
| 83 | #else /* not Visual C++ */ |
| 84 | |
| 85 | #define GG_LONGLONG(x) x##LL |
| 86 | #define GG_ULONGLONG(x) x##ULL |
| 87 | #define GG_LL_FORMAT "ll" // As in "%lld". Note that "q" is poor form also. |
| 88 | #define GG_LL_FORMAT_W L"ll" |
| 89 | |
| 90 | #endif // COMPILER_MSVC |
| 91 | |
| 92 | |
| 93 | static const uint8 kuint8max = (( uint8) 0xFF); |
| 94 | static const uint16 kuint16max = ((uint16) 0xFFFF); |
| 95 | static const uint32 kuint32max = ((uint32) 0xFFFFFFFF); |
| 96 | static const uint64 kuint64max = ((uint64) GG_LONGLONG(0xFFFFFFFFFFFFFFFF)); |
| 97 | static const int8 kint8min = (( int8) 0x80); |
| 98 | static const int8 kint8max = (( int8) 0x7F); |
| 99 | static const int16 kint16min = (( int16) 0x8000); |
| 100 | static const int16 kint16max = (( int16) 0x7FFF); |
| 101 | static const int32 kint32min = (( int32) 0x80000000); |
| 102 | static const int32 kint32max = (( int32) 0x7FFFFFFF); |
| 103 | static const int64 kint64min = (( int64) GG_LONGLONG(0x8000000000000000)); |
| 104 | static const int64 kint64max = (( int64) GG_LONGLONG(0x7FFFFFFFFFFFFFFF)); |
| 105 | |
| 106 | |
| 107 | #endif // BASE_INT_TYPES_H_ |
| 108 | |