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)))
28 /* Integer bit patterns
30 * Purpose: bitwise operations and modular arithmetic.
34 * A computation involving unsigned operands can never overflow, because a
35 * result that cannot be represented by the resulting unsigned integer type
36 * is reduced modulo the number that is one greater than the largest value
37 * that can be represented by the resulting type.
39 * Each type "bN" is guaranteed to hold at least N bits.
41 * However, the types must be at least the size of int, or else they will be
42 * silently converted to signed int in any expression, and lose the unsigned
47 * If an int can represent all values of the original type, the value is
48 * converted to an int; otherwise it is converted to an unsigned int.
51 typedef unsigned int b16;
52 typedef unsigned long b32;
53 typedef unsigned long long b64;
55 #define B16_MAX UINT_MAX
56 #define B32_MAX ULONG_MAX
57 #define B64_MAX ULLONG_MAX
59 #define B16_C(n) (n ## U)
60 #define B32_C(n) (n ## UL)
61 #define B64_C(n) (n ## ULL)
65 #define PRIxB64 "%llx"
67 /* Regular (signed) integers
69 * Purpose: normal arithmetical operations and measurement, without modular
70 * arithmetic expectations.
72 * Care is required with these types to avoid overflow, which is undefined
75 * Each type "iN" is guaranteed to hold at least N bits, but may be wider if
76 * that would be faster for the processor. The "_trim" variety attempts to save
77 * space for e.g. big arrays.
81 typedef signed char i8_trim;
84 typedef short i16_trim;
87 typedef int_least32_t i32_trim;
89 typedef long long i64;
90 typedef int_least64_t i64_trim;
92 #define I8_MIN INT_MIN
93 #define I8_MAX INT_MAX
94 #define I8_TRIM_MIN SCHAR_MIN
95 #define I8_TRIM_MAX SCHAR_MAX
96 #define I16_MIN INT_MIN
97 #define I16_MAX INT_MAX
98 #define I16_TRIM_MIN SHRT_MIN
99 #define I16_TRIM_MAX SHRT_MAX
100 #define I32_MIN LONG_MIN
101 #define I32_MAX LONG_MAX
102 #define I32_TRIM_MIN INT_LEAST32_MIN
103 #define I32_TRIM_MAX INT_LEAST32_MAX
104 #define I64_MIN LLONG_MIN
105 #define I64_MAX LLONG_MAX
106 #define I64_TRIM_MIN INT_LEAST64_MIN
107 #define I64_TRIM_MAX INT_LEAST64_MAX
111 #define I32_C(n) (n ## L)
112 #define I64_C(n) (n ## LL)
115 #define PRIdI8_TRIM "%hh"
117 #define PRIdI16_TRIM "%h"
118 #define PRIdI32 "%ld"
119 #define PRIdI32_TRIM PRIdLEAST32
120 #define PRIdI64 "%lld"
121 #define PRIdI64_TRIM PRIdLEAST64
123 /* Character types ********************************************************/
125 /* Character type 1: Smallest units of addressible storage.
127 * Purpose: load and store the raw aliased bytes of a C object. it's a rare
128 * situation, used inside functions like memcpy or fwrite. Also, due to signed
129 * integer promotion, this type shouldn't be used in arithmetic operations.
133 * Values stored in unsigned bit-fields *and objects of type unsigned char*
134 * shall be represented using a pure binary notation.
136 * Values stored in non-bit-field objects of any other object type consist of n
137 * x CHAR_BIT bits, where n is the size of an object of that type, in bytes.
138 * The value may be copied into an object of type unsigned char [n] (e.g., by
139 * memcpy); the resulting set of bytes is called the object representation of
144 * For unsigned integer types *other than unsigned char,* the bits of the
145 * object representation shall be divided into two groups: value bits and
146 * padding bits (there need not be any of the latter).
149 typedef unsigned char raw_byte;
151 /* Character type 2: Codepoints in a character encoding.
153 * The tasks of human language processing are too broad for a base language
154 * library, and the rules change as languages and locales do. Use a proper
155 * library for this, such as ICU, and its provided data types.
157 * - Note: pointers to native C string literals should be char*, as the
158 * signedness of char is implementation-defined.
161 // No extra definitions for codepoints
163 /* Character type 3: Small integers.
165 * Already covered with i8 and i8_trim.
168 // No extra definitions for small integers