xref: /freebsd/contrib/ntp/sntp/libopts/intprops.h (revision a466cc55373fc3cf86837f09da729535b57e69a1)
12b15cb3dSCy Schubert /* intprops.h -- properties of integer types
22b15cb3dSCy Schubert 
3*a466cc55SCy Schubert    Copyright (C) 2001-2018 Free Software Foundation, Inc.
42b15cb3dSCy Schubert 
5*a466cc55SCy Schubert    This program is free software: you can redistribute it and/or modify it
6*a466cc55SCy Schubert    under the terms of the GNU Lesser General Public License as published
7*a466cc55SCy Schubert    by the Free Software Foundation; either version 2.1 of the License, or
82b15cb3dSCy Schubert    (at your option) any later version.
92b15cb3dSCy Schubert 
102b15cb3dSCy Schubert    This program is distributed in the hope that it will be useful,
112b15cb3dSCy Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
122b15cb3dSCy Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
132b15cb3dSCy Schubert    GNU Lesser General Public License for more details.
142b15cb3dSCy Schubert 
152b15cb3dSCy Schubert    You should have received a copy of the GNU Lesser General Public License
16*a466cc55SCy Schubert    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
172b15cb3dSCy Schubert 
182b15cb3dSCy Schubert /* Written by Paul Eggert.  */
192b15cb3dSCy Schubert 
202b15cb3dSCy Schubert #ifndef _GL_INTPROPS_H
212b15cb3dSCy Schubert #define _GL_INTPROPS_H
222b15cb3dSCy Schubert 
232b15cb3dSCy Schubert #include <limits.h>
242b15cb3dSCy Schubert 
25*a466cc55SCy Schubert /* Return a value with the common real type of E and V and the value of V.  */
262b15cb3dSCy Schubert #define _GL_INT_CONVERT(e, v) (0 * (e) + (v))
272b15cb3dSCy Schubert 
282b15cb3dSCy Schubert /* Act like _GL_INT_CONVERT (E, -V) but work around a bug in IRIX 6.5 cc; see
29*a466cc55SCy Schubert    <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00406.html>.  */
302b15cb3dSCy Schubert #define _GL_INT_NEGATE_CONVERT(e, v) (0 * (e) - (v))
312b15cb3dSCy Schubert 
322b15cb3dSCy Schubert /* The extra casts in the following macros work around compiler bugs,
332b15cb3dSCy Schubert    e.g., in Cray C 5.0.3.0.  */
342b15cb3dSCy Schubert 
352b15cb3dSCy Schubert /* True if the arithmetic type T is an integer type.  bool counts as
362b15cb3dSCy Schubert    an integer.  */
372b15cb3dSCy Schubert #define TYPE_IS_INTEGER(t) ((t) 1.5 == 1)
382b15cb3dSCy Schubert 
39*a466cc55SCy Schubert /* True if the real type T is signed.  */
402b15cb3dSCy Schubert #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
412b15cb3dSCy Schubert 
42*a466cc55SCy Schubert /* Return 1 if the real expression E, after promotion, has a
43*a466cc55SCy Schubert    signed or floating type.  */
44*a466cc55SCy Schubert #define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)
452b15cb3dSCy Schubert 
462b15cb3dSCy Schubert 
47*a466cc55SCy Schubert /* Minimum and maximum values for integer types and expressions.  */
48*a466cc55SCy Schubert 
49*a466cc55SCy Schubert /* The width in bits of the integer type or expression T.
50*a466cc55SCy Schubert    Padding bits are not supported; this is checked at compile-time below.  */
51*a466cc55SCy Schubert #define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
522b15cb3dSCy Schubert 
532b15cb3dSCy Schubert /* The maximum and minimum values for the integer type T.  */
54*a466cc55SCy Schubert #define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t))
552b15cb3dSCy Schubert #define TYPE_MAXIMUM(t)                                                 \
562b15cb3dSCy Schubert   ((t) (! TYPE_SIGNED (t)                                               \
572b15cb3dSCy Schubert         ? (t) -1                                                        \
58*a466cc55SCy Schubert         : ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1)))
592b15cb3dSCy Schubert 
602b15cb3dSCy Schubert /* The maximum and minimum values for the type of the expression E,
612b15cb3dSCy Schubert    after integer promotion.  E should not have side effects.  */
622b15cb3dSCy Schubert #define _GL_INT_MINIMUM(e)                                              \
63*a466cc55SCy Schubert   (EXPR_SIGNED (e)                                                      \
64*a466cc55SCy Schubert    ? ~ _GL_SIGNED_INT_MAXIMUM (e)                                       \
652b15cb3dSCy Schubert    : _GL_INT_CONVERT (e, 0))
662b15cb3dSCy Schubert #define _GL_INT_MAXIMUM(e)                                              \
67*a466cc55SCy Schubert   (EXPR_SIGNED (e)                                                      \
682b15cb3dSCy Schubert    ? _GL_SIGNED_INT_MAXIMUM (e)                                         \
692b15cb3dSCy Schubert    : _GL_INT_NEGATE_CONVERT (e, 1))
702b15cb3dSCy Schubert #define _GL_SIGNED_INT_MAXIMUM(e)                                       \
71*a466cc55SCy Schubert   (((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH ((e) + 0) - 2)) - 1) * 2 + 1)
722b15cb3dSCy Schubert 
73*a466cc55SCy Schubert /* Work around OpenVMS incompatibility with C99.  */
74*a466cc55SCy Schubert #if !defined LLONG_MAX && defined __INT64_MAX
75*a466cc55SCy Schubert # define LLONG_MAX __INT64_MAX
76*a466cc55SCy Schubert # define LLONG_MIN __INT64_MIN
77*a466cc55SCy Schubert #endif
782b15cb3dSCy Schubert 
79*a466cc55SCy Schubert /* This include file assumes that signed types are two's complement without
80*a466cc55SCy Schubert    padding bits; the above macros have undefined behavior otherwise.
81*a466cc55SCy Schubert    If this is a problem for you, please let us know how to fix it for your host.
82*a466cc55SCy Schubert    This assumption is tested by the intprops-tests module.  */
83*a466cc55SCy Schubert 
84*a466cc55SCy Schubert /* Does the __typeof__ keyword work?  This could be done by
852b15cb3dSCy Schubert    'configure', but for now it's easier to do it by hand.  */
86*a466cc55SCy Schubert #if (2 <= __GNUC__ \
87*a466cc55SCy Schubert      || (1210 <= __IBMC__ && defined __IBM__TYPEOF__) \
882b15cb3dSCy Schubert      || (0x5110 <= __SUNPRO_C && !__STDC__))
892b15cb3dSCy Schubert # define _GL_HAVE___TYPEOF__ 1
902b15cb3dSCy Schubert #else
912b15cb3dSCy Schubert # define _GL_HAVE___TYPEOF__ 0
922b15cb3dSCy Schubert #endif
932b15cb3dSCy Schubert 
942b15cb3dSCy Schubert /* Return 1 if the integer type or expression T might be signed.  Return 0
952b15cb3dSCy Schubert    if it is definitely unsigned.  This macro does not evaluate its argument,
962b15cb3dSCy Schubert    and expands to an integer constant expression.  */
972b15cb3dSCy Schubert #if _GL_HAVE___TYPEOF__
982b15cb3dSCy Schubert # define _GL_SIGNED_TYPE_OR_EXPR(t) TYPE_SIGNED (__typeof__ (t))
992b15cb3dSCy Schubert #else
1002b15cb3dSCy Schubert # define _GL_SIGNED_TYPE_OR_EXPR(t) 1
1012b15cb3dSCy Schubert #endif
1022b15cb3dSCy Schubert 
1032b15cb3dSCy Schubert /* Bound on length of the string representing an unsigned integer
1042b15cb3dSCy Schubert    value representable in B bits.  log10 (2.0) < 146/485.  The
1052b15cb3dSCy Schubert    smallest value of B where this bound is not tight is 2621.  */
1062b15cb3dSCy Schubert #define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485)
1072b15cb3dSCy Schubert 
1082b15cb3dSCy Schubert /* Bound on length of the string representing an integer type or expression T.
1092b15cb3dSCy Schubert    Subtract 1 for the sign bit if T is signed, and then add 1 more for
1102b15cb3dSCy Schubert    a minus sign if needed.
1112b15cb3dSCy Schubert 
1122b15cb3dSCy Schubert    Because _GL_SIGNED_TYPE_OR_EXPR sometimes returns 0 when its argument is
1132b15cb3dSCy Schubert    signed, this macro may overestimate the true bound by one byte when
1142b15cb3dSCy Schubert    applied to unsigned types of size 2, 4, 16, ... bytes.  */
1152b15cb3dSCy Schubert #define INT_STRLEN_BOUND(t)                                     \
116*a466cc55SCy Schubert   (INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - _GL_SIGNED_TYPE_OR_EXPR (t)) \
1172b15cb3dSCy Schubert    + _GL_SIGNED_TYPE_OR_EXPR (t))
1182b15cb3dSCy Schubert 
1192b15cb3dSCy Schubert /* Bound on buffer size needed to represent an integer type or expression T,
1202b15cb3dSCy Schubert    including the terminating null.  */
1212b15cb3dSCy Schubert #define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1)
1222b15cb3dSCy Schubert 
1232b15cb3dSCy Schubert 
1242b15cb3dSCy Schubert /* Range overflow checks.
1252b15cb3dSCy Schubert 
1262b15cb3dSCy Schubert    The INT_<op>_RANGE_OVERFLOW macros return 1 if the corresponding C
1272b15cb3dSCy Schubert    operators might not yield numerically correct answers due to
1282b15cb3dSCy Schubert    arithmetic overflow.  They do not rely on undefined or
1292b15cb3dSCy Schubert    implementation-defined behavior.  Their implementations are simple
1302b15cb3dSCy Schubert    and straightforward, but they are a bit harder to use than the
1312b15cb3dSCy Schubert    INT_<op>_OVERFLOW macros described below.
1322b15cb3dSCy Schubert 
1332b15cb3dSCy Schubert    Example usage:
1342b15cb3dSCy Schubert 
1352b15cb3dSCy Schubert      long int i = ...;
1362b15cb3dSCy Schubert      long int j = ...;
1372b15cb3dSCy Schubert      if (INT_MULTIPLY_RANGE_OVERFLOW (i, j, LONG_MIN, LONG_MAX))
1382b15cb3dSCy Schubert        printf ("multiply would overflow");
1392b15cb3dSCy Schubert      else
1402b15cb3dSCy Schubert        printf ("product is %ld", i * j);
1412b15cb3dSCy Schubert 
1422b15cb3dSCy Schubert    Restrictions on *_RANGE_OVERFLOW macros:
1432b15cb3dSCy Schubert 
1442b15cb3dSCy Schubert    These macros do not check for all possible numerical problems or
1452b15cb3dSCy Schubert    undefined or unspecified behavior: they do not check for division
1462b15cb3dSCy Schubert    by zero, for bad shift counts, or for shifting negative numbers.
1472b15cb3dSCy Schubert 
1482b15cb3dSCy Schubert    These macros may evaluate their arguments zero or multiple times,
1492b15cb3dSCy Schubert    so the arguments should not have side effects.  The arithmetic
1502b15cb3dSCy Schubert    arguments (including the MIN and MAX arguments) must be of the same
1512b15cb3dSCy Schubert    integer type after the usual arithmetic conversions, and the type
1522b15cb3dSCy Schubert    must have minimum value MIN and maximum MAX.  Unsigned types should
1532b15cb3dSCy Schubert    use a zero MIN of the proper type.
1542b15cb3dSCy Schubert 
1552b15cb3dSCy Schubert    These macros are tuned for constant MIN and MAX.  For commutative
1562b15cb3dSCy Schubert    operations such as A + B, they are also tuned for constant B.  */
1572b15cb3dSCy Schubert 
1582b15cb3dSCy Schubert /* Return 1 if A + B would overflow in [MIN,MAX] arithmetic.
1592b15cb3dSCy Schubert    See above for restrictions.  */
1602b15cb3dSCy Schubert #define INT_ADD_RANGE_OVERFLOW(a, b, min, max)          \
1612b15cb3dSCy Schubert   ((b) < 0                                              \
1622b15cb3dSCy Schubert    ? (a) < (min) - (b)                                  \
1632b15cb3dSCy Schubert    : (max) - (b) < (a))
1642b15cb3dSCy Schubert 
1652b15cb3dSCy Schubert /* Return 1 if A - B would overflow in [MIN,MAX] arithmetic.
1662b15cb3dSCy Schubert    See above for restrictions.  */
1672b15cb3dSCy Schubert #define INT_SUBTRACT_RANGE_OVERFLOW(a, b, min, max)     \
1682b15cb3dSCy Schubert   ((b) < 0                                              \
1692b15cb3dSCy Schubert    ? (max) + (b) < (a)                                  \
1702b15cb3dSCy Schubert    : (a) < (min) + (b))
1712b15cb3dSCy Schubert 
1722b15cb3dSCy Schubert /* Return 1 if - A would overflow in [MIN,MAX] arithmetic.
1732b15cb3dSCy Schubert    See above for restrictions.  */
1742b15cb3dSCy Schubert #define INT_NEGATE_RANGE_OVERFLOW(a, min, max)          \
1752b15cb3dSCy Schubert   ((min) < 0                                            \
1762b15cb3dSCy Schubert    ? (a) < - (max)                                      \
1772b15cb3dSCy Schubert    : 0 < (a))
1782b15cb3dSCy Schubert 
1792b15cb3dSCy Schubert /* Return 1 if A * B would overflow in [MIN,MAX] arithmetic.
1802b15cb3dSCy Schubert    See above for restrictions.  Avoid && and || as they tickle
1812b15cb3dSCy Schubert    bugs in Sun C 5.11 2010/08/13 and other compilers; see
182*a466cc55SCy Schubert    <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00401.html>.  */
1832b15cb3dSCy Schubert #define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max)     \
1842b15cb3dSCy Schubert   ((b) < 0                                              \
1852b15cb3dSCy Schubert    ? ((a) < 0                                           \
1862b15cb3dSCy Schubert       ? (a) < (max) / (b)                               \
1872b15cb3dSCy Schubert       : (b) == -1                                       \
1882b15cb3dSCy Schubert       ? 0                                               \
1892b15cb3dSCy Schubert       : (min) / (b) < (a))                              \
1902b15cb3dSCy Schubert    : (b) == 0                                           \
1912b15cb3dSCy Schubert    ? 0                                                  \
1922b15cb3dSCy Schubert    : ((a) < 0                                           \
1932b15cb3dSCy Schubert       ? (a) < (min) / (b)                               \
1942b15cb3dSCy Schubert       : (max) / (b) < (a)))
1952b15cb3dSCy Schubert 
1962b15cb3dSCy Schubert /* Return 1 if A / B would overflow in [MIN,MAX] arithmetic.
1972b15cb3dSCy Schubert    See above for restrictions.  Do not check for division by zero.  */
1982b15cb3dSCy Schubert #define INT_DIVIDE_RANGE_OVERFLOW(a, b, min, max)       \
1992b15cb3dSCy Schubert   ((min) < 0 && (b) == -1 && (a) < - (max))
2002b15cb3dSCy Schubert 
2012b15cb3dSCy Schubert /* Return 1 if A % B would overflow in [MIN,MAX] arithmetic.
2022b15cb3dSCy Schubert    See above for restrictions.  Do not check for division by zero.
2032b15cb3dSCy Schubert    Mathematically, % should never overflow, but on x86-like hosts
2042b15cb3dSCy Schubert    INT_MIN % -1 traps, and the C standard permits this, so treat this
2052b15cb3dSCy Schubert    as an overflow too.  */
2062b15cb3dSCy Schubert #define INT_REMAINDER_RANGE_OVERFLOW(a, b, min, max)    \
2072b15cb3dSCy Schubert   INT_DIVIDE_RANGE_OVERFLOW (a, b, min, max)
2082b15cb3dSCy Schubert 
2092b15cb3dSCy Schubert /* Return 1 if A << B would overflow in [MIN,MAX] arithmetic.
2102b15cb3dSCy Schubert    See above for restrictions.  Here, MIN and MAX are for A only, and B need
2112b15cb3dSCy Schubert    not be of the same type as the other arguments.  The C standard says that
2122b15cb3dSCy Schubert    behavior is undefined for shifts unless 0 <= B < wordwidth, and that when
2132b15cb3dSCy Schubert    A is negative then A << B has undefined behavior and A >> B has
2142b15cb3dSCy Schubert    implementation-defined behavior, but do not check these other
2152b15cb3dSCy Schubert    restrictions.  */
2162b15cb3dSCy Schubert #define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max)   \
2172b15cb3dSCy Schubert   ((a) < 0                                              \
2182b15cb3dSCy Schubert    ? (a) < (min) >> (b)                                 \
2192b15cb3dSCy Schubert    : (max) >> (b) < (a))
2202b15cb3dSCy Schubert 
221*a466cc55SCy Schubert /* True if __builtin_add_overflow (A, B, P) works when P is non-null.  */
222*a466cc55SCy Schubert #if 5 <= __GNUC__ && !defined __ICC
223*a466cc55SCy Schubert # define _GL_HAS_BUILTIN_OVERFLOW 1
224*a466cc55SCy Schubert #else
225*a466cc55SCy Schubert # define _GL_HAS_BUILTIN_OVERFLOW 0
226*a466cc55SCy Schubert #endif
227*a466cc55SCy Schubert 
228*a466cc55SCy Schubert /* True if __builtin_add_overflow_p (A, B, C) works.  */
229*a466cc55SCy Schubert #define _GL_HAS_BUILTIN_OVERFLOW_P (7 <= __GNUC__)
2302b15cb3dSCy Schubert 
2312b15cb3dSCy Schubert /* The _GL*_OVERFLOW macros have the same restrictions as the
2322b15cb3dSCy Schubert    *_RANGE_OVERFLOW macros, except that they do not assume that operands
2332b15cb3dSCy Schubert    (e.g., A and B) have the same type as MIN and MAX.  Instead, they assume
2342b15cb3dSCy Schubert    that the result (e.g., A + B) has that type.  */
235*a466cc55SCy Schubert #if _GL_HAS_BUILTIN_OVERFLOW_P
236*a466cc55SCy Schubert # define _GL_ADD_OVERFLOW(a, b, min, max)                               \
237*a466cc55SCy Schubert    __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0)
238*a466cc55SCy Schubert # define _GL_SUBTRACT_OVERFLOW(a, b, min, max)                          \
239*a466cc55SCy Schubert    __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
240*a466cc55SCy Schubert # define _GL_MULTIPLY_OVERFLOW(a, b, min, max)                          \
241*a466cc55SCy Schubert    __builtin_mul_overflow_p (a, b, (__typeof__ ((a) * (b))) 0)
242*a466cc55SCy Schubert #else
2432b15cb3dSCy Schubert # define _GL_ADD_OVERFLOW(a, b, min, max)                                \
2442b15cb3dSCy Schubert    ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max)                  \
2452b15cb3dSCy Schubert     : (a) < 0 ? (b) <= (a) + (b)                                         \
2462b15cb3dSCy Schubert     : (b) < 0 ? (a) <= (a) + (b)                                         \
2472b15cb3dSCy Schubert     : (a) + (b) < (b))
2482b15cb3dSCy Schubert # define _GL_SUBTRACT_OVERFLOW(a, b, min, max)                           \
2492b15cb3dSCy Schubert    ((min) < 0 ? INT_SUBTRACT_RANGE_OVERFLOW (a, b, min, max)             \
2502b15cb3dSCy Schubert     : (a) < 0 ? 1                                                        \
2512b15cb3dSCy Schubert     : (b) < 0 ? (a) - (b) <= (a)                                         \
2522b15cb3dSCy Schubert     : (a) < (b))
2532b15cb3dSCy Schubert # define _GL_MULTIPLY_OVERFLOW(a, b, min, max)                           \
2542b15cb3dSCy Schubert    (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a))))       \
2552b15cb3dSCy Schubert     || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max))
256*a466cc55SCy Schubert #endif
2572b15cb3dSCy Schubert #define _GL_DIVIDE_OVERFLOW(a, b, min, max)                             \
2582b15cb3dSCy Schubert   ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max)  \
2592b15cb3dSCy Schubert    : (a) < 0 ? (b) <= (a) + (b) - 1                                     \
2602b15cb3dSCy Schubert    : (b) < 0 && (a) + (b) <= (a))
2612b15cb3dSCy Schubert #define _GL_REMAINDER_OVERFLOW(a, b, min, max)                          \
2622b15cb3dSCy Schubert   ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max)  \
2632b15cb3dSCy Schubert    : (a) < 0 ? (a) % (b) != ((max) - (b) + 1) % (b)                     \
2642b15cb3dSCy Schubert    : (b) < 0 && ! _GL_UNSIGNED_NEG_MULTIPLE (a, b, max))
2652b15cb3dSCy Schubert 
2662b15cb3dSCy Schubert /* Return a nonzero value if A is a mathematical multiple of B, where
2672b15cb3dSCy Schubert    A is unsigned, B is negative, and MAX is the maximum value of A's
2682b15cb3dSCy Schubert    type.  A's type must be the same as (A % B)'s type.  Normally (A %
2692b15cb3dSCy Schubert    -B == 0) suffices, but things get tricky if -B would overflow.  */
2702b15cb3dSCy Schubert #define _GL_UNSIGNED_NEG_MULTIPLE(a, b, max)                            \
2712b15cb3dSCy Schubert   (((b) < -_GL_SIGNED_INT_MAXIMUM (b)                                   \
2722b15cb3dSCy Schubert     ? (_GL_SIGNED_INT_MAXIMUM (b) == (max)                              \
2732b15cb3dSCy Schubert        ? (a)                                                            \
2742b15cb3dSCy Schubert        : (a) % (_GL_INT_CONVERT (a, _GL_SIGNED_INT_MAXIMUM (b)) + 1))   \
2752b15cb3dSCy Schubert     : (a) % - (b))                                                      \
2762b15cb3dSCy Schubert    == 0)
2772b15cb3dSCy Schubert 
278*a466cc55SCy Schubert /* Check for integer overflow, and report low order bits of answer.
2792b15cb3dSCy Schubert 
2802b15cb3dSCy Schubert    The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators
2812b15cb3dSCy Schubert    might not yield numerically correct answers due to arithmetic overflow.
282*a466cc55SCy Schubert    The INT_<op>_WRAPV macros also store the low-order bits of the answer.
283*a466cc55SCy Schubert    These macros work correctly on all known practical hosts, and do not rely
2842b15cb3dSCy Schubert    on undefined behavior due to signed arithmetic overflow.
2852b15cb3dSCy Schubert 
286*a466cc55SCy Schubert    Example usage, assuming A and B are long int:
2872b15cb3dSCy Schubert 
288*a466cc55SCy Schubert      if (INT_MULTIPLY_OVERFLOW (a, b))
289*a466cc55SCy Schubert        printf ("result would overflow\n");
2902b15cb3dSCy Schubert      else
291*a466cc55SCy Schubert        printf ("result is %ld (no overflow)\n", a * b);
292*a466cc55SCy Schubert 
293*a466cc55SCy Schubert    Example usage with WRAPV flavor:
294*a466cc55SCy Schubert 
295*a466cc55SCy Schubert      long int result;
296*a466cc55SCy Schubert      bool overflow = INT_MULTIPLY_WRAPV (a, b, &result);
297*a466cc55SCy Schubert      printf ("result is %ld (%s)\n", result,
298*a466cc55SCy Schubert              overflow ? "after overflow" : "no overflow");
299*a466cc55SCy Schubert 
300*a466cc55SCy Schubert    Restrictions on these macros:
3012b15cb3dSCy Schubert 
3022b15cb3dSCy Schubert    These macros do not check for all possible numerical problems or
3032b15cb3dSCy Schubert    undefined or unspecified behavior: they do not check for division
3042b15cb3dSCy Schubert    by zero, for bad shift counts, or for shifting negative numbers.
3052b15cb3dSCy Schubert 
3062b15cb3dSCy Schubert    These macros may evaluate their arguments zero or multiple times, so the
3072b15cb3dSCy Schubert    arguments should not have side effects.
3082b15cb3dSCy Schubert 
309*a466cc55SCy Schubert    The WRAPV macros are not constant expressions.  They support only
310*a466cc55SCy Schubert    +, binary -, and *.  The result type must be signed.
311*a466cc55SCy Schubert 
3122b15cb3dSCy Schubert    These macros are tuned for their last argument being a constant.
3132b15cb3dSCy Schubert 
3142b15cb3dSCy Schubert    Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B,
3152b15cb3dSCy Schubert    A % B, and A << B would overflow, respectively.  */
3162b15cb3dSCy Schubert 
3172b15cb3dSCy Schubert #define INT_ADD_OVERFLOW(a, b) \
3182b15cb3dSCy Schubert   _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW)
3192b15cb3dSCy Schubert #define INT_SUBTRACT_OVERFLOW(a, b) \
3202b15cb3dSCy Schubert   _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW)
321*a466cc55SCy Schubert #if _GL_HAS_BUILTIN_OVERFLOW_P
322*a466cc55SCy Schubert # define INT_NEGATE_OVERFLOW(a) INT_SUBTRACT_OVERFLOW (0, a)
323*a466cc55SCy Schubert #else
3242b15cb3dSCy Schubert # define INT_NEGATE_OVERFLOW(a) \
3252b15cb3dSCy Schubert    INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
326*a466cc55SCy Schubert #endif
3272b15cb3dSCy Schubert #define INT_MULTIPLY_OVERFLOW(a, b) \
3282b15cb3dSCy Schubert   _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW)
3292b15cb3dSCy Schubert #define INT_DIVIDE_OVERFLOW(a, b) \
3302b15cb3dSCy Schubert   _GL_BINARY_OP_OVERFLOW (a, b, _GL_DIVIDE_OVERFLOW)
3312b15cb3dSCy Schubert #define INT_REMAINDER_OVERFLOW(a, b) \
3322b15cb3dSCy Schubert   _GL_BINARY_OP_OVERFLOW (a, b, _GL_REMAINDER_OVERFLOW)
3332b15cb3dSCy Schubert #define INT_LEFT_SHIFT_OVERFLOW(a, b) \
3342b15cb3dSCy Schubert   INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
3352b15cb3dSCy Schubert                                  _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
3362b15cb3dSCy Schubert 
3372b15cb3dSCy Schubert /* Return 1 if the expression A <op> B would overflow,
3382b15cb3dSCy Schubert    where OP_RESULT_OVERFLOW (A, B, MIN, MAX) does the actual test,
3392b15cb3dSCy Schubert    assuming MIN and MAX are the minimum and maximum for the result type.
3402b15cb3dSCy Schubert    Arguments should be free of side effects.  */
3412b15cb3dSCy Schubert #define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow)        \
3422b15cb3dSCy Schubert   op_result_overflow (a, b,                                     \
3432b15cb3dSCy Schubert                       _GL_INT_MINIMUM (0 * (b) + (a)),          \
3442b15cb3dSCy Schubert                       _GL_INT_MAXIMUM (0 * (b) + (a)))
3452b15cb3dSCy Schubert 
346*a466cc55SCy Schubert /* Store the low-order bits of A + B, A - B, A * B, respectively, into *R.
347*a466cc55SCy Schubert    Return 1 if the result overflows.  See above for restrictions.  */
348*a466cc55SCy Schubert #define INT_ADD_WRAPV(a, b, r) \
349*a466cc55SCy Schubert   _GL_INT_OP_WRAPV (a, b, r, +, __builtin_add_overflow, INT_ADD_OVERFLOW)
350*a466cc55SCy Schubert #define INT_SUBTRACT_WRAPV(a, b, r) \
351*a466cc55SCy Schubert   _GL_INT_OP_WRAPV (a, b, r, -, __builtin_sub_overflow, INT_SUBTRACT_OVERFLOW)
352*a466cc55SCy Schubert #define INT_MULTIPLY_WRAPV(a, b, r) \
353*a466cc55SCy Schubert   _GL_INT_OP_WRAPV (a, b, r, *, __builtin_mul_overflow, INT_MULTIPLY_OVERFLOW)
354*a466cc55SCy Schubert 
355*a466cc55SCy Schubert /* Nonzero if this compiler has GCC bug 68193 or Clang bug 25390.  See:
356*a466cc55SCy Schubert    https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68193
357*a466cc55SCy Schubert    https://llvm.org/bugs/show_bug.cgi?id=25390
358*a466cc55SCy Schubert    For now, assume all versions of GCC-like compilers generate bogus
359*a466cc55SCy Schubert    warnings for _Generic.  This matters only for older compilers that
360*a466cc55SCy Schubert    lack __builtin_add_overflow.  */
361*a466cc55SCy Schubert #if __GNUC__
362*a466cc55SCy Schubert # define _GL__GENERIC_BOGUS 1
363*a466cc55SCy Schubert #else
364*a466cc55SCy Schubert # define _GL__GENERIC_BOGUS 0
365*a466cc55SCy Schubert #endif
366*a466cc55SCy Schubert 
367*a466cc55SCy Schubert /* Store the low-order bits of A <op> B into *R, where OP specifies
368*a466cc55SCy Schubert    the operation.  BUILTIN is the builtin operation, and OVERFLOW the
369*a466cc55SCy Schubert    overflow predicate.  Return 1 if the result overflows.  See above
370*a466cc55SCy Schubert    for restrictions.  */
371*a466cc55SCy Schubert #if _GL_HAS_BUILTIN_OVERFLOW
372*a466cc55SCy Schubert # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) builtin (a, b, r)
373*a466cc55SCy Schubert #elif 201112 <= __STDC_VERSION__ && !_GL__GENERIC_BOGUS
374*a466cc55SCy Schubert # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \
375*a466cc55SCy Schubert    (_Generic \
376*a466cc55SCy Schubert     (*(r), \
377*a466cc55SCy Schubert      signed char: \
378*a466cc55SCy Schubert        _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
379*a466cc55SCy Schubert                         signed char, SCHAR_MIN, SCHAR_MAX), \
380*a466cc55SCy Schubert      short int: \
381*a466cc55SCy Schubert        _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
382*a466cc55SCy Schubert                         short int, SHRT_MIN, SHRT_MAX), \
383*a466cc55SCy Schubert      int: \
384*a466cc55SCy Schubert        _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
385*a466cc55SCy Schubert                         int, INT_MIN, INT_MAX), \
386*a466cc55SCy Schubert      long int: \
387*a466cc55SCy Schubert        _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
388*a466cc55SCy Schubert                         long int, LONG_MIN, LONG_MAX), \
389*a466cc55SCy Schubert      long long int: \
390*a466cc55SCy Schubert        _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
391*a466cc55SCy Schubert                         long long int, LLONG_MIN, LLONG_MAX)))
392*a466cc55SCy Schubert #else
393*a466cc55SCy Schubert # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \
394*a466cc55SCy Schubert    (sizeof *(r) == sizeof (signed char) \
395*a466cc55SCy Schubert     ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
396*a466cc55SCy Schubert                        signed char, SCHAR_MIN, SCHAR_MAX) \
397*a466cc55SCy Schubert     : sizeof *(r) == sizeof (short int) \
398*a466cc55SCy Schubert     ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
399*a466cc55SCy Schubert                        short int, SHRT_MIN, SHRT_MAX) \
400*a466cc55SCy Schubert     : sizeof *(r) == sizeof (int) \
401*a466cc55SCy Schubert     ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
402*a466cc55SCy Schubert                        int, INT_MIN, INT_MAX) \
403*a466cc55SCy Schubert     : _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow))
404*a466cc55SCy Schubert # ifdef LLONG_MAX
405*a466cc55SCy Schubert #  define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
406*a466cc55SCy Schubert     (sizeof *(r) == sizeof (long int) \
407*a466cc55SCy Schubert      ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
408*a466cc55SCy Schubert                         long int, LONG_MIN, LONG_MAX) \
409*a466cc55SCy Schubert      : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
410*a466cc55SCy Schubert                         long long int, LLONG_MIN, LLONG_MAX))
411*a466cc55SCy Schubert # else
412*a466cc55SCy Schubert #  define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
413*a466cc55SCy Schubert     _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
414*a466cc55SCy Schubert                      long int, LONG_MIN, LONG_MAX)
415*a466cc55SCy Schubert # endif
416*a466cc55SCy Schubert #endif
417*a466cc55SCy Schubert 
418*a466cc55SCy Schubert /* Store the low-order bits of A <op> B into *R, where the operation
419*a466cc55SCy Schubert    is given by OP.  Use the unsigned type UT for calculation to avoid
420*a466cc55SCy Schubert    overflow problems.  *R's type is T, with extrema TMIN and TMAX.
421*a466cc55SCy Schubert    T must be a signed integer type.  Return 1 if the result overflows.  */
422*a466cc55SCy Schubert #define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \
423*a466cc55SCy Schubert   (sizeof ((a) op (b)) < sizeof (t) \
424*a466cc55SCy Schubert    ? _GL_INT_OP_CALC1 ((t) (a), (t) (b), r, op, overflow, ut, t, tmin, tmax) \
425*a466cc55SCy Schubert    : _GL_INT_OP_CALC1 (a, b, r, op, overflow, ut, t, tmin, tmax))
426*a466cc55SCy Schubert #define _GL_INT_OP_CALC1(a, b, r, op, overflow, ut, t, tmin, tmax) \
427*a466cc55SCy Schubert   ((overflow (a, b) \
428*a466cc55SCy Schubert     || (EXPR_SIGNED ((a) op (b)) && ((a) op (b)) < (tmin)) \
429*a466cc55SCy Schubert     || (tmax) < ((a) op (b))) \
430*a466cc55SCy Schubert    ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 1) \
431*a466cc55SCy Schubert    : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 0))
432*a466cc55SCy Schubert 
433*a466cc55SCy Schubert /* Return the low-order bits of A <op> B, where the operation is given
434*a466cc55SCy Schubert    by OP.  Use the unsigned type UT for calculation to avoid undefined
435*a466cc55SCy Schubert    behavior on signed integer overflow, and convert the result to type T.
436*a466cc55SCy Schubert    UT is at least as wide as T and is no narrower than unsigned int,
437*a466cc55SCy Schubert    T is two's complement, and there is no padding or trap representations.
438*a466cc55SCy Schubert    Assume that converting UT to T yields the low-order bits, as is
439*a466cc55SCy Schubert    done in all known two's-complement C compilers.  E.g., see:
440*a466cc55SCy Schubert    https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html
441*a466cc55SCy Schubert 
442*a466cc55SCy Schubert    According to the C standard, converting UT to T yields an
443*a466cc55SCy Schubert    implementation-defined result or signal for values outside T's
444*a466cc55SCy Schubert    range.  However, code that works around this theoretical problem
445*a466cc55SCy Schubert    runs afoul of a compiler bug in Oracle Studio 12.3 x86.  See:
446*a466cc55SCy Schubert    https://lists.gnu.org/r/bug-gnulib/2017-04/msg00049.html
447*a466cc55SCy Schubert    As the compiler bug is real, don't try to work around the
448*a466cc55SCy Schubert    theoretical problem.  */
449*a466cc55SCy Schubert 
450*a466cc55SCy Schubert #define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t) \
451*a466cc55SCy Schubert   ((t) ((ut) (a) op (ut) (b)))
452*a466cc55SCy Schubert 
4532b15cb3dSCy Schubert #endif /* _GL_INTPROPS_H */
454