From 6bce6acebb9b62791034e821e6215f53a8bf2002 Mon Sep 17 00:00:00 2001 From: "NODA, Kai" Date: Sun, 17 Dec 2017 18:19:43 +0800 Subject: [PATCH] libcore/num/mod.rs: simplify the int_impl! macro. We can simply use generic intrinsics since cd1848a1a6 Also, minimize unsafe blocks. Signed-off-by: NODA, Kai --- src/libcore/num/mod.rs | 203 +++++++++++------------------------------ 1 file changed, 52 insertions(+), 151 deletions(-) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 9ee86d0f1a0..00f36ca0f79 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -98,10 +98,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { // `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 @@ -862,11 +859,11 @@ pub fn wrapping_abs(self) -> 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) - } + let (a, b) = unsafe { + intrinsics::add_with_overflow(self as $ActualT, + rhs as $ActualT) + }; + (a as Self, b) } /// Calculates `self` - `rhs` @@ -888,11 +885,11 @@ pub fn overflowing_add(self, rhs: Self) -> (Self, bool) { #[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`. @@ -912,11 +909,11 @@ pub fn overflowing_sub(self, rhs: Self) -> (Self, bool) { #[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`. @@ -1204,82 +1201,50 @@ 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 } + 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 @@ -1343,7 +1308,7 @@ pub fn from_str_radix(src: &str, radix: u32) -> Result { #[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`. @@ -1378,7 +1343,7 @@ pub fn count_zeros(self) -> u32 { #[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 @@ -1474,7 +1439,7 @@ pub fn rotate_right(self, n: u32) -> Self { #[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. @@ -1978,11 +1943,11 @@ pub fn wrapping_shr(self, rhs: u32) -> 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) - } + let (a, b) = unsafe { + intrinsics::add_with_overflow(self as $ActualT, + rhs as $ActualT) + }; + (a as Self, b) } /// Calculates `self` - `rhs` @@ -2004,11 +1969,11 @@ pub fn overflowing_add(self, rhs: Self) -> (Self, bool) { #[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`. @@ -2028,11 +1993,11 @@ pub fn overflowing_sub(self, rhs: Self) -> (Self, bool) { #[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`. @@ -2217,7 +2182,7 @@ fn one_less_than_next_power_of_two(self) -> Self { // (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 } @@ -2264,15 +2229,7 @@ pub fn checked_next_power_of_two(self) -> Option { #[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. @@ -2818,95 +2775,39 @@ pub fn is_ascii_control(&self) -> bool { #[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.