Skip to content

Commit dc00aa4

Browse files
authored
Rollup merge of rust-lang#46820 - nodakai:simplify-int-impl, r=alexcrichton
libcore/num/mod.rs: simplify the int_impl! macro. We can simply use generic intrinsics since cd1848a by @alexcrichton Also, minimize unsafe blocks.
2 parents 0ee069c + 6bce6ac commit dc00aa4

File tree

1 file changed

+52
-151
lines changed

1 file changed

+52
-151
lines changed

src/libcore/num/mod.rs

Lines changed: 52 additions & 151 deletions
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
@@ -865,11 +862,11 @@ macro_rules! int_impl {
865862
#[inline]
866863
#[stable(feature = "wrapping", since = "1.7.0")]
867864
pub fn overflowing_add(self, rhs: Self) -> (Self, bool) {
868-
unsafe {
869-
let (a, b) = $add_with_overflow(self as $ActualT,
870-
rhs as $ActualT);
871-
(a as Self, b)
872-
}
865+
let (a, b) = unsafe {
866+
intrinsics::add_with_overflow(self as $ActualT,
867+
rhs as $ActualT)
868+
};
869+
(a as Self, b)
873870
}
874871

875872
/// Calculates `self` - `rhs`
@@ -891,11 +888,11 @@ macro_rules! int_impl {
891888
#[inline]
892889
#[stable(feature = "wrapping", since = "1.7.0")]
893890
pub fn overflowing_sub(self, rhs: Self) -> (Self, bool) {
894-
unsafe {
895-
let (a, b) = $sub_with_overflow(self as $ActualT,
896-
rhs as $ActualT);
897-
(a as Self, b)
898-
}
891+
let (a, b) = unsafe {
892+
intrinsics::sub_with_overflow(self as $ActualT,
893+
rhs as $ActualT)
894+
};
895+
(a as Self, b)
899896
}
900897

901898
/// Calculates the multiplication of `self` and `rhs`.
@@ -915,11 +912,11 @@ macro_rules! int_impl {
915912
#[inline]
916913
#[stable(feature = "wrapping", since = "1.7.0")]
917914
pub fn overflowing_mul(self, rhs: Self) -> (Self, bool) {
918-
unsafe {
919-
let (a, b) = $mul_with_overflow(self as $ActualT,
920-
rhs as $ActualT);
921-
(a as Self, b)
922-
}
915+
let (a, b) = unsafe {
916+
intrinsics::mul_with_overflow(self as $ActualT,
917+
rhs as $ActualT)
918+
};
919+
(a as Self, b)
923920
}
924921

925922
/// Calculates the divisor when `self` is divided by `rhs`.
@@ -1207,82 +1204,50 @@ macro_rules! int_impl {
12071204

12081205
#[lang = "i8"]
12091206
impl i8 {
1210-
int_impl! { i8, i8, u8, 8,
1211-
intrinsics::add_with_overflow,
1212-
intrinsics::sub_with_overflow,
1213-
intrinsics::mul_with_overflow }
1207+
int_impl! { i8, i8, u8, 8 }
12141208
}
12151209

12161210
#[lang = "i16"]
12171211
impl i16 {
1218-
int_impl! { i16, i16, u16, 16,
1219-
intrinsics::add_with_overflow,
1220-
intrinsics::sub_with_overflow,
1221-
intrinsics::mul_with_overflow }
1212+
int_impl! { i16, i16, u16, 16 }
12221213
}
12231214

12241215
#[lang = "i32"]
12251216
impl i32 {
1226-
int_impl! { i32, i32, u32, 32,
1227-
intrinsics::add_with_overflow,
1228-
intrinsics::sub_with_overflow,
1229-
intrinsics::mul_with_overflow }
1217+
int_impl! { i32, i32, u32, 32 }
12301218
}
12311219

12321220
#[lang = "i64"]
12331221
impl i64 {
1234-
int_impl! { i64, i64, u64, 64,
1235-
intrinsics::add_with_overflow,
1236-
intrinsics::sub_with_overflow,
1237-
intrinsics::mul_with_overflow }
1222+
int_impl! { i64, i64, u64, 64 }
12381223
}
12391224

12401225
#[lang = "i128"]
12411226
impl i128 {
1242-
int_impl! { i128, i128, u128, 128,
1243-
intrinsics::add_with_overflow,
1244-
intrinsics::sub_with_overflow,
1245-
intrinsics::mul_with_overflow }
1227+
int_impl! { i128, i128, u128, 128 }
12461228
}
12471229

12481230
#[cfg(target_pointer_width = "16")]
12491231
#[lang = "isize"]
12501232
impl isize {
1251-
int_impl! { isize, i16, u16, 16,
1252-
intrinsics::add_with_overflow,
1253-
intrinsics::sub_with_overflow,
1254-
intrinsics::mul_with_overflow }
1233+
int_impl! { isize, i16, u16, 16 }
12551234
}
12561235

12571236
#[cfg(target_pointer_width = "32")]
12581237
#[lang = "isize"]
12591238
impl isize {
1260-
int_impl! { isize, i32, u32, 32,
1261-
intrinsics::add_with_overflow,
1262-
intrinsics::sub_with_overflow,
1263-
intrinsics::mul_with_overflow }
1239+
int_impl! { isize, i32, u32, 32 }
12641240
}
12651241

12661242
#[cfg(target_pointer_width = "64")]
12671243
#[lang = "isize"]
12681244
impl isize {
1269-
int_impl! { isize, i64, u64, 64,
1270-
intrinsics::add_with_overflow,
1271-
intrinsics::sub_with_overflow,
1272-
intrinsics::mul_with_overflow }
1245+
int_impl! { isize, i64, u64, 64 }
12731246
}
12741247

12751248
// `Int` + `UnsignedInt` implemented for unsigned integers
12761249
macro_rules! uint_impl {
1277-
($SelfT:ty, $ActualT:ty, $BITS:expr,
1278-
$ctpop:path,
1279-
$ctlz:path,
1280-
$ctlz_nonzero:path,
1281-
$cttz:path,
1282-
$bswap:path,
1283-
$add_with_overflow:path,
1284-
$sub_with_overflow:path,
1285-
$mul_with_overflow:path) => {
1250+
($SelfT:ty, $ActualT:ty, $BITS:expr) => {
12861251
/// Returns the smallest value that can be represented by this integer type.
12871252
///
12881253
/// # Examples
@@ -1346,7 +1311,7 @@ macro_rules! uint_impl {
13461311
#[stable(feature = "rust1", since = "1.0.0")]
13471312
#[inline]
13481313
pub fn count_ones(self) -> u32 {
1349-
unsafe { $ctpop(self as $ActualT) as u32 }
1314+
unsafe { intrinsics::ctpop(self as $ActualT) as u32 }
13501315
}
13511316

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

13871352
/// Returns the number of trailing zeros in the binary representation
@@ -1480,7 +1445,7 @@ macro_rules! uint_impl {
14801445
#[stable(feature = "rust1", since = "1.0.0")]
14811446
#[inline]
14821447
pub fn swap_bytes(self) -> Self {
1483-
unsafe { $bswap(self as $ActualT) as Self }
1448+
unsafe { intrinsics::bswap(self as $ActualT) as Self }
14841449
}
14851450

14861451
/// Converts an integer from big endian to the target's endianness.
@@ -1984,11 +1949,11 @@ macro_rules! uint_impl {
19841949
#[inline]
19851950
#[stable(feature = "wrapping", since = "1.7.0")]
19861951
pub fn overflowing_add(self, rhs: Self) -> (Self, bool) {
1987-
unsafe {
1988-
let (a, b) = $add_with_overflow(self as $ActualT,
1989-
rhs as $ActualT);
1990-
(a as Self, b)
1991-
}
1952+
let (a, b) = unsafe {
1953+
intrinsics::add_with_overflow(self as $ActualT,
1954+
rhs as $ActualT)
1955+
};
1956+
(a as Self, b)
19921957
}
19931958

19941959
/// Calculates `self` - `rhs`
@@ -2010,11 +1975,11 @@ macro_rules! uint_impl {
20101975
#[inline]
20111976
#[stable(feature = "wrapping", since = "1.7.0")]
20121977
pub fn overflowing_sub(self, rhs: Self) -> (Self, bool) {
2013-
unsafe {
2014-
let (a, b) = $sub_with_overflow(self as $ActualT,
2015-
rhs as $ActualT);
2016-
(a as Self, b)
2017-
}
1978+
let (a, b) = unsafe {
1979+
intrinsics::sub_with_overflow(self as $ActualT,
1980+
rhs as $ActualT)
1981+
};
1982+
(a as Self, b)
20181983
}
20191984

20201985
/// Calculates the multiplication of `self` and `rhs`.
@@ -2034,11 +1999,11 @@ macro_rules! uint_impl {
20341999
#[inline]
20352000
#[stable(feature = "wrapping", since = "1.7.0")]
20362001
pub fn overflowing_mul(self, rhs: Self) -> (Self, bool) {
2037-
unsafe {
2038-
let (a, b) = $mul_with_overflow(self as $ActualT,
2039-
rhs as $ActualT);
2040-
(a as Self, b)
2041-
}
2002+
let (a, b) = unsafe {
2003+
intrinsics::mul_with_overflow(self as $ActualT,
2004+
rhs as $ActualT)
2005+
};
2006+
(a as Self, b)
20422007
}
20432008

20442009
/// Calculates the divisor when `self` is divided by `rhs`.
@@ -2223,7 +2188,7 @@ macro_rules! uint_impl {
22232188
// (such as intel pre-haswell) have more efficient ctlz
22242189
// intrinsics when the argument is non-zero.
22252190
let p = self - 1;
2226-
let z = unsafe { $ctlz_nonzero(p) };
2191+
let z = unsafe { intrinsics::ctlz_nonzero(p) };
22272192
<$SelfT>::max_value() >> z
22282193
}
22292194

@@ -2270,15 +2235,7 @@ macro_rules! uint_impl {
22702235

22712236
#[lang = "u8"]
22722237
impl u8 {
2273-
uint_impl! { u8, u8, 8,
2274-
intrinsics::ctpop,
2275-
intrinsics::ctlz,
2276-
intrinsics::ctlz_nonzero,
2277-
intrinsics::cttz,
2278-
intrinsics::bswap,
2279-
intrinsics::add_with_overflow,
2280-
intrinsics::sub_with_overflow,
2281-
intrinsics::mul_with_overflow }
2238+
uint_impl! { u8, u8, 8 }
22822239

22832240

22842241
/// Checks if the value is within the ASCII range.
@@ -2824,95 +2781,39 @@ impl u8 {
28242781

28252782
#[lang = "u16"]
28262783
impl u16 {
2827-
uint_impl! { u16, u16, 16,
2828-
intrinsics::ctpop,
2829-
intrinsics::ctlz,
2830-
intrinsics::ctlz_nonzero,
2831-
intrinsics::cttz,
2832-
intrinsics::bswap,
2833-
intrinsics::add_with_overflow,
2834-
intrinsics::sub_with_overflow,
2835-
intrinsics::mul_with_overflow }
2784+
uint_impl! { u16, u16, 16 }
28362785
}
28372786

28382787
#[lang = "u32"]
28392788
impl u32 {
2840-
uint_impl! { u32, u32, 32,
2841-
intrinsics::ctpop,
2842-
intrinsics::ctlz,
2843-
intrinsics::ctlz_nonzero,
2844-
intrinsics::cttz,
2845-
intrinsics::bswap,
2846-
intrinsics::add_with_overflow,
2847-
intrinsics::sub_with_overflow,
2848-
intrinsics::mul_with_overflow }
2789+
uint_impl! { u32, u32, 32 }
28492790
}
28502791

28512792
#[lang = "u64"]
28522793
impl u64 {
2853-
uint_impl! { u64, u64, 64,
2854-
intrinsics::ctpop,
2855-
intrinsics::ctlz,
2856-
intrinsics::ctlz_nonzero,
2857-
intrinsics::cttz,
2858-
intrinsics::bswap,
2859-
intrinsics::add_with_overflow,
2860-
intrinsics::sub_with_overflow,
2861-
intrinsics::mul_with_overflow }
2794+
uint_impl! { u64, u64, 64 }
28622795
}
28632796

28642797
#[lang = "u128"]
28652798
impl u128 {
2866-
uint_impl! { u128, u128, 128,
2867-
intrinsics::ctpop,
2868-
intrinsics::ctlz,
2869-
intrinsics::ctlz_nonzero,
2870-
intrinsics::cttz,
2871-
intrinsics::bswap,
2872-
intrinsics::add_with_overflow,
2873-
intrinsics::sub_with_overflow,
2874-
intrinsics::mul_with_overflow }
2799+
uint_impl! { u128, u128, 128 }
28752800
}
28762801

28772802
#[cfg(target_pointer_width = "16")]
28782803
#[lang = "usize"]
28792804
impl usize {
2880-
uint_impl! { usize, u16, 16,
2881-
intrinsics::ctpop,
2882-
intrinsics::ctlz,
2883-
intrinsics::ctlz_nonzero,
2884-
intrinsics::cttz,
2885-
intrinsics::bswap,
2886-
intrinsics::add_with_overflow,
2887-
intrinsics::sub_with_overflow,
2888-
intrinsics::mul_with_overflow }
2805+
uint_impl! { usize, u16, 16 }
28892806
}
28902807
#[cfg(target_pointer_width = "32")]
28912808
#[lang = "usize"]
28922809
impl usize {
2893-
uint_impl! { usize, u32, 32,
2894-
intrinsics::ctpop,
2895-
intrinsics::ctlz,
2896-
intrinsics::ctlz_nonzero,
2897-
intrinsics::cttz,
2898-
intrinsics::bswap,
2899-
intrinsics::add_with_overflow,
2900-
intrinsics::sub_with_overflow,
2901-
intrinsics::mul_with_overflow }
2810+
uint_impl! { usize, u32, 32 }
29022811
}
29032812

29042813
#[cfg(target_pointer_width = "64")]
29052814
#[lang = "usize"]
29062815
impl usize {
2907-
uint_impl! { usize, u64, 64,
2908-
intrinsics::ctpop,
2909-
intrinsics::ctlz,
2910-
intrinsics::ctlz_nonzero,
2911-
intrinsics::cttz,
2912-
intrinsics::bswap,
2913-
intrinsics::add_with_overflow,
2914-
intrinsics::sub_with_overflow,
2915-
intrinsics::mul_with_overflow }
2816+
uint_impl! { usize, u64, 64 }
29162817
}
29172818

29182819
/// A classification of floating point numbers.

0 commit comments

Comments
 (0)