Skip to content

Commit

Permalink
Auto merge of #24420 - pnkfelix:oflo-api, r=alexcrichton
Browse files Browse the repository at this point in the history
Fill in missing parts of Integer overflow API 

See todo list at #22020
  • Loading branch information
bors committed Apr 17, 2015
2 parents 1014ac4 + 4f67850 commit a52182f
Show file tree
Hide file tree
Showing 3 changed files with 392 additions and 1 deletion.
120 changes: 120 additions & 0 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
37 changes: 36 additions & 1 deletion src/libcore/num/wrapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
}
}
)*)
}
Expand Down Expand Up @@ -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)
}
}
)*)
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit a52182f

Please sign in to comment.