Skip to content

Commit 6bce6ac

Browse files
committed
libcore/num/mod.rs: simplify the int_impl! macro.
We can simply use generic intrinsics since cd1848a Also, minimize unsafe blocks. Signed-off-by: NODA, Kai <nodakai@gmail.com>
1 parent a3a7203 commit 6bce6ac

File tree

1 file changed

+52
-151
lines changed

1 file changed

+52
-151
lines changed

src/libcore/num/mod.rs

+52-151
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,7 @@ pub mod diy_float;
9898

9999
// `Int` + `SignedInt` implemented for signed integers
100100
macro_rules! int_impl {
101-
($SelfT:ty, $ActualT:ident, $UnsignedT:ty, $BITS:expr,
102-
$add_with_overflow:path,
103-
$sub_with_overflow:path,
104-
$mul_with_overflow:path) => {
101+
($SelfT:ty, $ActualT:ident, $UnsignedT:ty, $BITS:expr) => {
105102
/// Returns the smallest value that can be represented by this integer type.
106103
///
107104
/// # Examples
@@ -862,11 +859,11 @@ macro_rules! int_impl {
862859
#[inline]
863860
#[stable(feature = "wrapping", since = "1.7.0")]
864861
pub fn overflowing_add(self, rhs: Self) -> (Self, bool) {
865-
unsafe {
866-
let (a, b) = $add_with_overflow(self as $ActualT,
867-
rhs as $ActualT);
868-
(a as Self, b)
869-
}
862+
let (a, b) = unsafe {
863+
intrinsics::add_with_overflow(self as $ActualT,
864+
rhs as $ActualT)
865+
};
866+
(a as Self, b)
870867
}
871868

872869
/// Calculates `self` - `rhs`
@@ -888,11 +885,11 @@ macro_rules! int_impl {
888885
#[inline]
889886
#[stable(feature = "wrapping", since = "1.7.0")]
890887
pub fn overflowing_sub(self, rhs: Self) -> (Self, bool) {
891-
unsafe {
892-
let (a, b) = $sub_with_overflow(self as $ActualT,
893-
rhs as $ActualT);
894-
(a as Self, b)
895-
}
888+
let (a, b) = unsafe {
889+
intrinsics::sub_with_overflow(self as $ActualT,
890+
rhs as $ActualT)
891+
};
892+
(a as Self, b)
896893
}
897894

898895
/// Calculates the multiplication of `self` and `rhs`.
@@ -912,11 +909,11 @@ macro_rules! int_impl {
912909
#[inline]
913910
#[stable(feature = "wrapping", since = "1.7.0")]
914911
pub fn overflowing_mul(self, rhs: Self) -> (Self, bool) {
915-
unsafe {
916-
let (a, b) = $mul_with_overflow(self as $ActualT,
917-
rhs as $ActualT);
918-
(a as Self, b)
919-
}
912+
let (a, b) = unsafe {
913+
intrinsics::mul_with_overflow(self as $ActualT,
914+
rhs as $ActualT)
915+
};
916+
(a as Self, b)
920917
}
921918

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

12051202
#[lang = "i8"]
12061203
impl i8 {
1207-
int_impl! { i8, i8, u8, 8,
1208-
intrinsics::add_with_overflow,
1209-
intrinsics::sub_with_overflow,
1210-
intrinsics::mul_with_overflow }
1204+
int_impl! { i8, i8, u8, 8 }
12111205
}
12121206

12131207
#[lang = "i16"]
12141208
impl i16 {
1215-
int_impl! { i16, i16, u16, 16,
1216-
intrinsics::add_with_overflow,
1217-
intrinsics::sub_with_overflow,
1218-
intrinsics::mul_with_overflow }
1209+
int_impl! { i16, i16, u16, 16 }
12191210
}
12201211

12211212
#[lang = "i32"]
12221213
impl i32 {
1223-
int_impl! { i32, i32, u32, 32,
1224-
intrinsics::add_with_overflow,
1225-
intrinsics::sub_with_overflow,
1226-
intrinsics::mul_with_overflow }
1214+
int_impl! { i32, i32, u32, 32 }
12271215
}
12281216

12291217
#[lang = "i64"]
12301218
impl i64 {
1231-
int_impl! { i64, i64, u64, 64,
1232-
intrinsics::add_with_overflow,
1233-
intrinsics::sub_with_overflow,
1234-
intrinsics::mul_with_overflow }
1219+
int_impl! { i64, i64, u64, 64 }
12351220
}
12361221

12371222
#[lang = "i128"]
12381223
impl i128 {
1239-
int_impl! { i128, i128, u128, 128,
1240-
intrinsics::add_with_overflow,
1241-
intrinsics::sub_with_overflow,
1242-
intrinsics::mul_with_overflow }
1224+
int_impl! { i128, i128, u128, 128 }
12431225
}
12441226

12451227
#[cfg(target_pointer_width = "16")]
12461228
#[lang = "isize"]
12471229
impl isize {
1248-
int_impl! { isize, i16, u16, 16,
1249-
intrinsics::add_with_overflow,
1250-
intrinsics::sub_with_overflow,
1251-
intrinsics::mul_with_overflow }
1230+
int_impl! { isize, i16, u16, 16 }
12521231
}
12531232

12541233
#[cfg(target_pointer_width = "32")]
12551234
#[lang = "isize"]
12561235
impl isize {
1257-
int_impl! { isize, i32, u32, 32,
1258-
intrinsics::add_with_overflow,
1259-
intrinsics::sub_with_overflow,
1260-
intrinsics::mul_with_overflow }
1236+
int_impl! { isize, i32, u32, 32 }
12611237
}
12621238

12631239
#[cfg(target_pointer_width = "64")]
12641240
#[lang = "isize"]
12651241
impl isize {
1266-
int_impl! { isize, i64, u64, 64,
1267-
intrinsics::add_with_overflow,
1268-
intrinsics::sub_with_overflow,
1269-
intrinsics::mul_with_overflow }
1242+
int_impl! { isize, i64, u64, 64 }
12701243
}
12711244

12721245
// `Int` + `UnsignedInt` implemented for unsigned integers
12731246
macro_rules! uint_impl {
1274-
($SelfT:ty, $ActualT:ty, $BITS:expr,
1275-
$ctpop:path,
1276-
$ctlz:path,
1277-
$ctlz_nonzero:path,
1278-
$cttz:path,
1279-
$bswap:path,
1280-
$add_with_overflow:path,
1281-
$sub_with_overflow:path,
1282-
$mul_with_overflow:path) => {
1247+
($SelfT:ty, $ActualT:ty, $BITS:expr) => {
12831248
/// Returns the smallest value that can be represented by this integer type.
12841249
///
12851250
/// # Examples
@@ -1343,7 +1308,7 @@ macro_rules! uint_impl {
13431308
#[stable(feature = "rust1", since = "1.0.0")]
13441309
#[inline]
13451310
pub fn count_ones(self) -> u32 {
1346-
unsafe { $ctpop(self as $ActualT) as u32 }
1311+
unsafe { intrinsics::ctpop(self as $ActualT) as u32 }
13471312
}
13481313

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

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

14801445
/// Converts an integer from big endian to the target's endianness.
@@ -1978,11 +1943,11 @@ macro_rules! uint_impl {
19781943
#[inline]
19791944
#[stable(feature = "wrapping", since = "1.7.0")]
19801945
pub fn overflowing_add(self, rhs: Self) -> (Self, bool) {
1981-
unsafe {
1982-
let (a, b) = $add_with_overflow(self as $ActualT,
1983-
rhs as $ActualT);
1984-
(a as Self, b)
1985-
}
1946+
let (a, b) = unsafe {
1947+
intrinsics::add_with_overflow(self as $ActualT,
1948+
rhs as $ActualT)
1949+
};
1950+
(a as Self, b)
19861951
}
19871952

19881953
/// Calculates `self` - `rhs`
@@ -2004,11 +1969,11 @@ macro_rules! uint_impl {
20041969
#[inline]
20051970
#[stable(feature = "wrapping", since = "1.7.0")]
20061971
pub fn overflowing_sub(self, rhs: Self) -> (Self, bool) {
2007-
unsafe {
2008-
let (a, b) = $sub_with_overflow(self as $ActualT,
2009-
rhs as $ActualT);
2010-
(a as Self, b)
2011-
}
1972+
let (a, b) = unsafe {
1973+
intrinsics::sub_with_overflow(self as $ActualT,
1974+
rhs as $ActualT)
1975+
};
1976+
(a as Self, b)
20121977
}
20131978

20141979
/// Calculates the multiplication of `self` and `rhs`.
@@ -2028,11 +1993,11 @@ macro_rules! uint_impl {
20281993
#[inline]
20291994
#[stable(feature = "wrapping", since = "1.7.0")]
20301995
pub fn overflowing_mul(self, rhs: Self) -> (Self, bool) {
2031-
unsafe {
2032-
let (a, b) = $mul_with_overflow(self as $ActualT,
2033-
rhs as $ActualT);
2034-
(a as Self, b)
2035-
}
1996+
let (a, b) = unsafe {
1997+
intrinsics::mul_with_overflow(self as $ActualT,
1998+
rhs as $ActualT)
1999+
};
2000+
(a as Self, b)
20362001
}
20372002

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

@@ -2264,15 +2229,7 @@ macro_rules! uint_impl {
22642229

22652230
#[lang = "u8"]
22662231
impl u8 {
2267-
uint_impl! { u8, u8, 8,
2268-
intrinsics::ctpop,
2269-
intrinsics::ctlz,
2270-
intrinsics::ctlz_nonzero,
2271-
intrinsics::cttz,
2272-
intrinsics::bswap,
2273-
intrinsics::add_with_overflow,
2274-
intrinsics::sub_with_overflow,
2275-
intrinsics::mul_with_overflow }
2232+
uint_impl! { u8, u8, 8 }
22762233

22772234

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

28192776
#[lang = "u16"]
28202777
impl u16 {
2821-
uint_impl! { u16, u16, 16,
2822-
intrinsics::ctpop,
2823-
intrinsics::ctlz,
2824-
intrinsics::ctlz_nonzero,
2825-
intrinsics::cttz,
2826-
intrinsics::bswap,
2827-
intrinsics::add_with_overflow,
2828-
intrinsics::sub_with_overflow,
2829-
intrinsics::mul_with_overflow }
2778+
uint_impl! { u16, u16, 16 }
28302779
}
28312780

28322781
#[lang = "u32"]
28332782
impl u32 {
2834-
uint_impl! { u32, u32, 32,
2835-
intrinsics::ctpop,
2836-
intrinsics::ctlz,
2837-
intrinsics::ctlz_nonzero,
2838-
intrinsics::cttz,
2839-
intrinsics::bswap,
2840-
intrinsics::add_with_overflow,
2841-
intrinsics::sub_with_overflow,
2842-
intrinsics::mul_with_overflow }
2783+
uint_impl! { u32, u32, 32 }
28432784
}
28442785

28452786
#[lang = "u64"]
28462787
impl u64 {
2847-
uint_impl! { u64, u64, 64,
2848-
intrinsics::ctpop,
2849-
intrinsics::ctlz,
2850-
intrinsics::ctlz_nonzero,
2851-
intrinsics::cttz,
2852-
intrinsics::bswap,
2853-
intrinsics::add_with_overflow,
2854-
intrinsics::sub_with_overflow,
2855-
intrinsics::mul_with_overflow }
2788+
uint_impl! { u64, u64, 64 }
28562789
}
28572790

28582791
#[lang = "u128"]
28592792
impl u128 {
2860-
uint_impl! { u128, u128, 128,
2861-
intrinsics::ctpop,
2862-
intrinsics::ctlz,
2863-
intrinsics::ctlz_nonzero,
2864-
intrinsics::cttz,
2865-
intrinsics::bswap,
2866-
intrinsics::add_with_overflow,
2867-
intrinsics::sub_with_overflow,
2868-
intrinsics::mul_with_overflow }
2793+
uint_impl! { u128, u128, 128 }
28692794
}
28702795

28712796
#[cfg(target_pointer_width = "16")]
28722797
#[lang = "usize"]
28732798
impl usize {
2874-
uint_impl! { usize, u16, 16,
2875-
intrinsics::ctpop,
2876-
intrinsics::ctlz,
2877-
intrinsics::ctlz_nonzero,
2878-
intrinsics::cttz,
2879-
intrinsics::bswap,
2880-
intrinsics::add_with_overflow,
2881-
intrinsics::sub_with_overflow,
2882-
intrinsics::mul_with_overflow }
2799+
uint_impl! { usize, u16, 16 }
28832800
}
28842801
#[cfg(target_pointer_width = "32")]
28852802
#[lang = "usize"]
28862803
impl usize {
2887-
uint_impl! { usize, u32, 32,
2888-
intrinsics::ctpop,
2889-
intrinsics::ctlz,
2890-
intrinsics::ctlz_nonzero,
2891-
intrinsics::cttz,
2892-
intrinsics::bswap,
2893-
intrinsics::add_with_overflow,
2894-
intrinsics::sub_with_overflow,
2895-
intrinsics::mul_with_overflow }
2804+
uint_impl! { usize, u32, 32 }
28962805
}
28972806

28982807
#[cfg(target_pointer_width = "64")]
28992808
#[lang = "usize"]
29002809
impl usize {
2901-
uint_impl! { usize, u64, 64,
2902-
intrinsics::ctpop,
2903-
intrinsics::ctlz,
2904-
intrinsics::ctlz_nonzero,
2905-
intrinsics::cttz,
2906-
intrinsics::bswap,
2907-
intrinsics::add_with_overflow,
2908-
intrinsics::sub_with_overflow,
2909-
intrinsics::mul_with_overflow }
2810+
uint_impl! { usize, u64, 64 }
29102811
}
29112812

29122813
/// A classification of floating point numbers.

0 commit comments

Comments
 (0)