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