View source code
Display the source code in core/checkedint.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Module core.checkedint

This module implements integral arithmetic primitives that check for out-of-range results.

Integral arithmetic operators operate on fixed width types. Results that are not representable in those fixed widths are silently truncated to fit. This module offers integral arithmetic primitives that produce the same results, but set an 'overflow' flag when such truncation occurs. The setting is sticky, meaning that numerous operations can be cascaded and then the flag need only be checked at the end. Whether the operation is signed or unsigned is indicated by an 's' or 'u' suffix, respectively. While this could be achieved without such suffixes by using overloading on the signedness of the types, the suffix makes it clear which is happening without needing to examine the types.

While the generic versions of these functions are computationally expensive relative to the cost of the operation itself, compiler implementations are free to recognize them and generate equivalent and faster code.

The functions here are templates so they can be used with -betterC, as betterC does not link with this library.


Fast Integer Overflow Checks


adds(x, y, overflow) Add two signed integers, checking for overflow.
addu(x, y, overflow) Add two unsigned integers, checking for overflow (aka carry).
muls(x, y, overflow) Multiply two signed integers, checking for overflow.
mulu(x, y, overflow) Multiply two unsigned integers, checking for overflow (aka carry).
negs(x, overflow) Negate an integer.
subs(x, y, overflow) Subtract two signed integers, checking for overflow.
subu(x, y, overflow) Subtract two unsigned integers, checking for overflow (aka borrow).


Walter Bright


Boost License 1.0