root/maint/gnulib/lib/intprops.h

/* [previous][next][first][last][top][bottom][index][help] */

INCLUDED FROM


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

/* [previous][next][first][last][top][bottom][index][help] */