Bash  5.0-beta2
Bash - Bourne Again shell
typemax.h
Go to the documentation of this file.
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_SIGNED_MAGNITUDE
39 # define TYPE_SIGNED_MAGNITUDE(t) ((t) ~ (t) 0 < (t) -1)
40 #endif
41 
42 #ifndef TYPE_WIDTH
43 # define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
44 #endif
45 
46 #ifndef TYPE_MINIMUM
47 # define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t))
48 #endif
49 
50 #ifndef TYPE_MAXIMUM
51 # define TYPE_MAXIMUM(t) \
52  ((t) (! TYPE_SIGNED (t) \
53  ? (t) -1 \
54  : ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1)))
55 #endif
56 
57 #ifdef HAVE_LONG_LONG
58 # ifndef LLONG_MAX
59 # define LLONG_MAX TYPE_MAXIMUM(long long int)
60 # define LLONG_MIN TYPE_MINIMUM(long long int)
61 # endif
62 # ifndef ULLONG_MAX
63 # define ULLONG_MAX TYPE_MAXIMUM(unsigned long long int)
64 # endif
65 #endif
66 
67 #ifndef ULONG_MAX
68 # define ULONG_MAX ((unsigned long) ~(unsigned long) 0)
69 #endif
70 
71 #ifndef LONG_MAX
72 # define LONG_MAX ((long int) (ULONG_MAX >> 1))
73 # define LONG_MIN ((long int) (-LONG_MAX - 1L))
74 #endif
75 
76 #ifndef INT_MAX /* ouch */
77 # define INT_MAX TYPE_MAXIMUM(int)
78 # define INT_MIN TYPE_MINIMUM(int)
79 # define UINT_MAX ((unsigned int) ~(unsigned int)0)
80 #endif
81 
82 /* workaround for gcc bug in versions < 2.7 */
83 #if defined (HAVE_LONG_LONG) && __GNUC__ == 2 && __GNUC_MINOR__ < 7
84 static const unsigned long long int maxquad = ULLONG_MAX;
85 # undef ULLONG_MAX
86 # define ULLONG_MAX maxquad
87 #endif
88 
89 #if !defined (INTMAX_MAX) || !defined (INTMAX_MIN)
90 
91 #if SIZEOF_INTMAX_T == SIZEOF_LONG_LONG
92 # define INTMAX_MAX LLONG_MAX
93 # define INTMAX_MIN LLONG_MIN
94 #elif SIZEOF_INTMAX_T == SIZEOF_LONG
95 # define INTMAX_MAX LONG_MAX
96 # define INTMAX_MIN LONG_MIN
97 #else
98 # define INTMAX_MAX INT_MAX
99 # define INTMAX_MIN INT_MIN
100 #endif
101 
102 #endif
103 
104 #ifndef SSIZE_MAX
105 # define SSIZE_MAX 32767 /* POSIX minimum max */
106 #endif
107 
108 #ifndef SIZE_MAX
109 # define SIZE_MAX 65535 /* POSIX minimum max */
110 #endif
111 
112 #endif /* _SH_TYPEMAX_H */