diff options
| author | bors <bors@rust-lang.org> | 2015-04-17 00:28:48 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-04-17 00:28:48 +0000 |
| commit | a52182ffdedaabb3b72a11e8a67a411124ecb9ac (patch) | |
| tree | c1676995fc0a56d04a42805b17309d74fd2fff87 /src/libcore/num/mod.rs | |
| parent | 1014ac44f6271a55249b0488a2ce1cc49deed338 (diff) | |
| parent | 4f678509649a59daa17dc968b2aeb1023fb23c0f (diff) | |
| download | rust-a52182ffdedaabb3b72a11e8a67a411124ecb9ac.tar.gz rust-a52182ffdedaabb3b72a11e8a67a411124ecb9ac.zip | |
Auto merge of #24420 - pnkfelix:oflo-api, r=alexcrichton
Fill in missing parts of Integer overflow API See todo list at #22020
Diffstat (limited to 'src/libcore/num/mod.rs')
| -rw-r--r-- | src/libcore/num/mod.rs | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 9b1a384a0d0..c7714afc4fa 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 |
