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