summaryrefslogtreecommitdiffstats
path: root/gl/intprops.h
diff options
context:
space:
mode:
Diffstat (limited to 'gl/intprops.h')
-rw-r--r--gl/intprops.h484
1 files changed, 402 insertions, 82 deletions
diff --git a/gl/intprops.h b/gl/intprops.h
index f57f9b4..3fe64e8 100644
--- a/gl/intprops.h
+++ b/gl/intprops.h
@@ -1,34 +1,33 @@
1/* intprops.h -- properties of integer types 1/* intprops.h -- properties of integer types
2 2
3 Copyright (C) 2001-2005, 2009-2013 Free Software Foundation, Inc. 3 Copyright (C) 2001-2021 Free Software Foundation, Inc.
4 4
5 This program is free software: you can redistribute it and/or modify 5 This program is free software: you can redistribute it and/or modify it
6 it under the terms of the GNU General Public License as published by 6 under the terms of the GNU Lesser General Public License as published
7 the Free Software Foundation; either version 3 of the License, or 7 by the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version. 8 (at your option) any later version.
9 9
10 This program is distributed in the hope that it will be useful, 10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details. 13 GNU Lesser General Public License for more details.
14 14
15 You should have received a copy of the GNU General Public License 15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 17
18/* Written by Paul Eggert. */
19 18
20#ifndef _GL_INTPROPS_H 19#ifndef _GL_INTPROPS_H
21#define _GL_INTPROPS_H 20#define _GL_INTPROPS_H
22 21
23#include <limits.h> 22#include <limits.h>
24 23
25/* Return an integer value, converted to the same type as the integer 24/* Return a value with the common real type of E and V and the value of V.
26 expression E after integer type promotion. V is the unconverted value. */ 25 Do not evaluate E. */
27#define _GL_INT_CONVERT(e, v) (0 * (e) + (v)) 26#define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v))
28 27
29/* Act like _GL_INT_CONVERT (E, -V) but work around a bug in IRIX 6.5 cc; see 28/* Act like _GL_INT_CONVERT (E, -V) but work around a bug in IRIX 6.5 cc; see
30 <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00406.html>. */ 29 <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00406.html>. */
31#define _GL_INT_NEGATE_CONVERT(e, v) (0 * (e) - (v)) 30#define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v))
32 31
33/* The extra casts in the following macros work around compiler bugs, 32/* The extra casts in the following macros work around compiler bugs,
34 e.g., in Cray C 5.0.3.0. */ 33 e.g., in Cray C 5.0.3.0. */
@@ -37,67 +36,67 @@
37 an integer. */ 36 an integer. */
38#define TYPE_IS_INTEGER(t) ((t) 1.5 == 1) 37#define TYPE_IS_INTEGER(t) ((t) 1.5 == 1)
39 38
40/* True if negative values of the signed integer type T use two's 39/* True if the real type T is signed. */
41 complement, ones' complement, or signed magnitude representation,
42 respectively. Much GNU code assumes two's complement, but some
43 people like to be portable to all possible C hosts. */
44#define TYPE_TWOS_COMPLEMENT(t) ((t) ~ (t) 0 == (t) -1)
45#define TYPE_ONES_COMPLEMENT(t) ((t) ~ (t) 0 == 0)
46#define TYPE_SIGNED_MAGNITUDE(t) ((t) ~ (t) 0 < (t) -1)
47
48/* True if the signed integer expression E uses two's complement. */
49#define _GL_INT_TWOS_COMPLEMENT(e) (~ _GL_INT_CONVERT (e, 0) == -1)
50
51/* True if the arithmetic type T is signed. */
52#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1)) 40#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
53 41
54/* Return 1 if the integer expression E, after integer promotion, has 42/* Return 1 if the real expression E, after promotion, has a
55 a signed type. */ 43 signed or floating type. Do not evaluate E. */
56#define _GL_INT_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0) 44#define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)
57 45
58 46
59/* Minimum and maximum values for integer types and expressions. These 47/* Minimum and maximum values for integer types and expressions. */
60 macros have undefined behavior if T is signed and has padding bits. 48
61 If this is a problem for you, please let us know how to fix it for 49/* The width in bits of the integer type or expression T.
62 your host. */ 50 Do not evaluate T. T must not be a bit-field expression.
51 Padding bits are not supported; this is checked at compile-time below. */
52#define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
63 53
64/* The maximum and minimum values for the integer type T. */ 54/* The maximum and minimum values for the integer type T. */
65#define TYPE_MINIMUM(t) \ 55#define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t))
66 ((t) (! TYPE_SIGNED (t) \
67 ? (t) 0 \
68 : TYPE_SIGNED_MAGNITUDE (t) \
69 ? ~ (t) 0 \
70 : ~ TYPE_MAXIMUM (t)))
71#define TYPE_MAXIMUM(t) \ 56#define TYPE_MAXIMUM(t) \
72 ((t) (! TYPE_SIGNED (t) \ 57 ((t) (! TYPE_SIGNED (t) \
73 ? (t) -1 \ 58 ? (t) -1 \
74 : ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1))) 59 : ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1)))
75 60
76/* The maximum and minimum values for the type of the expression E, 61/* The maximum and minimum values for the type of the expression E,
77 after integer promotion. E should not have side effects. */ 62 after integer promotion. E is not evaluated. */
78#define _GL_INT_MINIMUM(e) \ 63#define _GL_INT_MINIMUM(e) \
79 (_GL_INT_SIGNED (e) \ 64 (EXPR_SIGNED (e) \
80 ? - _GL_INT_TWOS_COMPLEMENT (e) - _GL_SIGNED_INT_MAXIMUM (e) \ 65 ? ~ _GL_SIGNED_INT_MAXIMUM (e) \
81 : _GL_INT_CONVERT (e, 0)) 66 : _GL_INT_CONVERT (e, 0))
82#define _GL_INT_MAXIMUM(e) \ 67#define _GL_INT_MAXIMUM(e) \
83 (_GL_INT_SIGNED (e) \ 68 (EXPR_SIGNED (e) \
84 ? _GL_SIGNED_INT_MAXIMUM (e) \ 69 ? _GL_SIGNED_INT_MAXIMUM (e) \
85 : _GL_INT_NEGATE_CONVERT (e, 1)) 70 : _GL_INT_NEGATE_CONVERT (e, 1))
86#define _GL_SIGNED_INT_MAXIMUM(e) \ 71#define _GL_SIGNED_INT_MAXIMUM(e) \
87 (((_GL_INT_CONVERT (e, 1) << (sizeof ((e) + 0) * CHAR_BIT - 2)) - 1) * 2 + 1) 72 (((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH (+ (e)) - 2)) - 1) * 2 + 1)
88 73
74/* Work around OpenVMS incompatibility with C99. */
75#if !defined LLONG_MAX && defined __INT64_MAX
76# define LLONG_MAX __INT64_MAX
77# define LLONG_MIN __INT64_MIN
78#endif
79
80/* This include file assumes that signed types are two's complement without
81 padding bits; the above macros have undefined behavior otherwise.
82 If this is a problem for you, please let us know how to fix it for your host.
83 This assumption is tested by the intprops-tests module. */
89 84
90/* Return 1 if the __typeof__ keyword works. This could be done by 85/* Does the __typeof__ keyword work? This could be done by
91 'configure', but for now it's easier to do it by hand. */ 86 'configure', but for now it's easier to do it by hand. */
92#if 2 <= __GNUC__ || defined __IBM__TYPEOF__ || 0x5110 <= __SUNPRO_C 87#if (2 <= __GNUC__ \
88 || (4 <= __clang_major__) \
89 || (1210 <= __IBMC__ && defined __IBM__TYPEOF__) \
90 || (0x5110 <= __SUNPRO_C && !__STDC__))
93# define _GL_HAVE___TYPEOF__ 1 91# define _GL_HAVE___TYPEOF__ 1
94#else 92#else
95# define _GL_HAVE___TYPEOF__ 0 93# define _GL_HAVE___TYPEOF__ 0
96#endif 94#endif
97 95
98/* Return 1 if the integer type or expression T might be signed. Return 0 96/* Return 1 if the integer type or expression T might be signed. Return 0
99 if it is definitely unsigned. This macro does not evaluate its argument, 97 if it is definitely unsigned. T must not be a bit-field expression.
100 and expands to an integer constant expression. */ 98 This macro does not evaluate its argument, and expands to an
99 integer constant expression. */
101#if _GL_HAVE___TYPEOF__ 100#if _GL_HAVE___TYPEOF__
102# define _GL_SIGNED_TYPE_OR_EXPR(t) TYPE_SIGNED (__typeof__ (t)) 101# define _GL_SIGNED_TYPE_OR_EXPR(t) TYPE_SIGNED (__typeof__ (t))
103#else 102#else
@@ -110,19 +109,20 @@
110#define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485) 109#define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485)
111 110
112/* Bound on length of the string representing an integer type or expression T. 111/* Bound on length of the string representing an integer type or expression T.
112 T must not be a bit-field expression.
113
113 Subtract 1 for the sign bit if T is signed, and then add 1 more for 114 Subtract 1 for the sign bit if T is signed, and then add 1 more for
114 a minus sign if needed. 115 a minus sign if needed.
115 116
116 Because _GL_SIGNED_TYPE_OR_EXPR sometimes returns 0 when its argument is 117 Because _GL_SIGNED_TYPE_OR_EXPR sometimes returns 1 when its argument is
117 signed, this macro may overestimate the true bound by one byte when 118 unsigned, this macro may overestimate the true bound by one byte when
118 applied to unsigned types of size 2, 4, 16, ... bytes. */ 119 applied to unsigned types of size 2, 4, 16, ... bytes. */
119#define INT_STRLEN_BOUND(t) \ 120#define INT_STRLEN_BOUND(t) \
120 (INT_BITS_STRLEN_BOUND (sizeof (t) * CHAR_BIT \ 121 (INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - _GL_SIGNED_TYPE_OR_EXPR (t)) \
121 - _GL_SIGNED_TYPE_OR_EXPR (t)) \
122 + _GL_SIGNED_TYPE_OR_EXPR (t)) 122 + _GL_SIGNED_TYPE_OR_EXPR (t))
123 123
124/* Bound on buffer size needed to represent an integer type or expression T, 124/* Bound on buffer size needed to represent an integer type or expression T,
125 including the terminating null. */ 125 including the terminating null. T must not be a bit-field expression. */
126#define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1) 126#define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1)
127 127
128 128
@@ -132,7 +132,8 @@
132 operators might not yield numerically correct answers due to 132 operators might not yield numerically correct answers due to
133 arithmetic overflow. They do not rely on undefined or 133 arithmetic overflow. They do not rely on undefined or
134 implementation-defined behavior. Their implementations are simple 134 implementation-defined behavior. Their implementations are simple
135 and straightforward, but they are a bit harder to use than the 135 and straightforward, but they are harder to use and may be less
136 efficient than the INT_<op>_WRAPV, INT_<op>_OK, and
136 INT_<op>_OVERFLOW macros described below. 137 INT_<op>_OVERFLOW macros described below.
137 138
138 Example usage: 139 Example usage:
@@ -157,6 +158,9 @@
157 must have minimum value MIN and maximum MAX. Unsigned types should 158 must have minimum value MIN and maximum MAX. Unsigned types should
158 use a zero MIN of the proper type. 159 use a zero MIN of the proper type.
159 160
161 Because all arguments are subject to integer promotions, these
162 macros typically do not work on types narrower than 'int'.
163
160 These macros are tuned for constant MIN and MAX. For commutative 164 These macros are tuned for constant MIN and MAX. For commutative
161 operations such as A + B, they are also tuned for constant B. */ 165 operations such as A + B, they are also tuned for constant B. */
162 166
@@ -184,7 +188,7 @@
184/* Return 1 if A * B would overflow in [MIN,MAX] arithmetic. 188/* Return 1 if A * B would overflow in [MIN,MAX] arithmetic.
185 See above for restrictions. Avoid && and || as they tickle 189 See above for restrictions. Avoid && and || as they tickle
186 bugs in Sun C 5.11 2010/08/13 and other compilers; see 190 bugs in Sun C 5.11 2010/08/13 and other compilers; see
187 <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00401.html>. */ 191 <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00401.html>. */
188#define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \ 192#define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \
189 ((b) < 0 \ 193 ((b) < 0 \
190 ? ((a) < 0 \ 194 ? ((a) < 0 \
@@ -223,24 +227,66 @@
223 ? (a) < (min) >> (b) \ 227 ? (a) < (min) >> (b) \
224 : (max) >> (b) < (a)) 228 : (max) >> (b) < (a))
225 229
230/* True if __builtin_add_overflow (A, B, P) and __builtin_sub_overflow
231 (A, B, P) work when P is non-null. */
232/* __builtin_{add,sub}_overflow exists but is not reliable in GCC 5.x and 6.x,
233 see <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98269>. */
234#if 7 <= __GNUC__ && !defined __ICC
235# define _GL_HAS_BUILTIN_ADD_OVERFLOW 1
236#elif defined __has_builtin
237# define _GL_HAS_BUILTIN_ADD_OVERFLOW __has_builtin (__builtin_add_overflow)
238#else
239# define _GL_HAS_BUILTIN_ADD_OVERFLOW 0
240#endif
241
242/* True if __builtin_mul_overflow (A, B, P) works when P is non-null. */
243#ifdef __clang__
244/* Work around Clang bug <https://bugs.llvm.org/show_bug.cgi?id=16404>. */
245# define _GL_HAS_BUILTIN_MUL_OVERFLOW 0
246#else
247# define _GL_HAS_BUILTIN_MUL_OVERFLOW _GL_HAS_BUILTIN_ADD_OVERFLOW
248#endif
249
250/* True if __builtin_add_overflow_p (A, B, C) works, and similarly for
251 __builtin_sub_overflow_p and __builtin_mul_overflow_p. */
252#if defined __clang__ || defined __ICC
253/* Clang 11 lacks __builtin_mul_overflow_p, and even if it did it
254 would presumably run afoul of Clang bug 16404. ICC 2021.1's
255 __builtin_add_overflow_p etc. are not treated as integral constant
256 expressions even when all arguments are. */
257# define _GL_HAS_BUILTIN_OVERFLOW_P 0
258#elif defined __has_builtin
259# define _GL_HAS_BUILTIN_OVERFLOW_P __has_builtin (__builtin_mul_overflow_p)
260#else
261# define _GL_HAS_BUILTIN_OVERFLOW_P (7 <= __GNUC__)
262#endif
226 263
227/* The _GL*_OVERFLOW macros have the same restrictions as the 264/* The _GL*_OVERFLOW macros have the same restrictions as the
228 *_RANGE_OVERFLOW macros, except that they do not assume that operands 265 *_RANGE_OVERFLOW macros, except that they do not assume that operands
229 (e.g., A and B) have the same type as MIN and MAX. Instead, they assume 266 (e.g., A and B) have the same type as MIN and MAX. Instead, they assume
230 that the result (e.g., A + B) has that type. */ 267 that the result (e.g., A + B) has that type. */
231#define _GL_ADD_OVERFLOW(a, b, min, max) \ 268#if _GL_HAS_BUILTIN_OVERFLOW_P
232 ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \ 269# define _GL_ADD_OVERFLOW(a, b, min, max) \
233 : (a) < 0 ? (b) <= (a) + (b) \ 270 __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0)
234 : (b) < 0 ? (a) <= (a) + (b) \ 271# define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
235 : (a) + (b) < (b)) 272 __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
236#define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \ 273# define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
237 ((min) < 0 ? INT_SUBTRACT_RANGE_OVERFLOW (a, b, min, max) \ 274 __builtin_mul_overflow_p (a, b, (__typeof__ ((a) * (b))) 0)
238 : (a) < 0 ? 1 \ 275#else
239 : (b) < 0 ? (a) - (b) <= (a) \ 276# define _GL_ADD_OVERFLOW(a, b, min, max) \
240 : (a) < (b)) 277 ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \
241#define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \ 278 : (a) < 0 ? (b) <= (a) + (b) \
242 (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a)))) \ 279 : (b) < 0 ? (a) <= (a) + (b) \
243 || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max)) 280 : (a) + (b) < (b))
281# define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
282 ((min) < 0 ? INT_SUBTRACT_RANGE_OVERFLOW (a, b, min, max) \
283 : (a) < 0 ? 1 \
284 : (b) < 0 ? (a) - (b) <= (a) \
285 : (a) < (b))
286# define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
287 (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a)))) \
288 || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max))
289#endif
244#define _GL_DIVIDE_OVERFLOW(a, b, min, max) \ 290#define _GL_DIVIDE_OVERFLOW(a, b, min, max) \
245 ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \ 291 ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
246 : (a) < 0 ? (b) <= (a) + (b) - 1 \ 292 : (a) < 0 ? (b) <= (a) + (b) - 1 \
@@ -262,22 +308,31 @@
262 : (a) % - (b)) \ 308 : (a) % - (b)) \
263 == 0) 309 == 0)
264 310
265 311/* Check for integer overflow, and report low order bits of answer.
266/* Integer overflow checks.
267 312
268 The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators 313 The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators
269 might not yield numerically correct answers due to arithmetic overflow. 314 might not yield numerically correct answers due to arithmetic overflow.
270 They work correctly on all known practical hosts, and do not rely 315 The INT_<op>_WRAPV macros compute the low-order bits of the sum,
316 difference, and product of two C integers, and return 1 if these
317 low-order bits are not numerically correct.
318 These macros work correctly on all known practical hosts, and do not rely
271 on undefined behavior due to signed arithmetic overflow. 319 on undefined behavior due to signed arithmetic overflow.
272 320
273 Example usage: 321 Example usage, assuming A and B are long int:
274 322
275 long int i = ...; 323 if (INT_MULTIPLY_OVERFLOW (a, b))
276 long int j = ...; 324 printf ("result would overflow\n");
277 if (INT_MULTIPLY_OVERFLOW (i, j))
278 printf ("multiply would overflow");
279 else 325 else
280 printf ("product is %ld", i * j); 326 printf ("result is %ld (no overflow)\n", a * b);
327
328 Example usage with WRAPV flavor:
329
330 long int result;
331 bool overflow = INT_MULTIPLY_WRAPV (a, b, &result);
332 printf ("result is %ld (%s)\n", result,
333 overflow ? "after overflow" : "no overflow");
334
335 Restrictions on these macros:
281 336
282 These macros do not check for all possible numerical problems or 337 These macros do not check for all possible numerical problems or
283 undefined or unspecified behavior: they do not check for division 338 undefined or unspecified behavior: they do not check for division
@@ -286,7 +341,18 @@
286 These macros may evaluate their arguments zero or multiple times, so the 341 These macros may evaluate their arguments zero or multiple times, so the
287 arguments should not have side effects. 342 arguments should not have side effects.
288 343
289 These macros are tuned for their last argument being a constant. 344 The WRAPV macros are not constant expressions. They support only
345 +, binary -, and *.
346
347 Because the WRAPV macros convert the result, they report overflow
348 in different circumstances than the OVERFLOW macros do. For
349 example, in the typical case with 16-bit 'short' and 32-bit 'int',
350 if A, B and R are all of type 'short' then INT_ADD_OVERFLOW (A, B)
351 returns false because the addition cannot overflow after A and B
352 are converted to 'int', whereas INT_ADD_WRAPV (A, B, &R) returns
353 true or false depending on whether the sum fits into 'short'.
354
355 These macros are tuned for their last input argument being a constant.
290 356
291 Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B, 357 Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B,
292 A % B, and A << B would overflow, respectively. */ 358 A % B, and A << B would overflow, respectively. */
@@ -295,8 +361,12 @@
295 _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW) 361 _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW)
296#define INT_SUBTRACT_OVERFLOW(a, b) \ 362#define INT_SUBTRACT_OVERFLOW(a, b) \
297 _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW) 363 _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW)
298#define INT_NEGATE_OVERFLOW(a) \ 364#if _GL_HAS_BUILTIN_OVERFLOW_P
299 INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a)) 365# define INT_NEGATE_OVERFLOW(a) INT_SUBTRACT_OVERFLOW (0, a)
366#else
367# define INT_NEGATE_OVERFLOW(a) \
368 INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
369#endif
300#define INT_MULTIPLY_OVERFLOW(a, b) \ 370#define INT_MULTIPLY_OVERFLOW(a, b) \
301 _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW) 371 _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW)
302#define INT_DIVIDE_OVERFLOW(a, b) \ 372#define INT_DIVIDE_OVERFLOW(a, b) \
@@ -313,7 +383,257 @@
313 Arguments should be free of side effects. */ 383 Arguments should be free of side effects. */
314#define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \ 384#define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \
315 op_result_overflow (a, b, \ 385 op_result_overflow (a, b, \
316 _GL_INT_MINIMUM (0 * (b) + (a)), \ 386 _GL_INT_MINIMUM (_GL_INT_CONVERT (a, b)), \
317 _GL_INT_MAXIMUM (0 * (b) + (a))) 387 _GL_INT_MAXIMUM (_GL_INT_CONVERT (a, b)))
388
389/* Store the low-order bits of A + B, A - B, A * B, respectively, into *R.
390 Return 1 if the result overflows. See above for restrictions. */
391#if _GL_HAS_BUILTIN_ADD_OVERFLOW
392# define INT_ADD_WRAPV(a, b, r) __builtin_add_overflow (a, b, r)
393# define INT_SUBTRACT_WRAPV(a, b, r) __builtin_sub_overflow (a, b, r)
394#else
395# define INT_ADD_WRAPV(a, b, r) \
396 _GL_INT_OP_WRAPV (a, b, r, +, _GL_INT_ADD_RANGE_OVERFLOW)
397# define INT_SUBTRACT_WRAPV(a, b, r) \
398 _GL_INT_OP_WRAPV (a, b, r, -, _GL_INT_SUBTRACT_RANGE_OVERFLOW)
399#endif
400#if _GL_HAS_BUILTIN_MUL_OVERFLOW
401# if ((9 < __GNUC__ + (3 <= __GNUC_MINOR__) \
402 || (__GNUC__ == 8 && 4 <= __GNUC_MINOR__)) \
403 && !defined __ICC)
404# define INT_MULTIPLY_WRAPV(a, b, r) __builtin_mul_overflow (a, b, r)
405# else
406 /* Work around GCC bug 91450. */
407# define INT_MULTIPLY_WRAPV(a, b, r) \
408 ((!_GL_SIGNED_TYPE_OR_EXPR (*(r)) && EXPR_SIGNED (a) && EXPR_SIGNED (b) \
409 && _GL_INT_MULTIPLY_RANGE_OVERFLOW (a, b, 0, (__typeof__ (*(r))) -1)) \
410 ? ((void) __builtin_mul_overflow (a, b, r), 1) \
411 : __builtin_mul_overflow (a, b, r))
412# endif
413#else
414# define INT_MULTIPLY_WRAPV(a, b, r) \
415 _GL_INT_OP_WRAPV (a, b, r, *, _GL_INT_MULTIPLY_RANGE_OVERFLOW)
416#endif
417
418/* Nonzero if this compiler has GCC bug 68193 or Clang bug 25390. See:
419 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68193
420 https://llvm.org/bugs/show_bug.cgi?id=25390
421 For now, assume all versions of GCC-like compilers generate bogus
422 warnings for _Generic. This matters only for compilers that
423 lack relevant builtins. */
424#if __GNUC__ || defined __clang__
425# define _GL__GENERIC_BOGUS 1
426#else
427# define _GL__GENERIC_BOGUS 0
428#endif
429
430/* Store the low-order bits of A <op> B into *R, where OP specifies
431 the operation and OVERFLOW the overflow predicate. Return 1 if the
432 result overflows. See above for restrictions. */
433#if 201112 <= __STDC_VERSION__ && !_GL__GENERIC_BOGUS
434# define _GL_INT_OP_WRAPV(a, b, r, op, overflow) \
435 (_Generic \
436 (*(r), \
437 signed char: \
438 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
439 signed char, SCHAR_MIN, SCHAR_MAX), \
440 unsigned char: \
441 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
442 unsigned char, 0, UCHAR_MAX), \
443 short int: \
444 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
445 short int, SHRT_MIN, SHRT_MAX), \
446 unsigned short int: \
447 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
448 unsigned short int, 0, USHRT_MAX), \
449 int: \
450 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
451 int, INT_MIN, INT_MAX), \
452 unsigned int: \
453 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
454 unsigned int, 0, UINT_MAX), \
455 long int: \
456 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
457 long int, LONG_MIN, LONG_MAX), \
458 unsigned long int: \
459 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
460 unsigned long int, 0, ULONG_MAX), \
461 long long int: \
462 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
463 long long int, LLONG_MIN, LLONG_MAX), \
464 unsigned long long int: \
465 _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
466 unsigned long long int, 0, ULLONG_MAX)))
467#else
468/* Store the low-order bits of A <op> B into *R, where OP specifies
469 the operation and OVERFLOW the overflow predicate. If *R is
470 signed, its type is ST with bounds SMIN..SMAX; otherwise its type
471 is UT with bounds U..UMAX. ST and UT are narrower than int.
472 Return 1 if the result overflows. See above for restrictions. */
473# if _GL_HAVE___TYPEOF__
474# define _GL_INT_OP_WRAPV_SMALLISH(a,b,r,op,overflow,st,smin,smax,ut,umax) \
475 (TYPE_SIGNED (__typeof__ (*(r))) \
476 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, st, smin, smax) \
477 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, ut, 0, umax))
478# else
479# define _GL_INT_OP_WRAPV_SMALLISH(a,b,r,op,overflow,st,smin,smax,ut,umax) \
480 (overflow (a, b, smin, smax) \
481 ? (overflow (a, b, 0, umax) \
482 ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st), 1) \
483 : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st)) < 0) \
484 : (overflow (a, b, 0, umax) \
485 ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st)) >= 0 \
486 : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st), 0)))
487# endif
488
489# define _GL_INT_OP_WRAPV(a, b, r, op, overflow) \
490 (sizeof *(r) == sizeof (signed char) \
491 ? _GL_INT_OP_WRAPV_SMALLISH (a, b, r, op, overflow, \
492 signed char, SCHAR_MIN, SCHAR_MAX, \
493 unsigned char, UCHAR_MAX) \
494 : sizeof *(r) == sizeof (short int) \
495 ? _GL_INT_OP_WRAPV_SMALLISH (a, b, r, op, overflow, \
496 short int, SHRT_MIN, SHRT_MAX, \
497 unsigned short int, USHRT_MAX) \
498 : sizeof *(r) == sizeof (int) \
499 ? (EXPR_SIGNED (*(r)) \
500 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
501 int, INT_MIN, INT_MAX) \
502 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
503 unsigned int, 0, UINT_MAX)) \
504 : _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow))
505# ifdef LLONG_MAX
506# define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
507 (sizeof *(r) == sizeof (long int) \
508 ? (EXPR_SIGNED (*(r)) \
509 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
510 long int, LONG_MIN, LONG_MAX) \
511 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
512 unsigned long int, 0, ULONG_MAX)) \
513 : (EXPR_SIGNED (*(r)) \
514 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
515 long long int, LLONG_MIN, LLONG_MAX) \
516 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
517 unsigned long long int, 0, ULLONG_MAX)))
518# else
519# define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
520 (EXPR_SIGNED (*(r)) \
521 ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
522 long int, LONG_MIN, LONG_MAX) \
523 : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
524 unsigned long int, 0, ULONG_MAX))
525# endif
526#endif
527
528/* Store the low-order bits of A <op> B into *R, where the operation
529 is given by OP. Use the unsigned type UT for calculation to avoid
530 overflow problems. *R's type is T, with extrema TMIN and TMAX.
531 T must be a signed integer type. Return 1 if the result overflows. */
532#define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \
533 (overflow (a, b, tmin, tmax) \
534 ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 1) \
535 : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 0))
536
537/* Return the low-order bits of A <op> B, where the operation is given
538 by OP. Use the unsigned type UT for calculation to avoid undefined
539 behavior on signed integer overflow, and convert the result to type T.
540 UT is at least as wide as T and is no narrower than unsigned int,
541 T is two's complement, and there is no padding or trap representations.
542 Assume that converting UT to T yields the low-order bits, as is
543 done in all known two's-complement C compilers. E.g., see:
544 https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html
545
546 According to the C standard, converting UT to T yields an
547 implementation-defined result or signal for values outside T's
548 range. However, code that works around this theoretical problem
549 runs afoul of a compiler bug in Oracle Studio 12.3 x86. See:
550 https://lists.gnu.org/r/bug-gnulib/2017-04/msg00049.html
551 As the compiler bug is real, don't try to work around the
552 theoretical problem. */
553
554#define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t) \
555 ((t) ((ut) (a) op (ut) (b)))
556
557/* Return true if the numeric values A + B, A - B, A * B fall outside
558 the range TMIN..TMAX. Arguments should be integer expressions
559 without side effects. TMIN should be signed and nonpositive.
560 TMAX should be positive, and should be signed unless TMIN is zero. */
561#define _GL_INT_ADD_RANGE_OVERFLOW(a, b, tmin, tmax) \
562 ((b) < 0 \
563 ? (((tmin) \
564 ? ((EXPR_SIGNED (_GL_INT_CONVERT (a, (tmin) - (b))) || (b) < (tmin)) \
565 && (a) < (tmin) - (b)) \
566 : (a) <= -1 - (b)) \
567 || ((EXPR_SIGNED (a) ? 0 <= (a) : (tmax) < (a)) && (tmax) < (a) + (b))) \
568 : (a) < 0 \
569 ? (((tmin) \
570 ? ((EXPR_SIGNED (_GL_INT_CONVERT (b, (tmin) - (a))) || (a) < (tmin)) \
571 && (b) < (tmin) - (a)) \
572 : (b) <= -1 - (a)) \
573 || ((EXPR_SIGNED (_GL_INT_CONVERT (a, b)) || (tmax) < (b)) \
574 && (tmax) < (a) + (b))) \
575 : (tmax) < (b) || (tmax) - (b) < (a))
576#define _GL_INT_SUBTRACT_RANGE_OVERFLOW(a, b, tmin, tmax) \
577 (((a) < 0) == ((b) < 0) \
578 ? ((a) < (b) \
579 ? !(tmin) || -1 - (tmin) < (b) - (a) - 1 \
580 : (tmax) < (a) - (b)) \
581 : (a) < 0 \
582 ? ((!EXPR_SIGNED (_GL_INT_CONVERT ((a) - (tmin), b)) && (a) - (tmin) < 0) \
583 || (a) - (tmin) < (b)) \
584 : ((! (EXPR_SIGNED (_GL_INT_CONVERT (tmax, b)) \
585 && EXPR_SIGNED (_GL_INT_CONVERT ((tmax) + (b), a))) \
586 && (tmax) <= -1 - (b)) \
587 || (tmax) + (b) < (a)))
588#define _GL_INT_MULTIPLY_RANGE_OVERFLOW(a, b, tmin, tmax) \
589 ((b) < 0 \
590 ? ((a) < 0 \
591 ? (EXPR_SIGNED (_GL_INT_CONVERT (tmax, b)) \
592 ? (a) < (tmax) / (b) \
593 : ((INT_NEGATE_OVERFLOW (b) \
594 ? _GL_INT_CONVERT (b, tmax) >> (TYPE_WIDTH (+ (b)) - 1) \
595 : (tmax) / -(b)) \
596 <= -1 - (a))) \
597 : INT_NEGATE_OVERFLOW (_GL_INT_CONVERT (b, tmin)) && (b) == -1 \
598 ? (EXPR_SIGNED (a) \
599 ? 0 < (a) + (tmin) \
600 : 0 < (a) && -1 - (tmin) < (a) - 1) \
601 : (tmin) / (b) < (a)) \
602 : (b) == 0 \
603 ? 0 \
604 : ((a) < 0 \
605 ? (INT_NEGATE_OVERFLOW (_GL_INT_CONVERT (a, tmin)) && (a) == -1 \
606 ? (EXPR_SIGNED (b) ? 0 < (b) + (tmin) : -1 - (tmin) < (b) - 1) \
607 : (tmin) / (a) < (b)) \
608 : (tmax) / (b) < (a)))
609
610/* The following macros compute A + B, A - B, and A * B, respectively.
611 If no overflow occurs, they set *R to the result and return 1;
612 otherwise, they return 0 and may modify *R.
613
614 Example usage:
615
616 long int result;
617 if (INT_ADD_OK (a, b, &result))
618 printf ("result is %ld\n", result);
619 else
620 printf ("overflow\n");
621
622 A, B, and *R should be integers; they need not be the same type,
623 and they need not be all signed or all unsigned.
624
625 These macros work correctly on all known practical hosts, and do not rely
626 on undefined behavior due to signed arithmetic overflow.
627
628 These macros are not constant expressions.
629
630 These macros may evaluate their arguments zero or multiple times, so the
631 arguments should not have side effects.
632
633 These macros are tuned for B being a constant. */
634
635#define INT_ADD_OK(a, b, r) ! INT_ADD_WRAPV (a, b, r)
636#define INT_SUBTRACT_OK(a, b, r) ! INT_SUBTRACT_WRAPV (a, b, r)
637#define INT_MULTIPLY_OK(a, b, r) ! INT_MULTIPLY_WRAPV (a, b, r)
318 638
319#endif /* _GL_INTPROPS_H */ 639#endif /* _GL_INTPROPS_H */