From 4a4186e4d1168f9faf3df019596bcf87f0a4dc2b Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Tue, 29 Jan 2019 22:16:43 +0100 Subject: Use LLVM intrinsics for saturating add/sub --- src/libcore/num/mod.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/libcore/num') diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 7cf2317f4b3..f80f8392827 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -883,11 +883,16 @@ $EndFeature, " #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn saturating_add(self, rhs: Self) -> Self { + #[cfg(stage0)] match self.checked_add(rhs) { Some(x) => x, None if rhs >= 0 => Self::max_value(), None => Self::min_value(), } + #[cfg(not(stage0))] + { + intrinsics::saturating_add(self, rhs) + } } } @@ -908,11 +913,16 @@ $EndFeature, " #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn saturating_sub(self, rhs: Self) -> Self { + #[cfg(stage0)] match self.checked_sub(rhs) { Some(x) => x, None if rhs >= 0 => Self::min_value(), None => Self::max_value(), } + #[cfg(not(stage0))] + { + intrinsics::saturating_sub(self, rhs) + } } } @@ -2744,10 +2754,15 @@ assert_eq!(200u8.saturating_add(127), 255);", $EndFeature, " #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn saturating_add(self, rhs: Self) -> Self { + #[cfg(stage0)] match self.checked_add(rhs) { Some(x) => x, None => Self::max_value(), } + #[cfg(not(stage0))] + { + intrinsics::saturating_add(self, rhs) + } } } @@ -2766,10 +2781,15 @@ assert_eq!(13", stringify!($SelfT), ".saturating_sub(127), 0);", $EndFeature, " #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn saturating_sub(self, rhs: Self) -> Self { + #[cfg(stage0)] match self.checked_sub(rhs) { Some(x) => x, None => Self::min_value(), } + #[cfg(not(stage0))] + { + intrinsics::saturating_sub(self, rhs) + } } } -- cgit 1.4.1-3-g733a5