Skip to content

libcore/num/mod.rs: simplify the int_impl! macro. #46820

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 1 commit into from
Dec 22, 2017
Merged
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
203 changes: 52 additions & 151 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,7 @@ pub mod diy_float;

// `Int` + `SignedInt` implemented for signed integers
macro_rules! int_impl {
($SelfT:ty, $ActualT:ident, $UnsignedT:ty, $BITS:expr,
$add_with_overflow:path,
$sub_with_overflow:path,
$mul_with_overflow:path) => {
($SelfT:ty, $ActualT:ident, $UnsignedT:ty, $BITS:expr) => {
/// Returns the smallest value that can be represented by this integer type.
///
/// # Examples
Expand Down Expand Up @@ -862,11 +859,11 @@ macro_rules! int_impl {
#[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)
}
let (a, b) = unsafe {
intrinsics::add_with_overflow(self as $ActualT,
rhs as $ActualT)
};
(a as Self, b)
}

/// Calculates `self` - `rhs`
Expand All @@ -888,11 +885,11 @@ macro_rules! int_impl {
#[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)
}
let (a, b) = unsafe {
intrinsics::sub_with_overflow(self as $ActualT,
rhs as $ActualT)
};
(a as Self, b)
}

/// Calculates the multiplication of `self` and `rhs`.
Expand All @@ -912,11 +909,11 @@ macro_rules! int_impl {
#[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)
}
let (a, b) = unsafe {
intrinsics::mul_with_overflow(self as $ActualT,
rhs as $ActualT)
};
(a as Self, b)
}

/// Calculates the divisor when `self` is divided by `rhs`.
Expand Down Expand Up @@ -1204,82 +1201,50 @@ macro_rules! int_impl {

#[lang = "i8"]
impl i8 {
int_impl! { i8, i8, u8, 8,
intrinsics::add_with_overflow,
intrinsics::sub_with_overflow,
intrinsics::mul_with_overflow }
int_impl! { i8, i8, u8, 8 }
}

#[lang = "i16"]
impl i16 {
int_impl! { i16, i16, u16, 16,
intrinsics::add_with_overflow,
intrinsics::sub_with_overflow,
intrinsics::mul_with_overflow }
int_impl! { i16, i16, u16, 16 }
}

#[lang = "i32"]
impl i32 {
int_impl! { i32, i32, u32, 32,
intrinsics::add_with_overflow,
intrinsics::sub_with_overflow,
intrinsics::mul_with_overflow }
int_impl! { i32, i32, u32, 32 }
}

#[lang = "i64"]
impl i64 {
int_impl! { i64, i64, u64, 64,
intrinsics::add_with_overflow,
intrinsics::sub_with_overflow,
intrinsics::mul_with_overflow }
int_impl! { i64, i64, u64, 64 }
}

#[lang = "i128"]
impl i128 {
int_impl! { i128, i128, u128, 128,
intrinsics::add_with_overflow,
intrinsics::sub_with_overflow,
intrinsics::mul_with_overflow }
int_impl! { i128, i128, u128, 128 }
}

#[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 }
int_impl! { isize, i16, u16, 16 }
}

#[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 }
int_impl! { isize, i32, u32, 32 }
}

#[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 }
int_impl! { isize, i64, u64, 64 }
}

// `Int` + `UnsignedInt` implemented for unsigned integers
macro_rules! uint_impl {
($SelfT:ty, $ActualT:ty, $BITS:expr,
$ctpop:path,
$ctlz:path,
$ctlz_nonzero:path,
$cttz:path,
$bswap:path,
$add_with_overflow:path,
$sub_with_overflow:path,
$mul_with_overflow:path) => {
($SelfT:ty, $ActualT:ty, $BITS:expr) => {
/// Returns the smallest value that can be represented by this integer type.
///
/// # Examples
Expand Down Expand Up @@ -1343,7 +1308,7 @@ macro_rules! uint_impl {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn count_ones(self) -> u32 {
unsafe { $ctpop(self as $ActualT) as u32 }
unsafe { intrinsics::ctpop(self as $ActualT) as u32 }
}

/// Returns the number of zeros in the binary representation of `self`.
Expand Down Expand Up @@ -1378,7 +1343,7 @@ macro_rules! uint_impl {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn leading_zeros(self) -> u32 {
unsafe { $ctlz(self as $ActualT) as u32 }
unsafe { intrinsics::ctlz(self as $ActualT) as u32 }
}

/// Returns the number of trailing zeros in the binary representation
Expand Down Expand Up @@ -1474,7 +1439,7 @@ macro_rules! uint_impl {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn swap_bytes(self) -> Self {
unsafe { $bswap(self as $ActualT) as Self }
unsafe { intrinsics::bswap(self as $ActualT) as Self }
}

/// Converts an integer from big endian to the target's endianness.
Expand Down Expand Up @@ -1978,11 +1943,11 @@ macro_rules! uint_impl {
#[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)
}
let (a, b) = unsafe {
intrinsics::add_with_overflow(self as $ActualT,
rhs as $ActualT)
};
(a as Self, b)
}

/// Calculates `self` - `rhs`
Expand All @@ -2004,11 +1969,11 @@ macro_rules! uint_impl {
#[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)
}
let (a, b) = unsafe {
intrinsics::sub_with_overflow(self as $ActualT,
rhs as $ActualT)
};
(a as Self, b)
}

/// Calculates the multiplication of `self` and `rhs`.
Expand All @@ -2028,11 +1993,11 @@ macro_rules! uint_impl {
#[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)
}
let (a, b) = unsafe {
intrinsics::mul_with_overflow(self as $ActualT,
rhs as $ActualT)
};
(a as Self, b)
}

/// Calculates the divisor when `self` is divided by `rhs`.
Expand Down Expand Up @@ -2217,7 +2182,7 @@ macro_rules! uint_impl {
// (such as intel pre-haswell) have more efficient ctlz
// intrinsics when the argument is non-zero.
let p = self - 1;
let z = unsafe { $ctlz_nonzero(p) };
let z = unsafe { intrinsics::ctlz_nonzero(p) };
<$SelfT>::max_value() >> z
}

Expand Down Expand Up @@ -2264,15 +2229,7 @@ macro_rules! uint_impl {

#[lang = "u8"]
impl u8 {
uint_impl! { u8, u8, 8,
intrinsics::ctpop,
intrinsics::ctlz,
intrinsics::ctlz_nonzero,
intrinsics::cttz,
intrinsics::bswap,
intrinsics::add_with_overflow,
intrinsics::sub_with_overflow,
intrinsics::mul_with_overflow }
uint_impl! { u8, u8, 8 }


/// Checks if the value is within the ASCII range.
Expand Down Expand Up @@ -2818,95 +2775,39 @@ impl u8 {

#[lang = "u16"]
impl u16 {
uint_impl! { u16, u16, 16,
intrinsics::ctpop,
intrinsics::ctlz,
intrinsics::ctlz_nonzero,
intrinsics::cttz,
intrinsics::bswap,
intrinsics::add_with_overflow,
intrinsics::sub_with_overflow,
intrinsics::mul_with_overflow }
uint_impl! { u16, u16, 16 }
}

#[lang = "u32"]
impl u32 {
uint_impl! { u32, u32, 32,
intrinsics::ctpop,
intrinsics::ctlz,
intrinsics::ctlz_nonzero,
intrinsics::cttz,
intrinsics::bswap,
intrinsics::add_with_overflow,
intrinsics::sub_with_overflow,
intrinsics::mul_with_overflow }
uint_impl! { u32, u32, 32 }
}

#[lang = "u64"]
impl u64 {
uint_impl! { u64, u64, 64,
intrinsics::ctpop,
intrinsics::ctlz,
intrinsics::ctlz_nonzero,
intrinsics::cttz,
intrinsics::bswap,
intrinsics::add_with_overflow,
intrinsics::sub_with_overflow,
intrinsics::mul_with_overflow }
uint_impl! { u64, u64, 64 }
}

#[lang = "u128"]
impl u128 {
uint_impl! { u128, u128, 128,
intrinsics::ctpop,
intrinsics::ctlz,
intrinsics::ctlz_nonzero,
intrinsics::cttz,
intrinsics::bswap,
intrinsics::add_with_overflow,
intrinsics::sub_with_overflow,
intrinsics::mul_with_overflow }
uint_impl! { u128, u128, 128 }
}

#[cfg(target_pointer_width = "16")]
#[lang = "usize"]
impl usize {
uint_impl! { usize, u16, 16,
intrinsics::ctpop,
intrinsics::ctlz,
intrinsics::ctlz_nonzero,
intrinsics::cttz,
intrinsics::bswap,
intrinsics::add_with_overflow,
intrinsics::sub_with_overflow,
intrinsics::mul_with_overflow }
uint_impl! { usize, u16, 16 }
}
#[cfg(target_pointer_width = "32")]
#[lang = "usize"]
impl usize {
uint_impl! { usize, u32, 32,
intrinsics::ctpop,
intrinsics::ctlz,
intrinsics::ctlz_nonzero,
intrinsics::cttz,
intrinsics::bswap,
intrinsics::add_with_overflow,
intrinsics::sub_with_overflow,
intrinsics::mul_with_overflow }
uint_impl! { usize, u32, 32 }
}

#[cfg(target_pointer_width = "64")]
#[lang = "usize"]
impl usize {
uint_impl! { usize, u64, 64,
intrinsics::ctpop,
intrinsics::ctlz,
intrinsics::ctlz_nonzero,
intrinsics::cttz,
intrinsics::bswap,
intrinsics::add_with_overflow,
intrinsics::sub_with_overflow,
intrinsics::mul_with_overflow }
uint_impl! { usize, u64, 64 }
}

/// A classification of floating point numbers.
Expand Down