about summary refs log tree commit diff
path: root/src/libcore/num
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-04-17 00:28:48 +0000
committerbors <bors@rust-lang.org>2015-04-17 00:28:48 +0000
commita52182ffdedaabb3b72a11e8a67a411124ecb9ac (patch)
treec1676995fc0a56d04a42805b17309d74fd2fff87 /src/libcore/num
parent1014ac44f6271a55249b0488a2ce1cc49deed338 (diff)
parent4f678509649a59daa17dc968b2aeb1023fb23c0f (diff)
downloadrust-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')
-rw-r--r--src/libcore/num/mod.rs120
-rw-r--r--src/libcore/num/wrapping.rs37
2 files changed, 156 insertions, 1 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
diff --git a/src/libcore/num/wrapping.rs b/src/libcore/num/wrapping.rs
index 28276d0bf01..aa84708816b 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);
@@ -231,7 +232,7 @@ macro_rules! signed_overflowing_impl {
             #[inline(always)]
             fn overflowing_div(self, rhs: $t) -> ($t, bool) {
                 if self == $t::MIN && rhs == -1 {
-                    (1, true)
+                    (self, true)
                 } else {
                     (self/rhs, false)
                 }
@@ -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)