Debian Codegraph @

lib/intprops.h

raw
1 /* intprops.h -- properties of integer types
2
3 Copyright (C) 2001-2026 Free Software Foundation, Inc.
4
5 This program is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Lesser General Public License as published
7 by the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18 #ifndef _GL_INTPROPS_H
19 #define _GL_INTPROPS_H
20
21 #include "intprops-internal.h"
22
23 /* The extra casts in the following macros work around compiler bugs,
24 e.g., in Cray C 5.0.3.0. */
25
26 /* True if the arithmetic type T is an integer type. bool counts as
27 an integer. */
28 #define TYPE_IS_INTEGER(t) ((t) 1.5 == 1)
29
30 /* True if the real type T is signed. */
31 #define TYPE_SIGNED(t) _GL_TYPE_SIGNED (t)
32
33 /* Return 1 if the real expression E, after promotion, has a
34 signed or floating type. Do not evaluate E. */
35 #define EXPR_SIGNED(e) _GL_EXPR_SIGNED (e)
36
37 /* The same value as as the arithmetic expression E, but with E's type
38 after integer promotions. For example, if E is of type 'enum {A, B}'
39 then 'switch (INT_PROMOTE (E))' pacifies gcc -Wswitch-enum if some
40 enum values are deliberately omitted from the switch's cases.
41 Here, unary + is safer than a cast or inline function, as unary +
42 does only integer promotions and is disallowed on pointers. */
43 #define INT_PROMOTE(e) (+ (e))
44
45
46 /* Minimum and maximum values for integer types and expressions. */
47
48 /* The width in bits of the integer type or expression T.
49 Do not evaluate T. T must not be a bit-field expression.
50 Padding bits are not supported; this is checked at compile-time below. */
51 #define TYPE_WIDTH(t) _GL_TYPE_WIDTH (t)
52
53 /* The maximum and minimum values for the integer type T. */
54 #define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t))
55 #define TYPE_MAXIMUM(t) \
56 ((t) (! TYPE_SIGNED (t) \
57 ? (t) -1 \
58 : ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1)))
59
60 /* Bound on length of the string representing an unsigned integer
61 value representable in B bits. log10 (2.0) < 146/485. The
62 smallest value of B where this bound is not tight is 2621. */
63 #define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485)
64
65 /* Bound on length of the string representing an integer type or expression T.
66 T must not be a bit-field expression.
67
68 Subtract 1 for the sign bit if T is signed, and then add 1 more for
69 a minus sign if needed.
70
71 Because _GL_SIGNED_TYPE_OR_EXPR sometimes returns 1 when its argument is
72 unsigned, this macro may overestimate the true bound by one byte when
73 applied to unsigned types of size 2, 4, 16, ... bytes. */
74 #define INT_STRLEN_BOUND(t) \
75 (INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - _GL_SIGNED_TYPE_OR_EXPR (t)) \
76 + _GL_SIGNED_TYPE_OR_EXPR (t))
77
78 /* Bound on buffer size needed to represent an integer type or expression T,
79 including the terminating null. T must not be a bit-field expression. */
80 #define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1)
81
82
83 /* Range overflow checks.
84
85 The INT_<op>_RANGE_OVERFLOW macros return 1 if the corresponding C
86 operators overflow arithmetically when given the same arguments.
87 These macros do not rely on undefined or implementation-defined behavior.
88 Although their implementations are simple and straightforward,
89 they are harder to use and may be less efficient than the
90 INT_<op>_WRAPV, INT_<op>_OK, and INT_<op>_OVERFLOW macros described below.
91
92 Example usage:
93
94 long int i = ...;
95 long int j = ...;
96 if (INT_MULTIPLY_RANGE_OVERFLOW (i, j, LONG_MIN, LONG_MAX))
97 printf ("multiply would overflow");
98 else
99 printf ("product is %ld", i * j);
100
101 Restrictions on *_RANGE_OVERFLOW macros:
102
103 These macros do not check for all possible numerical problems or
104 undefined or unspecified behavior: they do not check for division
105 by zero, for bad shift counts, or for shifting negative numbers.
106
107 These macros may evaluate their arguments zero or multiple times,
108 so the arguments should not have side effects. The arithmetic
109 arguments (including the MIN and MAX arguments) must be of the same
110 integer type after the usual arithmetic conversions, and the type
111 must have minimum value MIN and maximum MAX. Unsigned types should
112 use a zero MIN of the proper type.
113
114 Because all arguments are subject to integer promotions, these
115 macros typically do not work on types narrower than 'int'.
116
117 These macros are tuned for constant MIN and MAX. For commutative
118 operations such as A + B, they are also tuned for constant B. */
119
120 /* Return 1 if A + B would overflow in [MIN,MAX] arithmetic.
121 See above for restrictions. */
122 #define INT_ADD_RANGE_OVERFLOW(a, b, min, max) \
123 ((b) < 0 \
124 ? (a) < (min) - (b) \
125 : (max) - (b) < (a))
126
127 /* Return 1 if A - B would overflow in [MIN,MAX] arithmetic.
128 See above for restrictions. */
129 #define INT_SUBTRACT_RANGE_OVERFLOW(a, b, min, max) \
130 ((b) < 0 \
131 ? (max) + (b) < (a) \
132 : (a) < (min) + (b))
133
134 /* Return 1 if - A would overflow in [MIN,MAX] arithmetic.
135 See above for restrictions. */
136 #define INT_NEGATE_RANGE_OVERFLOW(a, min, max) \
137 _GL_INT_NEGATE_RANGE_OVERFLOW (a, min, max)
138
139 /* Return 1 if A * B would overflow in [MIN,MAX] arithmetic.
140 See above for restrictions. Avoid && and || as they tickle
141 bugs in Sun C 5.11 2010/08/13 and other compilers; see
142 <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00401.html>. */
143 #define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \
144 ((b) < 0 \
145 ? ((a) < 0 \
146 ? (a) < (max) / (b) \
147 : (b) == -1 \
148 ? 0 \
149 : (min) / (b) < (a)) \
150 : (b) == 0 \
151 ? 0 \
152 : ((a) < 0 \
153 ? (a) < (min) / (b) \
154 : (max) / (b) < (a)))
155
156 /* Return 1 if A / B would overflow in [MIN,MAX] arithmetic.
157 See above for restrictions. Do not check for division by zero. */
158 #define INT_DIVIDE_RANGE_OVERFLOW(a, b, min, max) \
159 ((min) < 0 && (b) == -1 && (a) < - (max))
160
161 /* Return 1 if A % B would overflow in [MIN,MAX] arithmetic.
162 See above for restrictions. Do not check for division by zero.
163 Mathematically, % should never overflow, but on x86-like hosts
164 INT_MIN % -1 traps, and the C standard permits this, so treat this
165 as an overflow too. */
166 #define INT_REMAINDER_RANGE_OVERFLOW(a, b, min, max) \
167 INT_DIVIDE_RANGE_OVERFLOW (a, b, min, max)
168
169 /* Return 1 if A << B would overflow in [MIN,MAX] arithmetic.
170 See above for restrictions. Here, MIN and MAX are for A only, and B need
171 not be of the same type as the other arguments. The C standard says that
172 behavior is undefined for shifts unless 0 <= B < wordwidth, and that when
173 A is negative then A << B has undefined behavior and A >> B has
174 implementation-defined behavior, but do not check these other
175 restrictions. */
176 #define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \
177 ((a) < 0 \
178 ? (a) < (min) >> (b) \
179 : (max) >> (b) < (a))
180
181 /* The _GL*_OVERFLOW macros have the same restrictions as the
182 *_RANGE_OVERFLOW macros, except that they do not assume that operands
183 (e.g., A and B) have the same type as MIN and MAX. Instead, they assume
184 that the result (e.g., A + B) has that type. */
185 #if _GL_HAS_BUILTIN_OVERFLOW_P
186 # define _GL_ADD_OVERFLOW(a, b, min, max) \
187 __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0)
188 # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
189 __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
190 # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
191 __builtin_mul_overflow_p (a, b, (__typeof__ ((a) * (b))) 0)
192 #else
193 # define _GL_ADD_OVERFLOW(a, b, min, max) \
194 ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \
195 : (a) < 0 ? (b) <= (a) + (b) \
196 : (b) < 0 ? (a) <= (a) + (b) \
197 : (a) + (b) < (b))
198 # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
199 ((min) < 0 ? INT_SUBTRACT_RANGE_OVERFLOW (a, b, min, max) \
200 : (a) < 0 ? 1 \
201 : (b) < 0 ? (a) - (b) <= (a) \
202 : (a) < (b))
203 # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
204 (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a)))) \
205 || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max))
206 #endif
207 #define _GL_DIVIDE_OVERFLOW(a, b, min, max) \
208 ((min) < 0 ? (b) == _GL_INT_CONVERT (min, -1) && (a) < - (max) \
209 : (a) < 0 ? (b) <= (a) + (b) - 1 \
210 : (b) < 0 && (a) + (b) <= (a))
211 #define _GL_REMAINDER_OVERFLOW(a, b, min, max) \
212 ((min) < 0 ? (b) == _GL_INT_CONVERT (min, -1) && (a) < - (max) \
213 : (a) < 0 ? (a) % (b) != ((max) - (b) + 1) % (b) \
214 : (b) < 0 && ! _GL_UNSIGNED_NEG_MULTIPLE (a, b, max))
215
216 /* Return a nonzero value if A is a mathematical multiple of B, where
217 A is unsigned, B is negative, and MAX is the maximum value of A's
218 type. A's type must be the same as (A % B)'s type. Normally (A %
219 -B == 0) suffices, but things get tricky if -B would overflow. */
220 #define _GL_UNSIGNED_NEG_MULTIPLE(a, b, max) \
221 (((b) < -_GL_SIGNED_INT_MAXIMUM (b) \
222 ? (_GL_SIGNED_INT_MAXIMUM (b) == (max) \
223 ? (a) \
224 : (a) % (_GL_INT_CONVERT (a, _GL_SIGNED_INT_MAXIMUM (b)) + 1)) \
225 : (a) % - (b)) \
226 == 0)
227
228 /* Check for integer overflow, and report low order bits of answer.
229
230 The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators
231 might not yield numerically correct answers due to arithmetic overflow.
232 The INT_<op>_WRAPV macros compute the low-order bits of the sum,
233 difference, and product of two C integers, and return 1 if these
234 low-order bits are not numerically correct.
235 These macros work correctly on all known practical hosts, and do not rely
236 on undefined behavior due to signed arithmetic overflow.
237
238 Example usage, assuming A and B are long int:
239
240 if (INT_MULTIPLY_OVERFLOW (a, b))
241 printf ("result would overflow\n");
242 else
243 printf ("result is %ld (no overflow)\n", a * b);
244
245 Example usage with WRAPV flavor:
246
247 long int result;
248 bool overflow = INT_MULTIPLY_WRAPV (a, b, &result);
249 printf ("result is %ld (%s)\n", result,
250 overflow ? "after overflow" : "no overflow");
251
252 Restrictions on these macros:
253
254 These macros do not check for all possible numerical problems or
255 undefined or unspecified behavior: they do not check for division
256 by zero, for bad shift counts, or for shifting negative numbers.
257
258 These macros may evaluate their arguments zero or multiple times, so the
259 arguments should not have side effects.
260
261 The WRAPV macros are not constant expressions. They support only
262 +, binary -, and *.
263
264 Because the WRAPV macros convert the result, they report overflow
265 in different circumstances than the OVERFLOW macros do. For
266 example, in the typical case with 16-bit 'short' and 32-bit 'int',
267 if A, B and *R are all of type 'short' then INT_ADD_OVERFLOW (A, B)
268 returns false because the addition cannot overflow after A and B
269 are converted to 'int', whereas INT_ADD_WRAPV (A, B, R) returns
270 true or false depending on whether the sum fits into 'short'.
271
272 These macros are tuned for their last input argument being a constant.
273
274 A, B, and *R should be integers; they need not be the same type,
275 and they need not be all signed or all unsigned.
276 However, none of the integer types should be bit-precise,
277 and *R's type should not be char, bool, or an enumeration type.
278
279 Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B,
280 A % B, and A << B would overflow, respectively. */
281
282 #define INT_ADD_OVERFLOW(a, b) \
283 _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW)
284 #define INT_SUBTRACT_OVERFLOW(a, b) \
285 _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW)
286 #define INT_NEGATE_OVERFLOW(a) _GL_INT_NEGATE_OVERFLOW (a)
287 #define INT_MULTIPLY_OVERFLOW(a, b) \
288 _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW)
289 #define INT_DIVIDE_OVERFLOW(a, b) \
290 _GL_BINARY_OP_OVERFLOW (a, b, _GL_DIVIDE_OVERFLOW)
291 #define INT_REMAINDER_OVERFLOW(a, b) \
292 _GL_BINARY_OP_OVERFLOW (a, b, _GL_REMAINDER_OVERFLOW)
293 #define INT_LEFT_SHIFT_OVERFLOW(a, b) \
294 INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
295 _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
296
297 /* Return 1 if the expression A <op> B would overflow,
298 where OP_RESULT_OVERFLOW (A, B, MIN, MAX) does the actual test,
299 assuming MIN and MAX are the minimum and maximum for the result type.
300 Arguments should be free of side effects. */
301 #define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \
302 op_result_overflow (a, b, \
303 _GL_INT_MINIMUM (_GL_INT_CONVERT (a, b)), \
304 _GL_INT_MAXIMUM (_GL_INT_CONVERT (a, b)))
305
306 /* Store the low-order bits of A + B, A - B, A * B, respectively, into *R.
307 Return 1 if the result overflows. See above for restrictions. */
308 #define INT_ADD_WRAPV(a, b, r) _GL_INT_ADD_WRAPV (a, b, r)
309 #define INT_SUBTRACT_WRAPV(a, b, r) _GL_INT_SUBTRACT_WRAPV (a, b, r)
310 #define INT_MULTIPLY_WRAPV(a, b, r) _GL_INT_MULTIPLY_WRAPV (a, b, r)
311
312 /* The following macros compute A + B, A - B, and A * B, respectively.
313 If no overflow occurs, they set *R to the result and return 1;
314 otherwise, they return 0 and may modify *R.
315
316 Example usage:
317
318 long int result;
319 if (INT_ADD_OK (a, b, &result))
320 printf ("result is %ld\n", result);
321 else
322 printf ("overflow\n");
323
324 A, B, and *R should be integers; they need not be the same type,
325 and they need not be all signed or all unsigned.
326 However, none of the integer types should be bit-precise,
327 and *R's type should not be char, bool, or an enumeration type.
328
329 These macros work correctly on all known practical hosts, and do not rely
330 on undefined behavior due to signed arithmetic overflow.
331
332 These macros are not constant expressions.
333
334 These macros may evaluate their arguments zero or multiple times, so the
335 arguments should not have side effects.
336
337 These macros are tuned for B being a constant. */
338
339 #define INT_ADD_OK(a, b, r) (! INT_ADD_WRAPV (a, b, r))
340 #define INT_SUBTRACT_OK(a, b, r) (! INT_SUBTRACT_WRAPV (a, b, r))
341 #define INT_MULTIPLY_OK(a, b, r) (! INT_MULTIPLY_WRAPV (a, b, r))
342
343 #endif /* _GL_INTPROPS_H */