]> git.ipfire.org Git - thirdparty/bash.git/blob - include/typemax.h
bash-20130308 additional cleanup
[thirdparty/bash.git] / include / typemax.h
1 /* typemax.h -- encapsulate max values for long, long long, etc. */
2
3 /* Copyright (C) 2001 Free Software Foundation, Inc.
4
5 This file is part of GNU Bash, the Bourne Again SHell.
6
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /*
22 * NOTE: This should be included after config.h, limits.h, stdint.h, and
23 * inttypes.h
24 */
25
26 #ifndef _SH_TYPEMAX_H
27 #define _SH_TYPEMAX_H
28
29 #ifndef CHAR_BIT
30 # define CHAR_BIT 8
31 #endif
32
33 /* Nonzero if the integer type T is signed. */
34 #ifndef TYPE_SIGNED
35 # define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
36 #endif
37
38 #ifndef TYPE_MINIMUM
39 # define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
40 ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) \
41 : (t) 0))
42 #endif
43
44 #ifndef TYPE_MAXIMUM
45 # define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
46 #endif
47
48 #ifdef HAVE_LONG_LONG
49 # ifndef LLONG_MAX
50 # define LLONG_MAX TYPE_MAXIMUM(long long int)
51 # define LLONG_MIN TYPE_MINIMUM(long long int)
52 # endif
53 # ifndef ULLONG_MAX
54 # define ULLONG_MAX TYPE_MAXIMUM(unsigned long long int)
55 # endif
56 #endif
57
58 #ifndef ULONG_MAX
59 # define ULONG_MAX ((unsigned long) ~(unsigned long) 0)
60 #endif
61
62 #ifndef LONG_MAX
63 # define LONG_MAX ((long int) (ULONG_MAX >> 1))
64 # define LONG_MIN ((long int) (-LONG_MAX - 1L))
65 #endif
66
67 #ifndef INT_MAX /* ouch */
68 # define INT_MAX TYPE_MAXIMUM(int)
69 # define INT_MIN TYPE_MINIMUM(int)
70 # define UINT_MAX ((unsigned int) ~(unsigned int)0)
71 #endif
72
73 /* workaround for gcc bug in versions < 2.7 */
74 #if defined (HAVE_LONG_LONG) && __GNUC__ == 2 && __GNUC_MINOR__ < 7
75 static const unsigned long long int maxquad = ULLONG_MAX;
76 # undef ULLONG_MAX
77 # define ULLONG_MAX maxquad
78 #endif
79
80 #if !defined (INTMAX_MAX) || !defined (INTMAX_MIN)
81
82 #if SIZEOF_INTMAX_T == SIZEOF_LONG_LONG
83 # define INTMAX_MAX LLONG_MAX
84 # define INTMAX_MIN LLONG_MIN
85 #elif SIZEOF_INTMAX_T == SIZEOF_LONG
86 # define INTMAX_MAX LONG_MAX
87 # define INTMAX_MIN LONG_MIN
88 #else
89 # define INTMAX_MAX INT_MAX
90 # define INTMAX_MIN INT_MIN
91 #endif
92
93 #endif
94
95 #ifndef SSIZE_MAX
96 # define SSIZE_MAX 32767 /* POSIX minimum max */
97 #endif
98
99 #ifndef SIZE_MAX
100 # define SIZE_MAX 65535 /* POSIX minimum max */
101 #endif
102
103 #endif /* _SH_TYPEMAX_H */