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) __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0) 159 #define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \ 160 __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0) 161 #define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \ 162 __builtin_mul_overflow_p (a, b, (__typeof__ ((a) * (b))) 0) 163 #else 164 #define _GL_ADD_OVERFLOW(a, b, min, max) \ 165 ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \ 166 : (a) < 0 ? (b) <= (a) + (b) \ 167 : (b) < 0 ? (a) <= (a) + (b) \ 168 : (a) + (b) < (b)) 169 #define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \ 170 ((min) < 0 ? INT_SUBTRACT_RANGE_OVERFLOW (a, b, min, max) \ 171 : (a) < 0 ? 1 \ 172 : (b) < 0 ? (a) - (b) <= (a) \ 173 : (a) < (b)) 174 #define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \ 175 (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a)))) \ 176 || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max)) 177 #endif 178 #define _GL_DIVIDE_OVERFLOW(a, b, min, max) \ 179 ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < -(max) \ 180 : (a) < 0 ? (b) <= (a) + (b) - 1 \ 181 : (b) < 0 && (a) + (b) <= (a)) 182 #define _GL_REMAINDER_OVERFLOW(a, b, min, max) \ 183 ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < -(max) \ 184 : (a) < 0 ? (a) % (b) != ((max) - (b) + 1) % (b) \ 185 : (b) < 0 && !_GL_UNSIGNED_NEG_MULTIPLE (a, b, max)) 186 187 /* Return a nonzero value if A is a mathematical multiple of B, where 188 A is unsigned, B is negative, and MAX is the maximum value of A's 189 type. A's type must be the same as (A % B)'s type. Normally (A % 190 -B == 0) suffices, but things get tricky if -B would overflow. */ 191 #define _GL_UNSIGNED_NEG_MULTIPLE(a, b, max) \ 192 (((b) < -_GL_SIGNED_INT_MAXIMUM (b) \ 193 ? (_GL_SIGNED_INT_MAXIMUM (b) == (max) \ 194 ? (a) \ 195 : (a) % (_GL_INT_CONVERT (a, _GL_SIGNED_INT_MAXIMUM (b)) + 1)) \ 196 : (a) % -(b)) \ 197 == 0) 198 199 /* Check for integer overflow, and report low order bits of answer. 200 201 The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators 202 might not yield numerically correct answers due to arithmetic overflow. 203 The INT_<op>_WRAPV macros compute the low-order bits of the sum, 204 difference, and product of two C integers, and return 1 if these 205 low-order bits are not numerically correct. 206 These macros work correctly on all known practical hosts, and do not rely 207 on undefined behavior due to signed arithmetic overflow. 208 209 Example usage, assuming A and B are long int: 210 211 if (INT_MULTIPLY_OVERFLOW (a, b)) 212 printf ("result would overflow\n"); 213 else 214 printf ("result is %ld (no overflow)\n", a * b); 215 216 Example usage with WRAPV flavor: 217 218 long int result; 219 bool overflow = INT_MULTIPLY_WRAPV (a, b, &result); 220 printf ("result is %ld (%s)\n", result, 221 overflow ? "after overflow" : "no overflow"); 222 223 Restrictions on these macros: 224 225 These macros do not check for all possible numerical problems or 226 undefined or unspecified behavior: they do not check for division 227 by zero, for bad shift counts, or for shifting negative numbers. 228 229 These macros may evaluate their arguments zero or multiple times, so the 230 arguments should not have side effects. 231 232 The WRAPV macros are not constant expressions. They support only 233 +, binary -, and *. 234 235 Because the WRAPV macros convert the result, they report overflow 236 in different circumstances than the OVERFLOW macros do. For 237 example, in the typical case with 16-bit 'short' and 32-bit 'int', 238 if A, B and *R are all of type 'short' then INT_ADD_OVERFLOW (A, B) 239 returns false because the addition cannot overflow after A and B 240 are converted to 'int', whereas INT_ADD_WRAPV (A, B, R) returns 241 true or false depending on whether the sum fits into 'short'. 242 243 These macros are tuned for their last input argument being a constant. 244 245 A, B, and *R should be integers; they need not be the same type, 246 and they need not be all signed or all unsigned. 247 However, none of the integer types should be bit-precise, 248 and *R's type should not be char, bool, or an enumeration type. 249 250 Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B, 251 A % B, and A << B would overflow, respectively. */ 252 253 #define INT_ADD_OVERFLOW(a, b) _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW) 254 #define INT_SUBTRACT_OVERFLOW(a, b) _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW) 255 #define INT_NEGATE_OVERFLOW(a) _GL_INT_NEGATE_OVERFLOW (a) 256 #define INT_MULTIPLY_OVERFLOW(a, b) _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW) 257 #define INT_DIVIDE_OVERFLOW(a, b) _GL_BINARY_OP_OVERFLOW (a, b, _GL_DIVIDE_OVERFLOW) 258 #define INT_REMAINDER_OVERFLOW(a, b) _GL_BINARY_OP_OVERFLOW (a, b, _GL_REMAINDER_OVERFLOW) 259 #define INT_LEFT_SHIFT_OVERFLOW(a, b) \ 260 INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a)) 261 262 /* Return 1 if the expression A <op> B would overflow, 263 where OP_RESULT_OVERFLOW (A, B, MIN, MAX) does the actual test, 264 assuming MIN and MAX are the minimum and maximum for the result type. 265 Arguments should be free of side effects. */ 266 #define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \ 267 op_result_overflow (a, b, _GL_INT_MINIMUM (_GL_INT_CONVERT (a, b)), \ 268 _GL_INT_MAXIMUM (_GL_INT_CONVERT (a, b))) 269 270 /* Store the low-order bits of A + B, A - B, A * B, respectively, into *R. 271 Return 1 if the result overflows. See above for restrictions. */ 272 #define INT_ADD_WRAPV(a, b, r) _GL_INT_ADD_WRAPV (a, b, r) 273 #define INT_SUBTRACT_WRAPV(a, b, r) _GL_INT_SUBTRACT_WRAPV (a, b, r) 274 #define INT_MULTIPLY_WRAPV(a, b, r) _GL_INT_MULTIPLY_WRAPV (a, b, r) 275 276 /* The following macros compute A + B, A - B, and A * B, respectively. 277 If no overflow occurs, they set *R to the result and return 1; 278 otherwise, they return 0 and may modify *R. 279 280 Example usage: 281 282 long int result; 283 if (INT_ADD_OK (a, b, &result)) 284 printf ("result is %ld\n", result); 285 else 286 printf ("overflow\n"); 287 288 A, B, and *R should be integers; they need not be the same type, 289 and they need not be all signed or all unsigned. 290 However, none of the integer types should be bit-precise, 291 and *R's type should not be char, bool, or an enumeration type. 292 293 These macros work correctly on all known practical hosts, and do not rely 294 on undefined behavior due to signed arithmetic overflow. 295 296 These macros are not constant expressions. 297 298 These macros may evaluate their arguments zero or multiple times, so the 299 arguments should not have side effects. 300 301 These macros are tuned for B being a constant. */ 302 303 #define INT_ADD_OK(a, b, r) (!INT_ADD_WRAPV (a, b, r)) 304 #define INT_SUBTRACT_OK(a, b, r) (!INT_SUBTRACT_WRAPV (a, b, r)) 305 #define INT_MULTIPLY_OK(a, b, r) (!INT_MULTIPLY_WRAPV (a, b, r)) 306 307 #endif