comparison paper-stdint.h @ 131:35580b661882 default tip

clump it
author Paper <paper@tflc.us>
date Mon, 17 Nov 2025 21:40:24 -0500
parents
children
comparison
equal deleted inserted replaced
130:7b9795a60e59 131:35580b661882
1 /* paper-stdint.h -- replacement stdint.h that does as much as it can
2 * without outside help
3 *
4 * Copyright (C) 2025 Paper
5 *
6 * This software is provided 'as-is', without any express or implied
7 * warranty. In no event will the authors be held liable for any damages
8 * arising from the use of this software.
9 *
10 * Permission is granted to anyone to use this software for any purpose,
11 * including commercial applications, and to alter it and redistribute it
12 * freely, subject to the following restrictions:
13 *
14 * 1. The origin of this software must not be misrepresented; you must not
15 * claim that you wrote the original software. If you use this software
16 * in a product, an acknowledgment in the product documentation would be
17 * appreciated but is not required.
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 * 3. This notice may not be removed or altered from any source distribution.
21 */
22
23 /**
24 * This is a drop-in replacement stdint.h that provides a
25 * subset of the features that the C99 equivalent provides.
26 *
27 * There are some caveats:
28 * - [u]intptr_t is not here. There's no way to do it portably.
29 * Thus INTPTR_MAX and friends are not here either. It may
30 * be possible to do it with __LP64__ or something, but that
31 * really isn't portable either :(
32 * - ptrdiff_t is not here.
33 * - int64_t relies on platform-specific extensions, or C99
34 * long long. Add your platform for funsies!
35 * - SIZE_MAX cannot be done portably. The closest thing we have
36 * is (~(size_t)0), which does not work in preprocessor
37 * expressions.
38 * - WINT_MIN cannot be done portably, nor can WCHAR_MIN,
39 * WINT_MAX, or WCHAR_MAX.
40 * - I have not implemented scanf specifiers. I don't use
41 * them, and neither should you. (the real reason is that
42 * "%hhd" was only added in C99).
43 *
44 * Maybe there's just something I missed. As far as I know
45 * these are impossible to do without compiler-specific
46 * extensions. --paper
47 **/
48
49 #ifndef PAPER_STDINT_H_
50 #define PAPER_STDINT_H_
51
52 /* need limits.h for INT_MIN etc */
53 #include <limits.h>
54
55 #define INT8_MAX 0x7F
56 #define INT8_MIN (-INT8_MAX - 1)
57 #define INT16_MAX 0x7FFF
58 #define INT16_MIN (-INT16_MAX - 1)
59 #define INT32_MAX 0x7FFFFFFF
60 #define INT32_MIN (-INT32_MAX - 1)
61 #define INT64_MAX 0x7FFFFFFFFFFFFFFF
62 #define INT64_MIN (-INT64_MAX - 1)
63 #define UINT8_MAX 0xFFu
64 #define UINT16_MAX 0xFFFFu
65 #define UINT32_MAX 0xFFFFFFFFu
66 #define UINT64_MAX 0xFFFFFFFFFFFFFFFFu
67
68 /* ?? */
69 #ifndef SIZE_MAX
70 # define SIZE_MAX (~(size_t)0)
71 #endif
72
73 #if UCHAR_MAX == UINT8_MAX
74 typedef unsigned char uint8_t;
75 # define PRIu8 "u"
76 # define PRIo8 "o"
77 # define PRIx8 "x"
78 # define PRIX8 "X"
79 # define UINT8_C(x) x ## U
80 #else
81 # error "no unsigned 8-bit type"
82 #endif
83
84 #if UCHAR_MAX == UINT16_MAX
85 typedef unsigned char uint16_t;
86 # define PRIu16 "u"
87 # define PRIo16 "o"
88 # define PRIx16 "x"
89 # define PRIX16 "X"
90 # define UINT16_C(x) x ## U
91 #elif USHRT_MAX == UINT16_MAX
92 typedef unsigned short uint16_t;
93 # define PRIu16 "u"
94 # define PRIo16 "o"
95 # define PRIx16 "x"
96 # define PRIX16 "X"
97 # define UINT16_C(x) x ## U
98 #elif UINT_MAX == UINT16_MAX
99 typedef unsigned int uint16_t;
100 # define PRIu16 "u"
101 # define PRIo16 "o"
102 # define PRIx16 "x"
103 # define PRIX16 "X"
104 # define UINT16_C(x) x ## U
105 #else
106 # error "no unsigned 16-bit type"
107 #endif
108
109 #if UCHAR_MAX == UINT32_MAX
110 typedef unsigned char uint32_t;
111 # define PRIu32 "u"
112 # define PRIo32 "o"
113 # define PRIx32 "x"
114 # define PRIX32 "X"
115 # define UINT32_C(x) x ## U
116 #elif USHRT_MAX == UINT32_MAX
117 typedef unsigned short uint32_t;
118 # define PRIu32 "u"
119 # define PRIo32 "o"
120 # define PRIx32 "x"
121 # define PRIX32 "X"
122 # define UINT32_C(x) x ## U
123 #elif UINT_MAX == UINT32_MAX
124 typedef unsigned int uint32_t;
125 # define PRIu32 "u"
126 # define PRIo32 "o"
127 # define PRIx32 "x"
128 # define PRIX32 "X"
129 # define UINT32_C(x) x ## U
130 #elif ULONG_MAX == UINT32_MAX
131 typedef unsigned long uint32_t;
132 # define PRIu32 "lu"
133 # define PRIo32 "lo"
134 # define PRIx32 "lx"
135 # define PRIX32 "lX"
136 # define UINT32_C(x) x ## UL
137 #else
138 # error "no unsigned 32-bit type"
139 #endif
140
141 #if UCHAR_MAX == UINT64_MAX
142 typedef unsigned char uint64_t;
143 # define PRIu64 "u"
144 # define PRIo64 "o"
145 # define PRIx64 "x"
146 # define PRIX64 "X"
147 # define UINT64_C(x) x ## U
148 #elif USHRT_MAX == UINT64_MAX
149 typedef unsigned short uint64_t;
150 # define PRIu64 "u"
151 # define PRIo64 "o"
152 # define PRIx64 "x"
153 # define PRIX64 "X"
154 # define UINT64_C(x) x ## U
155 #elif UINT_MAX == UINT64_MAX
156 typedef unsigned int uint64_t;
157 # define PRIu64 "u"
158 # define PRIo64 "o"
159 # define PRIx64 "x"
160 # define PRIX64 "X"
161 # define UINT64_C(x) x ## U
162 #elif ULONG_MAX == UINT64_MAX
163 typedef unsigned long uint64_t;
164 # define PRIu64 "lu"
165 # define PRIo64 "lo"
166 # define PRIx64 "lx"
167 # define PRIX64 "lX"
168 # define UINT64_C(x) x ## UL
169 #elif defined(_MSC_VER)
170 typedef unsigned __int64 uint64_t;
171 # define PRIu64 "I64u"
172 # define PRIo64 "I64o"
173 # define PRIx64 "I64x"
174 # define PRIX64 "I64X"
175 # define UINT64_C(x) x ## Ui64
176 #elif ULLONG_MAX == UINT64_MAX
177 /* C99 */
178 typedef unsigned long long uint64_t;
179 # define PRIu64 "llu"
180 # define PRIo64 "llo"
181 # define PRIx64 "llx"
182 # define PRIX64 "llX"
183 # define UINT64_C(x) x ## ULL
184 #else
185 # error "no unsigned 64-bit type"
186 #endif
187
188 #if SCHAR_MIN == INT8_MIN && SCHAR_MAX == INT8_MAX
189 typedef signed char int8_t;
190 # define PRId8 "d"
191 # define INT8_C(x) x
192 #else
193 # error "no signed 8-bit type"
194 #endif
195
196 #if SCHAR_MIN == INT16_MIN && SCHAR_MAX == INT16_MAX
197 typedef signed char int16_t;
198 # define PRId16 "d"
199 # define INT16_C(x) x
200 #elif SHRT_MIN == INT16_MIN && SHRT_MAX == INT16_MAX
201 typedef signed short int16_t;
202 # define PRId16 "d"
203 # define INT16_C(x) x
204 #elif INT_MIN == INT16_MIN && INT_MAX == INT16_MAX
205 typedef signed int int16_t;
206 # define PRId16 "d"
207 # define INT16_C(x) x
208 #else
209 # error "no signed 16-bit type"
210 #endif
211
212 #if SCHAR_MIN == INT32_MIN && SCHAR_MAX == INT32_MAX
213 typedef signed char int32_t;
214 # define PRId32 "d"
215 # define INT32_C(x) x
216 #elif SHRT_MIN == INT32_MIN && SHRT_MAX == INT32_MAX
217 typedef signed short int32_t;
218 # define PRId32 "d"
219 # define INT32_C(x) x
220 #elif INT_MIN == INT32_MIN && INT_MAX == INT32_MAX
221 typedef signed int int32_t;
222 # define PRId32 "d"
223 # define INT32_C(x) x
224 #elif LONG_MIN == INT32_MIN && LONG_MAX == INT32_MAX
225 typedef signed long int32_t;
226 # define PRId32 "ld"
227 # define INT32_C(x) x ## L
228 #else
229 # error "no signed 32-bit type"
230 #endif
231
232 #if SCHAR_MIN == INT64_MIN && SCHAR_MAX == INT64_MAX
233 typedef signed char int64_t;
234 # define PRId64 "d"
235 # define INT64_C(x) x
236 #elif SHRT_MIN == INT64_MIN && SHRT_MAX == INT64_MAX
237 typedef signed short int64_t;
238 # define PRId64 "d"
239 # define INT64_C(x) x
240 #elif INT_MIN == INT64_MIN && INT_MAX == INT64_MAX
241 typedef signed int int64_t;
242 # define PRId64 "d"
243 # define INT64_C(x) x
244 #elif LONG_MIN == INT64_MIN && LONG_MAX == INT64_MAX
245 typedef signed long int64_t;
246 # define PRId64 "ld"
247 # define INT64_C(x) x ## L
248 #elif defined(_MSC_VER)
249 typedef signed __int64 int64_t;
250 # define PRId64 "I64d"
251 # define INT64_C(x) x ## i64
252 #elif LLONG_MIN == INT64_MIN && LLONG_MAX == INT64_MAX
253 typedef signed long long int64_t;
254 # define PRId64 "lld"
255 # define INT64_C(x) x ## LL
256 #else
257 # error "no signed 64-bit type"
258 #endif
259
260 #define PRIi8 PRId8
261 #define PRIi16 PRId16
262 #define PRIi32 PRId32
263 #define PRIi64 PRId64
264
265 /* -------------------------------------------------------------------------------- */
266 /* least types */
267
268 typedef int8_t int_least8_t;
269 typedef int16_t int_least16_t;
270 typedef int32_t int_least32_t;
271 typedef int64_t int_least64_t;
272
273 #define INT_LEAST8_MIN INT8_MIN
274 #define INT_LEAST8_MAX INT8_MAX
275 #define INT_LEAST16_MIN INT16_MIN
276 #define INT_LEAST16_MAX INT16_MAX
277 #define INT_LEAST32_MIN INT32_MIN
278 #define INT_LEAST32_MAX INT32_MAX
279 #define INT_LEAST64_MIN INT64_MIN
280 #define INT_LEAST64_MAX INT64_MAX
281
282 #define PRIdLEAST8 PRId8
283 #define PRIdLEAST16 PRId16
284 #define PRIdLEAST32 PRId32
285 #define PRIdLEAST64 PRId64
286 #define PRIxLEAST8 PRIx8
287 #define PRIxLEAST16 PRIx16
288 #define PRIxLEAST32 PRIx32
289 #define PRIxLEAST64 PRIx64
290 #define PRIoLEAST8 PRIo8
291 #define PRIoLEAST16 PRIo16
292 #define PRIoLEAST32 PRIo32
293 #define PRIoLEAST64 PRIo64
294 #define PRIiLEAST8 PRIi8
295 #define PRIiLEAST16 PRIi16
296 #define PRIiLEAST32 PRIi32
297 #define PRIiLEAST64 PRIi64
298 #define PRIXLEAST8 PRIX8
299 #define PRIXLEAST16 PRIX16
300 #define PRIXLEAST32 PRIX32
301 #define PRIXLEAST64 PRIX64
302 #define PRIuLEAST8 PRIu8
303 #define PRIuLEAST16 PRIu16
304 #define PRIuLEAST32 PRIu32
305 #define PRIuLEAST64 PRIu64
306
307 /* -------------------------------------------------------------------------------- */
308 /* fast types */
309
310 typedef int8_t int_fast8_t;
311 typedef int16_t int_fast16_t;
312 typedef int32_t int_fast32_t;
313 typedef int64_t int_fast64_t;
314
315 #define INT_FAST8_MIN INT8_MIN
316 #define INT_FAST8_MAX INT8_MAX
317 #define INT_FAST16_MIN INT16_MIN
318 #define INT_FAST16_MAX INT16_MAX
319 #define INT_FAST32_MIN INT32_MIN
320 #define INT_FAST32_MAX INT32_MAX
321 #define INT_FAST64_MIN INT64_MIN
322 #define INT_FAST64_MAX INT64_MAX
323
324 #define PRIdFAST8 PRId8
325 #define PRIdFAST16 PRId16
326 #define PRIdFAST32 PRId32
327 #define PRIdFAST64 PRId64
328 #define PRIxFAST8 PRIx8
329 #define PRIxFAST16 PRIx16
330 #define PRIxFAST32 PRIx32
331 #define PRIxFAST64 PRIx64
332 #define PRIoFAST8 PRIo8
333 #define PRIoFAST16 PRIo16
334 #define PRIoFAST32 PRIo32
335 #define PRIoFAST64 PRIo64
336 #define PRIiFAST8 PRIi8
337 #define PRIiFAST16 PRIi16
338 #define PRIiFAST32 PRIi32
339 #define PRIiFAST64 PRIi64
340 #define PRIXFAST8 PRIX8
341 #define PRIXFAST16 PRIX16
342 #define PRIXFAST32 PRIX32
343 #define PRIXFAST64 PRIX64
344 #define PRIuFAST8 PRIu8
345 #define PRIuFAST16 PRIu16
346 #define PRIuFAST32 PRIu32
347 #define PRIuFAST64 PRIu64
348
349 /* -------------------------------------------------------------------------------- */
350 /* [u]intmax_t -- this is hardcoded to 64-bit, beware! */
351
352 typedef int64_t intmax_t;
353 typedef uint64_t uintmax_t;
354
355 #define INTMAX_MIN INT64_MIN
356 #define INTMAX_MAX INT64_MAX
357 #define UINTMAX_MAX UINT64_MAX
358
359 #define PRIdMAX PRId64
360 #define PRIuMAX PRIu64
361 #define PRIxMAX PRIx64
362 #define PRIXMAX PRIX64
363 #define PRIoMAX PRIo64
364 #define PRIiMAX PRIi64
365
366 #define INTMAX_C(x) INT64_C(x)
367
368 #endif /* PAPER_STDINT_H_ */
369