|
|
1.1 ! root 1: #ifndef __COMMON__LIMITS_H__ ! 2: #define __COMMON__LIMITS_H__ ! 3: ! 4: /* ! 5: * This header file is the internal equivalent of the ISO C Standard header ! 6: * file <limits.h>. It defines many similar constants that are useful in ! 7: * system headers for parameterising definitions based on such things as ! 8: * the representation ranges of various types, but the names of the constants ! 9: * are prefixed with underscores to stay in the implementation namespace. ! 10: * ! 11: * Rather than just stating the constants, we calculate then from a small ! 12: * set of fundamental parameters. Doing it this way will give the right ! 13: * results as long as your preprocessor and compiler understand that ISO C ! 14: * type rules. If they don't, well, this will tell you that. ! 15: * ! 16: * Note that the "fundamental" constants can be determined at run-time in ! 17: * a portable fashion, but since we need them for the preprocessor we must ! 18: * depend on someone running such a program and supplying them. ! 19: * ! 20: * The fundamental parameters: ! 21: * __CHAR_BIT The number of bits in a character. While most systems ! 22: * built in the 1990s have 8, others are possible. ! 23: * __SHORT_DIV The factor by which to divide the number of bits in ! 24: * an integer by to give the number of bits in a short. ! 25: * __TWOSCOMP This contains 1 if the representation is twos- ! 26: * complement for negative integers, 0 if we have a ! 27: * sign-magnitude or ones-complement machine. ! 28: * __CHAR_SIGNED 1 if characters are signed, undefined if they are ! 29: * unsigned. ! 30: * ! 31: * Parameters that can be inferred from the type system: ! 32: * __INT_BIT The number of bits in an integer. ! 33: * __LONG_BIT The number of bits in a long. ! 34: */ ! 35: ! 36: #include <common/feature.h> ! 37: ! 38: #if __COHERENT__ || __BORLANDC__ || defined (GNUDOS) ! 39: ! 40: # define __CHAR_BIT 8 ! 41: # define __TWOSCOMP 1 ! 42: ! 43: #if '\x80' < 0 ! 44: # define __CHAR_SIGNED 1 ! 45: #endif ! 46: ! 47: # if __BORLANDC__ ! 48: # define __SHORT_DIV 1 ! 49: # else ! 50: # define __SHORT_DIV 2 ! 51: # endif ! 52: ! 53: #else ! 54: ! 55: # error The fundamental parameters of this system are not known. ! 56: ! 57: #endif ! 58: ! 59: #define __TOP_BIT_INT(bytes) (1U << (__CHAR_BIT * (bytes) - 1)) ! 60: #define __TOP_BIT_LONG(bytes) (1UL << (__CHAR_BIT * (bytes) - 1)) ! 61: ! 62: #if (~ 0U ^ (~ 0U / 2)) == __TOP_BIT_INT(1) ! 63: # define __INT_BIT (1 * __CHAR_BIT) ! 64: #elif (~ 0U ^ (~ 0U / 2)) == __TOP_BIT_INT(2) ! 65: # define __INT_BIT (2 * __CHAR_BIT) ! 66: #elif (~ 0U ^ (~ 0U / 2)) == __TOP_BIT_INT(4) ! 67: # define __INT_BIT (4 * __CHAR_BIT) ! 68: #elif (~ 0U ^ (~ 0U / 2)) == __TOP_BIT_INT(8) ! 69: # define __INT_BIT (8 * __CHAR_BIT) ! 70: #else ! 71: # error Cannot determine number of bits per int. ! 72: #endif ! 73: ! 74: #if (~ 0UL ^ (~ 0UL / 2)) == __TOP_BIT_LONG(1) ! 75: # define __LONG_BIT (1 * __CHAR_BIT) ! 76: #elif (~ 0UL ^ (~ 0UL / 2)) == __TOP_BIT_LONG(2) ! 77: # define __LONG_BIT (2 * __CHAR_BIT) ! 78: #elif (~ 0UL ^ (~ 0UL / 2)) == __TOP_BIT_LONG(4) ! 79: # define __LONG_BIT (4 * __CHAR_BIT) ! 80: #elif (~ 0UL ^ (~ 0UL / 2)) == __TOP_BIT_LONG(8) ! 81: # define __LONG_BIT (8 * __CHAR_BIT) ! 82: #else ! 83: # error Cannot determine number of bits per long. ! 84: #endif ! 85: ! 86: #define __SHRT_BIT (__INT_BIT / __SHORT_DIV) ! 87: ! 88: #define __UCHAR_MAX ((1U << (__CHAR_BIT - 1)) + \ ! 89: ((1U << (__CHAR_BIT - 1)) - 1)) ! 90: #define __SCHAR_MAX ((1 << (__CHAR_BIT - 2)) + \ ! 91: ((1 << (__CHAR_BIT - 2)) - 1)) ! 92: #define __SCHAR_MIN (- __SCHAR_MAX - __TWOSCOMP) ! 93: ! 94: #if __CHAR_SIGNED ! 95: # define __CHAR_MAX __SCHAR_MAX ! 96: # define __CHAR_MIN __SCHAR_MIN ! 97: #else ! 98: # define __CHAR_MAX __UCHAR_MAX ! 99: # define __CHAR_MIN 0 ! 100: #endif ! 101: ! 102: #define __USHRT_MAX ((1U << (__SHRT_BIT - 1)) + \ ! 103: ((1U << (__SHRT_BIT - 1)) - 1)) ! 104: #define __SHRT_MAX ((1 << (__SHRT_BIT - 2)) + \ ! 105: ((1 << (__SHRT_BIT - 2)) - 1)) ! 106: #define __SHRT_MIN (- __SHRT_MAX - __TWOSCOMP) ! 107: ! 108: #define __UINT_MAX ((1U << (__INT_BIT - 1)) + \ ! 109: ((1U << (__INT_BIT - 1)) - 1)) ! 110: #define __INT_MAX ((1 << (__INT_BIT - 2)) + \ ! 111: ((1 << (__INT_BIT - 2)) - 1)) ! 112: #define __INT_MIN (- __INT_MAX - __TWOSCOMP) ! 113: ! 114: #define __ULONG_MAX ((1U << (__LONG_BIT - 1)) + \ ! 115: ((1U << (__LONG_BIT - 1)) - 1)) ! 116: #define __LONG_MAX ((1 << (__LONG_BIT - 2)) + \ ! 117: ((1 << (__LONG_BIT - 2)) - 1)) ! 118: #define __LONG_MIN (- __LONG_MAX - __TWOSCOMP) ! 119: ! 120: ! 121: /* ! 122: * A fequent use of all the above shenanigans is to figure out a mapping from ! 123: * the C data types to machine 8/16/32-bit specific idioms, where the actual ! 124: * assignment of data types to machine widths is highly variable (perhaps even ! 125: * controlled by a compiler switch). ! 126: * ! 127: * Here we create standard aliases for the 8-bit-multiple cases, which careful ! 128: * programs can use token-pasting with to further parameterize things. If ! 129: * other macros use the __CONCAT... () facility from <sys/ccompat.h> to create ! 130: * type-encoded macro names, the extra level of indirection in the ! 131: * preprocessor that comes from using a concatenation macro will cause the ! 132: * following substitutions to be made before the final token is created, thus ! 133: * automagically mapping from types into word sizes. ! 134: * ! 135: * Because the following information is mainly used in token-pasting, please ! 136: * leave the decimal constants alone! ! 137: */ ! 138: ! 139: #if __CHAR_BIT == 8 ! 140: # define __CHAR 8 ! 141: #endif ! 142: ! 143: #if __SHRT_BIT == 16 ! 144: # define __SHORT 16 ! 145: #elif __SHRT_BIT == 32 ! 146: # define __SHORT 32 ! 147: #endif ! 148: ! 149: #if __INT_BIT == 16 ! 150: # define __INT 16 ! 151: #elif __INT_BIT == 32 ! 152: # define __INT 32 ! 153: #elif __INT_BIT == 64 ! 154: # define __INT 64 ! 155: #endif ! 156: ! 157: #if __LONG_BIT == 32 ! 158: # define __LONG 32 ! 159: #elif __LONG_BIT == 64 ! 160: # define __LONG 64 ! 161: #endif ! 162: ! 163: #endif /* ! defined (__COMMON__LIMITS_H__) */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.