Remaining API additions for int overflow:
`wrapping_div`, `wrapping_rem`, `wrapping_neg`, `wrapping_shl`, `wrapping_shr`. All marked unstable under `core` feature for now (with expectation of being marked as stable by 1.0 release).
This commit is contained in:
parent
a9d8065633
commit
4991c7acad
2 changed files with 155 additions and 0 deletions
|
@ -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.
|
/// Raises self to the power of `exp`, using exponentiation by squaring.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # 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.
|
/// Raises self to the power of `exp`, using exponentiation by squaring.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
|
|
|
@ -48,6 +48,7 @@ pub trait OverflowingOps {
|
||||||
|
|
||||||
fn overflowing_div(self, rhs: Self) -> (Self, bool);
|
fn overflowing_div(self, rhs: Self) -> (Self, bool);
|
||||||
fn overflowing_rem(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_shl(self, rhs: u32) -> (Self, bool);
|
||||||
fn overflowing_shr(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),
|
(self >> (rhs & self::shift_max::$t),
|
||||||
(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),
|
(self >> (rhs & self::shift_max::$t),
|
||||||
(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)
|
(r as usize, f)
|
||||||
}
|
}
|
||||||
#[inline(always)]
|
#[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) {
|
fn overflowing_shl(self, rhs: u32) -> (usize, bool) {
|
||||||
let (r, f) = (self as u64).overflowing_shl(rhs);
|
let (r, f) = (self as u64).overflowing_shl(rhs);
|
||||||
(r as usize, f)
|
(r as usize, f)
|
||||||
|
@ -386,6 +406,11 @@ impl OverflowingOps for usize {
|
||||||
(r as usize, f)
|
(r as usize, f)
|
||||||
}
|
}
|
||||||
#[inline(always)]
|
#[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) {
|
fn overflowing_shl(self, rhs: u32) -> (usize, bool) {
|
||||||
let (r, f) = (self as u32).overflowing_shl(rhs);
|
let (r, f) = (self as u32).overflowing_shl(rhs);
|
||||||
(r as usize, f)
|
(r as usize, f)
|
||||||
|
@ -431,6 +456,11 @@ impl OverflowingOps for isize {
|
||||||
(r as isize, f)
|
(r as isize, f)
|
||||||
}
|
}
|
||||||
#[inline(always)]
|
#[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) {
|
fn overflowing_shl(self, rhs: u32) -> (isize, bool) {
|
||||||
let (r, f) = (self as i64).overflowing_shl(rhs);
|
let (r, f) = (self as i64).overflowing_shl(rhs);
|
||||||
(r as isize, f)
|
(r as isize, f)
|
||||||
|
@ -476,6 +506,11 @@ impl OverflowingOps for isize {
|
||||||
(r as isize, f)
|
(r as isize, f)
|
||||||
}
|
}
|
||||||
#[inline(always)]
|
#[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) {
|
fn overflowing_shl(self, rhs: u32) -> (isize, bool) {
|
||||||
let (r, f) = (self as i32).overflowing_shl(rhs);
|
let (r, f) = (self as i32).overflowing_shl(rhs);
|
||||||
(r as isize, f)
|
(r as isize, f)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue