Menu

- Learn
- Documentation
  - Language Reference
  - Library Reference
  - Command-line Reference
  - Feature Overview
  - Articles
- Downloads
- Packages
- Community
  - Blog
  - Orgs using D
  - Twitter
  - Calendar
  - Forums
  - IRC
  - Community Discord
  - Wiki
  - GitHub
  - Issues
  - Get involved
  - Contributors
  - Foundation
  - Security Team
  - Donate
  - Sponsors
- Resources
  - Tour
  - Books
  - Tutorials
  - Tools
  - Editors
  - IDEs
  - run.dlang.io
  - Visual D
  - Acknowledgments
  - D Style
  - Glossary
  - Sitemap

Search
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 a local clone.

# 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.

References Fast Integer Overflow Checks

License:
Boost License 1.0
Authors:
Walter Bright

Source core/checkedint.d

int `adds`()(int `x`, int `y`, ref bool `overflow`);
long `adds`()(long `x`, long `y`, ref bool `overflow`);
  Add two signed integers, checking for overflow.
  The overflow is sticky, meaning a sequence of operations can be done and overflow need only be checked at the end.
  Parameters:

  int `x` | left operand
  int `y` | right operand
  bool `overflow` | set if an overflow occurs, is not affected otherwise

  Returns:
  the sum
uint `addu`()(uint `x`, uint `y`, ref bool `overflow`);
ulong `addu`()(ulong `x`, ulong `y`, ref bool `overflow`);
  Add two unsigned integers, checking for overflow (aka carry).
  The overflow is sticky, meaning a sequence of operations can be done and overflow need only be checked at the end.
  Parameters:

  uint `x` | left operand
  uint `y` | right operand
  bool `overflow` | set if an overflow occurs, is not affected otherwise

  Returns:
  the sum
int `subs`()(int `x`, int `y`, ref bool `overflow`);
long `subs`()(long `x`, long `y`, ref bool `overflow`);
  Subtract two signed integers, checking for overflow.
  The overflow is sticky, meaning a sequence of operations can be done and overflow need only be checked at the end.
  Parameters:

  int `x` | left operand
  int `y` | right operand
  bool `overflow` | set if an overflow occurs, is not affected otherwise

  Returns:
  the difference
uint `subu`()(uint `x`, uint `y`, ref bool `overflow`);
ulong `subu`()(ulong `x`, ulong `y`, ref bool `overflow`);
  Subtract two unsigned integers, checking for overflow (aka borrow).
  The overflow is sticky, meaning a sequence of operations can be done and overflow need only be checked at the end.
  Parameters:

  uint `x` | left operand
  uint `y` | right operand
  bool `overflow` | set if an overflow occurs, is not affected otherwise

  Returns:
  the difference
int `negs`()(int `x`, ref bool `overflow`);
long `negs`()(long `x`, ref bool `overflow`);
  Negate an integer.
  Parameters:

  int `x` | operand
  bool `overflow` | set if x cannot be negated, is not affected otherwise

  Returns:
  the negation of x
int `muls`()(int `x`, int `y`, ref bool `overflow`);
long `muls`()(long `x`, long `y`, ref bool `overflow`);
  Multiply two signed integers, checking for overflow.
  The overflow is sticky, meaning a sequence of operations can be done and overflow need only be checked at the end.
  Parameters:

  int `x` | left operand
  int `y` | right operand
  bool `overflow` | set if an overflow occurs, is not affected otherwise

  Returns:
  the product
uint `mulu`()(uint `x`, uint `y`, ref bool `overflow`);
ulong `mulu`()(ulong `x`, uint `y`, ref bool `overflow`);
ulong `mulu`()(ulong `x`, ulong `y`, ref bool `overflow`);
  Multiply two unsigned integers, checking for overflow (aka carry).
  The overflow is sticky, meaning a sequence of operations can be done and overflow need only be checked at the end.
  Parameters:

  uint `x` | left operand
  uint `y` | right operand
  bool `overflow` | set if an overflow occurs, is not affected otherwise

  Returns:
  the product

Copyright © 1999-2020 by the D Language Foundation | Page generated by Ddoc on Mon Mar 9 06:16:31 2020