Skip to content

Commit

Permalink
Rollup merge of rust-lang#46820 - nodakai:simplify-int-impl, r=alexcr…
Browse files Browse the repository at this point in the history
…ichton

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

We can simply use generic intrinsics since cd1848a by @alexcrichton

Also, minimize unsafe blocks.
  • Loading branch information
kennytm authored Dec 21, 2017
2 parents 0ee069c + 6bce6ac commit dc00aa4
Showing 1 changed file with 52 additions and 151 deletions.
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 @@ -865,11 +862,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 @@ -891,11 +888,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 @@ -915,11 +912,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 @@ -1207,82 +1204,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 @@ -1346,7 +1311,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 @@ -1381,7 +1346,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 @@ -1480,7 +1445,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 @@ -1984,11 +1949,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 @@ -2010,11 +1975,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 @@ -2034,11 +1999,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 @@ -2223,7 +2188,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 @@ -2270,15 +2235,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 @@ -2824,95 +2781,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

0 comments on commit dc00aa4

Please sign in to comment.