Skip to content

Fill in missing parts of Integer overflow API #24420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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