1 #ifndef NONSTD_INTEGRAL_H
2 #define NONSTD_INTEGRAL_H
4 /* The size of data objects.
6 * This is a numerical quantity, not a bit pattern. Thus it fits into the
7 * signed integer category. This notion conflicts with the definition of
8 * size_t. We provide a signed counterpart, with wrappers for sizeof() and
9 * offsetof() that do bounds checking and convert the result.
11 * My conjecture is that size_t was chosen to be unsigned to wring the most out
12 * of each available bit on e.g. segmented 16-bit memory architectures.
15 // ssize_t comes from special header built by configure script
16 #include "internal/ssize.h"
18 #define size2ssize(n) ((n) > (size_t)SSIZE_MAX ? -1 : (ssize_t)(n))
19 #define ssize2size(n) ((n) < 0 ? 0U : (size_t)(n))
21 #define ssizeof(x) size2ssize(sizeof(x))
22 #define soffsetof(t, m) size2ssize(offsetof((t), (m)))
29 /* Integer bit patterns
31 * Purpose: bitwise operations and modular arithmetic.
35 * A computation involving unsigned operands can never overflow, because a
36 * result that cannot be represented by the resulting unsigned integer type
37 * is reduced modulo the number that is one greater than the largest value
38 * that can be represented by the resulting type.
40 * Each type "bN" is guaranteed to hold at least N bits.
42 * However, the types must be at least the size of int, or else they will be
43 * silently converted to signed int in any expression, and lose the unsigned
48 * If an int can represent all values of the original type, the value is
49 * converted to an int; otherwise it is converted to an unsigned int.
52 typedef unsigned int b16;
53 typedef unsigned long b32;
54 typedef unsigned long long b64;
56 #define B16_MAX UINT_MAX
57 #define B32_MAX ULONG_MAX
58 #define B64_MAX ULLONG_MAX
60 #define B16_C(n) (n ## U)
61 #define B32_C(n) (n ## UL)
62 #define B64_C(n) (n ## ULL)
68 /* Regular (signed) integers
70 * Purpose: normal arithmetical operations and measurement, without modular
71 * arithmetic expectations.
73 * Care is required with these types to avoid overflow, which is undefined
76 * Each type "iN" is guaranteed to hold at least N bits, but may be wider if
77 * that would be faster for the processor. The "_trim" variety attempts to save
78 * space for e.g. big arrays.
82 typedef signed char i8_trim;
85 typedef short i16_trim;
88 typedef int_least32_t i32_trim;
90 typedef long long i64;
91 typedef int_least64_t i64_trim;
93 #define I8_MIN INT_MIN
94 #define I8_MAX INT_MAX
95 #define I8_TRIM_MIN ((i8_trim)SCHAR_MIN)
96 #define I8_TRIM_MAX ((i8_trim)SCHAR_MAX)
97 #define I16_MIN INT_MIN
98 #define I16_MAX INT_MAX
99 #define I16_TRIM_MIN ((i16_trim)SHRT_MIN)
100 #define I16_TRIM_MAX ((i16_trim)SHRT_MAX)
101 #define I32_MIN LONG_MIN
102 #define I32_MAX LONG_MAX
103 #define I32_TRIM_MIN INT_LEAST32_MIN
104 #define I32_TRIM_MAX INT_LEAST32_MAX
105 #define I64_MIN LLONG_MIN
106 #define I64_MAX LLONG_MAX
107 #define I64_TRIM_MIN INT_LEAST64_MIN
108 #define I64_TRIM_MAX INT_LEAST64_MAX
111 #define I8_TRIM_C(n) ((i8_trim)(n))
113 #define I16_TRIM_C(n) ((i16_trim)(n))
114 #define I32_C(n) (n ## L)
115 #define I32_TRIM_C(n) ((i32_trim)(n ## L))
116 #define I64_C(n) (n ## LL)
117 #define I64_TRIM_C(n) ((i64_trim)(n ## LL))
120 #define PRIdI8_TRIM "hhd"
122 #define PRIdI16_TRIM "hd"
124 #define PRIdI32_TRIM PRIdLEAST32
125 #define PRIdI64 "lld"
126 #define PRIdI64_TRIM PRIdLEAST64
128 /* Character types ********************************************************/
130 /* Character type 1: Smallest units of addressible storage.
132 * Purpose: load and store the raw aliased bytes of a C object. it's a rare
133 * situation, used inside functions like memcpy or fwrite. Also, due to signed
134 * integer promotion, this type shouldn't be used in arithmetic operations.
138 * Values stored in unsigned bit-fields *and objects of type unsigned char*
139 * shall be represented using a pure binary notation.
141 * Values stored in non-bit-field objects of any other object type consist of n
142 * x CHAR_BIT bits, where n is the size of an object of that type, in bytes.
143 * The value may be copied into an object of type unsigned char [n] (e.g., by
144 * memcpy); the resulting set of bytes is called the object representation of
149 * For unsigned integer types *other than unsigned char,* the bits of the
150 * object representation shall be divided into two groups: value bits and
151 * padding bits (there need not be any of the latter).
154 typedef unsigned char raw_byte;
156 /* Character type 2: Codepoints in a character encoding.
158 * The tasks of human language processing are too broad for a base language
159 * library, and the rules change as languages and locales do. Use a proper
160 * library for this, such as ICU, and its provided data types.
162 * - Note: pointers to native C string literals should be char*, as the
163 * signedness of char is implementation-defined.
166 // No extra definitions for codepoints
168 /* Character type 3: Small integers.
170 * Already covered with i8 and i8_trim.
173 // No extra definitions for small integers