Skip to content

Commit 812137c

Browse files
committed
Update wrapping_sh[lr] docs and examples
1 parent ba2142a commit 812137c

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

library/core/src/num/int_macros.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,6 +2288,13 @@ macro_rules! int_impl {
22882288
/// Panic-free bitwise shift-left; yields `self << mask(rhs)`, where `mask` removes
22892289
/// any high-order bits of `rhs` that would cause the shift to exceed the bitwidth of the type.
22902290
///
2291+
/// Beware that, unlike most other `wrapping_*` methods on integers, this
2292+
/// does *not* give the same result as doing the shift in infinite precision
2293+
/// then truncating as needed. The behaviour matches what shift instructions
2294+
/// do on many processors, and is what the `<<` operator does when overflow
2295+
/// checks are disabled, but numerically it's weird. Consider, instead,
2296+
/// using [`Self::unbounded_shl`] which has nicer behaviour.
2297+
///
22912298
/// Note that this is *not* the same as a rotate-left; the RHS of a wrapping shift-left is restricted to
22922299
/// the range of the type, rather than the bits shifted out of the LHS being returned to the other end.
22932300
/// The primitive integer types all implement a [`rotate_left`](Self::rotate_left) function,
@@ -2296,8 +2303,10 @@ macro_rules! int_impl {
22962303
/// # Examples
22972304
///
22982305
/// ```
2299-
#[doc = concat!("assert_eq!((-1", stringify!($SelfT), ").wrapping_shl(7), -128);")]
2300-
#[doc = concat!("assert_eq!((-1", stringify!($SelfT), ").wrapping_shl(128), -1);")]
2306+
#[doc = concat!("assert_eq!((-1_", stringify!($SelfT), ").wrapping_shl(7), -128);")]
2307+
#[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".wrapping_shl(", stringify!($SelfT), "::BITS), 42);")]
2308+
#[doc = concat!("assert_eq!((-1_", stringify!($SelfT), ").wrapping_shl(128), -1);")]
2309+
#[doc = concat!("assert_eq!(5_", stringify!($SelfT), ".wrapping_shl(1025), 10);")]
23012310
/// ```
23022311
#[stable(feature = "num_wrapping", since = "1.2.0")]
23032312
#[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
@@ -2315,6 +2324,13 @@ macro_rules! int_impl {
23152324
/// Panic-free bitwise shift-right; yields `self >> mask(rhs)`, where `mask`
23162325
/// removes any high-order bits of `rhs` that would cause the shift to exceed the bitwidth of the type.
23172326
///
2327+
/// Beware that, unlike most other `wrapping_*` methods on integers, this
2328+
/// does *not* give the same result as doing the shift in infinite precision
2329+
/// then truncating as needed. The behaviour matches what shift instructions
2330+
/// do on many processors, and is what the `>>` operator does when overflow
2331+
/// checks are disabled, but numerically it's weird. Consider, instead,
2332+
/// using [`Self::unbounded_shr`] which has nicer behaviour.
2333+
///
23182334
/// Note that this is *not* the same as a rotate-right; the RHS of a wrapping shift-right is restricted
23192335
/// to the range of the type, rather than the bits shifted out of the LHS being returned to the other
23202336
/// end. The primitive integer types all implement a [`rotate_right`](Self::rotate_right) function,
@@ -2323,8 +2339,10 @@ macro_rules! int_impl {
23232339
/// # Examples
23242340
///
23252341
/// ```
2326-
#[doc = concat!("assert_eq!((-128", stringify!($SelfT), ").wrapping_shr(7), -1);")]
2327-
/// assert_eq!((-128i16).wrapping_shr(64), -128);
2342+
#[doc = concat!("assert_eq!((-128_", stringify!($SelfT), ").wrapping_shr(7), -1);")]
2343+
#[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".wrapping_shr(", stringify!($SelfT), "::BITS), 42);")]
2344+
/// assert_eq!((-128_i16).wrapping_shr(64), -128);
2345+
#[doc = concat!("assert_eq!(10_", stringify!($SelfT), ".wrapping_shr(1025), 5);")]
23282346
/// ```
23292347
#[stable(feature = "num_wrapping", since = "1.2.0")]
23302348
#[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]

library/core/src/num/uint_macros.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2593,6 +2593,13 @@ macro_rules! uint_impl {
25932593
/// where `mask` removes any high-order bits of `rhs` that
25942594
/// would cause the shift to exceed the bitwidth of the type.
25952595
///
2596+
/// Beware that, unlike most other `wrapping_*` methods on integers, this
2597+
/// does *not* give the same result as doing the shift in infinite precision
2598+
/// then truncating as needed. The behaviour matches what shift instructions
2599+
/// do on many processors, and is what the `<<` operator does when overflow
2600+
/// checks are disabled, but numerically it's weird. Consider, instead,
2601+
/// using [`Self::unbounded_shl`] which has nicer behaviour.
2602+
///
25962603
/// Note that this is *not* the same as a rotate-left; the
25972604
/// RHS of a wrapping shift-left is restricted to the range
25982605
/// of the type, rather than the bits shifted out of the LHS
@@ -2603,8 +2610,16 @@ macro_rules! uint_impl {
26032610
/// # Examples
26042611
///
26052612
/// ```
2606-
#[doc = concat!("assert_eq!(1", stringify!($SelfT), ".wrapping_shl(7), 128);")]
2607-
#[doc = concat!("assert_eq!(1", stringify!($SelfT), ".wrapping_shl(128), 1);")]
2613+
#[doc = concat!("assert_eq!(1_", stringify!($SelfT), ".wrapping_shl(7), 128);")]
2614+
#[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".wrapping_shl(", stringify!($SelfT), "::BITS), 42);")]
2615+
/// assert_eq!(
2616+
#[doc = concat!(" 42_", stringify!($SelfT))]
2617+
#[doc = concat!(" .wrapping_shl(", stringify!($SelfT), "::BITS / 2)")]
2618+
#[doc = concat!(" .wrapping_shl(", stringify!($SelfT), "::BITS / 2),")]
2619+
/// 0,
2620+
/// );
2621+
#[doc = concat!("assert_eq!(1_", stringify!($SelfT), ".wrapping_shl(128), 1);")]
2622+
#[doc = concat!("assert_eq!(5_", stringify!($SelfT), ".wrapping_shl(1025), 10);")]
26082623
/// ```
26092624
#[stable(feature = "num_wrapping", since = "1.2.0")]
26102625
#[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]
@@ -2623,6 +2638,13 @@ macro_rules! uint_impl {
26232638
/// where `mask` removes any high-order bits of `rhs` that
26242639
/// would cause the shift to exceed the bitwidth of the type.
26252640
///
2641+
/// Beware that, unlike most other `wrapping_*` methods on integers, this
2642+
/// does *not* give the same result as doing the shift in infinite precision
2643+
/// then truncating as needed. The behaviour matches what shift instructions
2644+
/// do on many processors, and is what the `>>` operator does when overflow
2645+
/// checks are disabled, but numerically it's weird. Consider, instead,
2646+
/// using [`Self::unbounded_shr`] which has nicer behaviour.
2647+
///
26262648
/// Note that this is *not* the same as a rotate-right; the
26272649
/// RHS of a wrapping shift-right is restricted to the range
26282650
/// of the type, rather than the bits shifted out of the LHS
@@ -2633,8 +2655,10 @@ macro_rules! uint_impl {
26332655
/// # Examples
26342656
///
26352657
/// ```
2636-
#[doc = concat!("assert_eq!(128", stringify!($SelfT), ".wrapping_shr(7), 1);")]
2637-
#[doc = concat!("assert_eq!(128", stringify!($SelfT), ".wrapping_shr(128), 128);")]
2658+
#[doc = concat!("assert_eq!(128_", stringify!($SelfT), ".wrapping_shr(7), 1);")]
2659+
#[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".wrapping_shr(", stringify!($SelfT), "::BITS), 42);")]
2660+
#[doc = concat!("assert_eq!(128_", stringify!($SelfT), ".wrapping_shr(128), 128);")]
2661+
#[doc = concat!("assert_eq!(10_", stringify!($SelfT), ".wrapping_shr(1025), 5);")]
26382662
/// ```
26392663
#[stable(feature = "num_wrapping", since = "1.2.0")]
26402664
#[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]

0 commit comments

Comments
 (0)