-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsize_t_aux.nw
236 lines (209 loc) · 5.39 KB
/
size_t_aux.nw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
References:
- https://stackoverflow.com/questions/44401965/how-to-get-size-max-in-c89
- https://stackoverflow.com/questions/34580472/alternative-to-ssize-t-on-posix-unconformant-systems
- https://www.cplusplus.com/reference/cinttypes/
- https://www.alphacodingskills.com/c/c-inttypes-h.php
- https://stackoverflow.com/questions/15610053/correct-printf-format-specifier-for-size-t-zu-or-iu
- https://stackoverflow.com/questions/2524611/how-can-one-print-a-size-t-variable-portably-using-the-printf-family
Minimal includes and compilers handling.
<<size_t_aux.h>>=
#include <limits.h>
#include <inttypes.h>
#ifdef _MSC_VER
#if (_MSC_VER >= 1100 && _MSC_VER < 1900)
/* MSVC++ 5.0, _MSC_VER == 1100, Visual Studio 5.0
* MSVC++ 14.0, _MSC_VER == 1900, Visual Studio 2015
* See also: https://github.com/MicrosoftDocs/cpp-docs/issues/1490
*/
#define __PRIuSIZE "Iu"
#define __PRIxSIZE "Ix"
#define __PRIXSIZE "IX"
#define __PRIdSIZE "Id"
#define __PRIiSIZE "Ii"
#else /* _MSC_VER >= 1900 */
#define __PRIuSIZE "zu"
#define __PRIxSIZE "zx"
#define __PRIXSIZE "zX"
#define __PRIdSIZE "zd"
#define __PRIiSIZE "zi"
#endif
#else /* not _MSC_VER */
#define __PRIuSIZE "zu"
#define __PRIxSIZE "zx"
#define __PRIXSIZE "zX"
#define __PRIdSIZE "zd"
#define __PRIiSIZE "zi"
#endif
@
Portable static assert.
<<size_t_aux.h>>=
#ifndef PASTE2
#define PASTE2(x,y) x ## y
#endif
#ifndef EXPAND_THEN_PASTE
#define EXPAND_THEN_PASTE(a, b) PASTE2(a,b)
#endif
#ifndef C89_STATIC_ASSERT
#define C89_STATIC_ASSERT(x, msg) typedef char \
EXPAND_THEN_PASTE(STATIC_ASSERT_FAIL_AT_LINE_,__LINE__) \
[x ? (int)sizeof(msg) : -1]
#endif
@
Nice tip learnt from [how-to-get-size-max-in-c89].
<<size_t_aux.h>>=
#ifndef SIZE_MAX
#define SIZE_MAX ((size_t)(-1))
#endif
@
Char/byte sanity check.
<<size_t_aux.h>>=
#if CHAR_MAX != 127
#error platform has exotic char type
#endif
#if CHAR_MIN != -128
#error platform has exotic char type
#endif
#if UCHAR_MAX != 255
#error platform has exotic unsigned char type
#endif
@
UINT_MAX?
<<size_t_aux.h>>=
#if SIZE_MAX == UINT_MAX
#ifndef SSIZE_T
#define SSIZE_T int
#endif
#ifndef SSIZE_MIN
#define SSIZE_MIN ((int)INT_MIN)
#endif
#ifndef SSIZE_MAX
#define SSIZE_MAX ((int)INT_MAX)
#endif
#define PRIz "i"
#if (INT_MAX>>16) == SHRT_MAX
#define halfsize_t unsigned short
#define halfssize_t short
#else
#error platform has exotic INT_MAX
#endif
@
ULONG_MAX?
<<size_t_aux.h>>=
#elif SIZE_MAX == ULONG_MAX
#ifndef SSIZE_T
#define SSIZE_T long
#endif
#ifndef SSIZE_MIN
#define SSIZE_MIN ((long)LONG_MIN)
#endif
#ifndef SSIZE_MAX
#define SSIZE_MAX ((long)LONG_MAX)
#endif
#define PRIz "li"
#if (LONG_MAX>>32) == INT_MAX
#define halfsize_t unsigned int
#define halfssize_t int
#else
#error platform has exotic LONG_MAX
#endif
@
ULLONG_MAX?
<<size_t_aux.h>>=
#elif SIZE_MAX == ULLONG_MAX
#ifndef SSIZE_T
#define SSIZE_T long long
#endif
#ifndef SSIZE_MIN
#define SSIZE_MIN ((long long)LLONG_MIN)
#endif
#ifndef SSIZE_MAX
#define SSIZE_MAX ((long long)LLONG_MAX)
#endif
#define PRIz "lli"
#if (LLONG_MAX>>32) == INT_MAX
#define halfsize_t unsigned int
#define halfssize_t int
#else
#error platform has exotic LLONG_MAX
#endif
@
USHRT_MAX?
<<size_t_aux.h>>=
#elif SIZE_MAX == USHRT_MAX
#ifndef SSIZE_T
#define SSIZE_T short
#endif
#ifndef SSIZE_MIN
#define SSIZE_MIN ((short)SHRT_MIN)
#endif
#ifndef SSIZE_MAX
#define SSIZE_MAX ((short)SHRT_MAX)
#endif
#define PRIz "i"
#if (USHRT_MAX>>8) == UCHAR_MAX
#define halfsize_t unsigned char
#define halfssize_t signed char
#else
#error platform has exotic USHRT_MAX
#endif
@
<<size_t_aux.h>>=
#else
#error platform has exotic SIZE_MAX
#endif
@
Detect bits and bytes. This is useful for preprocessor decisions, as sizeof
cannot be used in preprocessor.
<<size_t_aux.h>>=
#if SIZE_MAX == UINT32_MAX
#define SIZE_BITS 32
#define SIZE_BYTES 4
#elif SIZE_MAX == UINT64_MAX
#define SIZE_BITS 64
#define SIZE_BYTES 8
#else
#error platform has exotic SIZE_MAX
#endif
@
Some more checks.
<<size_t_aux.h>>=
C89_STATIC_ASSERT(((SSIZE_T)-1) < 0, "SSIZE_T is signed");
C89_STATIC_ASSERT(sizeof(SSIZE_T) >= sizeof(long), "SSIZE_T can hold a long");
C89_STATIC_ASSERT(sizeof(SSIZE_T) == sizeof(size_t), "SSIZE_T can hold a size_t and vice-versa");
@
A simple test program to validate.
<<test.c>>=
#include <stdio.h>
#include <stdlib.h>
#include "size_t_aux.h"
int
main(int argc, char **argv)
{
printf("__PRIuSIZE:\t\t\"%s\"\n", __PRIuSIZE);
printf("__PRIxSIZE:\t\t\"%s\"\n", __PRIxSIZE);
printf("__PRIXSIZE:\t\t\"%s\"\n", __PRIXSIZE);
printf("__PRIdSIZE:\t\t\"%s\"\n", __PRIdSIZE);
printf("__PRIiSIZE:\t\t\"%s\"\n", __PRIiSIZE);
printf(" SHRT_MAX:\t\t%i\n", SHRT_MAX);
printf("USHRT_MAX:\t\t%u\n", USHRT_MAX);
printf(" INT_MAX:\t\t%i\n", INT_MAX);
printf("UINT_MAX:\t\t%u\n", UINT_MAX);
printf(" SIZE_MAX:\t\t%"__PRIuSIZE" (u)\n", (size_t)SIZE_MAX);
printf(" SIZE_MAX:\t\t%"__PRIxSIZE" (x)\n", (size_t)SIZE_MAX);
printf(" SIZE_MAX:\t\t%"__PRIXSIZE" (X)\n", (size_t)SIZE_MAX);
printf("SSIZE_MAX:\t\t%"__PRIdSIZE" (d)\n", (SSIZE_T)SSIZE_MAX);
printf("SSIZE_MIN:\t\t%"__PRIdSIZE" (d)\n", (SSIZE_T)SSIZE_MIN);
printf("SSIZE_MAX:\t\t%"__PRIiSIZE" (i)\n", (SSIZE_T)SSIZE_MAX);
printf("SSIZE_MIN:\t\t%"__PRIiSIZE" (i)\n", (SSIZE_T)SSIZE_MIN);
printf(" SIZE_BITS:\t\t%u\n", SIZE_BITS);
printf(" SIZE_BYTES:\t\t%u\n", SIZE_BYTES);
#if SIZE_BYTES == 4
printf("Using %u-bit size_t.\n", SIZE_BITS);
#elif SIZE_BYTES == 8
printf("Using %u-bit size_t.\n", SIZE_BITS);
#else
printf("Using unusual %u bytes for size_t.\n", SIZE_BYTES);
#endif
return 0;
}
@