195b7b453SJohn Marino /* intprops.h -- properties of integer types 295b7b453SJohn Marino 3*09d4459fSDaniel Fojt Copyright (C) 2001-2020 Free Software Foundation, Inc. 495b7b453SJohn Marino 5*09d4459fSDaniel Fojt This program is free software: you can redistribute it and/or modify it 6*09d4459fSDaniel Fojt under the terms of the GNU General Public License as published 7*09d4459fSDaniel Fojt by the Free Software Foundation; either version 3 of the License, or 895b7b453SJohn Marino (at your option) any later version. 995b7b453SJohn Marino 1095b7b453SJohn Marino This program is distributed in the hope that it will be useful, 1195b7b453SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of 1295b7b453SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1395b7b453SJohn Marino GNU General Public License for more details. 1495b7b453SJohn Marino 1595b7b453SJohn Marino You should have received a copy of the GNU General Public License 16*09d4459fSDaniel Fojt along with this program. If not, see <https://www.gnu.org/licenses/>. */ 1795b7b453SJohn Marino 1895b7b453SJohn Marino /* Written by Paul Eggert. */ 1995b7b453SJohn Marino 20200fbe8dSJohn Marino #ifndef _GL_INTPROPS_H 21200fbe8dSJohn Marino #define _GL_INTPROPS_H 2295b7b453SJohn Marino 2395b7b453SJohn Marino #include <limits.h> 2495b7b453SJohn Marino 25*09d4459fSDaniel Fojt /* Return a value with the common real type of E and V and the value of V. 26*09d4459fSDaniel Fojt Do not evaluate E. */ 27*09d4459fSDaniel Fojt #define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v)) 28200fbe8dSJohn Marino 29200fbe8dSJohn Marino /* Act like _GL_INT_CONVERT (E, -V) but work around a bug in IRIX 6.5 cc; see 30*09d4459fSDaniel Fojt <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00406.html>. */ 31*09d4459fSDaniel Fojt #define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v)) 32200fbe8dSJohn Marino 3395b7b453SJohn Marino /* The extra casts in the following macros work around compiler bugs, 3495b7b453SJohn Marino e.g., in Cray C 5.0.3.0. */ 3595b7b453SJohn Marino 3695b7b453SJohn Marino /* True if the arithmetic type T is an integer type. bool counts as 3795b7b453SJohn Marino an integer. */ 3895b7b453SJohn Marino #define TYPE_IS_INTEGER(t) ((t) 1.5 == 1) 3995b7b453SJohn Marino 40*09d4459fSDaniel Fojt /* True if the real type T is signed. */ 4195b7b453SJohn Marino #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1)) 4295b7b453SJohn Marino 43*09d4459fSDaniel Fojt /* Return 1 if the real expression E, after promotion, has a 44*09d4459fSDaniel Fojt signed or floating type. Do not evaluate E. */ 45*09d4459fSDaniel Fojt #define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0) 46200fbe8dSJohn Marino 47200fbe8dSJohn Marino 48*09d4459fSDaniel Fojt /* Minimum and maximum values for integer types and expressions. */ 49*09d4459fSDaniel Fojt 50*09d4459fSDaniel Fojt /* The width in bits of the integer type or expression T. 51*09d4459fSDaniel Fojt Do not evaluate T. 52*09d4459fSDaniel Fojt Padding bits are not supported; this is checked at compile-time below. */ 53*09d4459fSDaniel Fojt #define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT) 54200fbe8dSJohn Marino 55200fbe8dSJohn Marino /* The maximum and minimum values for the integer type T. */ 56*09d4459fSDaniel Fojt #define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t)) 5795b7b453SJohn Marino #define TYPE_MAXIMUM(t) \ 5895b7b453SJohn Marino ((t) (! TYPE_SIGNED (t) \ 5995b7b453SJohn Marino ? (t) -1 \ 60*09d4459fSDaniel Fojt : ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1))) 6195b7b453SJohn Marino 62200fbe8dSJohn Marino /* The maximum and minimum values for the type of the expression E, 63*09d4459fSDaniel Fojt after integer promotion. E is not evaluated. */ 64200fbe8dSJohn Marino #define _GL_INT_MINIMUM(e) \ 65*09d4459fSDaniel Fojt (EXPR_SIGNED (e) \ 66*09d4459fSDaniel Fojt ? ~ _GL_SIGNED_INT_MAXIMUM (e) \ 67200fbe8dSJohn Marino : _GL_INT_CONVERT (e, 0)) 68200fbe8dSJohn Marino #define _GL_INT_MAXIMUM(e) \ 69*09d4459fSDaniel Fojt (EXPR_SIGNED (e) \ 70200fbe8dSJohn Marino ? _GL_SIGNED_INT_MAXIMUM (e) \ 71200fbe8dSJohn Marino : _GL_INT_NEGATE_CONVERT (e, 1)) 72200fbe8dSJohn Marino #define _GL_SIGNED_INT_MAXIMUM(e) \ 73*09d4459fSDaniel Fojt (((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH ((e) + 0) - 2)) - 1) * 2 + 1) 74200fbe8dSJohn Marino 75*09d4459fSDaniel Fojt /* Work around OpenVMS incompatibility with C99. */ 76*09d4459fSDaniel Fojt #if !defined LLONG_MAX && defined __INT64_MAX 77*09d4459fSDaniel Fojt # define LLONG_MAX __INT64_MAX 78*09d4459fSDaniel Fojt # define LLONG_MIN __INT64_MIN 79*09d4459fSDaniel Fojt #endif 80200fbe8dSJohn Marino 81*09d4459fSDaniel Fojt /* This include file assumes that signed types are two's complement without 82*09d4459fSDaniel Fojt padding bits; the above macros have undefined behavior otherwise. 83*09d4459fSDaniel Fojt If this is a problem for you, please let us know how to fix it for your host. 84*09d4459fSDaniel Fojt This assumption is tested by the intprops-tests module. */ 85*09d4459fSDaniel Fojt 86*09d4459fSDaniel Fojt /* Does the __typeof__ keyword work? This could be done by 87200fbe8dSJohn Marino 'configure', but for now it's easier to do it by hand. */ 88*09d4459fSDaniel Fojt #if (2 <= __GNUC__ \ 89*09d4459fSDaniel Fojt || (1210 <= __IBMC__ && defined __IBM__TYPEOF__) \ 90680a9cb8SJohn Marino || (0x5110 <= __SUNPRO_C && !__STDC__)) 91200fbe8dSJohn Marino # define _GL_HAVE___TYPEOF__ 1 9295b7b453SJohn Marino #else 93200fbe8dSJohn Marino # define _GL_HAVE___TYPEOF__ 0 9495b7b453SJohn Marino #endif 9595b7b453SJohn Marino 96200fbe8dSJohn Marino /* Return 1 if the integer type or expression T might be signed. Return 0 97200fbe8dSJohn Marino if it is definitely unsigned. This macro does not evaluate its argument, 98200fbe8dSJohn Marino and expands to an integer constant expression. */ 99200fbe8dSJohn Marino #if _GL_HAVE___TYPEOF__ 100200fbe8dSJohn Marino # define _GL_SIGNED_TYPE_OR_EXPR(t) TYPE_SIGNED (__typeof__ (t)) 101200fbe8dSJohn Marino #else 102200fbe8dSJohn Marino # define _GL_SIGNED_TYPE_OR_EXPR(t) 1 103200fbe8dSJohn Marino #endif 104200fbe8dSJohn Marino 105200fbe8dSJohn Marino /* Bound on length of the string representing an unsigned integer 106200fbe8dSJohn Marino value representable in B bits. log10 (2.0) < 146/485. The 107200fbe8dSJohn Marino smallest value of B where this bound is not tight is 2621. */ 108200fbe8dSJohn Marino #define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485) 109200fbe8dSJohn Marino 11095b7b453SJohn Marino /* Bound on length of the string representing an integer type or expression T. 111200fbe8dSJohn Marino Subtract 1 for the sign bit if T is signed, and then add 1 more for 112200fbe8dSJohn Marino a minus sign if needed. 113200fbe8dSJohn Marino 114*09d4459fSDaniel Fojt Because _GL_SIGNED_TYPE_OR_EXPR sometimes returns 1 when its argument is 115*09d4459fSDaniel Fojt unsigned, this macro may overestimate the true bound by one byte when 116200fbe8dSJohn Marino applied to unsigned types of size 2, 4, 16, ... bytes. */ 11795b7b453SJohn Marino #define INT_STRLEN_BOUND(t) \ 118*09d4459fSDaniel Fojt (INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - _GL_SIGNED_TYPE_OR_EXPR (t)) \ 119200fbe8dSJohn Marino + _GL_SIGNED_TYPE_OR_EXPR (t)) 12095b7b453SJohn Marino 12195b7b453SJohn Marino /* Bound on buffer size needed to represent an integer type or expression T, 12295b7b453SJohn Marino including the terminating null. */ 12395b7b453SJohn Marino #define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1) 12495b7b453SJohn Marino 125200fbe8dSJohn Marino 126200fbe8dSJohn Marino /* Range overflow checks. 127200fbe8dSJohn Marino 128200fbe8dSJohn Marino The INT_<op>_RANGE_OVERFLOW macros return 1 if the corresponding C 129200fbe8dSJohn Marino operators might not yield numerically correct answers due to 130200fbe8dSJohn Marino arithmetic overflow. They do not rely on undefined or 131200fbe8dSJohn Marino implementation-defined behavior. Their implementations are simple 132200fbe8dSJohn Marino and straightforward, but they are a bit harder to use than the 133200fbe8dSJohn Marino INT_<op>_OVERFLOW macros described below. 134200fbe8dSJohn Marino 135200fbe8dSJohn Marino Example usage: 136200fbe8dSJohn Marino 137200fbe8dSJohn Marino long int i = ...; 138200fbe8dSJohn Marino long int j = ...; 139200fbe8dSJohn Marino if (INT_MULTIPLY_RANGE_OVERFLOW (i, j, LONG_MIN, LONG_MAX)) 140200fbe8dSJohn Marino printf ("multiply would overflow"); 141200fbe8dSJohn Marino else 142200fbe8dSJohn Marino printf ("product is %ld", i * j); 143200fbe8dSJohn Marino 144200fbe8dSJohn Marino Restrictions on *_RANGE_OVERFLOW macros: 145200fbe8dSJohn Marino 146200fbe8dSJohn Marino These macros do not check for all possible numerical problems or 147200fbe8dSJohn Marino undefined or unspecified behavior: they do not check for division 148200fbe8dSJohn Marino by zero, for bad shift counts, or for shifting negative numbers. 149200fbe8dSJohn Marino 150200fbe8dSJohn Marino These macros may evaluate their arguments zero or multiple times, 151200fbe8dSJohn Marino so the arguments should not have side effects. The arithmetic 152200fbe8dSJohn Marino arguments (including the MIN and MAX arguments) must be of the same 153200fbe8dSJohn Marino integer type after the usual arithmetic conversions, and the type 154200fbe8dSJohn Marino must have minimum value MIN and maximum MAX. Unsigned types should 155200fbe8dSJohn Marino use a zero MIN of the proper type. 156200fbe8dSJohn Marino 157200fbe8dSJohn Marino These macros are tuned for constant MIN and MAX. For commutative 158200fbe8dSJohn Marino operations such as A + B, they are also tuned for constant B. */ 159200fbe8dSJohn Marino 160200fbe8dSJohn Marino /* Return 1 if A + B would overflow in [MIN,MAX] arithmetic. 161200fbe8dSJohn Marino See above for restrictions. */ 162200fbe8dSJohn Marino #define INT_ADD_RANGE_OVERFLOW(a, b, min, max) \ 163200fbe8dSJohn Marino ((b) < 0 \ 164200fbe8dSJohn Marino ? (a) < (min) - (b) \ 165200fbe8dSJohn Marino : (max) - (b) < (a)) 166200fbe8dSJohn Marino 167200fbe8dSJohn Marino /* Return 1 if A - B would overflow in [MIN,MAX] arithmetic. 168200fbe8dSJohn Marino See above for restrictions. */ 169200fbe8dSJohn Marino #define INT_SUBTRACT_RANGE_OVERFLOW(a, b, min, max) \ 170200fbe8dSJohn Marino ((b) < 0 \ 171200fbe8dSJohn Marino ? (max) + (b) < (a) \ 172200fbe8dSJohn Marino : (a) < (min) + (b)) 173200fbe8dSJohn Marino 174200fbe8dSJohn Marino /* Return 1 if - A would overflow in [MIN,MAX] arithmetic. 175200fbe8dSJohn Marino See above for restrictions. */ 176200fbe8dSJohn Marino #define INT_NEGATE_RANGE_OVERFLOW(a, min, max) \ 177200fbe8dSJohn Marino ((min) < 0 \ 178200fbe8dSJohn Marino ? (a) < - (max) \ 179200fbe8dSJohn Marino : 0 < (a)) 180200fbe8dSJohn Marino 181200fbe8dSJohn Marino /* Return 1 if A * B would overflow in [MIN,MAX] arithmetic. 182200fbe8dSJohn Marino See above for restrictions. Avoid && and || as they tickle 183200fbe8dSJohn Marino bugs in Sun C 5.11 2010/08/13 and other compilers; see 184*09d4459fSDaniel Fojt <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00401.html>. */ 185200fbe8dSJohn Marino #define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \ 186200fbe8dSJohn Marino ((b) < 0 \ 187200fbe8dSJohn Marino ? ((a) < 0 \ 188200fbe8dSJohn Marino ? (a) < (max) / (b) \ 189200fbe8dSJohn Marino : (b) == -1 \ 190200fbe8dSJohn Marino ? 0 \ 191200fbe8dSJohn Marino : (min) / (b) < (a)) \ 192200fbe8dSJohn Marino : (b) == 0 \ 193200fbe8dSJohn Marino ? 0 \ 194200fbe8dSJohn Marino : ((a) < 0 \ 195200fbe8dSJohn Marino ? (a) < (min) / (b) \ 196200fbe8dSJohn Marino : (max) / (b) < (a))) 197200fbe8dSJohn Marino 198200fbe8dSJohn Marino /* Return 1 if A / B would overflow in [MIN,MAX] arithmetic. 199200fbe8dSJohn Marino See above for restrictions. Do not check for division by zero. */ 200200fbe8dSJohn Marino #define INT_DIVIDE_RANGE_OVERFLOW(a, b, min, max) \ 201200fbe8dSJohn Marino ((min) < 0 && (b) == -1 && (a) < - (max)) 202200fbe8dSJohn Marino 203200fbe8dSJohn Marino /* Return 1 if A % B would overflow in [MIN,MAX] arithmetic. 204200fbe8dSJohn Marino See above for restrictions. Do not check for division by zero. 205200fbe8dSJohn Marino Mathematically, % should never overflow, but on x86-like hosts 206200fbe8dSJohn Marino INT_MIN % -1 traps, and the C standard permits this, so treat this 207200fbe8dSJohn Marino as an overflow too. */ 208200fbe8dSJohn Marino #define INT_REMAINDER_RANGE_OVERFLOW(a, b, min, max) \ 209200fbe8dSJohn Marino INT_DIVIDE_RANGE_OVERFLOW (a, b, min, max) 210200fbe8dSJohn Marino 211200fbe8dSJohn Marino /* Return 1 if A << B would overflow in [MIN,MAX] arithmetic. 212200fbe8dSJohn Marino See above for restrictions. Here, MIN and MAX are for A only, and B need 213200fbe8dSJohn Marino not be of the same type as the other arguments. The C standard says that 214200fbe8dSJohn Marino behavior is undefined for shifts unless 0 <= B < wordwidth, and that when 215200fbe8dSJohn Marino A is negative then A << B has undefined behavior and A >> B has 216200fbe8dSJohn Marino implementation-defined behavior, but do not check these other 217200fbe8dSJohn Marino restrictions. */ 218200fbe8dSJohn Marino #define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \ 219200fbe8dSJohn Marino ((a) < 0 \ 220200fbe8dSJohn Marino ? (a) < (min) >> (b) \ 221200fbe8dSJohn Marino : (max) >> (b) < (a)) 222200fbe8dSJohn Marino 223*09d4459fSDaniel Fojt /* True if __builtin_add_overflow (A, B, P) and __builtin_sub_overflow 224*09d4459fSDaniel Fojt (A, B, P) work when P is non-null. */ 225*09d4459fSDaniel Fojt #if 5 <= __GNUC__ && !defined __ICC 226*09d4459fSDaniel Fojt # define _GL_HAS_BUILTIN_ADD_OVERFLOW 1 227*09d4459fSDaniel Fojt #elif defined __has_builtin 228*09d4459fSDaniel Fojt # define _GL_HAS_BUILTIN_ADD_OVERFLOW __has_builtin (__builtin_add_overflow) 229*09d4459fSDaniel Fojt #else 230*09d4459fSDaniel Fojt # define _GL_HAS_BUILTIN_ADD_OVERFLOW 0 231*09d4459fSDaniel Fojt #endif 232*09d4459fSDaniel Fojt 233*09d4459fSDaniel Fojt /* True if __builtin_mul_overflow (A, B, P) works when P is non-null. */ 234*09d4459fSDaniel Fojt #ifdef __clang__ 235*09d4459fSDaniel Fojt /* Work around Clang bug <https://bugs.llvm.org/show_bug.cgi?id=16404>. */ 236*09d4459fSDaniel Fojt # define _GL_HAS_BUILTIN_MUL_OVERFLOW 0 237*09d4459fSDaniel Fojt #else 238*09d4459fSDaniel Fojt # define _GL_HAS_BUILTIN_MUL_OVERFLOW _GL_HAS_BUILTIN_ADD_OVERFLOW 239*09d4459fSDaniel Fojt #endif 240*09d4459fSDaniel Fojt 241*09d4459fSDaniel Fojt /* True if __builtin_add_overflow_p (A, B, C) works, and similarly for 242*09d4459fSDaniel Fojt __builtin_mul_overflow_p and __builtin_mul_overflow_p. */ 243*09d4459fSDaniel Fojt #define _GL_HAS_BUILTIN_OVERFLOW_P (7 <= __GNUC__) 244200fbe8dSJohn Marino 245200fbe8dSJohn Marino /* The _GL*_OVERFLOW macros have the same restrictions as the 246200fbe8dSJohn Marino *_RANGE_OVERFLOW macros, except that they do not assume that operands 247200fbe8dSJohn Marino (e.g., A and B) have the same type as MIN and MAX. Instead, they assume 248200fbe8dSJohn Marino that the result (e.g., A + B) has that type. */ 249*09d4459fSDaniel Fojt #if _GL_HAS_BUILTIN_OVERFLOW_P 250*09d4459fSDaniel Fojt # define _GL_ADD_OVERFLOW(a, b, min, max) \ 251*09d4459fSDaniel Fojt __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0) 252*09d4459fSDaniel Fojt # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \ 253*09d4459fSDaniel Fojt __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0) 254*09d4459fSDaniel Fojt # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \ 255*09d4459fSDaniel Fojt __builtin_mul_overflow_p (a, b, (__typeof__ ((a) * (b))) 0) 256*09d4459fSDaniel Fojt #else 257200fbe8dSJohn Marino # define _GL_ADD_OVERFLOW(a, b, min, max) \ 258200fbe8dSJohn Marino ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \ 259200fbe8dSJohn Marino : (a) < 0 ? (b) <= (a) + (b) \ 260200fbe8dSJohn Marino : (b) < 0 ? (a) <= (a) + (b) \ 261200fbe8dSJohn Marino : (a) + (b) < (b)) 262200fbe8dSJohn Marino # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \ 263200fbe8dSJohn Marino ((min) < 0 ? INT_SUBTRACT_RANGE_OVERFLOW (a, b, min, max) \ 264200fbe8dSJohn Marino : (a) < 0 ? 1 \ 265200fbe8dSJohn Marino : (b) < 0 ? (a) - (b) <= (a) \ 266200fbe8dSJohn Marino : (a) < (b)) 267200fbe8dSJohn Marino # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \ 268200fbe8dSJohn Marino (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a)))) \ 269200fbe8dSJohn Marino || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max)) 270*09d4459fSDaniel Fojt #endif 271200fbe8dSJohn Marino #define _GL_DIVIDE_OVERFLOW(a, b, min, max) \ 272200fbe8dSJohn Marino ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \ 273200fbe8dSJohn Marino : (a) < 0 ? (b) <= (a) + (b) - 1 \ 274200fbe8dSJohn Marino : (b) < 0 && (a) + (b) <= (a)) 275200fbe8dSJohn Marino #define _GL_REMAINDER_OVERFLOW(a, b, min, max) \ 276200fbe8dSJohn Marino ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \ 277200fbe8dSJohn Marino : (a) < 0 ? (a) % (b) != ((max) - (b) + 1) % (b) \ 278200fbe8dSJohn Marino : (b) < 0 && ! _GL_UNSIGNED_NEG_MULTIPLE (a, b, max)) 279200fbe8dSJohn Marino 280200fbe8dSJohn Marino /* Return a nonzero value if A is a mathematical multiple of B, where 281200fbe8dSJohn Marino A is unsigned, B is negative, and MAX is the maximum value of A's 282200fbe8dSJohn Marino type. A's type must be the same as (A % B)'s type. Normally (A % 283200fbe8dSJohn Marino -B == 0) suffices, but things get tricky if -B would overflow. */ 284200fbe8dSJohn Marino #define _GL_UNSIGNED_NEG_MULTIPLE(a, b, max) \ 285200fbe8dSJohn Marino (((b) < -_GL_SIGNED_INT_MAXIMUM (b) \ 286200fbe8dSJohn Marino ? (_GL_SIGNED_INT_MAXIMUM (b) == (max) \ 287200fbe8dSJohn Marino ? (a) \ 288200fbe8dSJohn Marino : (a) % (_GL_INT_CONVERT (a, _GL_SIGNED_INT_MAXIMUM (b)) + 1)) \ 289200fbe8dSJohn Marino : (a) % - (b)) \ 290200fbe8dSJohn Marino == 0) 291200fbe8dSJohn Marino 292*09d4459fSDaniel Fojt /* Check for integer overflow, and report low order bits of answer. 293200fbe8dSJohn Marino 294200fbe8dSJohn Marino The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators 295200fbe8dSJohn Marino might not yield numerically correct answers due to arithmetic overflow. 296*09d4459fSDaniel Fojt The INT_<op>_WRAPV macros compute the low-order bits of the sum, 297*09d4459fSDaniel Fojt difference, and product of two C integers, and return 1 if these 298*09d4459fSDaniel Fojt low-order bits are not numerically correct. 299*09d4459fSDaniel Fojt These macros work correctly on all known practical hosts, and do not rely 300200fbe8dSJohn Marino on undefined behavior due to signed arithmetic overflow. 301200fbe8dSJohn Marino 302*09d4459fSDaniel Fojt Example usage, assuming A and B are long int: 303200fbe8dSJohn Marino 304*09d4459fSDaniel Fojt if (INT_MULTIPLY_OVERFLOW (a, b)) 305*09d4459fSDaniel Fojt printf ("result would overflow\n"); 306200fbe8dSJohn Marino else 307*09d4459fSDaniel Fojt printf ("result is %ld (no overflow)\n", a * b); 308*09d4459fSDaniel Fojt 309*09d4459fSDaniel Fojt Example usage with WRAPV flavor: 310*09d4459fSDaniel Fojt 311*09d4459fSDaniel Fojt long int result; 312*09d4459fSDaniel Fojt bool overflow = INT_MULTIPLY_WRAPV (a, b, &result); 313*09d4459fSDaniel Fojt printf ("result is %ld (%s)\n", result, 314*09d4459fSDaniel Fojt overflow ? "after overflow" : "no overflow"); 315*09d4459fSDaniel Fojt 316*09d4459fSDaniel Fojt Restrictions on these macros: 317200fbe8dSJohn Marino 318200fbe8dSJohn Marino These macros do not check for all possible numerical problems or 319200fbe8dSJohn Marino undefined or unspecified behavior: they do not check for division 320200fbe8dSJohn Marino by zero, for bad shift counts, or for shifting negative numbers. 321200fbe8dSJohn Marino 322200fbe8dSJohn Marino These macros may evaluate their arguments zero or multiple times, so the 323200fbe8dSJohn Marino arguments should not have side effects. 324200fbe8dSJohn Marino 325*09d4459fSDaniel Fojt The WRAPV macros are not constant expressions. They support only 326*09d4459fSDaniel Fojt +, binary -, and *. Because the WRAPV macros convert the result, 327*09d4459fSDaniel Fojt they report overflow in different circumstances than the OVERFLOW 328*09d4459fSDaniel Fojt macros do. 329*09d4459fSDaniel Fojt 330*09d4459fSDaniel Fojt These macros are tuned for their last input argument being a constant. 331200fbe8dSJohn Marino 332200fbe8dSJohn Marino Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B, 333200fbe8dSJohn Marino A % B, and A << B would overflow, respectively. */ 334200fbe8dSJohn Marino 335200fbe8dSJohn Marino #define INT_ADD_OVERFLOW(a, b) \ 336200fbe8dSJohn Marino _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW) 337200fbe8dSJohn Marino #define INT_SUBTRACT_OVERFLOW(a, b) \ 338200fbe8dSJohn Marino _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW) 339*09d4459fSDaniel Fojt #if _GL_HAS_BUILTIN_OVERFLOW_P 340*09d4459fSDaniel Fojt # define INT_NEGATE_OVERFLOW(a) INT_SUBTRACT_OVERFLOW (0, a) 341*09d4459fSDaniel Fojt #else 342200fbe8dSJohn Marino # define INT_NEGATE_OVERFLOW(a) \ 343200fbe8dSJohn Marino INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a)) 344*09d4459fSDaniel Fojt #endif 345200fbe8dSJohn Marino #define INT_MULTIPLY_OVERFLOW(a, b) \ 346200fbe8dSJohn Marino _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW) 347200fbe8dSJohn Marino #define INT_DIVIDE_OVERFLOW(a, b) \ 348200fbe8dSJohn Marino _GL_BINARY_OP_OVERFLOW (a, b, _GL_DIVIDE_OVERFLOW) 349200fbe8dSJohn Marino #define INT_REMAINDER_OVERFLOW(a, b) \ 350200fbe8dSJohn Marino _GL_BINARY_OP_OVERFLOW (a, b, _GL_REMAINDER_OVERFLOW) 351200fbe8dSJohn Marino #define INT_LEFT_SHIFT_OVERFLOW(a, b) \ 352200fbe8dSJohn Marino INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \ 353200fbe8dSJohn Marino _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a)) 354200fbe8dSJohn Marino 355200fbe8dSJohn Marino /* Return 1 if the expression A <op> B would overflow, 356200fbe8dSJohn Marino where OP_RESULT_OVERFLOW (A, B, MIN, MAX) does the actual test, 357200fbe8dSJohn Marino assuming MIN and MAX are the minimum and maximum for the result type. 358200fbe8dSJohn Marino Arguments should be free of side effects. */ 359200fbe8dSJohn Marino #define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \ 360200fbe8dSJohn Marino op_result_overflow (a, b, \ 361*09d4459fSDaniel Fojt _GL_INT_MINIMUM (_GL_INT_CONVERT (a, b)), \ 362*09d4459fSDaniel Fojt _GL_INT_MAXIMUM (_GL_INT_CONVERT (a, b))) 363*09d4459fSDaniel Fojt 364*09d4459fSDaniel Fojt /* Store the low-order bits of A + B, A - B, A * B, respectively, into *R. 365*09d4459fSDaniel Fojt Return 1 if the result overflows. See above for restrictions. */ 366*09d4459fSDaniel Fojt #if _GL_HAS_BUILTIN_ADD_OVERFLOW 367*09d4459fSDaniel Fojt # define INT_ADD_WRAPV(a, b, r) __builtin_add_overflow (a, b, r) 368*09d4459fSDaniel Fojt # define INT_SUBTRACT_WRAPV(a, b, r) __builtin_sub_overflow (a, b, r) 369*09d4459fSDaniel Fojt #else 370*09d4459fSDaniel Fojt # define INT_ADD_WRAPV(a, b, r) \ 371*09d4459fSDaniel Fojt _GL_INT_OP_WRAPV (a, b, r, +, _GL_INT_ADD_RANGE_OVERFLOW) 372*09d4459fSDaniel Fojt # define INT_SUBTRACT_WRAPV(a, b, r) \ 373*09d4459fSDaniel Fojt _GL_INT_OP_WRAPV (a, b, r, -, _GL_INT_SUBTRACT_RANGE_OVERFLOW) 374*09d4459fSDaniel Fojt #endif 375*09d4459fSDaniel Fojt #if _GL_HAS_BUILTIN_MUL_OVERFLOW 376*09d4459fSDaniel Fojt # if (9 < __GNUC__ + (3 <= __GNUC_MINOR__) \ 377*09d4459fSDaniel Fojt || (__GNUC__ == 8 && 4 <= __GNUC_MINOR__)) 378*09d4459fSDaniel Fojt # define INT_MULTIPLY_WRAPV(a, b, r) __builtin_mul_overflow (a, b, r) 379*09d4459fSDaniel Fojt # else 380*09d4459fSDaniel Fojt /* Work around GCC bug 91450. */ 381*09d4459fSDaniel Fojt # define INT_MULTIPLY_WRAPV(a, b, r) \ 382*09d4459fSDaniel Fojt ((!_GL_SIGNED_TYPE_OR_EXPR (*(r)) && EXPR_SIGNED (a) && EXPR_SIGNED (b) \ 383*09d4459fSDaniel Fojt && _GL_INT_MULTIPLY_RANGE_OVERFLOW (a, b, 0, (__typeof__ (*(r))) -1)) \ 384*09d4459fSDaniel Fojt ? ((void) __builtin_mul_overflow (a, b, r), 1) \ 385*09d4459fSDaniel Fojt : __builtin_mul_overflow (a, b, r)) 386*09d4459fSDaniel Fojt # endif 387*09d4459fSDaniel Fojt #else 388*09d4459fSDaniel Fojt # define INT_MULTIPLY_WRAPV(a, b, r) \ 389*09d4459fSDaniel Fojt _GL_INT_OP_WRAPV (a, b, r, *, _GL_INT_MULTIPLY_RANGE_OVERFLOW) 390*09d4459fSDaniel Fojt #endif 391*09d4459fSDaniel Fojt 392*09d4459fSDaniel Fojt /* Nonzero if this compiler has GCC bug 68193 or Clang bug 25390. See: 393*09d4459fSDaniel Fojt https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68193 394*09d4459fSDaniel Fojt https://llvm.org/bugs/show_bug.cgi?id=25390 395*09d4459fSDaniel Fojt For now, assume all versions of GCC-like compilers generate bogus 396*09d4459fSDaniel Fojt warnings for _Generic. This matters only for compilers that 397*09d4459fSDaniel Fojt lack relevant builtins. */ 398*09d4459fSDaniel Fojt #if __GNUC__ 399*09d4459fSDaniel Fojt # define _GL__GENERIC_BOGUS 1 400*09d4459fSDaniel Fojt #else 401*09d4459fSDaniel Fojt # define _GL__GENERIC_BOGUS 0 402*09d4459fSDaniel Fojt #endif 403*09d4459fSDaniel Fojt 404*09d4459fSDaniel Fojt /* Store the low-order bits of A <op> B into *R, where OP specifies 405*09d4459fSDaniel Fojt the operation and OVERFLOW the overflow predicate. Return 1 if the 406*09d4459fSDaniel Fojt result overflows. See above for restrictions. */ 407*09d4459fSDaniel Fojt #if 201112 <= __STDC_VERSION__ && !_GL__GENERIC_BOGUS 408*09d4459fSDaniel Fojt # define _GL_INT_OP_WRAPV(a, b, r, op, overflow) \ 409*09d4459fSDaniel Fojt (_Generic \ 410*09d4459fSDaniel Fojt (*(r), \ 411*09d4459fSDaniel Fojt signed char: \ 412*09d4459fSDaniel Fojt _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ 413*09d4459fSDaniel Fojt signed char, SCHAR_MIN, SCHAR_MAX), \ 414*09d4459fSDaniel Fojt unsigned char: \ 415*09d4459fSDaniel Fojt _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ 416*09d4459fSDaniel Fojt unsigned char, 0, UCHAR_MAX), \ 417*09d4459fSDaniel Fojt short int: \ 418*09d4459fSDaniel Fojt _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ 419*09d4459fSDaniel Fojt short int, SHRT_MIN, SHRT_MAX), \ 420*09d4459fSDaniel Fojt unsigned short int: \ 421*09d4459fSDaniel Fojt _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ 422*09d4459fSDaniel Fojt unsigned short int, 0, USHRT_MAX), \ 423*09d4459fSDaniel Fojt int: \ 424*09d4459fSDaniel Fojt _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ 425*09d4459fSDaniel Fojt int, INT_MIN, INT_MAX), \ 426*09d4459fSDaniel Fojt unsigned int: \ 427*09d4459fSDaniel Fojt _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ 428*09d4459fSDaniel Fojt unsigned int, 0, UINT_MAX), \ 429*09d4459fSDaniel Fojt long int: \ 430*09d4459fSDaniel Fojt _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \ 431*09d4459fSDaniel Fojt long int, LONG_MIN, LONG_MAX), \ 432*09d4459fSDaniel Fojt unsigned long int: \ 433*09d4459fSDaniel Fojt _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \ 434*09d4459fSDaniel Fojt unsigned long int, 0, ULONG_MAX), \ 435*09d4459fSDaniel Fojt long long int: \ 436*09d4459fSDaniel Fojt _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \ 437*09d4459fSDaniel Fojt long long int, LLONG_MIN, LLONG_MAX), \ 438*09d4459fSDaniel Fojt unsigned long long int: \ 439*09d4459fSDaniel Fojt _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \ 440*09d4459fSDaniel Fojt unsigned long long int, 0, ULLONG_MAX))) 441*09d4459fSDaniel Fojt #else 442*09d4459fSDaniel Fojt /* Store the low-order bits of A <op> B into *R, where OP specifies 443*09d4459fSDaniel Fojt the operation and OVERFLOW the overflow predicate. If *R is 444*09d4459fSDaniel Fojt signed, its type is ST with bounds SMIN..SMAX; otherwise its type 445*09d4459fSDaniel Fojt is UT with bounds U..UMAX. ST and UT are narrower than int. 446*09d4459fSDaniel Fojt Return 1 if the result overflows. See above for restrictions. */ 447*09d4459fSDaniel Fojt # if _GL_HAVE___TYPEOF__ 448*09d4459fSDaniel Fojt # define _GL_INT_OP_WRAPV_SMALLISH(a,b,r,op,overflow,st,smin,smax,ut,umax) \ 449*09d4459fSDaniel Fojt (TYPE_SIGNED (__typeof__ (*(r))) \ 450*09d4459fSDaniel Fojt ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, st, smin, smax) \ 451*09d4459fSDaniel Fojt : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, ut, 0, umax)) 452*09d4459fSDaniel Fojt # else 453*09d4459fSDaniel Fojt # define _GL_INT_OP_WRAPV_SMALLISH(a,b,r,op,overflow,st,smin,smax,ut,umax) \ 454*09d4459fSDaniel Fojt (overflow (a, b, smin, smax) \ 455*09d4459fSDaniel Fojt ? (overflow (a, b, 0, umax) \ 456*09d4459fSDaniel Fojt ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st), 1) \ 457*09d4459fSDaniel Fojt : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st)) < 0) \ 458*09d4459fSDaniel Fojt : (overflow (a, b, 0, umax) \ 459*09d4459fSDaniel Fojt ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st)) >= 0 \ 460*09d4459fSDaniel Fojt : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st), 0))) 461*09d4459fSDaniel Fojt # endif 462*09d4459fSDaniel Fojt 463*09d4459fSDaniel Fojt # define _GL_INT_OP_WRAPV(a, b, r, op, overflow) \ 464*09d4459fSDaniel Fojt (sizeof *(r) == sizeof (signed char) \ 465*09d4459fSDaniel Fojt ? _GL_INT_OP_WRAPV_SMALLISH (a, b, r, op, overflow, \ 466*09d4459fSDaniel Fojt signed char, SCHAR_MIN, SCHAR_MAX, \ 467*09d4459fSDaniel Fojt unsigned char, UCHAR_MAX) \ 468*09d4459fSDaniel Fojt : sizeof *(r) == sizeof (short int) \ 469*09d4459fSDaniel Fojt ? _GL_INT_OP_WRAPV_SMALLISH (a, b, r, op, overflow, \ 470*09d4459fSDaniel Fojt short int, SHRT_MIN, SHRT_MAX, \ 471*09d4459fSDaniel Fojt unsigned short int, USHRT_MAX) \ 472*09d4459fSDaniel Fojt : sizeof *(r) == sizeof (int) \ 473*09d4459fSDaniel Fojt ? (EXPR_SIGNED (*(r)) \ 474*09d4459fSDaniel Fojt ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ 475*09d4459fSDaniel Fojt int, INT_MIN, INT_MAX) \ 476*09d4459fSDaniel Fojt : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \ 477*09d4459fSDaniel Fojt unsigned int, 0, UINT_MAX)) \ 478*09d4459fSDaniel Fojt : _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow)) 479*09d4459fSDaniel Fojt # ifdef LLONG_MAX 480*09d4459fSDaniel Fojt # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \ 481*09d4459fSDaniel Fojt (sizeof *(r) == sizeof (long int) \ 482*09d4459fSDaniel Fojt ? (EXPR_SIGNED (*(r)) \ 483*09d4459fSDaniel Fojt ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \ 484*09d4459fSDaniel Fojt long int, LONG_MIN, LONG_MAX) \ 485*09d4459fSDaniel Fojt : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \ 486*09d4459fSDaniel Fojt unsigned long int, 0, ULONG_MAX)) \ 487*09d4459fSDaniel Fojt : (EXPR_SIGNED (*(r)) \ 488*09d4459fSDaniel Fojt ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \ 489*09d4459fSDaniel Fojt long long int, LLONG_MIN, LLONG_MAX) \ 490*09d4459fSDaniel Fojt : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \ 491*09d4459fSDaniel Fojt unsigned long long int, 0, ULLONG_MAX))) 492*09d4459fSDaniel Fojt # else 493*09d4459fSDaniel Fojt # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \ 494*09d4459fSDaniel Fojt (EXPR_SIGNED (*(r)) \ 495*09d4459fSDaniel Fojt ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \ 496*09d4459fSDaniel Fojt long int, LONG_MIN, LONG_MAX) \ 497*09d4459fSDaniel Fojt : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \ 498*09d4459fSDaniel Fojt unsigned long int, 0, ULONG_MAX)) 499*09d4459fSDaniel Fojt # endif 500*09d4459fSDaniel Fojt #endif 501*09d4459fSDaniel Fojt 502*09d4459fSDaniel Fojt /* Store the low-order bits of A <op> B into *R, where the operation 503*09d4459fSDaniel Fojt is given by OP. Use the unsigned type UT for calculation to avoid 504*09d4459fSDaniel Fojt overflow problems. *R's type is T, with extrema TMIN and TMAX. 505*09d4459fSDaniel Fojt T must be a signed integer type. Return 1 if the result overflows. */ 506*09d4459fSDaniel Fojt #define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \ 507*09d4459fSDaniel Fojt (overflow (a, b, tmin, tmax) \ 508*09d4459fSDaniel Fojt ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 1) \ 509*09d4459fSDaniel Fojt : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 0)) 510*09d4459fSDaniel Fojt 511*09d4459fSDaniel Fojt /* Return the low-order bits of A <op> B, where the operation is given 512*09d4459fSDaniel Fojt by OP. Use the unsigned type UT for calculation to avoid undefined 513*09d4459fSDaniel Fojt behavior on signed integer overflow, and convert the result to type T. 514*09d4459fSDaniel Fojt UT is at least as wide as T and is no narrower than unsigned int, 515*09d4459fSDaniel Fojt T is two's complement, and there is no padding or trap representations. 516*09d4459fSDaniel Fojt Assume that converting UT to T yields the low-order bits, as is 517*09d4459fSDaniel Fojt done in all known two's-complement C compilers. E.g., see: 518*09d4459fSDaniel Fojt https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html 519*09d4459fSDaniel Fojt 520*09d4459fSDaniel Fojt According to the C standard, converting UT to T yields an 521*09d4459fSDaniel Fojt implementation-defined result or signal for values outside T's 522*09d4459fSDaniel Fojt range. However, code that works around this theoretical problem 523*09d4459fSDaniel Fojt runs afoul of a compiler bug in Oracle Studio 12.3 x86. See: 524*09d4459fSDaniel Fojt https://lists.gnu.org/r/bug-gnulib/2017-04/msg00049.html 525*09d4459fSDaniel Fojt As the compiler bug is real, don't try to work around the 526*09d4459fSDaniel Fojt theoretical problem. */ 527*09d4459fSDaniel Fojt 528*09d4459fSDaniel Fojt #define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t) \ 529*09d4459fSDaniel Fojt ((t) ((ut) (a) op (ut) (b))) 530*09d4459fSDaniel Fojt 531*09d4459fSDaniel Fojt /* Return true if the numeric values A + B, A - B, A * B fall outside 532*09d4459fSDaniel Fojt the range TMIN..TMAX. Arguments should be integer expressions 533*09d4459fSDaniel Fojt without side effects. TMIN should be signed and nonpositive. 534*09d4459fSDaniel Fojt TMAX should be positive, and should be signed unless TMIN is zero. */ 535*09d4459fSDaniel Fojt #define _GL_INT_ADD_RANGE_OVERFLOW(a, b, tmin, tmax) \ 536*09d4459fSDaniel Fojt ((b) < 0 \ 537*09d4459fSDaniel Fojt ? (((tmin) \ 538*09d4459fSDaniel Fojt ? ((EXPR_SIGNED (_GL_INT_CONVERT (a, (tmin) - (b))) || (b) < (tmin)) \ 539*09d4459fSDaniel Fojt && (a) < (tmin) - (b)) \ 540*09d4459fSDaniel Fojt : (a) <= -1 - (b)) \ 541*09d4459fSDaniel Fojt || ((EXPR_SIGNED (a) ? 0 <= (a) : (tmax) < (a)) && (tmax) < (a) + (b))) \ 542*09d4459fSDaniel Fojt : (a) < 0 \ 543*09d4459fSDaniel Fojt ? (((tmin) \ 544*09d4459fSDaniel Fojt ? ((EXPR_SIGNED (_GL_INT_CONVERT (b, (tmin) - (a))) || (a) < (tmin)) \ 545*09d4459fSDaniel Fojt && (b) < (tmin) - (a)) \ 546*09d4459fSDaniel Fojt : (b) <= -1 - (a)) \ 547*09d4459fSDaniel Fojt || ((EXPR_SIGNED (_GL_INT_CONVERT (a, b)) || (tmax) < (b)) \ 548*09d4459fSDaniel Fojt && (tmax) < (a) + (b))) \ 549*09d4459fSDaniel Fojt : (tmax) < (b) || (tmax) - (b) < (a)) 550*09d4459fSDaniel Fojt #define _GL_INT_SUBTRACT_RANGE_OVERFLOW(a, b, tmin, tmax) \ 551*09d4459fSDaniel Fojt (((a) < 0) == ((b) < 0) \ 552*09d4459fSDaniel Fojt ? ((a) < (b) \ 553*09d4459fSDaniel Fojt ? !(tmin) || -1 - (tmin) < (b) - (a) - 1 \ 554*09d4459fSDaniel Fojt : (tmax) < (a) - (b)) \ 555*09d4459fSDaniel Fojt : (a) < 0 \ 556*09d4459fSDaniel Fojt ? ((!EXPR_SIGNED (_GL_INT_CONVERT ((a) - (tmin), b)) && (a) - (tmin) < 0) \ 557*09d4459fSDaniel Fojt || (a) - (tmin) < (b)) \ 558*09d4459fSDaniel Fojt : ((! (EXPR_SIGNED (_GL_INT_CONVERT (tmax, b)) \ 559*09d4459fSDaniel Fojt && EXPR_SIGNED (_GL_INT_CONVERT ((tmax) + (b), a))) \ 560*09d4459fSDaniel Fojt && (tmax) <= -1 - (b)) \ 561*09d4459fSDaniel Fojt || (tmax) + (b) < (a))) 562*09d4459fSDaniel Fojt #define _GL_INT_MULTIPLY_RANGE_OVERFLOW(a, b, tmin, tmax) \ 563*09d4459fSDaniel Fojt ((b) < 0 \ 564*09d4459fSDaniel Fojt ? ((a) < 0 \ 565*09d4459fSDaniel Fojt ? (EXPR_SIGNED (_GL_INT_CONVERT (tmax, b)) \ 566*09d4459fSDaniel Fojt ? (a) < (tmax) / (b) \ 567*09d4459fSDaniel Fojt : ((INT_NEGATE_OVERFLOW (b) \ 568*09d4459fSDaniel Fojt ? _GL_INT_CONVERT (b, tmax) >> (TYPE_WIDTH (b) - 1) \ 569*09d4459fSDaniel Fojt : (tmax) / -(b)) \ 570*09d4459fSDaniel Fojt <= -1 - (a))) \ 571*09d4459fSDaniel Fojt : INT_NEGATE_OVERFLOW (_GL_INT_CONVERT (b, tmin)) && (b) == -1 \ 572*09d4459fSDaniel Fojt ? (EXPR_SIGNED (a) \ 573*09d4459fSDaniel Fojt ? 0 < (a) + (tmin) \ 574*09d4459fSDaniel Fojt : 0 < (a) && -1 - (tmin) < (a) - 1) \ 575*09d4459fSDaniel Fojt : (tmin) / (b) < (a)) \ 576*09d4459fSDaniel Fojt : (b) == 0 \ 577*09d4459fSDaniel Fojt ? 0 \ 578*09d4459fSDaniel Fojt : ((a) < 0 \ 579*09d4459fSDaniel Fojt ? (INT_NEGATE_OVERFLOW (_GL_INT_CONVERT (a, tmin)) && (a) == -1 \ 580*09d4459fSDaniel Fojt ? (EXPR_SIGNED (b) ? 0 < (b) + (tmin) : -1 - (tmin) < (b) - 1) \ 581*09d4459fSDaniel Fojt : (tmin) / (a) < (b)) \ 582*09d4459fSDaniel Fojt : (tmax) / (b) < (a))) 583200fbe8dSJohn Marino 584200fbe8dSJohn Marino #endif /* _GL_INTPROPS_H */ 585