Skip to content

Commit 81078e4

Browse files
Rollup merge of rust-lang#143355 - hkBst:cleanup-shift-double-bitmask, r=Mark-Simulacrum
wrapping shift: remove first bitmask and table ```rust #[inline(always)] pub const fn wrapping_shl(self, rhs: u32) -> Self { // SAFETY: the masking by the bitsize of the type ensures that we do not shift // out of bounds unsafe { self.unchecked_shl(rhs & (Self::BITS - 1)) } } ``` already does the bitmask, so it seems unnecessary here. More context: internals.rust-lang.org/t/wrapping-shift-operator-code-doing-bitmasking-twice/23167
2 parents 0e1fe5a + f1b5a56 commit 81078e4

File tree

1 file changed

+6
-42
lines changed

1 file changed

+6
-42
lines changed

library/core/src/num/wrapping.rs

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ macro_rules! sh_impl_signed {
9494
#[inline]
9595
fn shl(self, other: $f) -> Wrapping<$t> {
9696
if other < 0 {
97-
Wrapping(self.0.wrapping_shr((-other & self::shift_max::$t as $f) as u32))
97+
Wrapping(self.0.wrapping_shr(-other as u32))
9898
} else {
99-
Wrapping(self.0.wrapping_shl((other & self::shift_max::$t as $f) as u32))
99+
Wrapping(self.0.wrapping_shl(other as u32))
100100
}
101101
}
102102
}
@@ -119,9 +119,9 @@ macro_rules! sh_impl_signed {
119119
#[inline]
120120
fn shr(self, other: $f) -> Wrapping<$t> {
121121
if other < 0 {
122-
Wrapping(self.0.wrapping_shl((-other & self::shift_max::$t as $f) as u32))
122+
Wrapping(self.0.wrapping_shl(-other as u32))
123123
} else {
124-
Wrapping(self.0.wrapping_shr((other & self::shift_max::$t as $f) as u32))
124+
Wrapping(self.0.wrapping_shr(other as u32))
125125
}
126126
}
127127
}
@@ -147,7 +147,7 @@ macro_rules! sh_impl_unsigned {
147147

148148
#[inline]
149149
fn shl(self, other: $f) -> Wrapping<$t> {
150-
Wrapping(self.0.wrapping_shl((other & self::shift_max::$t as $f) as u32))
150+
Wrapping(self.0.wrapping_shl(other as u32))
151151
}
152152
}
153153
forward_ref_binop! { impl Shl, shl for Wrapping<$t>, $f,
@@ -168,7 +168,7 @@ macro_rules! sh_impl_unsigned {
168168

169169
#[inline]
170170
fn shr(self, other: $f) -> Wrapping<$t> {
171-
Wrapping(self.0.wrapping_shr((other & self::shift_max::$t as $f) as u32))
171+
Wrapping(self.0.wrapping_shr(other as u32))
172172
}
173173
}
174174
forward_ref_binop! { impl Shr, shr for Wrapping<$t>, $f,
@@ -1052,39 +1052,3 @@ macro_rules! wrapping_int_impl_unsigned {
10521052
}
10531053

10541054
wrapping_int_impl_unsigned! { usize u8 u16 u32 u64 u128 }
1055-
1056-
mod shift_max {
1057-
#![allow(non_upper_case_globals)]
1058-
1059-
#[cfg(target_pointer_width = "16")]
1060-
mod platform {
1061-
pub(crate) const usize: u32 = super::u16;
1062-
pub(crate) const isize: u32 = super::i16;
1063-
}
1064-
1065-
#[cfg(target_pointer_width = "32")]
1066-
mod platform {
1067-
pub(crate) const usize: u32 = super::u32;
1068-
pub(crate) const isize: u32 = super::i32;
1069-
}
1070-
1071-
#[cfg(target_pointer_width = "64")]
1072-
mod platform {
1073-
pub(crate) const usize: u32 = super::u64;
1074-
pub(crate) const isize: u32 = super::i64;
1075-
}
1076-
1077-
pub(super) const i8: u32 = (1 << 3) - 1;
1078-
pub(super) const i16: u32 = (1 << 4) - 1;
1079-
pub(super) const i32: u32 = (1 << 5) - 1;
1080-
pub(super) const i64: u32 = (1 << 6) - 1;
1081-
pub(super) const i128: u32 = (1 << 7) - 1;
1082-
pub(super) use self::platform::isize;
1083-
1084-
pub(super) const u8: u32 = i8;
1085-
pub(super) const u16: u32 = i16;
1086-
pub(super) const u32: u32 = i32;
1087-
pub(super) const u64: u32 = i64;
1088-
pub(super) const u128: u32 = i128;
1089-
pub(super) use self::platform::usize;
1090-
}

0 commit comments

Comments
 (0)