@@ -1312,6 +1312,33 @@ macro_rules! int_impl {
13121312 }
13131313 }
13141314
1315+ /// Unbounded shift left. Computes `self << rhs`, without bounding the value of `rhs`
1316+ ///
1317+ /// If `rhs` is larger or equal to the number of bits in `self`,
1318+ /// the entire value is shifted out, and `0` is returned.
1319+ ///
1320+ /// # Examples
1321+ ///
1322+ /// Basic usage:
1323+ /// ```
1324+ #[ doc = concat!( "assert_eq!(0x1" , stringify!( $SelfT) , ".unbounded_shl(4), 0x10);" ) ]
1325+ #[ doc = concat!( "assert_eq!(0x1" , stringify!( $SelfT) , ".unbounded_shl(129), 0);" ) ]
1326+ /// ```
1327+ #[ unstable( feature = "unbounded_shifts" , issue = "129375" ) ]
1328+ #[ rustc_allow_const_fn_unstable( unchecked_shifts) ]
1329+ #[ must_use = "this returns the result of the operation, \
1330+ without modifying the original"]
1331+ #[ inline]
1332+ pub const fn unbounded_shl( self , v: u32 ) -> $SelfT{
1333+ if v < Self :: BITS {
1334+ // SAFETY:
1335+ // v is just checked to be in-range above
1336+ unsafe { self . unchecked_shl( v) }
1337+ } else{
1338+ 0
1339+ }
1340+ }
1341+
13151342 /// Checked shift right. Computes `self >> rhs`, returning `None` if `rhs` is
13161343 /// larger than or equal to the number of bits in `self`.
13171344 ///
@@ -1410,6 +1437,39 @@ macro_rules! int_impl {
14101437 }
14111438 }
14121439
1440+ /// Unbounded shift right. Computes `self >> rhs`, without bounding the value of `rhs`
1441+ ///
1442+ /// If `rhs` is larger or equal to the number of bits in `self`,
1443+ /// the entire value is shifted out, which yields `0` for a positive number,
1444+ /// and `-1` for a negative number.
1445+ ///
1446+ /// # Examples
1447+ ///
1448+ /// Basic usage:
1449+ /// ```
1450+ #[ doc = concat!( "assert_eq!(0x10" , stringify!( $SelfT) , ".unbounded_shl(4), 0x1);" ) ]
1451+ #[ doc = concat!( "assert_eq!(0x10" , stringify!( $SelfT) , ".unbounded_shr(129), 0);" ) ]
1452+ #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::MIN.unbounded_shr(129), -1);" ) ]
1453+ /// ```
1454+ #[ unstable( feature = "unbounded_shifts" , issue = "129375" ) ]
1455+ #[ rustc_allow_const_fn_unstable( unchecked_shifts) ]
1456+ #[ must_use = "this returns the result of the operation, \
1457+ without modifying the original"]
1458+ #[ inline]
1459+ pub const fn unbounded_shr( self , v: u32 ) -> $SelfT{
1460+ if v < Self :: BITS {
1461+ // SAFETY:
1462+ // v is just checked to be in-range above
1463+ unsafe { self . unchecked_shr( v) }
1464+ } else{
1465+ // A shift by `Self::BITS-1` suffices for signed integers, because the sign bit is copied for each of the shifted bits.
1466+
1467+ // SAFETY:
1468+ // `Self::BITS-1` is guaranteed to be less than `Self::BITS`
1469+ unsafe { self . unchecked_shr( Self :: BITS - 1 ) }
1470+ }
1471+ }
1472+
14131473 /// Checked absolute value. Computes `self.abs()`, returning `None` if
14141474 /// `self == MIN`.
14151475 ///
0 commit comments