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