Skip to content

Commit 39e3eed

Browse files
committed
Add inherent impls for unchecked math intrinsics
1 parent bdd946d commit 39e3eed

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

src/libcore/num/mod.rs

+102
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,23 @@ $EndFeature, "
710710
}
711711
}
712712

713+
doc_comment! {
714+
concat!("Unchecked integer addition. Computes `self + rhs, assuming overflow
715+
cannot occur. This results in undefined behavior when `self + rhs > ", stringify!($SelfT),
716+
"::max_value()` or `self + rhs < ", stringify!($SelfT), "::min_value()`."),
717+
#[unstable(
718+
feature = "unchecked_math",
719+
reason = "niche optimization path",
720+
issue = "none",
721+
)]
722+
#[must_use = "this returns the result of the operation, \
723+
without modifying the original"]
724+
#[inline]
725+
pub unsafe fn unchecked_add(self, rhs: Self) -> Self {
726+
intrinsics::unchecked_add(self, rhs)
727+
}
728+
}
729+
713730
doc_comment! {
714731
concat!("Checked integer subtraction. Computes `self - rhs`, returning `None` if
715732
overflow occurred.
@@ -734,6 +751,23 @@ $EndFeature, "
734751
}
735752
}
736753

754+
doc_comment! {
755+
concat!("Unchecked integer subtraction. Computes `self - rhs, assuming overflow
756+
cannot occur. This results in undefined behavior when `self - rhs > ", stringify!($SelfT),
757+
"::max_value()` or `self - rhs < ", stringify!($SelfT), "::min_value()`."),
758+
#[unstable(
759+
feature = "unchecked_math",
760+
reason = "niche optimization path",
761+
issue = "none",
762+
)]
763+
#[must_use = "this returns the result of the operation, \
764+
without modifying the original"]
765+
#[inline]
766+
pub unsafe fn unchecked_sub(self, rhs: Self) -> Self {
767+
intrinsics::unchecked_sub(self, rhs)
768+
}
769+
}
770+
737771
doc_comment! {
738772
concat!("Checked integer multiplication. Computes `self * rhs`, returning `None` if
739773
overflow occurred.
@@ -758,6 +792,23 @@ $EndFeature, "
758792
}
759793
}
760794

795+
doc_comment! {
796+
concat!("Unchecked integer multiplication. Computes `self * rhs, assuming overflow
797+
cannot occur. This results in undefined behavior when `self * rhs > ", stringify!($SelfT),
798+
"::max_value()` or `self * rhs < ", stringify!($SelfT), "::min_value()`."),
799+
#[unstable(
800+
feature = "unchecked_math",
801+
reason = "niche optimization path",
802+
issue = "none",
803+
)]
804+
#[must_use = "this returns the result of the operation, \
805+
without modifying the original"]
806+
#[inline]
807+
pub unsafe fn unchecked_mul(self, rhs: Self) -> Self {
808+
intrinsics::unchecked_mul(self, rhs)
809+
}
810+
}
811+
761812
doc_comment! {
762813
concat!("Checked integer division. Computes `self / rhs`, returning `None` if `rhs == 0`
763814
or the division results in overflow.
@@ -2856,6 +2907,23 @@ assert_eq!((", stringify!($SelfT), "::max_value() - 2).checked_add(3), None);",
28562907
}
28572908
}
28582909

2910+
doc_comment! {
2911+
concat!("Unchecked integer addition. Computes `self + rhs, assuming overflow
2912+
cannot occur. This results in undefined behavior when `self + rhs > ", stringify!($SelfT),
2913+
"::max_value()` or `self + rhs < ", stringify!($SelfT), "::min_value()`."),
2914+
#[unstable(
2915+
feature = "unchecked_math",
2916+
reason = "niche optimization path",
2917+
issue = "none",
2918+
)]
2919+
#[must_use = "this returns the result of the operation, \
2920+
without modifying the original"]
2921+
#[inline]
2922+
pub unsafe fn unchecked_add(self, rhs: Self) -> Self {
2923+
intrinsics::unchecked_add(self, rhs)
2924+
}
2925+
}
2926+
28592927
doc_comment! {
28602928
concat!("Checked integer subtraction. Computes `self - rhs`, returning
28612929
`None` if overflow occurred.
@@ -2878,6 +2946,23 @@ assert_eq!(0", stringify!($SelfT), ".checked_sub(1), None);", $EndFeature, "
28782946
}
28792947
}
28802948

2949+
doc_comment! {
2950+
concat!("Unchecked integer subtraction. Computes `self - rhs, assuming overflow
2951+
cannot occur. This results in undefined behavior when `self - rhs > ", stringify!($SelfT),
2952+
"::max_value()` or `self - rhs < ", stringify!($SelfT), "::min_value()`."),
2953+
#[unstable(
2954+
feature = "unchecked_math",
2955+
reason = "niche optimization path",
2956+
issue = "none",
2957+
)]
2958+
#[must_use = "this returns the result of the operation, \
2959+
without modifying the original"]
2960+
#[inline]
2961+
pub unsafe fn unchecked_sub(self, rhs: Self) -> Self {
2962+
intrinsics::unchecked_sub(self, rhs)
2963+
}
2964+
}
2965+
28812966
doc_comment! {
28822967
concat!("Checked integer multiplication. Computes `self * rhs`, returning
28832968
`None` if overflow occurred.
@@ -2900,6 +2985,23 @@ assert_eq!(", stringify!($SelfT), "::max_value().checked_mul(2), None);", $EndFe
29002985
}
29012986
}
29022987

2988+
doc_comment! {
2989+
concat!("Unchecked integer multiplication. Computes `self * rhs, assuming overflow
2990+
cannot occur. This results in undefined behavior when `self * rhs > ", stringify!($SelfT),
2991+
"::max_value()` or `self * rhs < ", stringify!($SelfT), "::min_value()`."),
2992+
#[unstable(
2993+
feature = "unchecked_math",
2994+
reason = "niche optimization path",
2995+
issue = "none",
2996+
)]
2997+
#[must_use = "this returns the result of the operation, \
2998+
without modifying the original"]
2999+
#[inline]
3000+
pub unsafe fn unchecked_mul(self, rhs: Self) -> Self {
3001+
intrinsics::unchecked_mul(self, rhs)
3002+
}
3003+
}
3004+
29033005
doc_comment! {
29043006
concat!("Checked integer division. Computes `self / rhs`, returning `None`
29053007
if `rhs == 0`.

0 commit comments

Comments
 (0)