diff options
author | Kristian Schuster <116557017+KriSchu@users.noreply.github.com> | 2023-02-19 22:49:18 (GMT) |
---|---|---|
committer | Kristian Schuster <116557017+KriSchu@users.noreply.github.com> | 2023-02-19 22:49:18 (GMT) |
commit | a0d42777217296c0a7bdb1e1be8d8f6de1b24dd7 (patch) | |
tree | 8effe94c57b2f9796ba36090b07551baa8f1e1cb /gl/intprops.h | |
parent | ca3d59cd6918c9e2739e783b721d4c1122640fd3 (diff) | |
parent | c07206f2ccc2356aa74bc6813a94c2190017d44e (diff) | |
download | monitoring-plugins-a0d42777217296c0a7bdb1e1be8d8f6de1b24dd7.tar.gz |
Merge remote-tracking branch 'origin/master' into feature_check_disk_add_ignore_missing_option
Diffstat (limited to 'gl/intprops.h')
-rw-r--r-- | gl/intprops.h | 250 |
1 files changed, 133 insertions, 117 deletions
diff --git a/gl/intprops.h b/gl/intprops.h index f57f9b4..44b5e60 100644 --- a/gl/intprops.h +++ b/gl/intprops.h | |||
@@ -1,34 +1,24 @@ | |||
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-2023 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 | |||
18 | /* Written by Paul Eggert. */ | ||
19 | 17 | ||
20 | #ifndef _GL_INTPROPS_H | 18 | #ifndef _GL_INTPROPS_H |
21 | #define _GL_INTPROPS_H | 19 | #define _GL_INTPROPS_H |
22 | 20 | ||
23 | #include <limits.h> | 21 | #include "intprops-internal.h" |
24 | |||
25 | /* Return an integer value, converted to the same type as the integer | ||
26 | expression E after integer type promotion. V is the unconverted value. */ | ||
27 | #define _GL_INT_CONVERT(e, v) (0 * (e) + (v)) | ||
28 | |||
29 | /* 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>. */ | ||
31 | #define _GL_INT_NEGATE_CONVERT(e, v) (0 * (e) - (v)) | ||
32 | 22 | ||
33 | /* The extra casts in the following macros work around compiler bugs, | 23 | /* The extra casts in the following macros work around compiler bugs, |
34 | e.g., in Cray C 5.0.3.0. */ | 24 | e.g., in Cray C 5.0.3.0. */ |
@@ -37,72 +27,27 @@ | |||
37 | an integer. */ | 27 | an integer. */ |
38 | #define TYPE_IS_INTEGER(t) ((t) 1.5 == 1) | 28 | #define TYPE_IS_INTEGER(t) ((t) 1.5 == 1) |
39 | 29 | ||
40 | /* True if negative values of the signed integer type T use two's | 30 | /* True if the real type T is signed. */ |
41 | complement, ones' complement, or signed magnitude representation, | 31 | #define TYPE_SIGNED(t) _GL_TYPE_SIGNED (t) |
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 | 32 | ||
48 | /* True if the signed integer expression E uses two's complement. */ | 33 | /* Return 1 if the real expression E, after promotion, has a |
49 | #define _GL_INT_TWOS_COMPLEMENT(e) (~ _GL_INT_CONVERT (e, 0) == -1) | 34 | signed or floating type. Do not evaluate E. */ |
35 | #define EXPR_SIGNED(e) _GL_EXPR_SIGNED (e) | ||
50 | 36 | ||
51 | /* True if the arithmetic type T is signed. */ | ||
52 | #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1)) | ||
53 | 37 | ||
54 | /* Return 1 if the integer expression E, after integer promotion, has | 38 | /* Minimum and maximum values for integer types and expressions. */ |
55 | a signed type. */ | ||
56 | #define _GL_INT_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0) | ||
57 | 39 | ||
58 | 40 | /* The width in bits of the integer type or expression T. | |
59 | /* Minimum and maximum values for integer types and expressions. These | 41 | Do not evaluate T. T must not be a bit-field expression. |
60 | macros have undefined behavior if T is signed and has padding bits. | 42 | Padding bits are not supported; this is checked at compile-time below. */ |
61 | If this is a problem for you, please let us know how to fix it for | 43 | #define TYPE_WIDTH(t) _GL_TYPE_WIDTH (t) |
62 | your host. */ | ||
63 | 44 | ||
64 | /* The maximum and minimum values for the integer type T. */ | 45 | /* The maximum and minimum values for the integer type T. */ |
65 | #define TYPE_MINIMUM(t) \ | 46 | #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) \ | 47 | #define TYPE_MAXIMUM(t) \ |
72 | ((t) (! TYPE_SIGNED (t) \ | 48 | ((t) (! TYPE_SIGNED (t) \ |
73 | ? (t) -1 \ | 49 | ? (t) -1 \ |
74 | : ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1))) | 50 | : ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1))) |
75 | |||
76 | /* The maximum and minimum values for the type of the expression E, | ||
77 | after integer promotion. E should not have side effects. */ | ||
78 | #define _GL_INT_MINIMUM(e) \ | ||
79 | (_GL_INT_SIGNED (e) \ | ||
80 | ? - _GL_INT_TWOS_COMPLEMENT (e) - _GL_SIGNED_INT_MAXIMUM (e) \ | ||
81 | : _GL_INT_CONVERT (e, 0)) | ||
82 | #define _GL_INT_MAXIMUM(e) \ | ||
83 | (_GL_INT_SIGNED (e) \ | ||
84 | ? _GL_SIGNED_INT_MAXIMUM (e) \ | ||
85 | : _GL_INT_NEGATE_CONVERT (e, 1)) | ||
86 | #define _GL_SIGNED_INT_MAXIMUM(e) \ | ||
87 | (((_GL_INT_CONVERT (e, 1) << (sizeof ((e) + 0) * CHAR_BIT - 2)) - 1) * 2 + 1) | ||
88 | |||
89 | |||
90 | /* Return 1 if the __typeof__ keyword works. This could be done by | ||
91 | 'configure', but for now it's easier to do it by hand. */ | ||
92 | #if 2 <= __GNUC__ || defined __IBM__TYPEOF__ || 0x5110 <= __SUNPRO_C | ||
93 | # define _GL_HAVE___TYPEOF__ 1 | ||
94 | #else | ||
95 | # define _GL_HAVE___TYPEOF__ 0 | ||
96 | #endif | ||
97 | |||
98 | /* 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, | ||
100 | and expands to an integer constant expression. */ | ||
101 | #if _GL_HAVE___TYPEOF__ | ||
102 | # define _GL_SIGNED_TYPE_OR_EXPR(t) TYPE_SIGNED (__typeof__ (t)) | ||
103 | #else | ||
104 | # define _GL_SIGNED_TYPE_OR_EXPR(t) 1 | ||
105 | #endif | ||
106 | 51 | ||
107 | /* Bound on length of the string representing an unsigned integer | 52 | /* Bound on length of the string representing an unsigned integer |
108 | value representable in B bits. log10 (2.0) < 146/485. The | 53 | value representable in B bits. log10 (2.0) < 146/485. The |
@@ -110,30 +55,31 @@ | |||
110 | #define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485) | 55 | #define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485) |
111 | 56 | ||
112 | /* Bound on length of the string representing an integer type or expression T. | 57 | /* Bound on length of the string representing an integer type or expression T. |
58 | T must not be a bit-field expression. | ||
59 | |||
113 | Subtract 1 for the sign bit if T is signed, and then add 1 more for | 60 | Subtract 1 for the sign bit if T is signed, and then add 1 more for |
114 | a minus sign if needed. | 61 | a minus sign if needed. |
115 | 62 | ||
116 | Because _GL_SIGNED_TYPE_OR_EXPR sometimes returns 0 when its argument is | 63 | 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 | 64 | unsigned, this macro may overestimate the true bound by one byte when |
118 | applied to unsigned types of size 2, 4, 16, ... bytes. */ | 65 | applied to unsigned types of size 2, 4, 16, ... bytes. */ |
119 | #define INT_STRLEN_BOUND(t) \ | 66 | #define INT_STRLEN_BOUND(t) \ |
120 | (INT_BITS_STRLEN_BOUND (sizeof (t) * CHAR_BIT \ | 67 | (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)) | 68 | + _GL_SIGNED_TYPE_OR_EXPR (t)) |
123 | 69 | ||
124 | /* Bound on buffer size needed to represent an integer type or expression T, | 70 | /* Bound on buffer size needed to represent an integer type or expression T, |
125 | including the terminating null. */ | 71 | including the terminating null. T must not be a bit-field expression. */ |
126 | #define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1) | 72 | #define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1) |
127 | 73 | ||
128 | 74 | ||
129 | /* Range overflow checks. | 75 | /* Range overflow checks. |
130 | 76 | ||
131 | The INT_<op>_RANGE_OVERFLOW macros return 1 if the corresponding C | 77 | The INT_<op>_RANGE_OVERFLOW macros return 1 if the corresponding C |
132 | operators might not yield numerically correct answers due to | 78 | operators overflow arithmetically when given the same arguments. |
133 | arithmetic overflow. They do not rely on undefined or | 79 | These macros do not rely on undefined or implementation-defined behavior. |
134 | implementation-defined behavior. Their implementations are simple | 80 | Although their implementations are simple and straightforward, |
135 | and straightforward, but they are a bit harder to use than the | 81 | they are harder to use and may be less efficient than the |
136 | INT_<op>_OVERFLOW macros described below. | 82 | INT_<op>_WRAPV, INT_<op>_OK, and INT_<op>_OVERFLOW macros described below. |
137 | 83 | ||
138 | Example usage: | 84 | Example usage: |
139 | 85 | ||
@@ -157,6 +103,9 @@ | |||
157 | must have minimum value MIN and maximum MAX. Unsigned types should | 103 | must have minimum value MIN and maximum MAX. Unsigned types should |
158 | use a zero MIN of the proper type. | 104 | use a zero MIN of the proper type. |
159 | 105 | ||
106 | Because all arguments are subject to integer promotions, these | ||
107 | macros typically do not work on types narrower than 'int'. | ||
108 | |||
160 | These macros are tuned for constant MIN and MAX. For commutative | 109 | These macros are tuned for constant MIN and MAX. For commutative |
161 | operations such as A + B, they are also tuned for constant B. */ | 110 | operations such as A + B, they are also tuned for constant B. */ |
162 | 111 | ||
@@ -177,14 +126,12 @@ | |||
177 | /* Return 1 if - A would overflow in [MIN,MAX] arithmetic. | 126 | /* Return 1 if - A would overflow in [MIN,MAX] arithmetic. |
178 | See above for restrictions. */ | 127 | See above for restrictions. */ |
179 | #define INT_NEGATE_RANGE_OVERFLOW(a, min, max) \ | 128 | #define INT_NEGATE_RANGE_OVERFLOW(a, min, max) \ |
180 | ((min) < 0 \ | 129 | _GL_INT_NEGATE_RANGE_OVERFLOW (a, min, max) |
181 | ? (a) < - (max) \ | ||
182 | : 0 < (a)) | ||
183 | 130 | ||
184 | /* Return 1 if A * B would overflow in [MIN,MAX] arithmetic. | 131 | /* Return 1 if A * B would overflow in [MIN,MAX] arithmetic. |
185 | See above for restrictions. Avoid && and || as they tickle | 132 | See above for restrictions. Avoid && and || as they tickle |
186 | bugs in Sun C 5.11 2010/08/13 and other compilers; see | 133 | 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>. */ | 134 | <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00401.html>. */ |
188 | #define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \ | 135 | #define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \ |
189 | ((b) < 0 \ | 136 | ((b) < 0 \ |
190 | ? ((a) < 0 \ | 137 | ? ((a) < 0 \ |
@@ -223,24 +170,32 @@ | |||
223 | ? (a) < (min) >> (b) \ | 170 | ? (a) < (min) >> (b) \ |
224 | : (max) >> (b) < (a)) | 171 | : (max) >> (b) < (a)) |
225 | 172 | ||
226 | |||
227 | /* The _GL*_OVERFLOW macros have the same restrictions as the | 173 | /* The _GL*_OVERFLOW macros have the same restrictions as the |
228 | *_RANGE_OVERFLOW macros, except that they do not assume that operands | 174 | *_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 | 175 | (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. */ | 176 | that the result (e.g., A + B) has that type. */ |
231 | #define _GL_ADD_OVERFLOW(a, b, min, max) \ | 177 | #if _GL_HAS_BUILTIN_OVERFLOW_P |
232 | ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \ | 178 | # define _GL_ADD_OVERFLOW(a, b, min, max) \ |
233 | : (a) < 0 ? (b) <= (a) + (b) \ | 179 | __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0) |
234 | : (b) < 0 ? (a) <= (a) + (b) \ | 180 | # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \ |
235 | : (a) + (b) < (b)) | 181 | __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0) |
236 | #define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \ | 182 | # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \ |
237 | ((min) < 0 ? INT_SUBTRACT_RANGE_OVERFLOW (a, b, min, max) \ | 183 | __builtin_mul_overflow_p (a, b, (__typeof__ ((a) * (b))) 0) |
238 | : (a) < 0 ? 1 \ | 184 | #else |
239 | : (b) < 0 ? (a) - (b) <= (a) \ | 185 | # define _GL_ADD_OVERFLOW(a, b, min, max) \ |
240 | : (a) < (b)) | 186 | ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \ |
241 | #define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \ | 187 | : (a) < 0 ? (b) <= (a) + (b) \ |
242 | (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a)))) \ | 188 | : (b) < 0 ? (a) <= (a) + (b) \ |
243 | || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max)) | 189 | : (a) + (b) < (b)) |
190 | # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \ | ||
191 | ((min) < 0 ? INT_SUBTRACT_RANGE_OVERFLOW (a, b, min, max) \ | ||
192 | : (a) < 0 ? 1 \ | ||
193 | : (b) < 0 ? (a) - (b) <= (a) \ | ||
194 | : (a) < (b)) | ||
195 | # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \ | ||
196 | (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a)))) \ | ||
197 | || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max)) | ||
198 | #endif | ||
244 | #define _GL_DIVIDE_OVERFLOW(a, b, min, max) \ | 199 | #define _GL_DIVIDE_OVERFLOW(a, b, min, max) \ |
245 | ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \ | 200 | ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \ |
246 | : (a) < 0 ? (b) <= (a) + (b) - 1 \ | 201 | : (a) < 0 ? (b) <= (a) + (b) - 1 \ |
@@ -262,22 +217,31 @@ | |||
262 | : (a) % - (b)) \ | 217 | : (a) % - (b)) \ |
263 | == 0) | 218 | == 0) |
264 | 219 | ||
265 | 220 | /* Check for integer overflow, and report low order bits of answer. | |
266 | /* Integer overflow checks. | ||
267 | 221 | ||
268 | The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators | 222 | The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators |
269 | might not yield numerically correct answers due to arithmetic overflow. | 223 | might not yield numerically correct answers due to arithmetic overflow. |
270 | They work correctly on all known practical hosts, and do not rely | 224 | The INT_<op>_WRAPV macros compute the low-order bits of the sum, |
225 | difference, and product of two C integers, and return 1 if these | ||
226 | low-order bits are not numerically correct. | ||
227 | These macros work correctly on all known practical hosts, and do not rely | ||
271 | on undefined behavior due to signed arithmetic overflow. | 228 | on undefined behavior due to signed arithmetic overflow. |
272 | 229 | ||
273 | Example usage: | 230 | Example usage, assuming A and B are long int: |
274 | 231 | ||
275 | long int i = ...; | 232 | if (INT_MULTIPLY_OVERFLOW (a, b)) |
276 | long int j = ...; | 233 | printf ("result would overflow\n"); |
277 | if (INT_MULTIPLY_OVERFLOW (i, j)) | ||
278 | printf ("multiply would overflow"); | ||
279 | else | 234 | else |
280 | printf ("product is %ld", i * j); | 235 | printf ("result is %ld (no overflow)\n", a * b); |
236 | |||
237 | Example usage with WRAPV flavor: | ||
238 | |||
239 | long int result; | ||
240 | bool overflow = INT_MULTIPLY_WRAPV (a, b, &result); | ||
241 | printf ("result is %ld (%s)\n", result, | ||
242 | overflow ? "after overflow" : "no overflow"); | ||
243 | |||
244 | Restrictions on these macros: | ||
281 | 245 | ||
282 | These macros do not check for all possible numerical problems or | 246 | These macros do not check for all possible numerical problems or |
283 | undefined or unspecified behavior: they do not check for division | 247 | undefined or unspecified behavior: they do not check for division |
@@ -286,7 +250,23 @@ | |||
286 | These macros may evaluate their arguments zero or multiple times, so the | 250 | These macros may evaluate their arguments zero or multiple times, so the |
287 | arguments should not have side effects. | 251 | arguments should not have side effects. |
288 | 252 | ||
289 | These macros are tuned for their last argument being a constant. | 253 | The WRAPV macros are not constant expressions. They support only |
254 | +, binary -, and *. | ||
255 | |||
256 | Because the WRAPV macros convert the result, they report overflow | ||
257 | in different circumstances than the OVERFLOW macros do. For | ||
258 | example, in the typical case with 16-bit 'short' and 32-bit 'int', | ||
259 | if A, B and *R are all of type 'short' then INT_ADD_OVERFLOW (A, B) | ||
260 | returns false because the addition cannot overflow after A and B | ||
261 | are converted to 'int', whereas INT_ADD_WRAPV (A, B, R) returns | ||
262 | true or false depending on whether the sum fits into 'short'. | ||
263 | |||
264 | These macros are tuned for their last input argument being a constant. | ||
265 | |||
266 | A, B, and *R should be integers; they need not be the same type, | ||
267 | and they need not be all signed or all unsigned. | ||
268 | However, none of the integer types should be bit-precise, | ||
269 | and *R's type should not be char, bool, or an enumeration type. | ||
290 | 270 | ||
291 | Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B, | 271 | Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B, |
292 | A % B, and A << B would overflow, respectively. */ | 272 | A % B, and A << B would overflow, respectively. */ |
@@ -295,8 +275,7 @@ | |||
295 | _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW) | 275 | _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW) |
296 | #define INT_SUBTRACT_OVERFLOW(a, b) \ | 276 | #define INT_SUBTRACT_OVERFLOW(a, b) \ |
297 | _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW) | 277 | _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW) |
298 | #define INT_NEGATE_OVERFLOW(a) \ | 278 | #define INT_NEGATE_OVERFLOW(a) _GL_INT_NEGATE_OVERFLOW (a) |
299 | INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a)) | ||
300 | #define INT_MULTIPLY_OVERFLOW(a, b) \ | 279 | #define INT_MULTIPLY_OVERFLOW(a, b) \ |
301 | _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW) | 280 | _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW) |
302 | #define INT_DIVIDE_OVERFLOW(a, b) \ | 281 | #define INT_DIVIDE_OVERFLOW(a, b) \ |
@@ -313,7 +292,44 @@ | |||
313 | Arguments should be free of side effects. */ | 292 | Arguments should be free of side effects. */ |
314 | #define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \ | 293 | #define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \ |
315 | op_result_overflow (a, b, \ | 294 | op_result_overflow (a, b, \ |
316 | _GL_INT_MINIMUM (0 * (b) + (a)), \ | 295 | _GL_INT_MINIMUM (_GL_INT_CONVERT (a, b)), \ |
317 | _GL_INT_MAXIMUM (0 * (b) + (a))) | 296 | _GL_INT_MAXIMUM (_GL_INT_CONVERT (a, b))) |
297 | |||
298 | /* Store the low-order bits of A + B, A - B, A * B, respectively, into *R. | ||
299 | Return 1 if the result overflows. See above for restrictions. */ | ||
300 | #define INT_ADD_WRAPV(a, b, r) _GL_INT_ADD_WRAPV (a, b, r) | ||
301 | #define INT_SUBTRACT_WRAPV(a, b, r) _GL_INT_SUBTRACT_WRAPV (a, b, r) | ||
302 | #define INT_MULTIPLY_WRAPV(a, b, r) _GL_INT_MULTIPLY_WRAPV (a, b, r) | ||
303 | |||
304 | /* The following macros compute A + B, A - B, and A * B, respectively. | ||
305 | If no overflow occurs, they set *R to the result and return 1; | ||
306 | otherwise, they return 0 and may modify *R. | ||
307 | |||
308 | Example usage: | ||
309 | |||
310 | long int result; | ||
311 | if (INT_ADD_OK (a, b, &result)) | ||
312 | printf ("result is %ld\n", result); | ||
313 | else | ||
314 | printf ("overflow\n"); | ||
315 | |||
316 | A, B, and *R should be integers; they need not be the same type, | ||
317 | and they need not be all signed or all unsigned. | ||
318 | However, none of the integer types should be bit-precise, | ||
319 | and *R's type should not be char, bool, or an enumeration type. | ||
320 | |||
321 | These macros work correctly on all known practical hosts, and do not rely | ||
322 | on undefined behavior due to signed arithmetic overflow. | ||
323 | |||
324 | These macros are not constant expressions. | ||
325 | |||
326 | These macros may evaluate their arguments zero or multiple times, so the | ||
327 | arguments should not have side effects. | ||
328 | |||
329 | These macros are tuned for B being a constant. */ | ||
330 | |||
331 | #define INT_ADD_OK(a, b, r) (! INT_ADD_WRAPV (a, b, r)) | ||
332 | #define INT_SUBTRACT_OK(a, b, r) (! INT_SUBTRACT_WRAPV (a, b, r)) | ||
333 | #define INT_MULTIPLY_OK(a, b, r) (! INT_MULTIPLY_WRAPV (a, b, r)) | ||
318 | 334 | ||
319 | #endif /* _GL_INTPROPS_H */ | 335 | #endif /* _GL_INTPROPS_H */ |