]> pd.if.org Git - pdclib/blob - _PDCLIB_int.h
Added note about v0.1 not being much tested.
[pdclib] / _PDCLIB_int.h
1 /* $Id$ */
2
3 /* Release $Name$ */
4
5 /* PDCLib internal integer logic <_PDCLIB_int.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 #ifndef _PDCLIB_AUX_H
22 #define _PDCLIB_AUX_H _PDCLIB_AUX_H
23 #include <_PDCLIB_aux.h>
24 #endif
25
26 /* null pointer constant */
27 #define _PDCLIB_NULL 0
28
29 /* -------------------------------------------------------------------------- */
30 /* Limits of native datatypes                                                 */
31 /* -------------------------------------------------------------------------- */
32 /* The definition of minimum limits for unsigned datatypes is done because    */
33 /* later on we will "construct" limits for other abstract types:              */
34 /* USHRT -> _PDCLIB_ + USHRT + _MIN -> _PDCLIB_USHRT_MIN -> 0                 */
35 /* INT -> _PDCLIB_ + INT + _MIN -> _PDCLIB_INT_MIN -> ... you get the idea.   */
36 /* -------------------------------------------------------------------------- */
37
38 /* Setting 'char' limits                                                      */
39 #define _PDCLIB_CHAR_BIT    8
40 #define _PDCLIB_UCHAR_MIN   0
41 #define _PDCLIB_UCHAR_MAX   0xff
42 #define _PDCLIB_SCHAR_MIN   (-0x7f - 1)
43 #define _PDCLIB_SCHAR_MAX   0x7f
44 #ifdef  _PDCLIB_CHAR_SIGNED
45 #define _PDCLIB_CHAR_MIN    _PDCLIB_SCHAR_MIN
46 #define _PDCLIB_CHAR_MAX    _PDCLIB_SCHAR_MAX
47 #else
48 #define _PDCLIB_CHAR_MIN    0
49 #define _PDCLIB_CHAR_MAX    _PDCLIB_UCHAR_MAX
50 #endif
51
52 /* Setting 'short' limits                                                     */
53 #if     _PDCLIB_SHRT_BYTES == 2
54 #define _PDCLIB_SHRT_MAX      0x7fff
55 #define _PDCLIB_SHRT_MIN      (-0x7fff - 1)
56 #define _PDCLIB_USHRT_MAX     0xffff
57 #else
58 #error Unsupported width of 'short' (not 16 bit).
59 #endif
60 #define _PDCLIB_USHRT_MIN 0
61
62 #if _PDCLIB_INT_BYTES < _PDCLIB_SHRT_BYTES
63 #error Bogus setting: short > int? Check _PDCLIB_config.h.
64 #endif
65
66 /* Setting 'int' limits                                                       */
67 #if     _PDCLIB_INT_BYTES == 2
68 #define _PDCLIB_INT_MAX   0x7fff
69 #define _PDCLIB_INT_MIN   (-0x7fff - 1)
70 #define _PDCLIB_UINT_MAX  0xffffU
71 #elif   _PDCLIB_INT_BYTES == 4
72 #define _PDCLIB_INT_MAX   0x7fffffff
73 #define _PDCLIB_INT_MIN   (-0x7fffffff - 1)
74 #define _PDCLIB_UINT_MAX  0xffffffffU
75 #elif _PDCLIB_INT_BYTES   == 8
76 #define _PDCLIB_INT_MAX   0x7fffffffffffffff
77 #define _PDCLIB_INT_MIN   (-0x7fffffffffffffff - 1)
78 #define _PDCLIB_UINT_MAX  0xffffffffffffffff
79 #else
80 #error Unsupported width of 'int' (neither 16, 32, nor 64 bit).
81 #endif
82 #define _PDCLIB_UINT_MIN 0
83
84 /* Setting 'long' limits                                                      */
85 #if   _PDCLIB_LONG_BYTES   == 4
86 #define _PDCLIB_LONG_MAX   0x7fffffffL
87 #define _PDCLIB_LONG_MIN   (-0x7fffffffL - 1L)
88 #define _PDCLIB_ULONG_MAX   0xffffffffUL
89 #elif   _PDCLIB_LONG_BYTES == 8
90 #define _PDCLIB_LONG_MAX   0x7fffffffffffffffL
91 #define _PDCLIB_LONG_MIN   (-0x7fffffffffffffffL - 1L)
92 #define _PDCLIB_ULONG_MAX  0xffffffffffffffffUL
93 #else
94 #error Unsupported width of 'long' (neither 32 nor 64 bit).
95 #endif
96 #define _PDCLIB_ULONG_MIN 0
97
98 /* Setting 'long long' limits                                                 */
99 #if _PDCLIB_LLONG_BYTES    == 8
100 #define _PDCLIB_LLONG_MAX  0x7fffffffffffffffLL
101 #define _PDCLIB_LLONG_MIN  (-0x7fffffffffffffffLL - 1LL)
102 #define _PDCLIB_ULLONG_MAX 0xffffffffffffffffULL
103 #elif _PDCLIB_LLONG_BYTES  == 16
104 #define _PDCLIB_LLONG_MAX  0x7fffffffffffffffffffffffffffffffLL
105 #define _PDCLIB_LLONG_MIN  (-0x7fffffffffffffffffffffffffffffffLL - 1LL)
106 #define _PDCLIB_ULLONG_MAX 0xffffffffffffffffffffffffffffffffLL
107 #else
108 #error Unsupported width of 'long long' (neither 64 nor 128 bit).
109 #endif
110 #define _PDCLIB_ULLONG_MIN 0
111
112 /* -------------------------------------------------------------------------- */
113 /* <stdint.h> exact-width types and their limits                              */
114 /* -------------------------------------------------------------------------- */
115
116 /* Setting 'int8_t', its limits, and its literal.                             */
117 #if     _PDCLIB_CHAR_BIT == 8
118 typedef signed char        _PDCLIB_int8_t;
119 typedef unsigned char      _PDCLIB_uint8_t;
120 #define _PDCLIB_INT8_MAX   _PDCLIB_CHAR_MAX
121 #define _PDCLIB_INT8_MIN   _PDCLIB_CHAR_MIN
122 #define _PDCLIB_UINT8_MAX  _PDCLIB_UCHAR_MAX
123 #else
124 #error Unsupported width of char (not 8 bits).
125 #endif
126
127 /* Setting 'int16_t', its limits, and its literal                             */
128 #if     _PDCLIB_INT_BYTES  == 2
129 typedef signed int         _PDCLIB_int16_t;
130 typedef unsigned int       _PDCLIB_uint16_t;
131 #define _PDCLIB_INT16_MAX  _PDCLIB_INT_MAX
132 #define _PDCLIB_INT16_MIN  _PDCLIB_INT_MIN
133 #define _PDCLIB_UINT16_MAX _PDCLIB_UINT_MAX
134 #elif   _PDCLIB_SHRT_BYTES == 2
135 typedef signed short       _PDCLIB_int16_t;
136 typedef unsigned short     _PDCLIB_uint16_t;
137 #define _PDCLIB_INT16_MAX  _PDCLIB_SHRT_MAX
138 #define _PDCLIB_INT16_MIN  _PDCLIB_SHRT_MIN
139 #define _PDCLIB_UINT16_MAX _PDCLIB_USHRT_MAX
140 #else
141 #error Neither 'short' nor 'int' are 16-bit.
142 #endif
143
144 /* Setting 'int32_t', its limits, and its literal                             */
145 #if     _PDCLIB_INT_BYTES  == 4
146 typedef signed int         _PDCLIB_int32_t;
147 typedef unsigned int       _PDCLIB_uint32_t;
148 #define _PDCLIB_INT32_MAX  _PDCLIB_INT_MAX
149 #define _PDCLIB_INT32_MIN  _PDCLIB_INT_MIN
150 #define _PDCLIB_UINT32_MAX _PDCLIB_UINT_MAX
151 #elif   _PDCLIB_LONG_BYTES == 4
152 typedef signed long        _PDCLIB_int32_t;
153 typedef unsigned long      _PDCLIB_uint32_t;
154 #define _PDCLIB_INT32_MAX  _PDCLIB_LONG_MAX
155 #define _PDCLIB_INT32_MIN  _PDCLIB_LONG_MIN
156 #define _PDCLIB_UINT32_MAX _PDCLIB_LONG_MAX
157 #else
158 #error Neither 'int' nor 'long' are 32-bit.
159 #endif
160
161 #if     _PDCLIB_LONG_BYTES == 8
162 typedef signed long        _PDCLIB_int64_t;
163 typedef unsigned long      _PDCLIB_uint64_t;
164 #define _PDCLIB_INT64_MAX  _PDCLIB_LONG_MAX
165 #define _PDCLIB_INT64_MIN  _PDCLIB_LONG_MIN
166 #define _PDCLIB_UINT64_MAX  _PDCLIB_ULONG_MAX
167 #elif _PDCLIB_LLONG_BYTES  == 8
168 typedef signed long long   _PDCLIB_int64_t;
169 typedef unsigned long long _PDCLIB_uint64_t;
170 #define _PDCLIB_INT64_MAX  _PDCLIB_LLONG_MAX
171 #define _PDCLIB_INT64_MIN  _PDCLIB_LLONG_MIN
172 #define _PDCLIB_UINT64_MAX  _PDCLIB_ULLONG_MAX
173 #else
174 #error Neither 'long' nor 'long long' are 64-bit.
175 #endif
176
177 /* -------------------------------------------------------------------------- */
178 /* <stdint.h> "fastest" types and their limits                                */
179 /* -------------------------------------------------------------------------- */
180 /* This is, admittedly, butt-ugly. But at least it's ugly where the average   */
181 /* user of PDCLib will never see it, and makes <_PDCLIB_config.h> much        */
182 /* cleaner.                                                                   */
183 /* -------------------------------------------------------------------------- */
184
185 typedef _PDCLIB_fast8          _PDCLIB_int_fast8_t;
186 typedef unsigned _PDCLIB_fast8 _PDCLIB_uint_fast8_t;
187 #define _PDCLIB_INT_FAST8_MIN  concat( concat( _PDCLIB_, _PDCLIB_FAST8 ), _MIN )
188 #define _PDCLIB_INT_FAST8_MAX  concat( concat( _PDCLIB_, _PDCLIB_FAST8 ), _MAX )
189 #define _PDCLIB_UINT_FAST8_MAX concat( concat( _PDCLIB_U, _PDCLIB_FAST8 ), _MAX )
190
191 typedef _PDCLIB_fast16          _PDCLIB_int_fast16_t;
192 typedef unsigned _PDCLIB_fast16 _PDCLIB_uint_fast16_t;
193 #define _PDCLIB_INT_FAST16_MIN  concat( concat( _PDCLIB_, _PDCLIB_FAST16 ), _MIN )
194 #define _PDCLIB_INT_FAST16_MAX  concat( concat( _PDCLIB_, _PDCLIB_FAST16 ), _MAX )
195 #define _PDCLIB_UINT_FAST16_MAX concat( concat( _PDCLIB_U, _PDCLIB_FAST16 ), _MAX )
196
197 typedef _PDCLIB_fast32          _PDCLIB_int_fast32_t;
198 typedef unsigned _PDCLIB_fast32 _PDCLIB_uint_fast32_t;
199 #define _PDCLIB_INT_FAST32_MIN  concat( concat( _PDCLIB_, _PDCLIB_FAST32 ), _MIN )
200 #define _PDCLIB_INT_FAST32_MAX  concat( concat( _PDCLIB_, _PDCLIB_FAST32 ), _MAX )
201 #define _PDCLIB_UINT_FAST32_MAX concat( concat( _PDCLIB_U, _PDCLIB_FAST32 ), _MAX )
202
203 typedef _PDCLIB_fast64          _PDCLIB_int_fast64_t;
204 typedef unsigned _PDCLIB_fast64 _PDCLIB_uint_fast64_t;
205 #define _PDCLIB_INT_FAST64_MIN  concat( concat( _PDCLIB_, _PDCLIB_FAST64 ), _MIN )
206 #define _PDCLIB_INT_FAST64_MAX  concat( concat( _PDCLIB_, _PDCLIB_FAST64 ), _MAX )
207 #define _PDCLIB_UINT_FAST64_MAX concat( concat( _PDCLIB_U, _PDCLIB_FAST64 ), _MAX )
208
209 /* -------------------------------------------------------------------------- */
210 /* Various <stddef.h> typedefs and limits                                     */
211 /* -------------------------------------------------------------------------- */
212
213 typedef _PDCLIB_ptrdiff     _PDCLIB_ptrdiff_t
214 #define _PDCLIB_PTRDIFF_MIN concat( concat( _PDCLIB_, _PDCLIB_PTRDIFF ), _MIN )
215 #define _PDCLIB_PTRDIFF_MAX concat( concat( _PDCLIB_, _PDCLIB_PTRDIFF ), _MAX )
216
217 #define _PDCLIB_SIG_ATOMIC_MIN concat( concat( _PDCLIB_, _PDCLIB_SIG_ATOMIC ), _MIN )
218 #define _PDCLIB_SIG_ATOMIC_MAX concat( concat( _PDCLIB_, _PDCLIB_SIG_ATOMIC ), _MAX )
219
220 typedef _PDCLIB_size     _PDCLIB_size_t
221 #define _PDCLIB_SIZE_MAX concat( concat( _PDCLIB_, _PDCLIB_SIZE ), _MAX )
222
223 typedef _PDCLIB_wchar     _PDCLIB_wchar_t
224 #define _PDCLIB_WCHAR_MIN concat( concat( _PDCLIB_, _PDCLIB_WCHAR ), _MIN )
225 #define _PDCLIB_WCHAR_MAX concat( concat( _PDCLIB_, _PDCLIB_WCHAR ), _MAX )
226
227 typedef _PDCLIB_intptr          _PDCLIB_intptr_t;
228 typedef unsigned _PDCLIB_intptr _PDCLIB_uintptr_t;
229 #define _PDCLIB_INTPTR_MIN  concat( concat( _PDCLIB_, _PDCLIB_INTPTR ), _MIN )
230 #define _PDCLIB_INTPTR_MAX  concat( concat( _PDCLIB_, _PDCLIB_INTPTR ), _MAX )
231 #define _PDCLIB_UINTPTR_MAX concat( concat( _PDCLIB_U, _PDCLIB_INTPTR ), _MAX )
232
233 typedef _PDCLIB_intmax          _PDCLIB_intmax_t;
234 typedef unsigned _PDCLIB_intmax _PDCLIB_uintmax_t;
235 #define _PDCLIB_INTMAX_MIN  concat( concat( _PDCLIB_, _PDCLIB_INTMAX ), _MIN )
236 #define _PDCLIB_INTMAX_MAX  concat( concat( _PDCLIB_, _PDCLIB_INTMAX ), _MAX )
237 #define _PDCLIB_UINTMAX_MAX concat( concat( _PDCLIB_U, _PDCLIB_INTMAX ), _MAX )
238
239 /* TODO: INTN_C / UINTN_C */
240