diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 3fd179cf86f..92b40f8a98f 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -1219,6 +1219,66 @@ macro_rules! int_impl { } } + /// Wrapping (modular) division. Computes `floor(self / other)`, + /// wrapping around at the boundary of the type. + /// + /// The only case where such wrapping can occur is when one + /// divides `MIN / -1` on a signed type (where `MIN` is the + /// negative minimal value for the type); this is equivalent + /// to `-MIN`, a positive value that is too large to represent + /// in the type. In such a case, this function returns `MIN` + /// itself.. + #[unstable(feature = "core", since = "1.0.0")] + #[inline(always)] + pub fn wrapping_div(self, rhs: $T) -> $T { + self.overflowing_div(rhs).0 + } + + /// Wrapping (modular) remainder. Computes `self % other`, + /// wrapping around at the boundary of the type. + /// + /// Such wrap-around never actually occurs mathematically; + /// implementation artifacts make `x % y` illegal for `MIN / + /// -1` on a signed type illegal (where `MIN` is the negative + /// minimal value). In such a case, this function returns `0`. + #[unstable(feature = "core", since = "1.0.0")] + #[inline(always)] + pub fn wrapping_rem(self, rhs: $T) -> $T { + self.overflowing_rem(rhs).0 + } + + /// Wrapping (modular) negation. Computes `-self`, + /// wrapping around at the boundary of the type. + /// + /// The only case where such wrapping can occur is when one + /// negates `MIN` on a signed type (where `MIN` is the + /// negative minimal value for the type); this is a positive + /// value that is too large to represent in the type. In such + /// a case, this function returns `MIN` itself. + #[unstable(feature = "core", since = "1.0.0")] + #[inline(always)] + pub fn wrapping_neg(self) -> $T { + self.overflowing_neg().0 + } + + /// Panic-free bitwise shift-left; yields `self << mask(rhs)`, + /// where `mask` removes any high-order bits of `rhs` that + /// would cause the shift to exceed the bitwidth of the type. + #[unstable(feature = "core", since = "1.0.0")] + #[inline(always)] + pub fn wrapping_shl(self, rhs: u32) -> $T { + self.overflowing_shl(rhs).0 + } + + /// Panic-free bitwise shift-left; yields `self >> mask(rhs)`, + /// where `mask` removes any high-order bits of `rhs` that + /// would cause the shift to exceed the bitwidth of the type. + #[unstable(feature = "core", since = "1.0.0")] + #[inline(always)] + pub fn wrapping_shr(self, rhs: u32) -> $T { + self.overflowing_shr(rhs).0 + } + /// Raises self to the power of `exp`, using exponentiation by squaring. /// /// # Examples @@ -1739,6 +1799,66 @@ macro_rules! uint_impl { } } + /// Wrapping (modular) division. Computes `floor(self / other)`, + /// wrapping around at the boundary of the type. + /// + /// The only case where such wrapping can occur is when one + /// divides `MIN / -1` on a signed type (where `MIN` is the + /// negative minimal value for the type); this is equivalent + /// to `-MIN`, a positive value that is too large to represent + /// in the type. In such a case, this function returns `MIN` + /// itself.. + #[unstable(feature = "core", since = "1.0.0")] + #[inline(always)] + pub fn wrapping_div(self, rhs: $T) -> $T { + self.overflowing_div(rhs).0 + } + + /// Wrapping (modular) remainder. Computes `self % other`, + /// wrapping around at the boundary of the type. + /// + /// Such wrap-around never actually occurs mathematically; + /// implementation artifacts make `x % y` illegal for `MIN / + /// -1` on a signed type illegal (where `MIN` is the negative + /// minimal value). In such a case, this function returns `0`. + #[unstable(feature = "core", since = "1.0.0")] + #[inline(always)] + pub fn wrapping_rem(self, rhs: $T) -> $T { + self.overflowing_rem(rhs).0 + } + + /// Wrapping (modular) negation. Computes `-self`, + /// wrapping around at the boundary of the type. + /// + /// The only case where such wrapping can occur is when one + /// negates `MIN` on a signed type (where `MIN` is the + /// negative minimal value for the type); this is a positive + /// value that is too large to represent in the type. In such + /// a case, this function returns `MIN` itself. + #[unstable(feature = "core", since = "1.0.0")] + #[inline(always)] + pub fn wrapping_neg(self) -> $T { + self.overflowing_neg().0 + } + + /// Panic-free bitwise shift-left; yields `self << mask(rhs)`, + /// where `mask` removes any high-order bits of `rhs` that + /// would cause the shift to exceed the bitwidth of the type. + #[unstable(feature = "core", since = "1.0.0")] + #[inline(always)] + pub fn wrapping_shl(self, rhs: u32) -> $T { + self.overflowing_shl(rhs).0 + } + + /// Panic-free bitwise shift-left; yields `self >> mask(rhs)`, + /// where `mask` removes any high-order bits of `rhs` that + /// would cause the shift to exceed the bitwidth of the type. + #[unstable(feature = "core", since = "1.0.0")] + #[inline(always)] + pub fn wrapping_shr(self, rhs: u32) -> $T { + self.overflowing_shr(rhs).0 + } + /// Raises self to the power of `exp`, using exponentiation by squaring. /// /// # Examples diff --git a/src/libcore/num/wrapping.rs b/src/libcore/num/wrapping.rs index 28276d0bf01..3ce2851f8e2 100644 --- a/src/libcore/num/wrapping.rs +++ b/src/libcore/num/wrapping.rs @@ -48,6 +48,7 @@ pub trait OverflowingOps { fn overflowing_div(self, rhs: Self) -> (Self, bool); fn overflowing_rem(self, rhs: Self) -> (Self, bool); + fn overflowing_neg(self) -> (Self, bool); fn overflowing_shl(self, rhs: u32) -> (Self, bool); fn overflowing_shr(self, rhs: u32) -> (Self, bool); @@ -255,6 +256,15 @@ macro_rules! signed_overflowing_impl { (self >> (rhs & self::shift_max::$t), (rhs > self::shift_max::$t)) } + + #[inline(always)] + fn overflowing_neg(self) -> ($t, bool) { + if self == $t::MIN { + ($t::MIN, true) + } else { + (-self, false) + } + } } )*) } @@ -300,6 +310,11 @@ macro_rules! unsigned_overflowing_impl { (self >> (rhs & self::shift_max::$t), (rhs > self::shift_max::$t)) } + + #[inline(always)] + fn overflowing_neg(self) -> ($t, bool) { + ((!self).wrapping_add(1), true) + } } )*) } @@ -341,6 +356,11 @@ impl OverflowingOps for usize { (r as usize, f) } #[inline(always)] + fn overflowing_neg(self) -> (usize, bool) { + let (r, f) = (self as u64).overflowing_neg(); + (r as usize, f) + } + #[inline(always)] fn overflowing_shl(self, rhs: u32) -> (usize, bool) { let (r, f) = (self as u64).overflowing_shl(rhs); (r as usize, f) @@ -386,6 +406,11 @@ impl OverflowingOps for usize { (r as usize, f) } #[inline(always)] + fn overflowing_neg(self) -> (usize, bool) { + let (r, f) = (self as u32).overflowing_neg(); + (r as usize, f) + } + #[inline(always)] fn overflowing_shl(self, rhs: u32) -> (usize, bool) { let (r, f) = (self as u32).overflowing_shl(rhs); (r as usize, f) @@ -431,6 +456,11 @@ impl OverflowingOps for isize { (r as isize, f) } #[inline(always)] + fn overflowing_neg(self) -> (isize, bool) { + let (r, f) = (self as i64).overflowing_neg(); + (r as isize, f) + } + #[inline(always)] fn overflowing_shl(self, rhs: u32) -> (isize, bool) { let (r, f) = (self as i64).overflowing_shl(rhs); (r as isize, f) @@ -476,6 +506,11 @@ impl OverflowingOps for isize { (r as isize, f) } #[inline(always)] + fn overflowing_neg(self) -> (isize, bool) { + let (r, f) = (self as i32).overflowing_neg(); + (r as isize, f) + } + #[inline(always)] fn overflowing_shl(self, rhs: u32) -> (isize, bool) { let (r, f) = (self as i32).overflowing_shl(rhs); (r as isize, f)