1 /* PDCLib internal integer logic <_PDCLIB_int.h>
3 This file is part of the Public Domain C Library (PDCLib).
4 Permission is granted to use, modify, and / or redistribute at will.
8 #define __PDCLIB_INT_H __PDCLIB_INT_H
10 /* -------------------------------------------------------------------------- */
11 /* You should not have to edit anything in this file; if you DO have to, it */
12 /* would be considered a bug / missing feature: notify the author(s). */
13 /* -------------------------------------------------------------------------- */
15 #include "_PDCLIB_config.h"
16 #include "_PDCLIB_aux.h"
18 /* null pointer constant */
19 #define _PDCLIB_NULL 0
21 /* -------------------------------------------------------------------------- */
22 /* Limits of native datatypes */
23 /* -------------------------------------------------------------------------- */
24 /* The definition of minimum limits for unsigned datatypes is done because */
25 /* later on we will "construct" limits for other abstract types: */
26 /* USHRT -> _PDCLIB_ + USHRT + _MIN -> _PDCLIB_USHRT_MIN -> 0 */
27 /* INT -> _PDCLIB_ + INT + _MIN -> _PDCLIB_INT_MIN -> ... you get the idea. */
28 /* -------------------------------------------------------------------------- */
30 /* Setting 'char' limits */
31 #define _PDCLIB_CHAR_BIT 8
32 #define _PDCLIB_UCHAR_MIN 0
33 #define _PDCLIB_UCHAR_MAX 0xff
34 #define _PDCLIB_SCHAR_MIN (-0x7f - 1)
35 #define _PDCLIB_SCHAR_MAX 0x7f
36 #if _PDCLIB_CHAR_SIGNED == 1
37 #define _PDCLIB_CHAR_MIN _PDCLIB_SCHAR_MIN
38 #define _PDCLIB_CHAR_MAX _PDCLIB_SCHAR_MAX
40 #define _PDCLIB_CHAR_MIN 0
41 #define _PDCLIB_CHAR_MAX _PDCLIB_UCHAR_MAX
44 /* Setting 'short' limits */
45 #if _PDCLIB_SHRT_BYTES == 2
46 #define _PDCLIB_SHRT_MAX 0x7fff
47 #define _PDCLIB_SHRT_MIN (-0x7fff - 1)
48 #define _PDCLIB_USHRT_MAX 0xffff
50 #error Unsupported width of 'short' (not 16 bit).
52 #define _PDCLIB_USHRT_MIN 0
54 #if _PDCLIB_INT_BYTES < _PDCLIB_SHRT_BYTES
55 #error Bogus setting: short > int? Check _PDCLIB_config.h.
58 /* Setting 'int' limits */
59 #if _PDCLIB_INT_BYTES == 2
60 #define _PDCLIB_INT_MAX 0x7fff
61 #define _PDCLIB_INT_MIN (-0x7fff - 1)
62 #define _PDCLIB_UINT_MAX 0xffffU
63 #elif _PDCLIB_INT_BYTES == 4
64 #define _PDCLIB_INT_MAX 0x7fffffff
65 #define _PDCLIB_INT_MIN (-0x7fffffff - 1)
66 #define _PDCLIB_UINT_MAX 0xffffffffU
67 #elif _PDCLIB_INT_BYTES == 8
68 #define _PDCLIB_INT_MAX 0x7fffffffffffffff
69 #define _PDCLIB_INT_MIN (-0x7fffffffffffffff - 1)
70 #define _PDCLIB_UINT_MAX 0xffffffffffffffff
72 #error Unsupported width of 'int' (neither 16, 32, nor 64 bit).
74 #define _PDCLIB_UINT_MIN 0
76 /* Setting 'long' limits */
77 #if _PDCLIB_LONG_BYTES == 4
78 #define _PDCLIB_LONG_MAX 0x7fffffffL
79 #define _PDCLIB_LONG_MIN (-0x7fffffffL - 1L)
80 #define _PDCLIB_ULONG_MAX 0xffffffffUL
81 #elif _PDCLIB_LONG_BYTES == 8
82 #define _PDCLIB_LONG_MAX 0x7fffffffffffffffL
83 #define _PDCLIB_LONG_MIN (-0x7fffffffffffffffL - 1L)
84 #define _PDCLIB_ULONG_MAX 0xffffffffffffffffUL
86 #error Unsupported width of 'long' (neither 32 nor 64 bit).
88 #define _PDCLIB_ULONG_MIN 0
90 /* Setting 'long long' limits */
91 #if _PDCLIB_LLONG_BYTES == 8
92 #define _PDCLIB_LLONG_MAX 0x7fffffffffffffffLL
93 #define _PDCLIB_LLONG_MIN (-0x7fffffffffffffffLL - 1LL)
94 #define _PDCLIB_ULLONG_MAX 0xffffffffffffffffULL
95 #elif _PDCLIB_LLONG_BYTES == 16
96 #define _PDCLIB_LLONG_MAX 0x7fffffffffffffffffffffffffffffffLL
97 #define _PDCLIB_LLONG_MIN (-0x7fffffffffffffffffffffffffffffffLL - 1LL)
98 #define _PDCLIB_ULLONG_MAX 0xffffffffffffffffffffffffffffffffULL
100 #error Unsupported width of 'long long' (neither 64 nor 128 bit).
102 #define _PDCLIB_ULLONG_MIN 0
104 /* -------------------------------------------------------------------------- */
105 /* <stdint.h> exact-width types and their limits */
106 /* -------------------------------------------------------------------------- */
107 /* Note that, for the "standard" widths of 8, 16, 32 and 64 bit, the "LEAST" */
108 /* types are identical to the "exact-width" types, by definition. */
110 /* Setting 'int8_t', its limits, its literal, and conversion macros. */
111 #if _PDCLIB_CHAR_BIT == 8
112 typedef signed char _PDCLIB_int8_t;
113 typedef unsigned char _PDCLIB_uint8_t;
114 typedef signed char _PDCLIB_int_least8_t;
115 typedef unsigned char _PDCLIB_uint_least8_t;
116 #define _PDCLIB_INT8_MAX _PDCLIB_CHAR_MAX
117 #define _PDCLIB_INT8_MIN _PDCLIB_CHAR_MIN
118 #define _PDCLIB_UINT8_MAX _PDCLIB_UCHAR_MAX
119 #define _PDCLIB_8_CONV hh
121 #error Unsupported width of char (not 8 bits).
124 /* Setting 'int16_t', its limits, its literal, and conversion macros. */
125 #if _PDCLIB_INT_BYTES == 2
126 typedef signed int _PDCLIB_int16_t;
127 typedef unsigned int _PDCLIB_uint16_t;
128 typedef signed int _PDCLIB_int_least16_t;
129 typedef unsigned int _PDCLIB_uint_least16_t;
130 #define _PDCLIB_INT16_MAX _PDCLIB_INT_MAX
131 #define _PDCLIB_INT16_MIN _PDCLIB_INT_MIN
132 #define _PDCLIB_UINT16_MAX _PDCLIB_UINT_MAX
133 #define _PDCLIB_16_CONV
134 #elif _PDCLIB_SHRT_BYTES == 2
135 typedef signed short _PDCLIB_int16_t;
136 typedef unsigned short _PDCLIB_uint16_t;
137 typedef signed short _PDCLIB_int_least16_t;
138 typedef unsigned short _PDCLIB_uint_least16_t;
139 #define _PDCLIB_INT16_MAX _PDCLIB_SHRT_MAX
140 #define _PDCLIB_INT16_MIN _PDCLIB_SHRT_MIN
141 #define _PDCLIB_UINT16_MAX _PDCLIB_USHRT_MAX
142 #define _PDCLIB_16_CONV h
144 #error Neither 'short' nor 'int' are 16-bit.
147 /* Setting 'int32_t', its limits, its literal, and conversion macros. */
148 #if _PDCLIB_INT_BYTES == 4
149 typedef signed int _PDCLIB_int32_t;
150 typedef unsigned int _PDCLIB_uint32_t;
151 typedef signed int _PDCLIB_int_least32_t;
152 typedef unsigned int _PDCLIB_uint_least32_t;
153 #define _PDCLIB_INT32_MAX _PDCLIB_INT_MAX
154 #define _PDCLIB_INT32_MIN _PDCLIB_INT_MIN
155 #define _PDCLIB_UINT32_MAX _PDCLIB_UINT_MAX
156 #define _PDCLIB_INT32_LITERAL
157 #define _PDCLIB_UINT32_LITERAL
158 #define _PDCLIB_32_CONV
159 #elif _PDCLIB_LONG_BYTES == 4
160 typedef signed long _PDCLIB_int32_t;
161 typedef unsigned long _PDCLIB_uint32_t;
162 typedef signed long _PDCLIB_int_least32_t;
163 typedef unsigned long _PDCLIB_uint_least32_t;
164 #define _PDCLIB_INT32_MAX _PDCLIB_LONG_MAX
165 #define _PDCLIB_INT32_MIN _PDCLIB_LONG_MIN
166 #define _PDCLIB_UINT32_MAX _PDCLIB_LONG_MAX
167 #define _PDCLIB_INT32_LITERAL l
168 #define _PDCLIB_UINT32_LITERAL ul
169 #define _PDCLIB_32_CONV l
171 #error Neither 'int' nor 'long' are 32-bit.
174 /* Setting 'int64_t', its limits, its literal, and conversion macros. */
175 #if _PDCLIB_LONG_BYTES == 8 && !defined(_PDCLIB_INT64_IS_LLONG)
176 typedef signed long _PDCLIB_int64_t;
177 typedef unsigned long _PDCLIB_uint64_t;
178 typedef signed long _PDCLIB_int_least64_t;
179 typedef unsigned long _PDCLIB_uint_least64_t;
180 #define _PDCLIB_INT64_MAX _PDCLIB_LONG_MAX
181 #define _PDCLIB_INT64_MIN _PDCLIB_LONG_MIN
182 #define _PDCLIB_UINT64_MAX _PDCLIB_ULONG_MAX
183 #define _PDCLIB_INT64_LITERAL l
184 #define _PDCLIB_UINT64_LITERAL ul
185 #define _PDCLIB_64_CONV l
186 #elif _PDCLIB_LLONG_BYTES == 8
187 typedef signed long long _PDCLIB_int64_t;
188 typedef unsigned long long _PDCLIB_uint64_t;
189 typedef signed long long _PDCLIB_int_least64_t;
190 typedef unsigned long long _PDCLIB_uint_least64_t;
191 #define _PDCLIB_INT64_MAX _PDCLIB_LLONG_MAX
192 #define _PDCLIB_INT64_MIN _PDCLIB_LLONG_MIN
193 #define _PDCLIB_UINT64_MAX _PDCLIB_ULLONG_MAX
194 #define _PDCLIB_INT64_LITERAL ll
195 #define _PDCLIB_UINT64_LITERAL ull
196 #define _PDCLIB_64_CONV ll
198 #error Neither 'long' nor 'long long' are 64-bit.
201 /* -------------------------------------------------------------------------- */
202 /* <stdint.h> "fastest" types and their limits */
203 /* -------------------------------------------------------------------------- */
204 /* This is, admittedly, butt-ugly. But at least it's ugly where the average */
205 /* user of PDCLib will never see it, and makes <_PDCLIB_config.h> much */
207 /* -------------------------------------------------------------------------- */
209 typedef _PDCLIB_fast8 _PDCLIB_int_fast8_t;
210 typedef unsigned _PDCLIB_fast8 _PDCLIB_uint_fast8_t;
211 #define _PDCLIB_INT_FAST8_MIN _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_FAST8 ), _MIN )
212 #define _PDCLIB_INT_FAST8_MAX _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_FAST8 ), _MAX )
213 #define _PDCLIB_UINT_FAST8_MAX _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_U, _PDCLIB_FAST8 ), _MAX )
215 typedef _PDCLIB_fast16 _PDCLIB_int_fast16_t;
216 typedef unsigned _PDCLIB_fast16 _PDCLIB_uint_fast16_t;
217 #define _PDCLIB_INT_FAST16_MIN _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_FAST16 ), _MIN )
218 #define _PDCLIB_INT_FAST16_MAX _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_FAST16 ), _MAX )
219 #define _PDCLIB_UINT_FAST16_MAX _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_U, _PDCLIB_FAST16 ), _MAX )
221 typedef _PDCLIB_fast32 _PDCLIB_int_fast32_t;
222 typedef unsigned _PDCLIB_fast32 _PDCLIB_uint_fast32_t;
223 #define _PDCLIB_INT_FAST32_MIN _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_FAST32 ), _MIN )
224 #define _PDCLIB_INT_FAST32_MAX _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_FAST32 ), _MAX )
225 #define _PDCLIB_UINT_FAST32_MAX _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_U, _PDCLIB_FAST32 ), _MAX )
227 typedef _PDCLIB_fast64 _PDCLIB_int_fast64_t;
228 typedef unsigned _PDCLIB_fast64 _PDCLIB_uint_fast64_t;
229 #define _PDCLIB_INT_FAST64_MIN _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_FAST64 ), _MIN )
230 #define _PDCLIB_INT_FAST64_MAX _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_FAST64 ), _MAX )
231 #define _PDCLIB_UINT_FAST64_MAX _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_U, _PDCLIB_FAST64 ), _MAX )
233 /* -------------------------------------------------------------------------- */
234 /* Various <stddef.h> typedefs and limits */
235 /* -------------------------------------------------------------------------- */
237 typedef _PDCLIB_ptrdiff _PDCLIB_ptrdiff_t;
238 #define _PDCLIB_PTRDIFF_MIN _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_PTRDIFF ), _MIN )
239 #define _PDCLIB_PTRDIFF_MAX _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_PTRDIFF ), _MAX )
241 typedef _PDCLIB_size _PDCLIB_size_t;
242 #define _PDCLIB_SIZE_MAX _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_SIZE ), _MAX )
244 typedef _PDCLIB_wint _PDCLIB_wint_t;
247 typedef _PDCLIB_wchar _PDCLIB_wchar_t;
249 typedef wchar_t _PDCLIB_wchar_t;
251 #define _PDCLIB_WCHAR_MIN _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_WCHAR ), _MIN )
252 #define _PDCLIB_WCHAR_MAX _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_WCHAR ), _MAX )
254 #define _PDCLIB_SIG_ATOMIC_MIN _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_SIG_ATOMIC ), _MIN )
255 #define _PDCLIB_SIG_ATOMIC_MAX _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_SIG_ATOMIC ), _MAX )
257 typedef _PDCLIB_intptr _PDCLIB_intptr_t;
258 typedef unsigned _PDCLIB_intptr _PDCLIB_uintptr_t;
259 #define _PDCLIB_INTPTR_MIN _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_INTPTR ), _MIN )
260 #define _PDCLIB_INTPTR_MAX _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_INTPTR ), _MAX )
261 #define _PDCLIB_UINTPTR_MAX _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_U, _PDCLIB_INTPTR ), _MAX )
263 typedef _PDCLIB_intmax _PDCLIB_intmax_t;
264 typedef unsigned _PDCLIB_intmax _PDCLIB_uintmax_t;
265 #define _PDCLIB_INTMAX_MIN _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_INTMAX ), _MIN )
266 #define _PDCLIB_INTMAX_MAX _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_, _PDCLIB_INTMAX ), _MAX )
267 #define _PDCLIB_UINTMAX_MAX _PDCLIB_concat( _PDCLIB_concat( _PDCLIB_U, _PDCLIB_INTMAX ), _MAX )
268 #define _PDCLIB_INTMAX_C( value ) _PDCLIB_concat( value, _PDCLIB_INTMAX_LITERAL )
269 #define _PDCLIB_UINTMAX_C( value ) _PDCLIB_concat( value, _PDCLIB_concat( u, _PDCLIB_INTMAX_LITERAL ) )
271 /* -------------------------------------------------------------------------- */
272 /* Various <time.h> internals */
273 /* -------------------------------------------------------------------------- */
275 typedef _PDCLIB_time _PDCLIB_time_t;
276 typedef _PDCLIB_clock _PDCLIB_clock_t;
278 /* -------------------------------------------------------------------------- */
279 /* Internal data types */
280 /* -------------------------------------------------------------------------- */
282 /* Structure required by both atexit() and exit() for handling atexit functions */
283 struct _PDCLIB_exitfunc_t
285 struct _PDCLIB_exitfunc_t * next;
286 void (*func)( void );
289 /* -------------------------------------------------------------------------- */
290 /* Declaration of helper functions (implemented in functions/_PDCLIB). */
291 /* -------------------------------------------------------------------------- */
293 /* This is the main function called by atoi(), atol() and atoll(). */
294 _PDCLIB_intmax_t _PDCLIB_atomax( const char * s );
296 /* Two helper functions used by strtol(), strtoul() and long long variants. */
297 const char * _PDCLIB_strtox_prelim( const char * p, char * sign, int * base );
298 _PDCLIB_uintmax_t _PDCLIB_strtox_main( const char ** p, unsigned int base, _PDCLIB_uintmax_t error, _PDCLIB_uintmax_t limval, int limdigit, char * sign );
300 /* Digits arrays used by various integer conversion functions */
301 extern char _PDCLIB_digits[];
302 extern char _PDCLIB_Xdigits[];
304 /* -------------------------------------------------------------------------- */
306 /* -------------------------------------------------------------------------- */
308 #if _PDCLIB_C_VERSION >= 2011
309 _Static_assert( sizeof( short ) == _PDCLIB_SHRT_BYTES, "Compiler disagrees on _PDCLIB_SHRT_BYTES." );
310 _Static_assert( sizeof( int ) == _PDCLIB_INT_BYTES, "Compiler disagrees on _PDCLIB_INT_BYTES." );
311 _Static_assert( sizeof( long ) == _PDCLIB_LONG_BYTES, "Compiler disagrees on _PDCLIB_LONG_BYTES." );
312 _Static_assert( sizeof( long long ) == _PDCLIB_LLONG_BYTES, "Compiler disagrees on _PDCLIB_LLONG_BYTES." );
314 _Static_assert( ( (char)-1 < 0 ) == _PDCLIB_CHAR_SIGNED, "Compiler disagrees on _PDCLIB_CHAR_SIGNED." );
315 _Static_assert( sizeof( sizeof( int ) ) == sizeof( _PDCLIB_size ), "Compiler disagrees on _PDCLIB_size." );
317 _Static_assert( sizeof( _PDCLIB_wchar ) == sizeof( L'x' ), "Compiler disagrees on _PDCLIB_wchar." );
319 _Static_assert( sizeof( void * ) == sizeof( _PDCLIB_intptr ), "Compiler disagrees on _PDCLIB_intptr." );
321 _Static_assert( sizeof( &_PDCLIB_digits[1] - &_PDCLIB_digits[0] ) == sizeof( _PDCLIB_ptrdiff ), "Compiler disagrees on _PDCLIB_ptrdiff." );
324 /* -------------------------------------------------------------------------- */
325 /* locale / wchar / uchar */
326 /* -------------------------------------------------------------------------- */
329 typedef _PDCLIB_uint16_t _PDCLIB_char16_t;
330 typedef _PDCLIB_uint32_t _PDCLIB_char32_t;
332 typedef char16_t _PDCLIB_char16_t;
333 typedef char32_t _PDCLIB_char32_t;
336 typedef struct _PDCLIB_mbstate {
338 /* Is this the best way to represent this? Is this big enough? */
339 _PDCLIB_uint64_t _St64[15];
340 _PDCLIB_uint32_t _St32[31];
341 _PDCLIB_uint16_t _St16[62];
342 unsigned char _StUC[124];
343 signed char _StSC[124];
347 /* c16/related functions: Surrogate storage
349 * If zero, no surrogate pending. If nonzero, surrogate.
351 _PDCLIB_uint16_t _Surrogate;
353 /* In cases where the underlying codec is capable of regurgitating a
354 * character without consuming any extra input (e.g. a surrogate pair in a
355 * UCS-4 to UTF-16 conversion) then these fields are used to track that
356 * state. In particular, they are used to buffer/fake the input for mbrtowc
357 * and similar functions.
359 * See _PDCLIB_encoding.h for values of _PendState and the resultant value
362 unsigned char _PendState;
366 typedef struct _PDCLIB_locale *_PDCLIB_locale_t;
367 typedef struct lconv _PDCLIB_lconv_t;
369 _PDCLIB_size_t _PDCLIB_mb_cur_max( void );
371 /* wide-character EOF */
372 #define _PDCLIB_WEOF ((wint_t) -1
374 /* -------------------------------------------------------------------------- */
376 /* -------------------------------------------------------------------------- */
378 /* Position / status structure for getpos() / fsetpos(). */
379 typedef struct _PDCLIB_fpos
381 _PDCLIB_int_fast64_t offset; /* File position offset */
382 _PDCLIB_mbstate_t mbs; /* Multibyte parsing state */
385 typedef struct _PDCLIB_fileops _PDCLIB_fileops_t;
386 typedef union _PDCLIB_fd _PDCLIB_fd_t;
387 typedef struct _PDCLIB_file _PDCLIB_file_t; // Rename to _PDCLIB_FILE?
389 /* Status structure required by _PDCLIB_print(). */
390 struct _PDCLIB_status_t
392 /* XXX This structure is horrible now. scanf needs its own */
394 int base; /* base to which the value shall be converted */
395 _PDCLIB_int_fast32_t flags; /* flags and length modifiers */
396 unsigned n; /* print: maximum characters to be written (snprintf) */
397 /* scan: number matched conversion specifiers */
398 unsigned i; /* number of characters read/written */
399 unsigned current;/* chars read/written in the CURRENT conversion */
400 unsigned width; /* specified field width */
401 int prec; /* specified field precision */
404 void * ctx; /* context for callback */
405 const char * s; /* input string for scanf */
409 _PDCLIB_size_t ( *write ) ( void *p, const char *buf, _PDCLIB_size_t size );
410 _PDCLIB_file_t *stream; /* for scanf */
412 _PDCLIB_va_list arg; /* argument stack */