]> pd.if.org Git - pdclib/blob - _PDCLIB_int.h
Second try. Freestanding only, for now.
[pdclib] / _PDCLIB_int.h
1 /* $Id$ */
2
3 /* Internal PDCLib logic <_PDCLIB_internal.h>
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 /* -------------------------------------------------------------------------- */
10 /* You should not have to edit anything in this file; if you DO have to, it   */
11 /* would be considered a bug / missing feature: notify the author.            */
12 /* -------------------------------------------------------------------------- */
13
14 #include <_PDCLIB_config.h>
15
16 /* null pointer constant */
17 #define _PDCLIB_NULL 0
18
19 /* -------------------------------------------------------------------------- */
20 /* Helper macros:                                                             */
21 /* _PDCLIB_cc( x, y ) concatenates two preprocessor tokens without extending  */
22 /* _PDCLIB_concat( x, y ) concatenates two preprocessor tokens with extending */
23 /* -------------------------------------------------------------------------- */
24
25 #define _PDCLIB_cc( x, y ) x ## y
26 #define _PDCLIB_concat( x, y ) _PDCLIB_cc( x, y )
27
28 /* -------------------------------------------------------------------------- */
29 /* Limits of native datatypes                                                 */
30 /* -------------------------------------------------------------------------- */
31 /* The definition of minimum limits for unsigned datatypes is done because    */
32 /* later on we will "construct" limits for other abstract types:              */
33 /* USHRT -> _PDCLIB_ + USHRT + _MIN -> _PDCLIB_USHRT_MIN -> 0                 */
34 /* INT -> _PDCLIB_ + INT + _MIN -> _PDCLIB_INT_MIN -> ... you get the idea.   */
35 /* -------------------------------------------------------------------------- */
36
37 /* Setting 'char' limits                                                      */
38 #define _PDCLIB_CHAR_BIT    8
39 #define _PDCLIB_UCHAR_MIN   0
40 #define _PDCLIB_UCHAR_MAX   0xff
41 #define _PDCLIB_SCHAR_MIN   (-0x7f - 1)
42 #define _PDCLIB_SCHAR_MAX   0x7f
43 #ifdef  _PDCLIB_CHAR_SIGNED
44 #define _PDCLIB_CHAR_MIN    _PDCLIB_SCHAR_MIN
45 #define _PDCLIB_CHAR_MAX    _PDCLIB_SCHAR_MAX
46 #else
47 #define _PDCLIB_CHAR_MIN    0
48 #define _PDCLIB_CHAR_MAX    _PDCLIB_UCHAR_MAX
49 #endif
50
51 /* Setting 'short' limits                                                     */
52 #if     _PDCLIB_SHRT_BYTES == 1
53 #define _PDCLIB_SHRT_MAX   0x7f
54 #define _PDCLIB_SHRT_MIN   (-0x7f - 1)
55 #define _PDCLIB_USHRT_MAX  0xff
56 #elif   _PDCLIB_SHRT_BYTES == 2
57 #define _PDCLIB_SHRT_MAX   0x7fff
58 #define _PDCLIB_SHRT_MIN   (-0x7fff - 1)
59 #define _PDCLIB_USHRT_MAX  0xffff
60 #else
61 #error Unsupported width of 'short' (neither 8 nor 16 bit).
62 #endif
63 #define _PDCLIB_USHRT_MIN 0
64
65 /* Setting 'int' limits                                                       */
66 #if     _PDCLIB_INT_BYTES == 2
67 #define _PDCLIB_INT_MAX   0x7fff
68 #define _PDCLIB_INT_MIN   (-0x7fff - 1)
69 #define _PDCLIB_UINT_MAX  0xffffU
70 #elif   _PDCLIB_INT_BYTES == 4
71 #define _PDCLIB_INT_MAX   0x7fffffff
72 #define _PDCLIB_INT_MIN   (-0x7fffffff - 1)
73 #define _PDCLIB_UINT_MAX  0xffffffffU
74 #elif _PDCLIB_INT_BYTES   == 8
75 #define _PDCLIB_INT_MAX   0x7fffffffffffffff
76 #define _PDCLIB_INT_MIN   (-0x7fffffffffffffff - 1)
77 #define _PDCLIB_UINT_MAX  0xffffffffffffffff
78 #else
79 #error Unsupported width of 'int' (neither 16, 32, nor 64 bit).
80 #endif
81 #define _PDCLIB_UINT_MIN 0
82
83 /* Setting 'long' limits                                                      */
84 #if   _PDCLIB_LONG_BYTES   == 4
85 #define _PDCLIB_LONG_MAX   0x7fffffffL
86 #define _PDCLIB_LONG_MIN   (-0x7fffffffL - 1L)
87 #define _PDCLIB_LONG_MAX   0xffffffffUL
88 #elif   _PDCLIB_LONG_BYTES == 8
89 #define _PDCLIB_LONG_MAX   0x7fffffffffffffffL
90 #define _PDCLIB_LONG_MIN   (-0x7fffffffffffffffL - 1L)
91 #define _PDCLIB_ULONG_MAX  0xffffffffffffffffUL
92 #else
93 #error Unsupported width of 'long' (neither 32 nor 64 bit).
94 #endif
95 #define _PDCLIB_ULONG_MIN 0
96
97 /* Setting 'long long' limits                                                 */
98 #if _PDCLIB_LLONG_BYTES    == 8
99 #define _PDCLIB_LLONG_MAX  0x7fffffffffffffffLL
100 #define _PDCLIB_LLONG_MIN  (-0x7fffffffffffffffLL - 1LL)
101 #define _PDCLIB_ULLONG_MAX 0xffffffffffffffffULL
102 #elif _PDCLIB_LLONG_BYTES  == 16
103 #define _PDCLIB_LLONG_MAX  0x7fffffffffffffffffffffffffffffffLL
104 #define _PDCLIB_LLONG_MIN  (-0x7fffffffffffffffffffffffffffffffLL - 1LL)
105 #define _PDCLIB_ULLONG_MAX 0xffffffffffffffffffffffffffffffffLL
106 #else
107 #error Unsupported width of 'long long' (neither 64 nor 128 bit).
108 #endif
109 #define _PDCLIB_ULLONG_MIN 0
110
111 /* -------------------------------------------------------------------------- */
112 /* <stdint.h> exact-width types, their limits and literals                    */
113 /* -------------------------------------------------------------------------- */
114
115 /* Setting 'int8_t', its limits, and its literal.                             */
116 #if     _PDCLIB_SHRT_BYTES == 1
117 typedef signed short       _PDCLIB_int8_t;
118 typedef unsigned short     _PDCLIB_int8_t;
119 #define _PDCLIB_INT8_MAX   _PDCLIB_SHRT_MAX
120 #define _PDCLIB_INT8_MIN   _PDCLIB_SHRT_MIN
121 #define _PDCLIB_UINT8_MAX  _PDCLIB_USHRT_MAX
122 #else
123 typedef signed char        _PDCLIB_int8_t;
124 typedef unsigned char      _PDCLIB_uint8_t;
125 #define _PDCLIB_INT8_MAX   _PDCLIB_CHAR_MAX
126 #define _PDCLIB_INT8_MIN   _PDCLIB_CHAR_MIN
127 #define _PDCLIB_UINT8_MAX  _PDCLIB_UCHAR_MAX
128 #endif
129
130 /* Setting 'int16_t', its limits, and its literal                             */
131 #if     _PDCLIB_INT_BYTES  == 2
132 typedef signed int         _PDCLIB_int16_t;
133 typedef unsigned int       _PDCLIB_uint16_t;
134 #define _PDCLIB_INT16_MAX  _PDCLIB_INT_MAX
135 #define _PDCLIB_INT16_MIN  _PDCLIB_INT_MIN
136 #define _PDCLIB_UINT16_MAX _PDCLIB_UINT_MAX
137 #elif   _PDCLIB_SHRT_BYTES == 2
138 typedef signed short       _PDCLIB_int16_t;
139 typedef unsigned short     _PDCLIB_int16_t;
140 #define _PDCLIB_INT16_MAX  _PDCLIB_SHRT_MAX
141 #define _PDCLIB_INT16_MIN  _PDCLIB_SHRT_MIN
142 #define _PDCLIB_UINT16_MAX _PDCLIB_USHRT_MAX
143 #else
144 #error Neither 'short' nor 'int' are 16-bit.
145 #endif
146
147 /* Setting 'int32_t', its limits, and its literal                             */
148 #if     _PDCLIB_INT_BYTES  == 4
149 typedef signed int         _PDCLIB_int32_t;
150 typedef unsigned int       _PDCLIB_int32_t;
151 #define _PDCLIB_INT32_MAX  _PDCLIB_INT_MAX
152 #define _PDCLIB_INT32_MIN  _PDCLIB_INT_MIN
153 #define _PDCLIB_UINT32_MAX _PDCLIB_UINT_MAX
154 #elif   _PDCLIB_LONG_BYTES == 4
155 typedef signed long        _PDCLIB_int32_t;
156 typedef unsigned long      _PDCLIB_int32_t;
157 #define _PDCLIB_INT32_MAX  _PDCLIB_LONG_MAX
158 #define _PDCLIB_INT32_MIN  _PDCLIB_LONG_MIN
159 #define _PDCLIB_UINT32_MAX _PDCLIB_LONG_MAX
160 #else
161 #error Neither 'int' nor 'long' are 32-bit.
162 #endif
163
164 #if     _PDCLIB_LONG_BYTES == 8
165 typedef signed long        _PDCLIB_int64_t;
166 typedef unsigned long      _PDCLIB_int64_t;
167 #define _PDCLIB_INT64_MAX  _PDCLIB_LONG_MAX
168 #define _PDCLIB_INT64_MIN  _PDCLIB_LONG_MIN
169 #define _PDCLIB_INT64_MAX  _PDCLIB_ULONG_MAX
170 #elif _PDCLIB_LLONG_BYTES  == 8
171 typedef signed long long   _PDCLIB_int64_t;
172 typedef unsigned long long _PDCLIB_int64_t;
173 #define _PDCLIB_INT64_MAX  _PDCLIB_LLONG_MAX
174 #define _PDCLIB_INT64_MIN  _PDCLIB_LLONG_MIN
175 #define _PDCLIB_INT64_MAX  _PDCLIB_ULLONG_MAX
176 #else
177 #error Neither 'long' nor 'long long' are 64-bit.
178 #endif
179
180 /* -------------------------------------------------------------------------- */
181 /* <stdint.h> "fastest" types and their limits                                */
182 /* -------------------------------------------------------------------------- */
183 /* This is, admittedly, butt-ugly. But at least it's ugly where the average   */
184 /* user of PDCLib will never see it, and makes <_PDCLIB_config.h> much        */
185 /* cleaner.                                                                   */
186 /* -------------------------------------------------------------------------- */
187
188 typedef _PDCLIB_fast8          _PDCLIB_int_fast8_t;
189 typedef unsigned _PDCLIB_fast8 _PDCLIB_uint_fast8_t;
190 #define _PDCLIB_INT_FAST8_MIN  concat( concat( _PDCLIB_, _PDCLIB_FAST8 ), _MIN )
191 #define _PDCLIB_INT_FAST8_MAX  concat( concat( _PDCLIB_, _PDCLIB_FAST8 ), _MAX )
192 #define _PDCLIB_UINT_FAST8_MAX concat( concat( _PDCLIB_U, _PDCLIB_FAST8 ), _MAX )
193
194 typedef _PDCLIB_fast16          _PDCLIB_int_fast16_t;
195 typedef unsigned _PDCLIB_fast16 _PDCLIB_uint_fast16_t;
196 #define _PDCLIB_INT_FAST16_MIN  concat( concat( _PDCLIB_, _PDCLIB_FAST16 ), _MIN )
197 #define _PDCLIB_INT_FAST16_MAX  concat( concat( _PDCLIB_, _PDCLIB_FAST16 ), _MAX )
198 #define _PDCLIB_UINT_FAST16_MAX concat( concat( _PDCLIB_U, _PDCLIB_FAST16 ), _MAX )
199
200 typedef _PDCLIB_fast32          _PDCLIB_int_fast32_t;
201 typedef unsigned _PDCLIB_fast32 _PDCLIB_uint_fast32_t;
202 #define _PDCLIB_INT_FAST32_MIN  concat( concat( _PDCLIB_, _PDCLIB_FAST32 ), _MIN )
203 #define _PDCLIB_INT_FAST32_MAX  concat( concat( _PDCLIB_, _PDCLIB_FAST32 ), _MAX )
204 #define _PDCLIB_UINT_FAST32_MAX concat( concat( _PDCLIB_U, _PDCLIB_FAST32 ), _MAX )
205
206 typedef _PDCLIB_fast64          _PDCLIB_int_fast64_t;
207 typedef unsigned _PDCLIB_fast64 _PDCLIB_uint_fast64_t;
208 #define _PDCLIB_INT_FAST64_MIN  concat( concat( _PDCLIB_, _PDCLIB_FAST64 ), _MIN )
209 #define _PDCLIB_INT_FAST64_MAX  concat( concat( _PDCLIB_, _PDCLIB_FAST64 ), _MAX )
210 #define _PDCLIB_UINT_FAST64_MAX concat( concat( _PDCLIB_U, _PDCLIB_FAST64 ), _MAX )
211
212 /* -------------------------------------------------------------------------- */
213 /* Various <stddef.h> limits                                                  */
214 /* -------------------------------------------------------------------------- */
215
216 #define _PDCLIB_PTRDIFF_MIN concat( concat( _PDCLIB_, _PDCLIB_PTRDIFF ), _MIN )
217 #define _PDCLIB_PTRDIFF_MAX concat( concat( _PDCLIB_, _PDCLIB_PTRDIFF ), _MAX )
218
219 #define _PDCLIB_SIG_ATOMIC_MIN concat( concat( _PDCLIB_, _PDCLIB_SIG_ATOMIC ), _MIN )
220 #define _PDCLIB_SIG_ATOMIC_MAX concat( concat( _PDCLIB_, _PDCLIB_SIG_ATOMIC ), _MAX )
221
222 #define _PDCLIB_SIZE_MAX concat( concat( _PDCLIB_, _PDCLIB_SIZE ), _MAX )
223
224 #define _PDCLIB_WCHAR_MIN concat( concat( _PDCLIB_, _PDCLIB_WCHAR ), _MIN )
225 #define _PDCLIB_WCHAR_MAX concat( concat( _PDCLIB_, _PDCLIB_WCHAR ), _MAX )
226
227 /* -------------------------------------------------------------------------- */
228 /* <stdint.h> "exact width" literal suffixes                                  */
229 /* -------------------------------------------------------------------------- */
230
231 #define _PDCLIB_INT8_LITERAL  c
232 #define _PDCLIB_INT16_LITERAL s
233 #define _PDCLIB_INT32_LITERAL l
234 #define _PDCLIB_INT64_LITERAL ll
235 #define _PDCLIB_UINT8_LITERAL uc
236 #define _PDCLIB_UINT16_LITERAL us
237 #define _PDCLIB_UINT32_LITERAL ul
238 #define _PDCLIB_UINT64_LITERAL ull
239
240 /* -------------------------------------------------------------------------- */
241 /* <stdint.h> "fast" typedefs                                                 */
242 /* -------------------------------------------------------------------------- */
243
244 typedef int                 _PDCLIB_intptr_t;
245 #define _PDCLIB_INTPTR_MIN  (-0x7fffff - 1)
246 #define _PDCLIB_INTPTR_MAX  0x7fffff
247 typedef unsigned int        _PDCLIB_uintptr_t;
248 #define _PDCLIB_UINTPTR_MAX 0xffffff
249
250 typedef signed long long int   _PDCLIB_intmax_t;
251 #define _PDCLIB_INTMAX_MIN     (-0x7fffffffffffffff - 1)
252 #define _PDCLIB_INTMAX_MAX     0x7fffffffffffffff
253 typedef unsigned long long int _PDCLIB_uintmax_t;
254 #define _PDCLIB_UINTMAX_MAX    0xffffffffffffffff