#![stable(feature = "rust1", since = "1.0.0")]
use fmt;
use intrinsics;
use mem::size_of;
#[stable(feature = "rust1", since = "1.0.0")]
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Default, Hash)]
pub struct Wrapping<T>(#[stable(feature = "rust1", since = "1.0.0")]
pub T);
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: fmt::Debug> fmt::Debug for Wrapping<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
#[stable(feature = "wrapping_display", since = "1.10.0")]
impl<T: fmt::Display> fmt::Display for Wrapping<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
#[stable(feature = "wrapping_fmt", since = "1.11.0")]
impl<T: fmt::Binary> fmt::Binary for Wrapping<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
#[stable(feature = "wrapping_fmt", since = "1.11.0")]
impl<T: fmt::Octal> fmt::Octal for Wrapping<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
#[stable(feature = "wrapping_fmt", since = "1.11.0")]
impl<T: fmt::LowerHex> fmt::LowerHex for Wrapping<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
#[stable(feature = "wrapping_fmt", since = "1.11.0")]
impl<T: fmt::UpperHex> fmt::UpperHex for Wrapping<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
mod wrapping;
macro_rules! checked_op {
($U:ty, $op:path, $x:expr, $y:expr) => {{
let (result, overflowed) = unsafe { $op($x as $U, $y as $U) };
if overflowed { None } else { Some(result as Self) }
}}
}
macro_rules! int_impl {
($SelfT:ty, $ActualT:ident, $UnsignedT:ty, $BITS:expr,
$add_with_overflow:path,
$sub_with_overflow:path,
$mul_with_overflow:path) => {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub const fn min_value() -> Self {
!0 ^ ((!0 as $UnsignedT) >> 1) as Self
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub const fn max_value() -> Self {
!Self::min_value()
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn count_ones(self) -> u32 { (self as $UnsignedT).count_ones() }
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn count_zeros(self) -> u32 {
(!self).count_ones()
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn leading_zeros(self) -> u32 {
(self as $UnsignedT).leading_zeros()
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn trailing_zeros(self) -> u32 {
(self as $UnsignedT).trailing_zeros()
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn rotate_left(self, n: u32) -> Self {
(self as $UnsignedT).rotate_left(n) as Self
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn rotate_right(self, n: u32) -> Self {
(self as $UnsignedT).rotate_right(n) as Self
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn swap_bytes(self) -> Self {
(self as $UnsignedT).swap_bytes() as Self
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn from_be(x: Self) -> Self {
if cfg!(target_endian = "big") { x } else { x.swap_bytes() }
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn from_le(x: Self) -> Self {
if cfg!(target_endian = "little") { x } else { x.swap_bytes() }
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn to_be(self) -> Self { if cfg!(target_endian = "big") { self } else { self.swap_bytes() }
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn to_le(self) -> Self {
if cfg!(target_endian = "little") { self } else { self.swap_bytes() }
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn checked_add(self, other: Self) -> Option<Self> {
let (a, b) = self.overflowing_add(other);
if b {None} else {Some(a)}
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn checked_sub(self, other: Self) -> Option<Self> {
let (a, b) = self.overflowing_sub(other);
if b {None} else {Some(a)}
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn checked_mul(self, other: Self) -> Option<Self> {
let (a, b) = self.overflowing_mul(other);
if b {None} else {Some(a)}
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn checked_div(self, other: Self) -> Option<Self> {
if other == 0 || (self == Self::min_value() && other == -1) {
None
} else {
Some(unsafe { intrinsics::unchecked_div(self, other) })
}
}
#[stable(feature = "wrapping", since = "1.7.0")]
#[inline]
pub fn checked_rem(self, other: Self) -> Option<Self> {
if other == 0 || (self == Self::min_value() && other == -1) {
None
} else {
Some(unsafe { intrinsics::unchecked_rem(self, other) })
}
}
#[stable(feature = "wrapping", since = "1.7.0")]
#[inline]
pub fn checked_neg(self) -> Option<Self> {
let (a, b) = self.overflowing_neg();
if b {None} else {Some(a)}
}
#[stable(feature = "wrapping", since = "1.7.0")]
#[inline]
pub fn checked_shl(self, rhs: u32) -> Option<Self> {
let (a, b) = self.overflowing_shl(rhs);
if b {None} else {Some(a)}
}
#[stable(feature = "wrapping", since = "1.7.0")]
#[inline]
pub fn checked_shr(self, rhs: u32) -> Option<Self> {
let (a, b) = self.overflowing_shr(rhs);
if b {None} else {Some(a)}
}
#[stable(feature = "no_panic_abs", since = "1.13.0")]
#[inline]
pub fn checked_abs(self) -> Option<Self> {
if self.is_negative() {
self.checked_neg()
} else {
Some(self)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn saturating_add(self, other: Self) -> Self {
match self.checked_add(other) {
Some(x) => x,
None if other >= 0 => Self::max_value(),
None => Self::min_value(),
}
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn saturating_sub(self, other: Self) -> Self {
match self.checked_sub(other) {
Some(x) => x,
None if other >= 0 => Self::min_value(),
None => Self::max_value(),
}
}
#[stable(feature = "wrapping", since = "1.7.0")]
#[inline]
pub fn saturating_mul(self, other: Self) -> Self {
self.checked_mul(other).unwrap_or_else(|| {
if (self < 0 && other < 0) || (self > 0 && other > 0) {
Self::max_value()
} else {
Self::min_value()
}
})
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn wrapping_add(self, rhs: Self) -> Self {
unsafe {
intrinsics::overflowing_add(self, rhs)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn wrapping_sub(self, rhs: Self) -> Self {
unsafe {
intrinsics::overflowing_sub(self, rhs)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn wrapping_mul(self, rhs: Self) -> Self {
unsafe {
intrinsics::overflowing_mul(self, rhs)
}
}
#[stable(feature = "num_wrapping", since = "1.2.0")]
#[inline(always)]
pub fn wrapping_div(self, rhs: Self) -> Self {
self.overflowing_div(rhs).0
}
#[stable(feature = "num_wrapping", since = "1.2.0")]
#[inline(always)]
pub fn wrapping_rem(self, rhs: Self) -> Self {
self.overflowing_rem(rhs).0
}
#[stable(feature = "num_wrapping", since = "1.2.0")]
#[inline(always)]
pub fn wrapping_neg(self) -> Self {
self.overflowing_neg().0
}
#[stable(feature = "num_wrapping", since = "1.2.0")]
#[inline(always)]
#[cfg(not(stage0))]
pub fn wrapping_shl(self, rhs: u32) -> Self {
unsafe {
intrinsics::unchecked_shl(self, (rhs & ($BITS - 1)) as $SelfT)
}
}
#[stable(feature = "num_wrapping", since = "1.2.0")]
#[inline(always)]
#[cfg(stage0)]
pub fn wrapping_shl(self, rhs: u32) -> Self {
self.overflowing_shl(rhs).0
}
#[stable(feature = "num_wrapping", since = "1.2.0")]
#[inline(always)]
#[cfg(not(stage0))]
pub fn wrapping_shr(self, rhs: u32) -> Self {
unsafe {
intrinsics::unchecked_shr(self, (rhs & ($BITS - 1)) as $SelfT)
}
}
#[stable(feature = "num_wrapping", since = "1.2.0")]
#[inline(always)]
#[cfg(stage0)]
pub fn wrapping_shr(self, rhs: u32) -> Self {
self.overflowing_shr(rhs).0
}
#[stable(feature = "no_panic_abs", since = "1.13.0")]
#[inline(always)]
pub fn wrapping_abs(self) -> Self {
if self.is_negative() {
self.wrapping_neg()
} else {
self
}
}
#[inline]
#[stable(feature = "wrapping", since = "1.7.0")]
pub fn overflowing_add(self, rhs: Self) -> (Self, bool) {
unsafe {
let (a, b) = $add_with_overflow(self as $ActualT,
rhs as $ActualT);
(a as Self, b)
}
}
#[inline]
#[stable(feature = "wrapping", since = "1.7.0")]
pub fn overflowing_sub(self, rhs: Self) -> (Self, bool) {
unsafe {
let (a, b) = $sub_with_overflow(self as $ActualT,
rhs as $ActualT);
(a as Self, b)
}
}
#[inline]
#[stable(feature = "wrapping", since = "1.7.0")]
pub fn overflowing_mul(self, rhs: Self) -> (Self, bool) {
unsafe {
let (a, b) = $mul_with_overflow(self as $ActualT,
rhs as $ActualT);
(a as Self, b)
}
}
#[inline]
#[stable(feature = "wrapping", since = "1.7.0")]
pub fn overflowing_div(self, rhs: Self) -> (Self, bool) {
if self == Self::min_value() && rhs == -1 {
(self, true)
} else {
(self / rhs, false)
}
}
#[inline]
#[stable(feature = "wrapping", since = "1.7.0")]
pub fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
if self == Self::min_value() && rhs == -1 {
(0, true)
} else {
(self % rhs, false)
}
}
#[inline]
#[stable(feature = "wrapping", since = "1.7.0")]
pub fn overflowing_neg(self) -> (Self, bool) {
if self == Self::min_value() {
(Self::min_value(), true)
} else {
(-self, false)
}
}
#[inline]
#[stable(feature = "wrapping", since = "1.7.0")]
#[cfg(not(stage0))]
pub fn overflowing_shl(self, rhs: u32) -> (Self, bool) {
(self.wrapping_shl(rhs), (rhs > ($BITS - 1)))
}
#[inline]
#[stable(feature = "wrapping", since = "1.7.0")]
#[cfg(stage0)]
pub fn overflowing_shl(self, rhs: u32) -> (Self, bool) {
(self << (rhs & ($BITS - 1)), (rhs > ($BITS - 1)))
}
#[inline]
#[stable(feature = "wrapping", since = "1.7.0")]
#[cfg(not(stage0))]
pub fn overflowing_shr(self, rhs: u32) -> (Self, bool) {
(self.wrapping_shr(rhs), (rhs > ($BITS - 1)))
}
#[inline]
#[stable(feature = "wrapping", since = "1.7.0")]
#[cfg(stage0)]
pub fn overflowing_shr(self, rhs: u32) -> (Self, bool) {
(self >> (rhs & ($BITS - 1)), (rhs > ($BITS - 1)))
}
#[stable(feature = "no_panic_abs", since = "1.13.0")]
#[inline]
pub fn overflowing_abs(self) -> (Self, bool) {
if self.is_negative() {
self.overflowing_neg()
} else {
(self, false)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
#[rustc_inherit_overflow_checks]
pub fn pow(self, mut exp: u32) -> Self {
let mut base = self;
let mut acc = 1;
while exp > 1 {
if (exp & 1) == 1 {
acc = acc * base;
}
exp /= 2;
base = base * base;
}
if exp == 1 {
acc = acc * base;
}
acc
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
#[rustc_inherit_overflow_checks]
pub fn abs(self) -> Self {
if self.is_negative() {
-self
} else {
self
}
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn signum(self) -> Self {
match self {
n if n > 0 => 1,
0 => 0,
_ => -1,
}
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn is_positive(self) -> bool { self > 0 }
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn is_negative(self) -> bool { self < 0 }
}
}
#[lang = "i8"]
impl i8 {
int_impl! { i8, i8, u8, 8,
intrinsics::add_with_overflow,
intrinsics::sub_with_overflow,
intrinsics::mul_with_overflow }
}
#[lang = "i16"]
impl i16 {
int_impl! { i16, i16, u16, 16,
intrinsics::add_with_overflow,
intrinsics::sub_with_overflow,
intrinsics::mul_with_overflow }
}
#[lang = "i32"]
impl i32 {
int_impl! { i32, i32, u32, 32,
intrinsics::add_with_overflow,
intrinsics::sub_with_overflow,
intrinsics::mul_with_overflow }
}
#[lang = "i64"]
impl i64 {
int_impl! { i64, i64, u64, 64,
intrinsics::add_with_overflow,
intrinsics::sub_with_overflow,
intrinsics::mul_with_overflow }
}
#[cfg(target_pointer_width = "16")]
#[lang = "isize"]
impl isize {
int_impl! { isize, i16, u16, 16,
intrinsics::add_with_overflow,
intrinsics::sub_with_overflow,
intrinsics::mul_with_overflow }
}
#[cfg(target_pointer_width = "32")]
#[lang = "isize"]
impl isize {
int_impl! { isize, i32, u32, 32,
intrinsics::add_with_overflow,
intrinsics::sub_with_overflow,
intrinsics::mul_with_overflow }
}
#[cfg(target_pointer_width = "64")]
#[lang = "isize"]
impl isize {
int_impl! { isize, i64, u64, 64,
intrinsics::add_with_overflow,
intrinsics::sub_with_overflow,
intrinsics::mul_with_overflow }
}
macro_rules! uint_impl {
($SelfT:ty, $ActualT:ty, $BITS:expr,
$ctpop:path,
$ctlz:path,
$cttz:path,
$bswap:path,
$add_with_overflow:path,
$sub_with_overflow:path,
$mul_with_overflow:path) => {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub const fn min_value() -> Self { 0 }
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub const fn max_value() -> Self { !0 }
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn count_ones(self) -> u32 {
unsafe { $ctpop(self as $ActualT) as u32 }
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn count_zeros(self) -> u32 {
(!self).count_ones()
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn leading_zeros(self) -> u32 {
unsafe { $ctlz(self as $ActualT) as u32 }
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn trailing_zeros(self) -> u32 {
unsafe {
if $BITS == 8 {
intrinsics::cttz(self as u16 | 0x100) as u32
} else {
intrinsics::cttz(self) as u32
}
}
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn rotate_left(self, n: u32) -> Self {
let n = n % $BITS;
(self << n) | (self >> (($BITS - n) % $BITS))
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn rotate_right(self, n: u32) -> Self {
let n = n % $BITS;
(self >> n) | (self << (($BITS - n) % $BITS))
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn swap_bytes(self) -> Self {
unsafe { $bswap(self as $ActualT) as Self }
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn from_be(x: Self) -> Self {
if cfg!(target_endian = "big") { x } else { x.swap_bytes() }
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn from_le(x: Self) -> Self {
if cfg!(target_endian = "little") { x } else { x.swap_bytes() }
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn to_be(self) -> Self { if cfg!(target_endian = "big") { self } else { self.swap_bytes() }
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn to_le(self) -> Self {
if cfg!(target_endian = "little") { self } else { self.swap_bytes() }
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn checked_add(self, other: Self) -> Option<Self> {
let (a, b) = self.overflowing_add(other);
if b {None} else {Some(a)}
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn checked_sub(self, other: Self) -> Option<Self> {
let (a, b) = self.overflowing_sub(other);
if b {None} else {Some(a)}
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn checked_mul(self, other: Self) -> Option<Self> {
let (a, b) = self.overflowing_mul(other);
if b {None} else {Some(a)}
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn checked_div(self, other: Self) -> Option<Self> {
match other {
0 => None,
other => Some(unsafe { intrinsics::unchecked_div(self, other) }),
}
}
#[stable(feature = "wrapping", since = "1.7.0")]
#[inline]
pub fn checked_rem(self, other: Self) -> Option<Self> {
if other == 0 {
None
} else {
Some(unsafe { intrinsics::unchecked_rem(self, other) })
}
}
#[stable(feature = "wrapping", since = "1.7.0")]
#[inline]
pub fn checked_neg(self) -> Option<Self> {
let (a, b) = self.overflowing_neg();
if b {None} else {Some(a)}
}
#[stable(feature = "wrapping", since = "1.7.0")]
#[inline]
pub fn checked_shl(self, rhs: u32) -> Option<Self> {
let (a, b) = self.overflowing_shl(rhs);
if b {None} else {Some(a)}
}
#[stable(feature = "wrapping", since = "1.7.0")]
#[inline]
pub fn checked_shr(self, rhs: u32) -> Option<Self> {
let (a, b) = self.overflowing_shr(rhs);
if b {None} else {Some(a)}
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn saturating_add(self, other: Self) -> Self {
match self.checked_add(other) {
Some(x) => x,
None => Self::max_value(),
}
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn saturating_sub(self, other: Self) -> Self {
match self.checked_sub(other) {
Some(x) => x,
None => Self::min_value(),
}
}
#[stable(feature = "wrapping", since = "1.7.0")]
#[inline]
pub fn saturating_mul(self, other: Self) -> Self {
self.checked_mul(other).unwrap_or(Self::max_value())
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn wrapping_add(self, rhs: Self) -> Self {
unsafe {
intrinsics::overflowing_add(self, rhs)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn wrapping_sub(self, rhs: Self) -> Self {
unsafe {
intrinsics::overflowing_sub(self, rhs)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn wrapping_mul(self, rhs: Self) -> Self {
unsafe {
intrinsics::overflowing_mul(self, rhs)
}
}
#[stable(feature = "num_wrapping", since = "1.2.0")]
#[inline(always)]
pub fn wrapping_div(self, rhs: Self) -> Self {
self / rhs
}
#[stable(feature = "num_wrapping", since = "1.2.0")]
#[inline(always)]
pub fn wrapping_rem(self, rhs: Self) -> Self {
self % rhs
}
#[stable(feature = "num_wrapping", since = "1.2.0")]
#[inline(always)]
pub fn wrapping_neg(self) -> Self {
self.overflowing_neg().0
}
#[stable(feature = "num_wrapping", since = "1.2.0")]
#[inline(always)]
#[cfg(not(stage0))]
pub fn wrapping_shl(self, rhs: u32) -> Self {
unsafe {
intrinsics::unchecked_shl(self, (rhs & ($BITS - 1)) as $SelfT)
}
}
#[stable(feature = "num_wrapping", since = "1.2.0")]
#[inline(always)]
#[cfg(stage0)]
pub fn wrapping_shl(self, rhs: u32) -> Self {
self.overflowing_shl(rhs).0
}
#[stable(feature = "num_wrapping", since = "1.2.0")]
#[inline(always)]
#[cfg(not(stage0))]
pub fn wrapping_shr(self, rhs: u32) -> Self {
unsafe {
intrinsics::unchecked_shr(self, (rhs & ($BITS - 1)) as $SelfT)
}
}
#[stable(feature = "num_wrapping", since = "1.2.0")]
#[inline(always)]
#[cfg(stage0)]
pub fn wrapping_shr(self, rhs: u32) -> Self {
self.overflowing_shr(rhs).0
}
#[inline]
#[stable(feature = "wrapping", since = "1.7.0")]
pub fn overflowing_add(self, rhs: Self) -> (Self, bool) {
unsafe {
let (a, b) = $add_with_overflow(self as $ActualT,
rhs as $ActualT);
(a as Self, b)
}
}
#[inline]
#[stable(feature = "wrapping", since = "1.7.0")]
pub fn overflowing_sub(self, rhs: Self) -> (Self, bool) {
unsafe {
let (a, b) = $sub_with_overflow(self as $ActualT,
rhs as $ActualT);
(a as Self, b)
}
}
#[inline]
#[stable(feature = "wrapping", since = "1.7.0")]
pub fn overflowing_mul(self, rhs: Self) -> (Self, bool) {
unsafe {
let (a, b) = $mul_with_overflow(self as $ActualT,
rhs as $ActualT);
(a as Self, b)
}
}
#[inline]
#[stable(feature = "wrapping", since = "1.7.0")]
pub fn overflowing_div(self, rhs: Self) -> (Self, bool) {
(self / rhs, false)
}
#[inline]
#[stable(feature = "wrapping", since = "1.7.0")]
pub fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
(self % rhs, false)
}
#[inline]
#[stable(feature = "wrapping", since = "1.7.0")]
pub fn overflowing_neg(self) -> (Self, bool) {
((!self).wrapping_add(1), self != 0)
}
#[inline]
#[stable(feature = "wrapping", since = "1.7.0")]
#[cfg(not(stage0))]
pub fn overflowing_shl(self, rhs: u32) -> (Self, bool) {
(self.wrapping_shl(rhs), (rhs > ($BITS - 1)))
}
#[inline]
#[stable(feature = "wrapping", since = "1.7.0")]
#[cfg(stage0)]
pub fn overflowing_shl(self, rhs: u32) -> (Self, bool) {
(self << (rhs & ($BITS - 1)), (rhs > ($BITS - 1)))
}
#[inline]
#[stable(feature = "wrapping", since = "1.7.0")]
#[cfg(not(stage0))]
pub fn overflowing_shr(self, rhs: u32) -> (Self, bool) {
(self.wrapping_shr(rhs), (rhs > ($BITS - 1)))
}
#[inline]
#[stable(feature = "wrapping", since = "1.7.0")]
#[cfg(stage0)]
pub fn overflowing_shr(self, rhs: u32) -> (Self, bool) {
(self >> (rhs & ($BITS - 1)), (rhs > ($BITS - 1)))
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
#[rustc_inherit_overflow_checks]
pub fn pow(self, mut exp: u32) -> Self {
let mut base = self;
let mut acc = 1;
while exp > 1 {
if (exp & 1) == 1 {
acc = acc * base;
}
exp /= 2;
base = base * base;
}
if exp == 1 {
acc = acc * base;
}
acc
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn is_power_of_two(self) -> bool {
(self.wrapping_sub(1)) & self == 0 && !(self == 0)
}
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn next_power_of_two(self) -> Self {
let bits = size_of::<Self>() * 8;
let one: Self = 1;
one << ((bits - self.wrapping_sub(one).leading_zeros() as usize) % bits)
}
#[stable(feature = "rust1", since = "1.0.0")]
pub fn checked_next_power_of_two(self) -> Option<Self> {
let npot = self.next_power_of_two();
if npot >= self {
Some(npot)
} else {
None
}
}
}
}
#[lang = "u8"]
impl u8 {
uint_impl! { u8, u8, 8,
intrinsics::ctpop,
intrinsics::ctlz,
intrinsics::cttz,
intrinsics::bswap,
intrinsics::add_with_overflow,
intrinsics::sub_with_overflow,
intrinsics::mul_with_overflow }
}
#[lang = "u16"]
impl u16 {
uint_impl! { u16, u16, 16,
intrinsics::ctpop,
intrinsics::ctlz,
intrinsics::cttz,
intrinsics::bswap,
intrinsics::add_with_overflow,
intrinsics::sub_with_overflow,
intrinsics::mul_with_overflow }
}
#[lang = "u32"]
impl u32 {
uint_impl! { u32, u32, 32,
intrinsics::ctpop,
intrinsics::ctlz,
intrinsics::cttz,
intrinsics::bswap,
intrinsics::add_with_overflow,
intrinsics::sub_with_overflow,
intrinsics::mul_with_overflow }
}
#[lang = "u64"]
impl u64 {
uint_impl! { u64, u64, 64,
intrinsics::ctpop,
intrinsics::ctlz,
intrinsics::cttz,
intrinsics::bswap,
intrinsics::add_with_overflow,
intrinsics::sub_with_overflow,
intrinsics::mul_with_overflow }
}
#[cfg(target_pointer_width = "16")]
#[lang = "usize"]
impl usize {
uint_impl! { usize, u16, 16,
intrinsics::ctpop,
intrinsics::ctlz,
intrinsics::cttz,
intrinsics::bswap,
intrinsics::add_with_overflow,
intrinsics::sub_with_overflow,
intrinsics::mul_with_overflow }
}
#[cfg(target_pointer_width = "32")]
#[lang = "usize"]
impl usize {
uint_impl! { usize, u32, 32,
intrinsics::ctpop,
intrinsics::ctlz,
intrinsics::cttz,
intrinsics::bswap,
intrinsics::add_with_overflow,
intrinsics::sub_with_overflow,
intrinsics::mul_with_overflow }
}
#[cfg(target_pointer_width = "64")]
#[lang = "usize"]
impl usize {
uint_impl! { usize, u64, 64,
intrinsics::ctpop,
intrinsics::ctlz,
intrinsics::cttz,
intrinsics::bswap,
intrinsics::add_with_overflow,
intrinsics::sub_with_overflow,
intrinsics::mul_with_overflow }
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[stable(feature = "rust1", since = "1.0.0")]
pub enum FpCategory {
#[stable(feature = "rust1", since = "1.0.0")]
Nan,
#[stable(feature = "rust1", since = "1.0.0")]
Infinite,
#[stable(feature = "rust1", since = "1.0.0")]
Zero,
#[stable(feature = "rust1", since = "1.0.0")]
Subnormal,
#[stable(feature = "rust1", since = "1.0.0")]
Normal,
}
#[doc(hidden)]
#[unstable(feature = "core_float",
reason = "stable interface is via `impl f{32,64}` in later crates",
issue = "32110")]
pub trait Float: Sized {
#[stable(feature = "core", since = "1.6.0")]
fn is_nan(self) -> bool;
#[stable(feature = "core", since = "1.6.0")]
fn is_infinite(self) -> bool;
#[stable(feature = "core", since = "1.6.0")]
fn is_finite(self) -> bool;
#[stable(feature = "core", since = "1.6.0")]
fn is_normal(self) -> bool;
#[stable(feature = "core", since = "1.6.0")]
fn classify(self) -> FpCategory;
#[stable(feature = "core", since = "1.6.0")]
fn abs(self) -> Self;
#[stable(feature = "core", since = "1.6.0")]
fn signum(self) -> Self;
#[stable(feature = "core", since = "1.6.0")]
fn is_sign_positive(self) -> bool;
#[stable(feature = "core", since = "1.6.0")]
fn is_sign_negative(self) -> bool;
#[stable(feature = "core", since = "1.6.0")]
fn recip(self) -> Self;
#[stable(feature = "core", since = "1.6.0")]
fn powi(self, n: i32) -> Self;
#[stable(feature = "deg_rad_conversions", since="1.7.0")]
fn to_degrees(self) -> Self;
#[stable(feature = "deg_rad_conversions", since="1.7.0")]
fn to_radians(self) -> Self;
}
#[doc(hidden)]
trait FromStrRadixHelper: PartialOrd + Copy {
fn min_value() -> Self;
fn max_value() -> Self;
fn from_u32(u: u32) -> Self;
fn checked_mul(&self, other: u32) -> Option<Self>;
fn checked_sub(&self, other: u32) -> Option<Self>;
fn checked_add(&self, other: u32) -> Option<Self>;
}
macro_rules! doit {
($($t:ty)*) => ($(impl FromStrRadixHelper for $t {
fn min_value() -> Self { Self::min_value() }
fn max_value() -> Self { Self::max_value() }
fn from_u32(u: u32) -> Self { u as Self }
fn checked_mul(&self, other: u32) -> Option<Self> {
Self::checked_mul(*self, other as Self)
}
fn checked_sub(&self, other: u32) -> Option<Self> {
Self::checked_sub(*self, other as Self)
}
fn checked_add(&self, other: u32) -> Option<Self> {
Self::checked_add(*self, other as Self)
}
})*)
}
doit! { i8 i16 i32 isize u8 u16 u32 usize }
macro_rules! impl_from {
($Small: ty, $Large: ty) => {
#[stable(feature = "lossless_prim_conv", since = "1.5.0")]
impl From<$Small> for $Large {
#[inline]
fn from(small: $Small) -> $Large {
small as $Large
}
}
}
}
impl_from! { u8, u16 }
impl_from! { u8, u32 }
impl_from! { u8, u64 }
impl_from! { u8, u128 }
impl_from! { u8, usize }
impl_from! { u16, u32 }
impl_from! { u16, u64 }
impl_from! { u16, u128 }
impl_from! { u32, u64 }
impl_from! { u32, u128 }
impl_from! { u64, u128 }
impl_from! { i8, i16 }
impl_from! { i8, i32 }
impl_from! { i8, i64 }
impl_from! { i8, i128 }
impl_from! { i8, isize }
impl_from! { i16, i32 }
impl_from! { i16, i64 }
impl_from! { i16, i128 }
impl_from! { i32, i64 }
impl_from! { i32, i128 }
impl_from! { i64, i128 }
impl_from! { u8, i16 }
impl_from! { u8, i32 }
impl_from! { u8, i64 }
impl_from! { u8, i128 }
impl_from! { u16, i32 }
impl_from! { u16, i64 }
impl_from! { u16, i128 }
impl_from! { u32, i64 }
impl_from! { u32, i128 }
impl_from! { u64, i128 }
impl_from! { i8, f32 }
impl_from! { i8, f64 }
impl_from! { i16, f32 }
impl_from! { i16, f64 }
impl_from! { i32, f64 }
impl_from! { u8, f32 }
impl_from! { u8, f64 }
impl_from! { u16, f32 }
impl_from! { u16, f64 }
impl_from! { u32, f64 }
impl_from! { f32, f64 }