Next: Integer Type Overflow, Previous: Integer Bounds, Up: Integer Properties [Contents][Index]
Signed integer arithmetic has undefined behavior on overflow in C.
Although almost all modern computers use two’s complement signed
arithmetic that is well-defined to wrap around, C compilers routinely
optimize assuming that signed integer overflow cannot occur, which
means that a C program cannot easily get at the underlying machine
arithmetic. For example, on a typical machine with 32-bit two’s
complement int
the expression INT_MAX + 1
does not
necessarily yield INT_MIN
, because the compiler may do
calculations with a 64-bit register, or may generate code that
traps on signed integer overflow.
The following macros work around this problem by storing the
wraparound value, i.e., the low-order bits of the correct answer, and
by returning an overflow indication. For example, if i
is of
type int
, INT_ADD_WRAPV (INT_MAX, 1, &i)
sets i
to INT_MIN
and returns 1 on a two’s complement machine. On
newer platforms, these macros are typically more efficient than the
overflow-checking macros. See Integer Type Overflow.
Example usage:
#include <intprops.h> #include <stdio.h> /* Print the low order bits of A * B, reporting whether overflow occurred. */ void print_product (long int a, long int b) { long int r; int overflow = INT_MULTIPLY_WRAPV (a, b, &r); printf ("result is %ld (%s)\n", r, (overflow ? "after overflow" : "no overflow")); }
These macros have the following restrictions:
INT_ADD_WRAPV (a, b, r)
Store the low-order bits of the sum of a and b into
*r
. Return true if overflow occurred, false if the
low-order bits are the mathematically-correct sum. See above for
restrictions.
INT_SUBTRACT_WRAPV (a, b, r)
Store the low-order bits of the difference between a and b
into *r
. Return true if overflow occurred, false if the
low-order bits are the mathematically-correct difference. See above
for restrictions.
INT_MULTIPLY_WRAPV (a, b, r)
Store the low-order bits of the product of a and b into
*r
. Return true if overflow occurred, false if the
low-order bits are the mathematically-correct product. See above for
restrictions.
Next: Integer Type Overflow, Previous: Integer Bounds, Up: Integer Properties [Contents][Index]