1 /*
2 Copyright 2020, Dirk Krause. All rights reserved.
3 SPDX-License-Identifier:	BSD-3-Clause
4 */
5 
6 /**	@file	dk4numco.h	Numeric constants.
7 
8 	Typically the constants from limits.h and stdint.h are used.
9 	This file contains fallback definitions if the files
10 	are not available or some definitions are missing.
11 */
12 #ifndef DK4NUMCO_H_INCLUDED
13 
14 /**	Protection against multiple inclusion.
15 */
16 #define	DK4NUMCO_H_INCLUDED	1
17 
18 #ifndef DK4CONF_H_INCLUDED
19 #if DK4_BUILDING_DKTOOLS4
20 #include "dk4conf.h"
21 #else
22 #include <dktools-4/dk4conf.h>
23 #endif
24 #endif
25 
26 #if DK4_HAVE_LIMITS_H
27 #ifndef LIMITS_H_INCLUDED
28 #include <limits.h>
29 #define LIMITS_H_INCLUDED 1
30 #endif
31 #endif
32 #if DK4_HAVE_STDINT_H
33 #ifndef STDINT_H_INCLUDED
34 #if defined(__cplusplus) && (!defined(__STDC_LIMIT_MACROS))
35 /**	For legacy compilers, use macros for integer limits.
36 */
37 #define __STDC_LIMIT_MACROS 1
38 #endif
39 #if defined(__cplusplus) && (!defined(__STDC_CONSTANT_MACROS))
40 /**	For legacy compilers, use macros for constants.
41 */
42 #define __STDC_CONSTANT_MACROS 1
43 #endif
44 #include <stdint.h>
45 #define STDINT_H_INCLUDED 1
46 #endif
47 #endif
48 #if DK4_HAVE_INTTYPES_H
49 #ifndef INTTYPES_H_INCLUDED
50 #include <inttypes.h>
51 #define INTTYPES_H_INCLUDED 1
52 #endif
53 #endif
54 
55 /**	Decision: Use fallback definitions for missing constants.
56 	On some systems the minimum/maximum constants are not
57 	defined.
58 	Here you can choose, whether the compiler should complain
59 	and bail out (0) or use a self-made definition (1).
60 	The default is to complain and bail out.
61 */
62 #ifndef DK4_USE_FALLBACK_NUMERIC_LIMITS
63 #define	DK4_USE_FALLBACK_NUMERIC_LIMITS	0
64 #endif
65 
66 
67 
68 #ifndef SIZE_MAX
69 #if !DK4_USE_FALLBACK_NUMERIC_LIMITS
70 #error "SIZE_MAX undefined! See file dk4numco.h, line 10."
71 #endif
72 #if DK4_SIZEOF_SIZE_T > 2
73 #if DK4_SIZEOF_SIZE_T > 4
74 /** Maximum size_t value. */
75 #define	SIZE_MAX 18446744073709551615ULL
76 #else
77 /** Maximum size_t value. */
78 #define SIZE_MAX 4294967295UL
79 #endif
80 #else
81 /** Maximum size_t value. */
82 #define SIZE_MAX 65535U
83 #endif
84 #endif
85 /* ifndef SIZE_MAX */
86 
87 
88 
89 #ifndef SCHAR_MIN
90 #if !DK4_USE_FALLBACK_NUMERIC_LIMITS
91 #error "SCHAR_MIN undefined! See file dk4numco.h, line 10."
92 #endif
93 /** Minimum signed char value. */
94 #define SCHAR_MIN	(-128)
95 #endif
96 /* ifndef SCHAR_MIN */
97 
98 
99 
100 #ifndef SCHAR_MAX
101 #if !DK4_USE_FALLBACK_NUMERIC_LIMITS
102 #error "SCHAR_MAX undefined! See file dk4numco.h, line 10."
103 #endif
104 /** Maximum signed char value. */
105 #define	SCHAR_MAX	127
106 #endif
107 /* ifndef SCHAR_MAX */
108 
109 
110 
111 #ifndef UCHAR_MAX
112 #if !DK4_USE_FALLBACK_NUMERIC_LIMITS
113 #error "UCHAR_MAX undefined! See file dk4numco.h, line 10."
114 #endif
115 /** Maximum unsigned char value. */
116 #define	UCHAR_MAX	255
117 #endif
118 /* ifndef UCHAR_MAX */
119 
120 
121 
122 #ifndef SHRT_MIN
123 #if !DK4_USE_FALLBACK_NUMERIC_LIMITS
124 #error "SHRT_MIN undefined! See file dk4numco.h, line 10."
125 #endif
126 /** Minimum signed short value. */
127 #define	SHRT_MIN	(-32768)
128 #endif
129 /* ifndef SHRT_MIN */
130 
131 
132 
133 #ifndef SHRT_MAX
134 #if !DK4_USE_FALLBACK_NUMERIC_LIMITS
135 #error "SHRT_MAX undefined! See file dk4numco.h, line 10."
136 #endif
137 /** Maximum signed short value. */
138 #define	SHRT_MAX	32767
139 #endif
140 /* ifndef SHRT_MAX */
141 
142 
143 
144 #ifndef USHRT_MAX
145 #if !DK4_USE_FALLBACK_NUMERIC_LIMITS
146 #error "USHRT_MAX undefined! See file dk4numco.h, line 10."
147 #endif
148 /** Maximum unsigned short value. */
149 #define	USHRT_MAX	65535
150 #endif
151 /* ifndef USHRT_MAX */
152 
153 
154 
155 #ifndef	INT_MAX
156 #if !DK4_USE_FALLBACK_NUMERIC_LIMITS
157 #error "INT_MAX undefined! See file dk4numco.h, line 10."
158 #endif
159 #if DK4_SIZEOF_INT > 2
160 #if DK4_SIZEOF_INT > 4
161 #if DK4_SIZEOF_INT > 8
162 #error "Not prepared for such large int numbers!"
163 #else
164 /** Maximum signed int value. */
165 #define	INT_MAX 9223372036854775807
166 #endif
167 #else
168 /** Maximum signed int value. */
169 #define	INT_MAX	2147483647
170 #endif
171 #else
172 /** Maximum signed int value. */
173 #define	INT_MAX	32767
174 #endif
175 #endif
176 /* ifndef INT_MAX */
177 
178 
179 
180 #ifndef INT_MIN
181 #if !DK4_USE_FALLBACK_NUMERIC_LIMITS
182 #error "INT_MIN undefined! See file dk4numco.h, line 10."
183 #endif
184 /** Minimum signed int value. */
185 #define	INT_MIN	(-INT_MAX - 1)
186 #endif
187 /* ifndef INT_MIN */
188 
189 
190 
191 #ifndef UINT_MAX
192 #if !DK4_USE_FALLBACK_NUMERIC_LIMITS
193 #error "LONG_MAX undefined! See file dk4numco.h, line 10."
194 #endif
195 #if DK4_SIZEOF_INT > 2
196 #if DK4_SIZEOF_INT > 4
197 #if DK4_SIZEOF_INT > 8
198 #error "Not prepared for such large int numbers!"
199 #else
200 /** Maximum unsigned int value. */
201 #define	UINT_MAX	18446744073709551615U
202 #endif
203 #else
204 /** Maximum unsigned int value. */
205 #define	UINT_MAX	4294967295U
206 #endif
207 #else
208 /** Maximum unsigned int value. */
209 #define	UINT_MAX	65535U
210 #endif
211 #endif
212 /* ifndef UINT_MAX */
213 
214 
215 
216 #ifndef LONG_MAX
217 #if !DK4_USE_FALLBACK_NUMERIC_LIMITS
218 #error "LONG_MAX undefined! See file dk4numco.h, line 10."
219 #endif
220 #if DK4_SIZEOF_LONG > 2
221 #if DK4_SIZEOF_LONG > 4
222 #if DK4_SIZEOF_LONG > 8
223 #error "Not prepared for such large long numbers!"
224 #else
225 /** Maximum signed long value. */
226 #define	LONG_MAX	9223372036854775807L
227 #endif
228 #else
229 /** Maximum signed long value. */
230 #define	LONG_MAX	2147483647L
231 #endif
232 #else
233 /** Maximum signed long value. */
234 #define	LONG_MAX	32767L
235 #endif
236 #endif
237 /* ifndef LONG_MAX */
238 
239 
240 
241 #ifndef LONG_MIN
242 #if !DK4_USE_FALLBACK_NUMERIC_LIMITS
243 #error "LONG_MIN undefined! See file dk4numco.h, line 10."
244 #endif
245 /** Minimum signed long value. */
246 #define	LONG_MIN	(-LONG_MAX - 1L)
247 #endif
248 /* ifndef LONG_MIN */
249 
250 
251 
252 #ifndef ULONG_MAX
253 #if !DK4_USE_FALLBACK_NUMERIC_LIMITS
254 #error "ULONG_MAX undefined! See file dk4numco.h, line 10."
255 #endif
256 #if DK4_SIZEOF_LONG > 2
257 #if DK4_SIZEOF_LONG > 4
258 #if DK4_SIZEOF_LONG > 8
259 #error "Not prepared for such large long numbers!"
260 #else
261 /** Maximum unsigned long value. */
262 #define	ULONG_MAX	18446744073709551615UL
263 #endif
264 #else
265 /** Maximum unsigned long value. */
266 #define	ULONG_MAX	4294967295UL
267 #endif
268 #else
269 /** Maximum unsigned long value. */
270 #define	ULONG_MAX	65535UL
271 #endif
272 #endif
273 /* ifndef ULONG_MAX */
274 
275 
276 
277 #if DK4_HAVE_LONG_LONG
278 
279 
280 
281 #ifndef LLONG_MAX
282 #if !DK4_USE_FALLBACK_NUMERIC_LIMITS
283 #error "LLONG_MAX undefined! See file dk4numco.h, line 10."
284 #endif
285 #if DK4_SIZEOF_LONG_LONG > 2
286 #if DK4_SIZEOF_LONG_LONG > 4
287 #if DK4_SIZEOF_LONG_LONG > 8
288 #error "Not prepared for such large long long numbers!"
289 #else
290 /** Maximum signed long long value. */
291 #define	LLONG_MAX	9223372036854775807LL
292 #endif
293 #else
294 /** Maximum signed long long value. */
295 #define	LLONG_MAX	2147483647LL
296 #endif
297 #else
298 /** Maximum signed long long value. */
299 #define	LLONG_MAX	32767LL
300 #endif
301 #endif
302 /* ifndef LLONG_MAX */
303 
304 
305 #ifndef LLONG_MIN
306 #if !DK4_USE_FALLBACK_NUMERIC_LIMITS
307 #error "LLONG_MIN undefined! See file dk4numco.h, line 10."
308 #endif
309 /** Minimum signed long long value. */
310 #define	LLONG_MIN	(-LLONG_MAX - 1LL)
311 #endif
312 /* ifndef LLONG_MIN */
313 
314 
315 
316 #ifndef ULLONG_MAX
317 #if !DK4_USE_FALLBACK_NUMERIC_LIMITS
318 #error "ULLONG_MAX undefined! See file dk4numco.h, line 10."
319 #endif
320 #if DK4_SIZEOF_LONG_LONG > 2
321 #if DK4_SIZEOF_LONG_LONG > 4
322 #if DK4_SIZEOF_LONG_LONG > 8
323 #error "Not prepared for such large long long numbers!"
324 #else
325 /** Maximum unsigned long long value. */
326 #define	ULLONG_MAX	18446744073709551615ULL
327 #endif
328 #else
329 /** Maximum unsigned long long value. */
330 #define	ULLONG_MAX	4294967295ULL
331 #endif
332 #else
333 /** Maximum unsigned long long value. */
334 #define	ULLONG_MAX	65535ULL
335 #endif
336 #endif
337 /* ifndef ULLONG_MAX */
338 
339 
340 
341 #endif
342 /* if DK4_HAVE_LONG_LONG */
343 
344 
345 
346 
347 #if DK4_HAVE_INTMAX_T
348 
349 
350 #ifndef INTMAX_MAX
351 #if !DK4_USE_FALLBACK_NUMERIC_LIMITS
352 #error "INTMAX_MAX undefined! See file dk4numco.h, line 10."
353 #endif
354 #if DK4_SIZEOF_INTMAX_T == DK4_SIZEOF_INT
355 /** Maximum intmax_t value. */
356 #define	INTMAX_MAX	(INT_MAX)
357 #else
358 #if DK4_SIZEOF_INTMAX_T == DK4_SIZEOF_LONG
359 /** Maximum intmax_t value. */
360 #define	INTMAX_MAX	(LONG_MAX)
361 #else
362 #if DK4_HAVE_LONG_LONG
363 #if DK4_SIZEOF_INTMAX_T == DK4_SIZEOF_LONG_LONG
364 /** Maximum intmax_t value. */
365 #define	INTMAX_MAX	(LLONG_MAX)
366 #else
367 #error "Not prepared for such large intmax_t numbers!"
368 #endif
369 #else
370 #error "Not prepared for such large intmax_t numbers!"
371 #endif
372 #endif
373 #endif
374 #endif
375 /* ifndef INTMAX_MAX */
376 
377 
378 
379 #ifndef INTMAX_MIN
380 #if !DK4_USE_FALLBACK_NUMERIC_LIMITS
381 #error "INTMAX_MIN undefined! See file dk4numco.h, line 10."
382 #endif
383 /** Minimum intmax_t value. */
384 #define	INTMAX_MIN	(-(INTMAX_MAX) - (intmax_t)1)
385 #endif
386 /* ifndef INTMAX_MIN */
387 
388 
389 
390 #ifndef UINTMAX_MAX
391 #if !DK4_USE_FALLBACK_NUMERIC_LIMITS
392 #error "UINTMAX_MAX undefined! See file dk4numco.h, line 10."
393 #endif
394 #if DK4_SIZEOF_INTMAX_T == DK4_SIZEOF_INT
395 /** Maximum uintmax_t value. */
396 #define	UINTMAX_MAX	(UINT_MAX)
397 #else
398 #if DK4_SIZEOF_INTMAX_T == DK4_SIZEOF_LONG
399 /** Maximum uintmax_t value. */
400 #define	UINTMAX_MAX	(ULONG_MAX)
401 #else
402 #if DK4_HAVE_LONG_LONG
403 #if DK4_SIZEOF_INTMAX_T == DK4_SIZEOF_LONG_LONG
404 /** Maximum uintmax_t value. */
405 #define	UINTMAX_MAX	(ULLONG_MAX)
406 #else
407 #error "Not prepared for such large uintmax_t numbers!"
408 #endif
409 #else
410 #error "Not prepared for such large uintmax_t numbers!"
411 #endif
412 #endif
413 #endif
414 #endif
415 /* ifndef UINTMAX_MAX */
416 
417 
418 
419 #endif
420 /* if DK4_HAVE_INTMAX_T */
421 
422 
423 
424 #if DK4_HAVE_INTMAX_T
425 /**	Maximum dk4_um_t value.
426 */
427 #define	DK4_UM_MAX	(UINTMAX_MAX)
428 /**	Maximum dk4_im_t value.
429 */
430 #define	DK4_IM_MAX	(INTMAX_MAX)
431 /**	Minimum dk4_im_t value.
432 */
433 #define	DK4_IM_MIN	(INTMAX_MIN)
434 #else
435 #if DK4_HAVE_LONG_LONG
436 /**	Maximum dk4_um_t value.
437 */
438 #define	DK4_UM_MAX	(ULLONG_MAX)
439 /**	Maximum dk4_im_t value.
440 */
441 #define	DK4_IM_MAX	(LLONG_MAX)
442 /**	Minimum dk4_im_t value.
443 */
444 #define	DK4_IM_MIN	(LLONG_MIN)
445 #else
446 /**	Maximum dk4_um_t value.
447 */
448 #define	DK4_UM_MAX	(ULONG_MAX)
449 /**	Maximum dk4_im_t value.
450 */
451 #define	DK4_IM_MAX	(LONG_MAX)
452 /**	Minimum dk4_im_t value.
453 */
454 #define	DK4_IM_MIN	(LONG_MIN)
455 #endif
456 #endif
457 
458 #endif
459 /* ifndef DK4NUMCO_H_INCLUDED */
460 
461