1 /* intprops.h -- properties of integer types 2 3 Copyright (C) 2001-2024 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 #ifndef _GL_INTPROPS_H 19 #define _GL_INTPROPS_H 20 21 #include "intprops-internal.h" 22 23 /* The extra casts in the following macros work around compiler bugs, 24 e.g., in Cray C 5.0.3.0. */ 25 26 /* True if the arithmetic type T is an integer type. bool counts as 27 an integer. */ 28 #define TYPE_IS_INTEGER(t) ((t) 1.5 == 1) 29 30 /* True if the real type T is signed. */ 31 #define TYPE_SIGNED(t) _GL_TYPE_SIGNED (t) 32 33 /* Return 1 if the real expression E, after promotion, has a 34 signed or floating type. Do not evaluate E. */ 35 #define EXPR_SIGNED(e) _GL_EXPR_SIGNED (e) 36 37 /* Minimum and maximum values for integer types and expressions. */ 38 39 /* The width in bits of the integer type or expression T. 40 Do not evaluate T. T must not be a bit-field expression. 41 Padding bits are not supported; this is checked at compile-time below. */ 42 #define TYPE_WIDTH(t) _GL_TYPE_WIDTH (t) 43 44 /* The maximum and minimum values for the integer type T. */ 45 #define TYPE_MINIMUM(t) ((t) ~TYPE_MAXIMUM (t)) 46 #define TYPE_MAXIMUM(t) \ 47 ((t) (!TYPE_SIGNED (t) ? (t) - 1 : ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1))) 48 49 /* Bound on length of the string representing an unsigned integer 50 value representable in B bits. log10 (2.0) < 146/485. The 51 smallest value of B where this bound is not tight is 2621. */ 52 #define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485) 53 54 /* Bound on length of the string representing an integer type or expression T. 55 T must not be a bit-field expression. 56 57 Subtract 1 for the sign bit if T is signed, and then add 1 more for 58 a minus sign if needed. 59 60 Because _GL_SIGNED_TYPE_OR_EXPR sometimes returns 1 when its argument is 61 unsigned, this macro may overestimate the true bound by one byte when 62 applied to unsigned types of size 2, 4, 16, ... bytes. */ 63 #define INT_STRLEN_BOUND(t) \ 64 (INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - _GL_SIGNED_TYPE_OR_EXPR (t)) \ 65 + _GL_SIGNED_TYPE_OR_EXPR (t)) 66 67 /* Bound on buffer size needed to represent an integer type or expression T, 68 including the terminating null. T must not be a bit-field expression. */ 69 #define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1) 70 71 /* Range overflow checks. 72 73 The INT_<op>_RANGE_OVERFLOW macros return 1 if the corresponding C 74 operators overflow arithmetically when given the same arguments. 75 These macros do not rely on undefined or implementation-defined behavior. 76 Although their implementations are simple and straightforward, 77 they are harder to use and may be less efficient than the 78 INT_<op>_WRAPV, INT_<op>_OK, and INT_<op>_OVERFLOW macros described below. 79 80 Example usage: 81 82 long int i = ...; 83 long int j = ...; 84 if (INT_MULTIPLY_RANGE_OVERFLOW (i, j, LONG_MIN, LONG_MAX)) 85 printf ("multiply would overflow"); 86 else 87 printf ("product is %ld", i * j); 88 89 Restrictions on *_RANGE_OVERFLOW macros: 90 91 These macros do not check for all possible numerical problems or 92 undefined or unspecified behavior: they do not check for division 93 by zero, for bad shift counts, or for shifting negative numbers. 94 95 These macros may evaluate their arguments zero or multiple times, 96 so the arguments should not have side effects. The arithmetic 97 arguments (including the MIN and MAX arguments) must be of the same 98 integer type after the usual arithmetic conversions, and the type 99 must have minimum value MIN and maximum MAX. Unsigned types should 100 use a zero MIN of the proper type. 101 102 Because all arguments are subject to integer promotions, these 103 macros typically do not work on types narrower than 'int'. 104 105 These macros are tuned for constant MIN and MAX. For commutative 106 operations such as A + B, they are also tuned for constant B. */ 107 108 /* Return 1 if A + B would overflow in [MIN,MAX] arithmetic. 109 See above for restrictions. */ 110 #define INT_ADD_RANGE_OVERFLOW(a, b, min, max) ((b) < 0 ? (a) < (min) - (b) : (max) - (b) < (a)) 111 112 /* Return 1 if A - B would overflow in [MIN,MAX] arithmetic. 113 See above for restrictions. */ 114 #define INT_SUBTRACT_RANGE_OVERFLOW(a, b, min, max) \ 115 ((b) < 0 ? (max) + (b) < (a) : (a) < (min) + (b)) 116 117 /* Return 1 if - A would overflow in [MIN,MAX] arithmetic. 118 See above for restrictions. */ 119 #define INT_NEGATE_RANGE_OVERFLOW(a, min, max) _GL_INT_NEGATE_RANGE_OVERFLOW (a, min, max) 120 121 /* Return 1 if A * B would overflow in [MIN,MAX] arithmetic. 122 See above for restrictions. Avoid && and || as they tickle 123 bugs in Sun C 5.11 2010/08/13 and other compilers; see 124 <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00401.html>. */ 125 #define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \ 126 ((b) < 0 ? ((a) < 0 ? (a) < (max) / (b) \ 127 : (b) == -1 ? 0 \ 128 : (min) / (b) < (a)) \ 129 : (b) == 0 ? 0 \ 130 : ((a) < 0 ? (a) < (min) / (b) : (max) / (b) < (a))) 131 132 /* Return 1 if A / B would overflow in [MIN,MAX] arithmetic. 133 See above for restrictions. Do not check for division by zero. */ 134 #define INT_DIVIDE_RANGE_OVERFLOW(a, b, min, max) ((min) < 0 && (b) == -1 && (a) < -(max)) 135 136 /* Return 1 if A % B would overflow in [MIN,MAX] arithmetic. 137 See above for restrictions. Do not check for division by zero. 138 Mathematically, % should never overflow, but on x86-like hosts 139 INT_MIN % -1 traps, and the C standard permits this, so treat this 140 as an overflow too. */ 141 #define INT_REMAINDER_RANGE_OVERFLOW(a, b, min, max) INT_DIVIDE_RANGE_OVERFLOW (a, b, min, max) 142 143 /* Return 1 if A << B would overflow in [MIN,MAX] arithmetic. 144 See above for restrictions. Here, MIN and MAX are for A only, and B need 145 not be of the same type as the other arguments. The C standard says that 146 behavior is undefined for shifts unless 0 <= B < wordwidth, and that when 147 A is negative then A << B has undefined behavior and A >> B has 148 implementation-defined behavior, but do not check these other 149 restrictions. */ 150 #define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \ 151 ((a) < 0 ? (a) < (min) >> (b) : (max) >> (b) < (a)) 152 153 /* The _GL*_OVERFLOW macros have the same restrictions as the 154 *_RANGE_OVERFLOW macros, except that they do not assume that operands 155 (e.g., A and B) have the same type as MIN and MAX. Instead, they assume 156 that the result (e.g., A + B) has that type. */ 157 #if _GL_HAS_BUILTIN_OVERFLOW_P 158 # define _GL_ADD_OVERFLOW(a, b, min, max) \ 159 __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0) 160 # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \ 161 __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0) 162 # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \ 163 __builtin_mul_overflow_p (a, b, (__typeof__ ((a) * (b))) 0) 164 #else 165 # define _GL_ADD_OVERFLOW(a, b, min, max) \ 166 ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \ 167 : (a) < 0 ? (b) <= (a) + (b) \ 168 : (b) < 0 ? (a) <= (a) + (b) \ 169 : (a) + (b) < (b)) 170 # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \ 171 ((min) < 0 ? INT_SUBTRACT_RANGE_OVERFLOW (a, b, min, max) \ 172 : (a) < 0 ? 1 \ 173 : (b) < 0 ? (a) - (b) <= (a) \ 174 : (a) < (b)) 175 # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \ 176 (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a)))) \ 177 || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max)) 178 #endif 179 #define _GL_DIVIDE_OVERFLOW(a, b, min, max) \ 180 ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < -(max) \ 181 : (a) < 0 ? (b) <= (a) + (b) - 1 \ 182 : (b) < 0 && (a) + (b) <= (a)) 183 #define _GL_REMAINDER_OVERFLOW(a, b, min, max) \ 184 ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < -(max) \ 185 : (a) < 0 ? (a) % (b) != ((max) - (b) + 1) % (b) \ 186 : (b) < 0 && !_GL_UNSIGNED_NEG_MULTIPLE (a, b, max)) 187 188 /* Return a nonzero value if A is a mathematical multiple of B, where 189 A is unsigned, B is negative, and MAX is the maximum value of A's 190 type. A's type must be the same as (A % B)'s type. Normally (A % 191 -B == 0) suffices, but things get tricky if -B would overflow. */ 192 #define _GL_UNSIGNED_NEG_MULTIPLE(a, b, max) \ 193 (((b) < -_GL_SIGNED_INT_MAXIMUM (b) \ 194 ? (_GL_SIGNED_INT_MAXIMUM (b) == (max) \ 195 ? (a) \ 196 : (a) % (_GL_INT_CONVERT (a, _GL_SIGNED_INT_MAXIMUM (b)) + 1)) \ 197 : (a) % -(b)) \ 198 == 0) 199 200 /* Check for integer overflow, and report low order bits of answer. 201 202 The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators 203 might not yield numerically correct answers due to arithmetic overflow. 204 The INT_<op>_WRAPV macros compute the low-order bits of the sum, 205 difference, and product of two C integers, and return 1 if these 206 low-order bits are not numerically correct. 207 These macros work correctly on all known practical hosts, and do not rely 208 on undefined behavior due to signed arithmetic overflow. 209 210 Example usage, assuming A and B are long int: 211 212 if (INT_MULTIPLY_OVERFLOW (a, b)) 213 printf ("result would overflow\n"); 214 else 215 printf ("result is %ld (no overflow)\n", a * b); 216 217 Example usage with WRAPV flavor: 218 219 long int result; 220 bool overflow = INT_MULTIPLY_WRAPV (a, b, &result); 221 printf ("result is %ld (%s)\n", result, 222 overflow ? "after overflow" : "no overflow"); 223 224 Restrictions on these macros: 225 226 These macros do not check for all possible numerical problems or 227 undefined or unspecified behavior: they do not check for division 228 by zero, for bad shift counts, or for shifting negative numbers. 229 230 These macros may evaluate their arguments zero or multiple times, so the 231 arguments should not have side effects. 232 233 The WRAPV macros are not constant expressions. They support only 234 +, binary -, and *. 235 236 Because the WRAPV macros convert the result, they report overflow 237 in different circumstances than the OVERFLOW macros do. For 238 example, in the typical case with 16-bit 'short' and 32-bit 'int', 239 if A, B and *R are all of type 'short' then INT_ADD_OVERFLOW (A, B) 240 returns false because the addition cannot overflow after A and B 241 are converted to 'int', whereas INT_ADD_WRAPV (A, B, R) returns 242 true or false depending on whether the sum fits into 'short'. 243 244 These macros are tuned for their last input argument being a constant. 245 246 A, B, and *R should be integers; they need not be the same type, 247 and they need not be all signed or all unsigned. 248 However, none of the integer types should be bit-precise, 249 and *R's type should not be char, bool, or an enumeration type. 250 251 Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B, 252 A % B, and A << B would overflow, respectively. */ 253 254 #define INT_ADD_OVERFLOW(a, b) _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW) 255 #define INT_SUBTRACT_OVERFLOW(a, b) _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW) 256 #define INT_NEGATE_OVERFLOW(a) _GL_INT_NEGATE_OVERFLOW (a) 257 #define INT_MULTIPLY_OVERFLOW(a, b) _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW) 258 #define INT_DIVIDE_OVERFLOW(a, b) _GL_BINARY_OP_OVERFLOW (a, b, _GL_DIVIDE_OVERFLOW) 259 #define INT_REMAINDER_OVERFLOW(a, b) _GL_BINARY_OP_OVERFLOW (a, b, _GL_REMAINDER_OVERFLOW) 260 #define INT_LEFT_SHIFT_OVERFLOW(a, b) \ 261 INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a)) 262 263 /* Return 1 if the expression A <op> B would overflow, 264 where OP_RESULT_OVERFLOW (A, B, MIN, MAX) does the actual test, 265 assuming MIN and MAX are the minimum and maximum for the result type. 266 Arguments should be free of side effects. */ 267 #define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \ 268 op_result_overflow (a, b, _GL_INT_MINIMUM (_GL_INT_CONVERT (a, b)), \ 269 _GL_INT_MAXIMUM (_GL_INT_CONVERT (a, b))) 270 271 /* Store the low-order bits of A + B, A - B, A * B, respectively, into *R. 272 Return 1 if the result overflows. See above for restrictions. */ 273 #define INT_ADD_WRAPV(a, b, r) _GL_INT_ADD_WRAPV (a, b, r) 274 #define INT_SUBTRACT_WRAPV(a, b, r) _GL_INT_SUBTRACT_WRAPV (a, b, r) 275 #define INT_MULTIPLY_WRAPV(a, b, r) _GL_INT_MULTIPLY_WRAPV (a, b, r) 276 277 /* The following macros compute A + B, A - B, and A * B, respectively. 278 If no overflow occurs, they set *R to the result and return 1; 279 otherwise, they return 0 and may modify *R. 280 281 Example usage: 282 283 long int result; 284 if (INT_ADD_OK (a, b, &result)) 285 printf ("result is %ld\n", result); 286 else 287 printf ("overflow\n"); 288 289 A, B, and *R should be integers; they need not be the same type, 290 and they need not be all signed or all unsigned. 291 However, none of the integer types should be bit-precise, 292 and *R's type should not be char, bool, or an enumeration type. 293 294 These macros work correctly on all known practical hosts, and do not rely 295 on undefined behavior due to signed arithmetic overflow. 296 297 These macros are not constant expressions. 298 299 These macros may evaluate their arguments zero or multiple times, so the 300 arguments should not have side effects. 301 302 These macros are tuned for B being a constant. */ 303 304 #define INT_ADD_OK(a, b, r) (!INT_ADD_WRAPV (a, b, r)) 305 #define INT_SUBTRACT_OK(a, b, r) (!INT_SUBTRACT_WRAPV (a, b, r)) 306 #define INT_MULTIPLY_OK(a, b, r) (!INT_MULTIPLY_WRAPV (a, b, r)) 307 308 #endif