diff options
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/intrinsics.rs | 13 | ||||
| -rw-r--r-- | src/libcore/num/mod.rs | 20 |
2 files changed, 33 insertions, 0 deletions
diff --git a/src/libcore/intrinsics.rs b/src/libcore/intrinsics.rs index e66a8465370..e927ed40d7f 100644 --- a/src/libcore/intrinsics.rs +++ b/src/libcore/intrinsics.rs @@ -1493,6 +1493,19 @@ extern "rust-intrinsic" { /// [`std::u32::wrapping_mul`](../../std/primitive.u32.html#method.wrapping_mul) pub fn overflowing_mul<T>(a: T, b: T) -> T; + /// Computes `a + b`, while saturating at numeric bounds. + /// The stabilized versions of this intrinsic are available on the integer + /// primitives via the `saturating_add` method. For example, + /// [`std::u32::saturating_add`](../../std/primitive.u32.html#method.saturating_add) + #[cfg(not(stage0))] + pub fn saturating_add<T>(a: T, b: T) -> T; + /// Computes `a - b`, while saturating at numeric bounds. + /// The stabilized versions of this intrinsic are available on the integer + /// primitives via the `saturating_sub` method. For example, + /// [`std::u32::saturating_sub`](../../std/primitive.u32.html#method.saturating_sub) + #[cfg(not(stage0))] + pub fn saturating_sub<T>(a: T, b: T) -> T; + /// Returns the value of the discriminant for the variant in 'v', /// cast to a `u64`; if `T` has no discriminant, returns 0. pub fn discriminant_value<T>(v: &T) -> u64; 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) + } } } |
