@@ -1312,6 +1312,33 @@ macro_rules! int_impl {
1312
1312
}
1313
1313
}
1314
1314
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
+
1315
1342
/// Checked shift right. Computes `self >> rhs`, returning `None` if `rhs` is
1316
1343
/// larger than or equal to the number of bits in `self`.
1317
1344
///
@@ -1410,6 +1437,39 @@ macro_rules! int_impl {
1410
1437
}
1411
1438
}
1412
1439
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
+
1413
1473
/// Checked absolute value. Computes `self.abs()`, returning `None` if
1414
1474
/// `self == MIN`.
1415
1475
///
0 commit comments