Skip to content

Commit

Permalink
Add inherent impls for unchecked math intrinsics
Browse files Browse the repository at this point in the history
  • Loading branch information
CAD97 committed Feb 3, 2020
1 parent bdd946d commit 39e3eed
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,23 @@ $EndFeature, "
}
}

doc_comment! {
concat!("Unchecked integer addition. Computes `self + rhs, assuming overflow
cannot occur. This results in undefined behavior when `self + rhs > ", stringify!($SelfT),
"::max_value()` or `self + rhs < ", stringify!($SelfT), "::min_value()`."),
#[unstable(
feature = "unchecked_math",
reason = "niche optimization path",
issue = "none",
)]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub unsafe fn unchecked_add(self, rhs: Self) -> Self {
intrinsics::unchecked_add(self, rhs)
}
}

doc_comment! {
concat!("Checked integer subtraction. Computes `self - rhs`, returning `None` if
overflow occurred.
Expand All @@ -734,6 +751,23 @@ $EndFeature, "
}
}

doc_comment! {
concat!("Unchecked integer subtraction. Computes `self - rhs, assuming overflow
cannot occur. This results in undefined behavior when `self - rhs > ", stringify!($SelfT),
"::max_value()` or `self - rhs < ", stringify!($SelfT), "::min_value()`."),
#[unstable(
feature = "unchecked_math",
reason = "niche optimization path",
issue = "none",
)]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub unsafe fn unchecked_sub(self, rhs: Self) -> Self {
intrinsics::unchecked_sub(self, rhs)
}
}

doc_comment! {
concat!("Checked integer multiplication. Computes `self * rhs`, returning `None` if
overflow occurred.
Expand All @@ -758,6 +792,23 @@ $EndFeature, "
}
}

doc_comment! {
concat!("Unchecked integer multiplication. Computes `self * rhs, assuming overflow
cannot occur. This results in undefined behavior when `self * rhs > ", stringify!($SelfT),
"::max_value()` or `self * rhs < ", stringify!($SelfT), "::min_value()`."),
#[unstable(
feature = "unchecked_math",
reason = "niche optimization path",
issue = "none",
)]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub unsafe fn unchecked_mul(self, rhs: Self) -> Self {
intrinsics::unchecked_mul(self, rhs)
}
}

doc_comment! {
concat!("Checked integer division. Computes `self / rhs`, returning `None` if `rhs == 0`
or the division results in overflow.
Expand Down Expand Up @@ -2856,6 +2907,23 @@ assert_eq!((", stringify!($SelfT), "::max_value() - 2).checked_add(3), None);",
}
}

doc_comment! {
concat!("Unchecked integer addition. Computes `self + rhs, assuming overflow
cannot occur. This results in undefined behavior when `self + rhs > ", stringify!($SelfT),
"::max_value()` or `self + rhs < ", stringify!($SelfT), "::min_value()`."),
#[unstable(
feature = "unchecked_math",
reason = "niche optimization path",
issue = "none",
)]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub unsafe fn unchecked_add(self, rhs: Self) -> Self {
intrinsics::unchecked_add(self, rhs)
}
}

doc_comment! {
concat!("Checked integer subtraction. Computes `self - rhs`, returning
`None` if overflow occurred.
Expand All @@ -2878,6 +2946,23 @@ assert_eq!(0", stringify!($SelfT), ".checked_sub(1), None);", $EndFeature, "
}
}

doc_comment! {
concat!("Unchecked integer subtraction. Computes `self - rhs, assuming overflow
cannot occur. This results in undefined behavior when `self - rhs > ", stringify!($SelfT),
"::max_value()` or `self - rhs < ", stringify!($SelfT), "::min_value()`."),
#[unstable(
feature = "unchecked_math",
reason = "niche optimization path",
issue = "none",
)]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub unsafe fn unchecked_sub(self, rhs: Self) -> Self {
intrinsics::unchecked_sub(self, rhs)
}
}

doc_comment! {
concat!("Checked integer multiplication. Computes `self * rhs`, returning
`None` if overflow occurred.
Expand All @@ -2900,6 +2985,23 @@ assert_eq!(", stringify!($SelfT), "::max_value().checked_mul(2), None);", $EndFe
}
}

doc_comment! {
concat!("Unchecked integer multiplication. Computes `self * rhs, assuming overflow
cannot occur. This results in undefined behavior when `self * rhs > ", stringify!($SelfT),
"::max_value()` or `self * rhs < ", stringify!($SelfT), "::min_value()`."),
#[unstable(
feature = "unchecked_math",
reason = "niche optimization path",
issue = "none",
)]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub unsafe fn unchecked_mul(self, rhs: Self) -> Self {
intrinsics::unchecked_mul(self, rhs)
}
}

doc_comment! {
concat!("Checked integer division. Computes `self / rhs`, returning `None`
if `rhs == 0`.
Expand Down

0 comments on commit 39e3eed

Please sign in to comment.