/* intprops.h -- properties of integer types | |
Copyright (C) 2001-2023 Free Software Foundation, Inc. | |
This program is free software: you can redistribute it and/or modify it | |
under the terms of the GNU Lesser General Public License as published | |
by the Free Software Foundation; either version 2.1 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU Lesser General Public License for more details. | |
You should have received a copy of the GNU Lesser General Public License | |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ | |
/* The extra casts in the following macros work around compiler bugs, | |
e.g., in Cray C 5.0.3.0. */ | |
/* True if the arithmetic type T is an integer type. bool counts as | |
an integer. */ | |
/* True if the real type T is signed. */ | |
/* Return 1 if the real expression E, after promotion, has a | |
signed or floating type. Do not evaluate E. */ | |
/* Minimum and maximum values for integer types and expressions. */ | |
/* The width in bits of the integer type or expression T. | |
Do not evaluate T. T must not be a bit-field expression. | |
Padding bits are not supported; this is checked at compile-time below. */ | |
/* The maximum and minimum values for the integer type T. */ | |
/* Bound on length of the string representing an unsigned integer | |
value representable in B bits. log10 (2.0) < 146/485. The | |
smallest value of B where this bound is not tight is 2621. */ | |
/* Bound on length of the string representing an integer type or expression T. | |
T must not be a bit-field expression. | |
Subtract 1 for the sign bit if T is signed, and then add 1 more for | |
a minus sign if needed. | |
Because _GL_SIGNED_TYPE_OR_EXPR sometimes returns 1 when its argument is | |
unsigned, this macro may overestimate the true bound by one byte when | |
applied to unsigned types of size 2, 4, 16, ... bytes. */ | |
/* Bound on buffer size needed to represent an integer type or expression T, | |
including the terminating null. T must not be a bit-field expression. */ | |
/* Range overflow checks. | |
The INT_<op>_RANGE_OVERFLOW macros return 1 if the corresponding C | |
operators overflow arithmetically when given the same arguments. | |
These macros do not rely on undefined or implementation-defined behavior. | |
Although their implementations are simple and straightforward, | |
they are harder to use and may be less efficient than the | |
INT_<op>_WRAPV, INT_<op>_OK, and INT_<op>_OVERFLOW macros described below. | |
Example usage: | |
long int i = ...; | |
long int j = ...; | |
if (INT_MULTIPLY_RANGE_OVERFLOW (i, j, LONG_MIN, LONG_MAX)) | |
printf ("multiply would overflow"); | |
else | |
printf ("product is %ld", i * j); | |
Restrictions on *_RANGE_OVERFLOW macros: | |
These macros do not check for all possible numerical problems or | |
undefined or unspecified behavior: they do not check for division | |
by zero, for bad shift counts, or for shifting negative numbers. | |
These macros may evaluate their arguments zero or multiple times, | |
so the arguments should not have side effects. The arithmetic | |
arguments (including the MIN and MAX arguments) must be of the same | |
integer type after the usual arithmetic conversions, and the type | |
must have minimum value MIN and maximum MAX. Unsigned types should | |
use a zero MIN of the proper type. | |
Because all arguments are subject to integer promotions, these | |
macros typically do not work on types narrower than 'int'. | |
These macros are tuned for constant MIN and MAX. For commutative | |
operations such as A + B, they are also tuned for constant B. */ | |
/* Return 1 if A + B would overflow in [MIN,MAX] arithmetic. | |
See above for restrictions. */ | |
/* Return 1 if A - B would overflow in [MIN,MAX] arithmetic. | |
See above for restrictions. */ | |
/* Return 1 if - A would overflow in [MIN,MAX] arithmetic. | |
See above for restrictions. */ | |
/* Return 1 if A * B would overflow in [MIN,MAX] arithmetic. | |
See above for restrictions. Avoid && and || as they tickle | |
bugs in Sun C 5.11 2010/08/13 and other compilers; see | |
<https://lists.gnu.org/r/bug-gnulib/2011-05/msg00401.html>. */ | |
/* Return 1 if A / B would overflow in [MIN,MAX] arithmetic. | |
See above for restrictions. Do not check for division by zero. */ | |
/* Return 1 if A % B would overflow in [MIN,MAX] arithmetic. | |
See above for restrictions. Do not check for division by zero. | |
Mathematically, % should never overflow, but on x86-like hosts | |
INT_MIN % -1 traps, and the C standard permits this, so treat this | |
as an overflow too. */ | |
/* Return 1 if A << B would overflow in [MIN,MAX] arithmetic. | |
See above for restrictions. Here, MIN and MAX are for A only, and B need | |
not be of the same type as the other arguments. The C standard says that | |
behavior is undefined for shifts unless 0 <= B < wordwidth, and that when | |
A is negative then A << B has undefined behavior and A >> B has | |
implementation-defined behavior, but do not check these other | |
restrictions. */ | |
/* The _GL*_OVERFLOW macros have the same restrictions as the | |
*_RANGE_OVERFLOW macros, except that they do not assume that operands | |
(e.g., A and B) have the same type as MIN and MAX. Instead, they assume | |
that the result (e.g., A + B) has that type. */ | |
/* Return a nonzero value if A is a mathematical multiple of B, where | |
A is unsigned, B is negative, and MAX is the maximum value of A's | |
type. A's type must be the same as (A % B)'s type. Normally (A % | |
-B == 0) suffices, but things get tricky if -B would overflow. */ | |
/* Check for integer overflow, and report low order bits of answer. | |
The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators | |
might not yield numerically correct answers due to arithmetic overflow. | |
The INT_<op>_WRAPV macros compute the low-order bits of the sum, | |
difference, and product of two C integers, and return 1 if these | |
low-order bits are not numerically correct. | |
These macros work correctly on all known practical hosts, and do not rely | |
on undefined behavior due to signed arithmetic overflow. | |
Example usage, assuming A and B are long int: | |
if (INT_MULTIPLY_OVERFLOW (a, b)) | |
printf ("result would overflow\n"); | |
else | |
printf ("result is %ld (no overflow)\n", a * b); | |
Example usage with WRAPV flavor: | |
long int result; | |
bool overflow = INT_MULTIPLY_WRAPV (a, b, &result); | |
printf ("result is %ld (%s)\n", result, | |
overflow ? "after overflow" : "no overflow"); | |
Restrictions on these macros: | |
These macros do not check for all possible numerical problems or | |
undefined or unspecified behavior: they do not check for division | |
by zero, for bad shift counts, or for shifting negative numbers. | |
These macros may evaluate their arguments zero or multiple times, so the | |
arguments should not have side effects. | |
The WRAPV macros are not constant expressions. They support only | |
+, binary -, and *. | |
Because the WRAPV macros convert the result, they report overflow | |
in different circumstances than the OVERFLOW macros do. For | |
example, in the typical case with 16-bit 'short' and 32-bit 'int', | |
if A, B and *R are all of type 'short' then INT_ADD_OVERFLOW (A, B) | |
returns false because the addition cannot overflow after A and B | |
are converted to 'int', whereas INT_ADD_WRAPV (A, B, R) returns | |
true or false depending on whether the sum fits into 'short'. | |
These macros are tuned for their last input argument being a constant. | |
A, B, and *R should be integers; they need not be the same type, | |
and they need not be all signed or all unsigned. | |
However, none of the integer types should be bit-precise, | |
and *R's type should not be char, bool, or an enumeration type. | |
Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B, | |
A % B, and A << B would overflow, respectively. */ | |
/* Return 1 if the expression A <op> B would overflow, | |
where OP_RESULT_OVERFLOW (A, B, MIN, MAX) does the actual test, | |
assuming MIN and MAX are the minimum and maximum for the result type. | |
Arguments should be free of side effects. */ | |
/* Store the low-order bits of A + B, A - B, A * B, respectively, into *R. | |
Return 1 if the result overflows. See above for restrictions. */ | |
/* The following macros compute A + B, A - B, and A * B, respectively. | |
If no overflow occurs, they set *R to the result and return 1; | |
otherwise, they return 0 and may modify *R. | |
Example usage: | |
long int result; | |
if (INT_ADD_OK (a, b, &result)) | |
printf ("result is %ld\n", result); | |
else | |
printf ("overflow\n"); | |
A, B, and *R should be integers; they need not be the same type, | |
and they need not be all signed or all unsigned. | |
However, none of the integer types should be bit-precise, | |
and *R's type should not be char, bool, or an enumeration type. | |
These macros work correctly on all known practical hosts, and do not rely | |
on undefined behavior due to signed arithmetic overflow. | |
These macros are not constant expressions. | |
These macros may evaluate their arguments zero or multiple times, so the | |
arguments should not have side effects. | |
These macros are tuned for B being a constant. */ | |