]> begriffs open source - libnonstd/blob - include/nonstd/integral.h
Test the types with printf
[libnonstd] / include / nonstd / integral.h
1 #ifndef NONSTD_INTEGRAL_H
2 #define NONSTD_INTEGRAL_H
3
4 /* The size of data objects.
5  *
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.
10  *
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.
13  */
14
15 // ssize_t comes from special header built by configure script
16 #include "internal/ssize.h"
17
18 #define size2ssize(n) ((n) > (size_t)SSIZE_MAX ? -1 : (ssize_t)(n))
19 #define ssize2size(n) ((n) < 0                 ? 0U : (size_t)(n))
20
21 #define ssizeof(x) size2ssize(sizeof(x))
22 #define soffsetof(t, m) size2ssize(offsetof((t), (m)))
23
24 #include <inttypes.h>
25 #include <limits.h>
26 #include <stddef.h>
27 #include <stdint.h>
28
29 /* Integer bit patterns
30  *
31  * Purpose: bitwise operations and modular arithmetic.
32  *
33  * 6.2.5
34  *
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.
39  *
40  * Each type "bN" is guaranteed to hold at least N bits.
41  *
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
44  * guarantees.
45  *
46  * 6.3.1.1
47  *
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.
50  */
51
52 typedef unsigned int       b16;
53 typedef unsigned long      b32;
54 typedef unsigned long long b64;
55
56 #define B16_MAX            UINT_MAX
57 #define B32_MAX            ULONG_MAX
58 #define B64_MAX            ULLONG_MAX
59
60 #define B16_C(n)           (n ## U)
61 #define B32_C(n)           (n ## UL)
62 #define B64_C(n)           (n ## ULL)
63
64 #define PRIxB16            "x"
65 #define PRIxB32            "lx"
66 #define PRIxB64            "llx"
67
68 /* Regular (signed) integers
69  *
70  * Purpose: normal arithmetical operations and measurement, without modular
71  * arithmetic expectations.
72  *
73  * Care is required with these types to avoid overflow, which is undefined
74  * behavior.
75  *
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.
79  */
80
81 typedef int           i8;
82 typedef signed char   i8_trim;
83
84 typedef int           i16;
85 typedef short         i16_trim;
86
87 typedef long          i32;
88 typedef int_least32_t i32_trim;
89
90 typedef long long     i64;
91 typedef int_least64_t i64_trim;
92
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
109
110 #define I8_C(n)       (n)
111 #define I8_TRIM_C(n)  ((i8_trim)(n))
112 #define I16_C(n)      (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))
118
119 #define PRIdI8        "d"
120 #define PRIdI8_TRIM   "hhd"
121 #define PRIdI16       "d"
122 #define PRIdI16_TRIM  "hd"
123 #define PRIdI32       "ld"
124 #define PRIdI32_TRIM  PRIdLEAST32
125 #define PRIdI64       "lld"
126 #define PRIdI64_TRIM  PRIdLEAST64
127
128 /* Character types ********************************************************/
129
130 /* Character type 1: Smallest units of addressible storage.
131  *
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.
135  *
136  * 6.2.6.1
137  *
138  *      Values stored in unsigned bit-fields *and objects of type unsigned char*
139  *      shall be represented using a pure binary notation.
140  *
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
145  *      the value.
146  *
147  * 6.2.6.2
148  *
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).
152  */
153
154 typedef unsigned char raw_byte;
155
156 /* Character type 2: Codepoints in a character encoding.
157  *
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.
161  *
162  * - Note: pointers to native C string literals should be char*, as the
163  *   signedness of char is implementation-defined.
164  */
165
166 // No extra definitions for codepoints
167
168 /* Character type 3: Small integers.
169  *
170  * Already covered with i8 and i8_trim.
171  */
172
173 // No extra definitions for small integers
174
175 #endif