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:

- Their first two arguments must be integer expressions.
- Their last argument must be a non-null pointer to a signed integer. To calculate a wraparound unsigned integer you can use ordinary C arithmetic; to tell whether it overflowed, you can use the overflow-checking macros.
- They may evaluate their arguments zero or multiple times, so the arguments should not have side effects.
- They are not necessarily constant expressions, even if all their arguments are constant expressions.

`INT_ADD_WRAPV (`

`a`,`b`,`r`)-
Store the low-order bits of the sum of

`a`and`b`into`*`

. Return true if overflow occurred, false if the low-order bits are the mathematically-correct sum. See above for restrictions.`r` `INT_SUBTRACT_WRAPV (`

`a`,`b`,`r`)-
Store the low-order bits of the difference between

`a`and`b`into`*`

. Return true if overflow occurred, false if the low-order bits are the mathematically-correct difference. See above for restrictions.`r` `INT_MULTIPLY_WRAPV (`

`a`,`b`,`r`)-
Store the low-order bits of the product of

`a`and`b`into`*`

. Return true if overflow occurred, false if the low-order bits are the mathematically-correct product. See above for restrictions.`r`

Next: Integer Type Overflow, Previous: Integer Bounds, Up: Integer Properties [Contents][Index]