Skip to content

Commit b70e7fd

Browse files
committed
Add inherent impls for unchecked math intrinsics
1 parent 42abbd8 commit b70e7fd

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
@@ -697,6 +697,23 @@ $EndFeature, "
697697
}
698698
}
699699

700+
doc_comment! {
701+
concat!("Unchecked integer addition. Computes `self + rhs, assuming overflow
702+
cannot occur. This results in undefined behavior when `self + rhs > ", stringify!($SelfT),
703+
"::max_value()` or `self + rhs < ", stringify!($SelfT), "::min_value()`."),
704+
#[unstable(
705+
feature = "unchecked_math",
706+
reason = "niche optimization path",
707+
issue = "none",
708+
)]
709+
#[must_use = "this returns the result of the operation, \
710+
without modifying the original"]
711+
#[inline]
712+
pub unsafe fn unchecked_add(self, rhs: Self) -> Self {
713+
intrinsics::unchecked_add(self, rhs)
714+
}
715+
}
716+
700717
doc_comment! {
701718
concat!("Checked integer subtraction. Computes `self - rhs`, returning `None` if
702719
overflow occurred.
@@ -722,6 +739,23 @@ $EndFeature, "
722739
}
723740
}
724741

742+
doc_comment! {
743+
concat!("Unchecked integer subtraction. Computes `self - rhs, assuming overflow
744+
cannot occur. This results in undefined behavior when `self - rhs > ", stringify!($SelfT),
745+
"::max_value()` or `self - rhs < ", stringify!($SelfT), "::min_value()`."),
746+
#[unstable(
747+
feature = "unchecked_math",
748+
reason = "niche optimization path",
749+
issue = "none",
750+
)]
751+
#[must_use = "this returns the result of the operation, \
752+
without modifying the original"]
753+
#[inline]
754+
pub unsafe fn unchecked_sub(self, rhs: Self) -> Self {
755+
intrinsics::unchecked_sub(self, rhs)
756+
}
757+
}
758+
725759
doc_comment! {
726760
concat!("Checked integer multiplication. Computes `self * rhs`, returning `None` if
727761
overflow occurred.
@@ -747,6 +781,23 @@ $EndFeature, "
747781
}
748782
}
749783

784+
doc_comment! {
785+
concat!("Unchecked integer multiplication. Computes `self * rhs, assuming overflow
786+
cannot occur. This results in undefined behavior when `self * rhs > ", stringify!($SelfT),
787+
"::max_value()` or `self * rhs < ", stringify!($SelfT), "::min_value()`."),
788+
#[unstable(
789+
feature = "unchecked_math",
790+
reason = "niche optimization path",
791+
issue = "none",
792+
)]
793+
#[must_use = "this returns the result of the operation, \
794+
without modifying the original"]
795+
#[inline]
796+
pub unsafe fn unchecked_mul(self, rhs: Self) -> Self {
797+
intrinsics::unchecked_mul(self, rhs)
798+
}
799+
}
800+
750801
doc_comment! {
751802
concat!("Checked integer division. Computes `self / rhs`, returning `None` if `rhs == 0`
752803
or the division results in overflow.
@@ -2884,6 +2935,23 @@ assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add(3), None);", $EndFeat
28842935
}
28852936
}
28862937

2938+
doc_comment! {
2939+
concat!("Unchecked integer addition. Computes `self + rhs, assuming overflow
2940+
cannot occur. This results in undefined behavior when `self + rhs > ", stringify!($SelfT),
2941+
"::max_value()` or `self + rhs < ", stringify!($SelfT), "::min_value()`."),
2942+
#[unstable(
2943+
feature = "unchecked_math",
2944+
reason = "niche optimization path",
2945+
issue = "none",
2946+
)]
2947+
#[must_use = "this returns the result of the operation, \
2948+
without modifying the original"]
2949+
#[inline]
2950+
pub unsafe fn unchecked_add(self, rhs: Self) -> Self {
2951+
intrinsics::unchecked_add(self, rhs)
2952+
}
2953+
}
2954+
28872955
doc_comment! {
28882956
concat!("Checked integer subtraction. Computes `self - rhs`, returning
28892957
`None` if overflow occurred.
@@ -2907,6 +2975,23 @@ assert_eq!(0", stringify!($SelfT), ".checked_sub(1), None);", $EndFeature, "
29072975
}
29082976
}
29092977

2978+
doc_comment! {
2979+
concat!("Unchecked integer subtraction. Computes `self - rhs, assuming overflow
2980+
cannot occur. This results in undefined behavior when `self - rhs > ", stringify!($SelfT),
2981+
"::max_value()` or `self - rhs < ", stringify!($SelfT), "::min_value()`."),
2982+
#[unstable(
2983+
feature = "unchecked_math",
2984+
reason = "niche optimization path",
2985+
issue = "none",
2986+
)]
2987+
#[must_use = "this returns the result of the operation, \
2988+
without modifying the original"]
2989+
#[inline]
2990+
pub unsafe fn unchecked_sub(self, rhs: Self) -> Self {
2991+
intrinsics::unchecked_sub(self, rhs)
2992+
}
2993+
}
2994+
29102995
doc_comment! {
29112996
concat!("Checked integer multiplication. Computes `self * rhs`, returning
29122997
`None` if overflow occurred.
@@ -2930,6 +3015,23 @@ assert_eq!(", stringify!($SelfT), "::MAX.checked_mul(2), None);", $EndFeature, "
29303015
}
29313016
}
29323017

3018+
doc_comment! {
3019+
concat!("Unchecked integer multiplication. Computes `self * rhs, assuming overflow
3020+
cannot occur. This results in undefined behavior when `self * rhs > ", stringify!($SelfT),
3021+
"::max_value()` or `self * rhs < ", stringify!($SelfT), "::min_value()`."),
3022+
#[unstable(
3023+
feature = "unchecked_math",
3024+
reason = "niche optimization path",
3025+
issue = "none",
3026+
)]
3027+
#[must_use = "this returns the result of the operation, \
3028+
without modifying the original"]
3029+
#[inline]
3030+
pub unsafe fn unchecked_mul(self, rhs: Self) -> Self {
3031+
intrinsics::unchecked_mul(self, rhs)
3032+
}
3033+
}
3034+
29333035
doc_comment! {
29343036
concat!("Checked integer division. Computes `self / rhs`, returning `None`
29353037
if `rhs == 0`.

0 commit comments

Comments
 (0)