]> begriffs open source - libnonstd/blob - include/nonstd/integral.h
Emphasize the b?? types are bit patterns in output
[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 <limits.h>
25 #include <stddef.h>
26 #include <stdint.h>
27
28 /* Integer bit patterns
29  *
30  * Purpose: bitwise operations and modular arithmetic.
31  *
32  * 6.2.5
33  *
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.
38  *
39  * Each type "bN" is guaranteed to hold at least N bits.
40  *
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
43  * guarantees.
44  *
45  * 6.3.1.1
46  *
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.
49  */
50
51 typedef unsigned int       b16;
52 typedef unsigned long      b32;
53 typedef unsigned long long b64;
54
55 #define B16_MAX            UINT_MAX
56 #define B32_MAX            ULONG_MAX
57 #define B64_MAX            ULLONG_MAX
58
59 #define B16_C(n)           (n ## U)
60 #define B32_C(n)           (n ## UL)
61 #define B64_C(n)           (n ## ULL)
62
63 #define PRIxB16            "%x"
64 #define PRIxB32            "%lx"
65 #define PRIxB64            "%llx"
66
67 /* Regular (signed) integers
68  *
69  * Purpose: normal arithmetical operations and measurement, without modular
70  * arithmetic expectations.
71  *
72  * Care is required with these types to avoid overflow, which is undefined
73  * behavior.
74  *
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.
78  */
79
80 typedef int           i8;
81 typedef signed char   i8_trim;
82
83 typedef int           i16;
84 typedef short         i16_trim;
85
86 typedef long          i32;
87 typedef int_least32_t i32_trim;
88
89 typedef long long     i64;
90 typedef int_least64_t i64_trim;
91
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
108
109 #define I8_C(n)       (n)
110 #define I16_C(n)      (n)
111 #define I32_C(n)      (n ## L)
112 #define I64_C(n)      (n ## LL)
113
114 #define PRIdI8        "%d"
115 #define PRIdI8_TRIM   "%hh"
116 #define PRIdI16       "%d"
117 #define PRIdI16_TRIM  "%h"
118 #define PRIdI32       "%ld"
119 #define PRIdI32_TRIM  PRIdLEAST32
120 #define PRIdI64       "%lld"
121 #define PRIdI64_TRIM  PRIdLEAST64
122
123 /* Character types ********************************************************/
124
125 /* Character type 1: Smallest units of addressible storage.
126  *
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.
130  *
131  * 6.2.6.1
132  *
133  *      Values stored in unsigned bit-fields *and objects of type unsigned char*
134  *      shall be represented using a pure binary notation.
135  *
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
140  *      the value.
141  *
142  * 6.2.6.2
143  *
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).
147  */
148
149 typedef unsigned char raw_byte;
150
151 /* Character type 2: Codepoints in a character encoding.
152  *
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.
156  *
157  * - Note: pointers to native C string literals should be char*, as the
158  *   signedness of char is implementation-defined.
159  */
160
161 // No extra definitions for codepoints
162
163 /* Character type 3: Small integers.
164  *
165  * Already covered with i8 and i8_trim.
166  */
167
168 // No extra definitions for small integers
169
170 #endif