Commit | Line | Data |
---|---|---|
6ec2e0f5 SDJ |
1 | /* intprops.h -- properties of integer types |
2 | ||
5e8754f9 | 3 | Copyright (C) 2001-2016 Free Software Foundation, Inc. |
6ec2e0f5 SDJ |
4 | |
5 | This program is free software: you can redistribute it and/or modify it | |
6 | under the terms of the GNU General Public License as published | |
7 | by the Free Software Foundation; either version 3 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 General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
5e8754f9 | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
6ec2e0f5 SDJ |
17 | |
18 | /* Written by Paul Eggert. */ | |
19 | ||
20 | #ifndef _GL_INTPROPS_H | |
21 | #define _GL_INTPROPS_H | |
22 | ||
23 | #include <limits.h> | |
5e8754f9 SDJ |
24 | #include <verify.h> |
25 | ||
26 | #ifndef __has_builtin | |
27 | # define __has_builtin(x) 0 | |
28 | #endif | |
6ec2e0f5 SDJ |
29 | |
30 | /* Return a value with the common real type of E and V and the value of V. */ | |
31 | #define _GL_INT_CONVERT(e, v) (0 * (e) + (v)) | |
32 | ||
33 | /* Act like _GL_INT_CONVERT (E, -V) but work around a bug in IRIX 6.5 cc; see | |
5e8754f9 | 34 | <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00406.html>. */ |
6ec2e0f5 SDJ |
35 | #define _GL_INT_NEGATE_CONVERT(e, v) (0 * (e) - (v)) |
36 | ||
37 | /* The extra casts in the following macros work around compiler bugs, | |
38 | e.g., in Cray C 5.0.3.0. */ | |
39 | ||
40 | /* True if the arithmetic type T is an integer type. bool counts as | |
41 | an integer. */ | |
42 | #define TYPE_IS_INTEGER(t) ((t) 1.5 == 1) | |
43 | ||
44 | /* True if the real type T is signed. */ | |
45 | #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1)) | |
46 | ||
47 | /* Return 1 if the real expression E, after promotion, has a | |
48 | signed or floating type. */ | |
49 | #define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0) | |
50 | ||
51 | ||
52 | /* Minimum and maximum values for integer types and expressions. */ | |
53 | ||
54 | /* The width in bits of the integer type or expression T. | |
55 | Padding bits are not supported; this is checked at compile-time below. */ | |
56 | #define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT) | |
57 | ||
58 | /* The maximum and minimum values for the integer type T. */ | |
59 | #define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t)) | |
60 | #define TYPE_MAXIMUM(t) \ | |
61 | ((t) (! TYPE_SIGNED (t) \ | |
62 | ? (t) -1 \ | |
63 | : ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1))) | |
64 | ||
65 | /* The maximum and minimum values for the type of the expression E, | |
66 | after integer promotion. E should not have side effects. */ | |
67 | #define _GL_INT_MINIMUM(e) \ | |
68 | (EXPR_SIGNED (e) \ | |
69 | ? ~ _GL_SIGNED_INT_MAXIMUM (e) \ | |
70 | : _GL_INT_CONVERT (e, 0)) | |
71 | #define _GL_INT_MAXIMUM(e) \ | |
72 | (EXPR_SIGNED (e) \ | |
73 | ? _GL_SIGNED_INT_MAXIMUM (e) \ | |
74 | : _GL_INT_NEGATE_CONVERT (e, 1)) | |
75 | #define _GL_SIGNED_INT_MAXIMUM(e) \ | |
76 | (((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH ((e) + 0) - 2)) - 1) * 2 + 1) | |
77 | ||
78 | /* Work around OpenVMS incompatibility with C99. */ | |
79 | #if !defined LLONG_MAX && defined __INT64_MAX | |
80 | # define LLONG_MAX __INT64_MAX | |
81 | # define LLONG_MIN __INT64_MIN | |
82 | #endif | |
83 | ||
84 | /* This include file assumes that signed types are two's complement without | |
85 | padding bits; the above macros have undefined behavior otherwise. | |
86 | If this is a problem for you, please let us know how to fix it for your host. | |
5e8754f9 SDJ |
87 | As a sanity check, test the assumption for some signed types that |
88 | <limits.h> bounds. */ | |
89 | verify (TYPE_MINIMUM (signed char) == SCHAR_MIN); | |
90 | verify (TYPE_MAXIMUM (signed char) == SCHAR_MAX); | |
91 | verify (TYPE_MINIMUM (short int) == SHRT_MIN); | |
92 | verify (TYPE_MAXIMUM (short int) == SHRT_MAX); | |
93 | verify (TYPE_MINIMUM (int) == INT_MIN); | |
94 | verify (TYPE_MAXIMUM (int) == INT_MAX); | |
95 | verify (TYPE_MINIMUM (long int) == LONG_MIN); | |
96 | verify (TYPE_MAXIMUM (long int) == LONG_MAX); | |
97 | #ifdef LLONG_MAX | |
98 | verify (TYPE_MINIMUM (long long int) == LLONG_MIN); | |
99 | verify (TYPE_MAXIMUM (long long int) == LLONG_MAX); | |
100 | #endif | |
101 | /* Similarly, sanity-check one ISO/IEC TS 18661-1:2014 macro if defined. */ | |
102 | #ifdef UINT_WIDTH | |
103 | verify (TYPE_WIDTH (unsigned int) == UINT_WIDTH); | |
104 | #endif | |
6ec2e0f5 SDJ |
105 | |
106 | /* Does the __typeof__ keyword work? This could be done by | |
107 | 'configure', but for now it's easier to do it by hand. */ | |
108 | #if (2 <= __GNUC__ \ | |
109 | || (1210 <= __IBMC__ && defined __IBM__TYPEOF__) \ | |
110 | || (0x5110 <= __SUNPRO_C && !__STDC__)) | |
111 | # define _GL_HAVE___TYPEOF__ 1 | |
112 | #else | |
113 | # define _GL_HAVE___TYPEOF__ 0 | |
114 | #endif | |
115 | ||
116 | /* Return 1 if the integer type or expression T might be signed. Return 0 | |
117 | if it is definitely unsigned. This macro does not evaluate its argument, | |
118 | and expands to an integer constant expression. */ | |
119 | #if _GL_HAVE___TYPEOF__ | |
120 | # define _GL_SIGNED_TYPE_OR_EXPR(t) TYPE_SIGNED (__typeof__ (t)) | |
121 | #else | |
122 | # define _GL_SIGNED_TYPE_OR_EXPR(t) 1 | |
123 | #endif | |
124 | ||
125 | /* Bound on length of the string representing an unsigned integer | |
126 | value representable in B bits. log10 (2.0) < 146/485. The | |
127 | smallest value of B where this bound is not tight is 2621. */ | |
128 | #define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485) | |
129 | ||
130 | /* Bound on length of the string representing an integer type or expression T. | |
131 | Subtract 1 for the sign bit if T is signed, and then add 1 more for | |
132 | a minus sign if needed. | |
133 | ||
134 | Because _GL_SIGNED_TYPE_OR_EXPR sometimes returns 0 when its argument is | |
135 | signed, this macro may overestimate the true bound by one byte when | |
136 | applied to unsigned types of size 2, 4, 16, ... bytes. */ | |
137 | #define INT_STRLEN_BOUND(t) \ | |
138 | (INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - _GL_SIGNED_TYPE_OR_EXPR (t)) \ | |
139 | + _GL_SIGNED_TYPE_OR_EXPR (t)) | |
140 | ||
141 | /* Bound on buffer size needed to represent an integer type or expression T, | |
142 | including the terminating null. */ | |
143 | #define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1) | |
144 | ||
145 | ||
146 | /* Range overflow checks. | |
147 | ||
148 | The INT_<op>_RANGE_OVERFLOW macros return 1 if the corresponding C | |
149 | operators might not yield numerically correct answers due to | |
150 | arithmetic overflow. They do not rely on undefined or | |
151 | implementation-defined behavior. Their implementations are simple | |
152 | and straightforward, but they are a bit harder to use than the | |
153 | INT_<op>_OVERFLOW macros described below. | |
154 | ||
155 | Example usage: | |
156 | ||
157 | long int i = ...; | |
158 | long int j = ...; | |
159 | if (INT_MULTIPLY_RANGE_OVERFLOW (i, j, LONG_MIN, LONG_MAX)) | |
160 | printf ("multiply would overflow"); | |
161 | else | |
162 | printf ("product is %ld", i * j); | |
163 | ||
164 | Restrictions on *_RANGE_OVERFLOW macros: | |
165 | ||
166 | These macros do not check for all possible numerical problems or | |
167 | undefined or unspecified behavior: they do not check for division | |
168 | by zero, for bad shift counts, or for shifting negative numbers. | |
169 | ||
170 | These macros may evaluate their arguments zero or multiple times, | |
171 | so the arguments should not have side effects. The arithmetic | |
172 | arguments (including the MIN and MAX arguments) must be of the same | |
173 | integer type after the usual arithmetic conversions, and the type | |
174 | must have minimum value MIN and maximum MAX. Unsigned types should | |
175 | use a zero MIN of the proper type. | |
176 | ||
177 | These macros are tuned for constant MIN and MAX. For commutative | |
178 | operations such as A + B, they are also tuned for constant B. */ | |
179 | ||
180 | /* Return 1 if A + B would overflow in [MIN,MAX] arithmetic. | |
181 | See above for restrictions. */ | |
182 | #define INT_ADD_RANGE_OVERFLOW(a, b, min, max) \ | |
183 | ((b) < 0 \ | |
184 | ? (a) < (min) - (b) \ | |
185 | : (max) - (b) < (a)) | |
186 | ||
187 | /* Return 1 if A - B would overflow in [MIN,MAX] arithmetic. | |
188 | See above for restrictions. */ | |
189 | #define INT_SUBTRACT_RANGE_OVERFLOW(a, b, min, max) \ | |
190 | ((b) < 0 \ | |
191 | ? (max) + (b) < (a) \ | |
192 | : (a) < (min) + (b)) | |
193 | ||
194 | /* Return 1 if - A would overflow in [MIN,MAX] arithmetic. | |
195 | See above for restrictions. */ | |
196 | #define INT_NEGATE_RANGE_OVERFLOW(a, min, max) \ | |
197 | ((min) < 0 \ | |
198 | ? (a) < - (max) \ | |
199 | : 0 < (a)) | |
200 | ||
201 | /* Return 1 if A * B would overflow in [MIN,MAX] arithmetic. | |
202 | See above for restrictions. Avoid && and || as they tickle | |
203 | bugs in Sun C 5.11 2010/08/13 and other compilers; see | |
5e8754f9 | 204 | <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00401.html>. */ |
6ec2e0f5 SDJ |
205 | #define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \ |
206 | ((b) < 0 \ | |
207 | ? ((a) < 0 \ | |
208 | ? (a) < (max) / (b) \ | |
209 | : (b) == -1 \ | |
210 | ? 0 \ | |
211 | : (min) / (b) < (a)) \ | |
212 | : (b) == 0 \ | |
213 | ? 0 \ | |
214 | : ((a) < 0 \ | |
215 | ? (a) < (min) / (b) \ | |
216 | : (max) / (b) < (a))) | |
217 | ||
218 | /* Return 1 if A / B would overflow in [MIN,MAX] arithmetic. | |
219 | See above for restrictions. Do not check for division by zero. */ | |
220 | #define INT_DIVIDE_RANGE_OVERFLOW(a, b, min, max) \ | |
221 | ((min) < 0 && (b) == -1 && (a) < - (max)) | |
222 | ||
223 | /* Return 1 if A % B would overflow in [MIN,MAX] arithmetic. | |
224 | See above for restrictions. Do not check for division by zero. | |
225 | Mathematically, % should never overflow, but on x86-like hosts | |
226 | INT_MIN % -1 traps, and the C standard permits this, so treat this | |
227 | as an overflow too. */ | |
228 | #define INT_REMAINDER_RANGE_OVERFLOW(a, b, min, max) \ | |
229 | INT_DIVIDE_RANGE_OVERFLOW (a, b, min, max) | |
230 | ||
231 | /* Return 1 if A << B would overflow in [MIN,MAX] arithmetic. | |
232 | See above for restrictions. Here, MIN and MAX are for A only, and B need | |
233 | not be of the same type as the other arguments. The C standard says that | |
234 | behavior is undefined for shifts unless 0 <= B < wordwidth, and that when | |
235 | A is negative then A << B has undefined behavior and A >> B has | |
236 | implementation-defined behavior, but do not check these other | |
237 | restrictions. */ | |
238 | #define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \ | |
239 | ((a) < 0 \ | |
240 | ? (a) < (min) >> (b) \ | |
241 | : (max) >> (b) < (a)) | |
242 | ||
243 | /* True if __builtin_add_overflow (A, B, P) works when P is non-null. */ | |
5e8754f9 SDJ |
244 | #define _GL_HAS_BUILTIN_OVERFLOW \ |
245 | (5 <= __GNUC__ || __has_builtin (__builtin_add_overflow)) | |
6ec2e0f5 SDJ |
246 | |
247 | /* True if __builtin_add_overflow_p (A, B, C) works. */ | |
5e8754f9 SDJ |
248 | #define _GL_HAS_BUILTIN_OVERFLOW_P \ |
249 | (7 <= __GNUC__ || __has_builtin (__builtin_add_overflow_p)) | |
6ec2e0f5 SDJ |
250 | |
251 | /* The _GL*_OVERFLOW macros have the same restrictions as the | |
252 | *_RANGE_OVERFLOW macros, except that they do not assume that operands | |
253 | (e.g., A and B) have the same type as MIN and MAX. Instead, they assume | |
254 | that the result (e.g., A + B) has that type. */ | |
255 | #if _GL_HAS_BUILTIN_OVERFLOW_P | |
256 | # define _GL_ADD_OVERFLOW(a, b, min, max) \ | |
257 | __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0) | |
258 | # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \ | |
259 | __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0) | |
260 | # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \ | |
261 | __builtin_mul_overflow_p (a, b, (__typeof__ ((a) * (b))) 0) | |
262 | #else | |
263 | # define _GL_ADD_OVERFLOW(a, b, min, max) \ | |
264 | ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \ | |
265 | : (a) < 0 ? (b) <= (a) + (b) \ | |
266 | : (b) < 0 ? (a) <= (a) + (b) \ | |
267 | : (a) + (b) < (b)) | |
268 | # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \ | |
269 | ((min) < 0 ? INT_SUBTRACT_RANGE_OVERFLOW (a, b, min, max) \ | |
270 | : (a) < 0 ? 1 \ | |
271 | : (b) < 0 ? (a) - (b) <= (a) \ | |
272 | : (a) < (b)) | |
273 | # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \ | |
274 | (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a)))) \ | |
275 | || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max)) | |
276 | #endif | |
277 | #define _GL_DIVIDE_OVERFLOW(a, b, min, max) \ | |
278 | ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \ | |
279 | : (a) < 0 ? (b) <= (a) + (b) - 1 \ | |
280 | : (b) < 0 && (a) + (b) <= (a)) | |
281 | #define _GL_REMAINDER_OVERFLOW(a, b, min, max) \ | |
282 | ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \ | |
283 | : (a) < 0 ? (a) % (b) != ((max) - (b) + 1) % (b) \ | |
284 | : (b) < 0 && ! _GL_UNSIGNED_NEG_MULTIPLE (a, b, max)) | |
285 | ||
286 | /* Return a nonzero value if A is a mathematical multiple of B, where | |
287 | A is unsigned, B is negative, and MAX is the maximum value of A's | |
288 | type. A's type must be the same as (A % B)'s type. Normally (A % | |
289 | -B == 0) suffices, but things get tricky if -B would overflow. */ | |
290 | #define _GL_UNSIGNED_NEG_MULTIPLE(a, b, max) \ | |
291 | (((b) < -_GL_SIGNED_INT_MAXIMUM (b) \ | |
292 | ? (_GL_SIGNED_INT_MAXIMUM (b) == (max) \ | |
293 | ? (a) \ | |
294 | : (a) % (_GL_INT_CONVERT (a, _GL_SIGNED_INT_MAXIMUM (b)) + 1)) \ | |
295 | : (a) % - (b)) \ | |
296 | == 0) | |
297 | ||
298 | /* Check for integer overflow, and report low order bits of answer. | |
299 | ||
300 | The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators | |
301 | might not yield numerically correct answers due to arithmetic overflow. | |
302 | The INT_<op>_WRAPV macros also store the low-order bits of the answer. | |
303 | These macros work correctly on all known practical hosts, and do not rely | |
304 | on undefined behavior due to signed arithmetic overflow. | |
305 | ||
306 | Example usage, assuming A and B are long int: | |
307 | ||
308 | if (INT_MULTIPLY_OVERFLOW (a, b)) | |
309 | printf ("result would overflow\n"); | |
310 | else | |
311 | printf ("result is %ld (no overflow)\n", a * b); | |
312 | ||
313 | Example usage with WRAPV flavor: | |
314 | ||
315 | long int result; | |
316 | bool overflow = INT_MULTIPLY_WRAPV (a, b, &result); | |
317 | printf ("result is %ld (%s)\n", result, | |
318 | overflow ? "after overflow" : "no overflow"); | |
319 | ||
320 | Restrictions on these macros: | |
321 | ||
322 | These macros do not check for all possible numerical problems or | |
323 | undefined or unspecified behavior: they do not check for division | |
324 | by zero, for bad shift counts, or for shifting negative numbers. | |
325 | ||
326 | These macros may evaluate their arguments zero or multiple times, so the | |
327 | arguments should not have side effects. | |
328 | ||
329 | The WRAPV macros are not constant expressions. They support only | |
330 | +, binary -, and *. The result type must be signed. | |
331 | ||
332 | These macros are tuned for their last argument being a constant. | |
333 | ||
334 | Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B, | |
335 | A % B, and A << B would overflow, respectively. */ | |
336 | ||
337 | #define INT_ADD_OVERFLOW(a, b) \ | |
338 | _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW) | |
339 | #define INT_SUBTRACT_OVERFLOW(a, b) \ | |
340 | _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW) | |
341 | #if _GL_HAS_BUILTIN_OVERFLOW_P | |
342 | # define INT_NEGATE_OVERFLOW(a) INT_SUBTRACT_OVERFLOW (0, a) | |
343 | #else | |
344 | # define INT_NEGATE_OVERFLOW(a) \ | |
345 | INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a)) | |
346 | #endif | |
347 | #define INT_MULTIPLY_OVERFLOW(a, b) \ | |
348 | _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW) | |
349 | #define INT_DIVIDE_OVERFLOW(a, b) \ | |
350 | _GL_BINARY_OP_OVERFLOW (a, b, _GL_DIVIDE_OVERFLOW) | |
351 | #define INT_REMAINDER_OVERFLOW(a, b) \ | |
352 | _GL_BINARY_OP_OVERFLOW (a, b, _GL_REMAINDER_OVERFLOW) | |
353 | #define INT_LEFT_SHIFT_OVERFLOW(a, b) \ | |
354 | INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \ | |
355 | _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a)) | |
356 | ||
357 | /* Return 1 if the expression A <op> B would overflow, | |
358 | where OP_RESULT_OVERFLOW (A, B, MIN, MAX) does the actual test, | |
359 | assuming MIN and MAX are the minimum and maximum for the result type. | |
360 | Arguments should be free of side effects. */ | |
361 | #define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \ | |
362 | op_result_overflow (a, b, \ | |
363 | _GL_INT_MINIMUM (0 * (b) + (a)), \ | |
364 | _GL_INT_MAXIMUM (0 * (b) + (a))) | |
365 | ||
366 | /* Store the low-order bits of A + B, A - B, A * B, respectively, into *R. | |
367 | Return 1 if the result overflows. See above for restrictions. */ | |
368 | #define INT_ADD_WRAPV(a, b, r) \ | |
369 | _GL_INT_OP_WRAPV (a, b, r, +, __builtin_add_overflow, INT_ADD_OVERFLOW) | |
370 | #define INT_SUBTRACT_WRAPV(a, b, r) \ | |
371 | _GL_INT_OP_WRAPV (a, b, r, -, __builtin_sub_overflow, INT_SUBTRACT_OVERFLOW) | |
372 | #define INT_MULTIPLY_WRAPV(a, b, r) \ | |
373 | _GL_INT_OP_WRAPV (a, b, r, *, __builtin_mul_overflow, INT_MULTIPLY_OVERFLOW) | |
374 | ||
375 | /* Nonzero if this compiler has GCC bug 68193 or Clang bug 25390. See: | |
376 | https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68193 | |
377 | https://llvm.org/bugs/show_bug.cgi?id=25390 | |
378 | For now, assume all versions of GCC-like compilers generate bogus | |
379 | warnings for _Generic. This matters only for older compilers that | |
380 | lack __builtin_add_overflow. */ | |
381 | #if __GNUC__ | |
382 | # define _GL__GENERIC_BOGUS 1 | |
383 | #else | |
384 | # define _GL__GENERIC_BOGUS 0 | |
385 | #endif | |
386 | ||
387 | /* Store the low-order bits of A <op> B into *R, where OP specifies | |
388 | the operation. BUILTIN is the builtin operation, and OVERFLOW the | |
389 | overflow predicate. Return 1 if the result overflows. See above | |
390 | for restrictions. */ | |
391 | #if _GL_HAS_BUILTIN_OVERFLOW | |
392 | # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) builtin (a, b, r) | |
393 | #elif 201112 <= __STDC_VERSION__ && !_GL__GENERIC_BOGUS | |
394 | # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \ | |
395 | (_Generic \ | |
396 | (*(r), \ | |
397 | signed char: \ | |
5e8754f9 | 398 | _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned char, \ |
6ec2e0f5 SDJ |
399 | signed char, SCHAR_MIN, SCHAR_MAX), \ |
400 | short int: \ | |
5e8754f9 | 401 | _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned short int, \ |
6ec2e0f5 SDJ |
402 | short int, SHRT_MIN, SHRT_MAX), \ |
403 | int: \ | |
404 | _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ | |
405 | int, INT_MIN, INT_MAX), \ | |
406 | long int: \ | |
407 | _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \ | |
408 | long int, LONG_MIN, LONG_MAX), \ | |
409 | long long int: \ | |
410 | _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \ | |
411 | long long int, LLONG_MIN, LLONG_MAX))) | |
412 | #else | |
413 | # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \ | |
414 | (sizeof *(r) == sizeof (signed char) \ | |
5e8754f9 | 415 | ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned char, \ |
6ec2e0f5 SDJ |
416 | signed char, SCHAR_MIN, SCHAR_MAX) \ |
417 | : sizeof *(r) == sizeof (short int) \ | |
5e8754f9 | 418 | ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned short int, \ |
6ec2e0f5 SDJ |
419 | short int, SHRT_MIN, SHRT_MAX) \ |
420 | : sizeof *(r) == sizeof (int) \ | |
421 | ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ | |
422 | int, INT_MIN, INT_MAX) \ | |
423 | : _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow)) | |
424 | # ifdef LLONG_MAX | |
425 | # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \ | |
426 | (sizeof *(r) == sizeof (long int) \ | |
427 | ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \ | |
428 | long int, LONG_MIN, LONG_MAX) \ | |
429 | : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \ | |
430 | long long int, LLONG_MIN, LLONG_MAX)) | |
431 | # else | |
432 | # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \ | |
433 | _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \ | |
434 | long int, LONG_MIN, LONG_MAX) | |
435 | # endif | |
436 | #endif | |
437 | ||
438 | /* Store the low-order bits of A <op> B into *R, where the operation | |
439 | is given by OP. Use the unsigned type UT for calculation to avoid | |
5e8754f9 SDJ |
440 | overflow problems. *R's type is T, with extremal values TMIN and |
441 | TMAX. T must be a signed integer type. Return 1 if the result | |
442 | overflows. */ | |
6ec2e0f5 SDJ |
443 | #define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \ |
444 | (sizeof ((a) op (b)) < sizeof (t) \ | |
445 | ? _GL_INT_OP_CALC1 ((t) (a), (t) (b), r, op, overflow, ut, t, tmin, tmax) \ | |
446 | : _GL_INT_OP_CALC1 (a, b, r, op, overflow, ut, t, tmin, tmax)) | |
447 | #define _GL_INT_OP_CALC1(a, b, r, op, overflow, ut, t, tmin, tmax) \ | |
448 | ((overflow (a, b) \ | |
449 | || (EXPR_SIGNED ((a) op (b)) && ((a) op (b)) < (tmin)) \ | |
450 | || (tmax) < ((a) op (b))) \ | |
5e8754f9 SDJ |
451 | ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t, tmin, tmax), 1) \ |
452 | : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t, tmin, tmax), 0)) | |
453 | ||
454 | /* Return A <op> B, where the operation is given by OP. Use the | |
455 | unsigned type UT for calculation to avoid overflow problems. | |
456 | Convert the result to type T without overflow by subtracting TMIN | |
457 | from large values before converting, and adding it afterwards. | |
458 | Compilers can optimize all the operations except OP. */ | |
459 | #define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t, tmin, tmax) \ | |
460 | (((ut) (a) op (ut) (b)) <= (tmax) \ | |
461 | ? (t) ((ut) (a) op (ut) (b)) \ | |
462 | : ((t) (((ut) (a) op (ut) (b)) - (tmin)) + (tmin))) | |
6ec2e0f5 SDJ |
463 | |
464 | #endif /* _GL_INTPROPS_H */ |