1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* intprops.h -- properties of integer types
5 Copyright (C) 2001-2005, 2009-2013 Free Software Foundation, Inc.
7 This program 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.
12 This program 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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* Written by Paul Eggert. */
22 #ifndef _GL_INTPROPS_H
23 #define _GL_INTPROPS_H
27 /* Return an integer value, converted to the same type as the integer
28 expression E after integer type promotion. V is the unconverted value. */
29 #define _GL_INT_CONVERT(e, v) (0 * (e) + (v))
31 /* Act like _GL_INT_CONVERT (E, -V) but work around a bug in IRIX 6.5 cc; see
32 <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00406.html>. */
33 #define _GL_INT_NEGATE_CONVERT(e, v) (0 * (e) - (v))
35 /* The extra casts in the following macros work around compiler bugs,
36 e.g., in Cray C 5.0.3.0. */
38 /* True if the arithmetic type T is an integer type. bool counts as
40 #define TYPE_IS_INTEGER(t) ((t) 1.5 == 1)
42 /* True if negative values of the signed integer type T use two's
43 complement, ones' complement, or signed magnitude representation,
44 respectively. Much GNU code assumes two's complement, but some
45 people like to be portable to all possible C hosts. */
46 #define TYPE_TWOS_COMPLEMENT(t) ((t) ~ (t) 0 == (t) -1)
47 #define TYPE_ONES_COMPLEMENT(t) ((t) ~ (t) 0 == 0)
48 #define TYPE_SIGNED_MAGNITUDE(t) ((t) ~ (t) 0 < (t) -1)
50 /* True if the signed integer expression E uses two's complement. */
51 #define _GL_INT_TWOS_COMPLEMENT(e) (~ _GL_INT_CONVERT (e, 0) == -1)
53 /* True if the arithmetic type T is signed. */
54 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
56 /* Return 1 if the integer expression E, after integer promotion, has
58 #define _GL_INT_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)
61 /* Minimum and maximum values for integer types and expressions. These
62 macros have undefined behavior if T is signed and has padding bits.
63 If this is a problem for you, please let us know how to fix it for
66 /* The maximum and minimum values for the integer type T. */
67 #define TYPE_MINIMUM(t) \
68 ((t) (! TYPE_SIGNED (t) \
70 : TYPE_SIGNED_MAGNITUDE (t) \
72 : ~ TYPE_MAXIMUM (t)))
73 #define TYPE_MAXIMUM(t) \
74 ((t) (! TYPE_SIGNED (t) \
76 : ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1)))
78 /* The maximum and minimum values for the type of the expression E,
79 after integer promotion. E should not have side effects. */
80 #define _GL_INT_MINIMUM(e) \
82 ? - _GL_INT_TWOS_COMPLEMENT (e) - _GL_SIGNED_INT_MAXIMUM (e) \
83 : _GL_INT_CONVERT (e, 0))
84 #define _GL_INT_MAXIMUM(e) \
86 ? _GL_SIGNED_INT_MAXIMUM (e) \
87 : _GL_INT_NEGATE_CONVERT (e, 1))
88 #define _GL_SIGNED_INT_MAXIMUM(e) \
89 (((_GL_INT_CONVERT (e, 1) << (sizeof ((e) + 0) * CHAR_BIT - 2)) - 1) * 2 + 1)
92 /* Return 1 if the __typeof__ keyword works. This could be done by
93 'configure', but for now it's easier to do it by hand. */
94 #if 2 <= __GNUC__ || defined __IBM__TYPEOF__ || 0x5110 <= __SUNPRO_C
95 # define _GL_HAVE___TYPEOF__ 1
97 # define _GL_HAVE___TYPEOF__ 0
100 /* Return 1 if the integer type or expression T might be signed. Return 0
101 if it is definitely unsigned. This macro does not evaluate its argument,
102 and expands to an integer constant expression. */
103 #if _GL_HAVE___TYPEOF__
104 # define _GL_SIGNED_TYPE_OR_EXPR(t) TYPE_SIGNED (__typeof__ (t))
106 # define _GL_SIGNED_TYPE_OR_EXPR(t) 1
109 /* Bound on length of the string representing an unsigned integer
110 value representable in B bits. log10 (2.0) < 146/485. The
111 smallest value of B where this bound is not tight is 2621. */
112 #define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485)
114 /* Bound on length of the string representing an integer type or expression T.
115 Subtract 1 for the sign bit if T is signed, and then add 1 more for
116 a minus sign if needed.
118 Because _GL_SIGNED_TYPE_OR_EXPR sometimes returns 0 when its argument is
119 signed, this macro may overestimate the true bound by one byte when
120 applied to unsigned types of size 2, 4, 16, ... bytes. */
121 #define INT_STRLEN_BOUND(t) \
122 (INT_BITS_STRLEN_BOUND (sizeof (t) * CHAR_BIT \
123 - _GL_SIGNED_TYPE_OR_EXPR (t)) \
124 + _GL_SIGNED_TYPE_OR_EXPR (t))
126 /* Bound on buffer size needed to represent an integer type or expression T,
127 including the terminating null. */
128 #define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1)
131 /* Range overflow checks.
133 The INT_<op>_RANGE_OVERFLOW macros return 1 if the corresponding C
134 operators might not yield numerically correct answers due to
135 arithmetic overflow. They do not rely on undefined or
136 implementation-defined behavior. Their implementations are simple
137 and straightforward, but they are a bit harder to use than the
138 INT_<op>_OVERFLOW macros described below.
144 if (INT_MULTIPLY_RANGE_OVERFLOW (i, j, LONG_MIN, LONG_MAX))
145 printf ("multiply would overflow");
147 printf ("product is %ld", i * j);
149 Restrictions on *_RANGE_OVERFLOW macros:
151 These macros do not check for all possible numerical problems or
152 undefined or unspecified behavior: they do not check for division
153 by zero, for bad shift counts, or for shifting negative numbers.
155 These macros may evaluate their arguments zero or multiple times,
156 so the arguments should not have side effects. The arithmetic
157 arguments (including the MIN and MAX arguments) must be of the same
158 integer type after the usual arithmetic conversions, and the type
159 must have minimum value MIN and maximum MAX. Unsigned types should
160 use a zero MIN of the proper type.
162 These macros are tuned for constant MIN and MAX. For commutative
163 operations such as A + B, they are also tuned for constant B. */
165 /* Return 1 if A + B would overflow in [MIN,MAX] arithmetic.
166 See above for restrictions. */
167 #define INT_ADD_RANGE_OVERFLOW(a, b, min, max) \
169 ? (a) < (min) - (b) \
172 /* Return 1 if A - B would overflow in [MIN,MAX] arithmetic.
173 See above for restrictions. */
174 #define INT_SUBTRACT_RANGE_OVERFLOW(a, b, min, max) \
176 ? (max) + (b) < (a) \
179 /* Return 1 if - A would overflow in [MIN,MAX] arithmetic.
180 See above for restrictions. */
181 #define INT_NEGATE_RANGE_OVERFLOW(a, min, max) \
186 /* Return 1 if A * B would overflow in [MIN,MAX] arithmetic.
187 See above for restrictions. Avoid && and || as they tickle
188 bugs in Sun C 5.11 2010/08/13 and other compilers; see
189 <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00401.html>. */
190 #define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \
193 ? (a) < (max) / (b) \
196 : (min) / (b) < (a)) \
200 ? (a) < (min) / (b) \
201 : (max) / (b) < (a)))
203 /* Return 1 if A / B would overflow in [MIN,MAX] arithmetic.
204 See above for restrictions. Do not check for division by zero. */
205 #define INT_DIVIDE_RANGE_OVERFLOW(a, b, min, max) \
206 ((min) < 0 && (b) == -1 && (a) < - (max))
208 /* Return 1 if A % B would overflow in [MIN,MAX] arithmetic.
209 See above for restrictions. Do not check for division by zero.
210 Mathematically, % should never overflow, but on x86-like hosts
211 INT_MIN % -1 traps, and the C standard permits this, so treat this
212 as an overflow too. */
213 #define INT_REMAINDER_RANGE_OVERFLOW(a, b, min, max) \
214 INT_DIVIDE_RANGE_OVERFLOW (a, b, min, max)
216 /* Return 1 if A << B would overflow in [MIN,MAX] arithmetic.
217 See above for restrictions. Here, MIN and MAX are for A only, and B need
218 not be of the same type as the other arguments. The C standard says that
219 behavior is undefined for shifts unless 0 <= B < wordwidth, and that when
220 A is negative then A << B has undefined behavior and A >> B has
221 implementation-defined behavior, but do not check these other
223 #define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \
225 ? (a) < (min) >> (b) \
226 : (max) >> (b) < (a))
229 /* The _GL*_OVERFLOW macros have the same restrictions as the
230 *_RANGE_OVERFLOW macros, except that they do not assume that operands
231 (e.g., A and B) have the same type as MIN and MAX. Instead, they assume
232 that the result (e.g., A + B) has that type. */
233 #define _GL_ADD_OVERFLOW(a, b, min, max) \
234 ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \
235 : (a) < 0 ? (b) <= (a) + (b) \
236 : (b) < 0 ? (a) <= (a) + (b) \
238 #define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
239 ((min) < 0 ? INT_SUBTRACT_RANGE_OVERFLOW (a, b, min, max) \
241 : (b) < 0 ? (a) - (b) <= (a) \
243 #define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
244 (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a)))) \
245 || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max))
246 #define _GL_DIVIDE_OVERFLOW(a, b, min, max) \
247 ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
248 : (a) < 0 ? (b) <= (a) + (b) - 1 \
249 : (b) < 0 && (a) + (b) <= (a))
250 #define _GL_REMAINDER_OVERFLOW(a, b, min, max) \
251 ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
252 : (a) < 0 ? (a) % (b) != ((max) - (b) + 1) % (b) \
253 : (b) < 0 && ! _GL_UNSIGNED_NEG_MULTIPLE (a, b, max))
255 /* Return a nonzero value if A is a mathematical multiple of B, where
256 A is unsigned, B is negative, and MAX is the maximum value of A's
257 type. A's type must be the same as (A % B)'s type. Normally (A %
258 -B == 0) suffices, but things get tricky if -B would overflow. */
259 #define _GL_UNSIGNED_NEG_MULTIPLE(a, b, max) \
260 (((b) < -_GL_SIGNED_INT_MAXIMUM (b) \
261 ? (_GL_SIGNED_INT_MAXIMUM (b) == (max) \
263 : (a) % (_GL_INT_CONVERT (a, _GL_SIGNED_INT_MAXIMUM (b)) + 1)) \
268 /* Integer overflow checks.
270 The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators
271 might not yield numerically correct answers due to arithmetic overflow.
272 They work correctly on all known practical hosts, and do not rely
273 on undefined behavior due to signed arithmetic overflow.
279 if (INT_MULTIPLY_OVERFLOW (i, j))
280 printf ("multiply would overflow");
282 printf ("product is %ld", i * j);
284 These macros do not check for all possible numerical problems or
285 undefined or unspecified behavior: they do not check for division
286 by zero, for bad shift counts, or for shifting negative numbers.
288 These macros may evaluate their arguments zero or multiple times, so the
289 arguments should not have side effects.
291 These macros are tuned for their last argument being a constant.
293 Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B,
294 A % B, and A << B would overflow, respectively. */
296 #define INT_ADD_OVERFLOW(a, b) \
297 _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW)
298 #define INT_SUBTRACT_OVERFLOW(a, b) \
299 _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW)
300 #define INT_NEGATE_OVERFLOW(a) \
301 INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
302 #define INT_MULTIPLY_OVERFLOW(a, b) \
303 _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW)
304 #define INT_DIVIDE_OVERFLOW(a, b) \
305 _GL_BINARY_OP_OVERFLOW (a, b, _GL_DIVIDE_OVERFLOW)
306 #define INT_REMAINDER_OVERFLOW(a, b) \
307 _GL_BINARY_OP_OVERFLOW (a, b, _GL_REMAINDER_OVERFLOW)
308 #define INT_LEFT_SHIFT_OVERFLOW(a, b) \
309 INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
310 _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
312 /* Return 1 if the expression A <op> B would overflow,
313 where OP_RESULT_OVERFLOW (A, B, MIN, MAX) does the actual test,
314 assuming MIN and MAX are the minimum and maximum for the result type.
315 Arguments should be free of side effects. */
316 #define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \
317 op_result_overflow (a, b, \
318 _GL_INT_MINIMUM (0 * (b) + (a)), \
319 _GL_INT_MAXIMUM (0 * (b) + (a)))
321 #endif /* _GL_INTPROPS_H */