Skip to content

Tracking Issue for unbounded_shifts #129375

Closed

Description

@chorman0773

Feature gate: #![feature(unbounded_shifts)]

This is a tracking issue for ACP 428.

This adds shifts functions to the integer types that ignore type bounds when shifting, with out of range values for the rhs yielding the result of shifting the entire value out (0 for left shifts, and unsigned right shifts, and either 0 or -1 for signed right shifts).

Public API

impl uN{
     pub const fn unbounded_shl(self, rhs: u32) -> Self;
     pub const fn unbounded_shr(self, rhs: u32) -> Self;
}

impl iN{
    pub const fn unbounded_shl(self, rhs: u32) -> Self;
    pub const fn unbounded_shr(self, rhs: u32) -> Self;
}

Steps / History

Unresolved Questions

  • None yet.

Footnotes

  1. https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html ↩

Activity

  1. added
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    T-libs-api[DEPRECATED; DO NOT USE]
    on Aug 21, 2024
  2. added a commit that references this issue on Aug 26, 2024
  3. added a commit that references this issue on Aug 26, 2024
  4. joshtriplett commented on Nov 12, 2024

    @joshtriplett
    Member

    This seems ready to stabilize.

    @rfcbot merge

  5. rfcbot commented on Nov 12, 2024

    @rfcbot

    Team member @joshtriplett has proposed to merge this. The next step is review by the rest of the tagged team members:

    No concerns currently listed.

    Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

    See this document for info about what commands tagged team members can give me.

  6. added
    proposed-final-comment-periodProposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off.
    disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.
    on Nov 12, 2024
  7. chorman0773 commented on Nov 12, 2024

    @chorman0773
    ContributorAuthor

    I was going to do a stabilization report.

    Just to be clear, my intention was to stabilize it both as a function and as a const fn in one shot, so team should keep that in mind during the pFCP.

  8. chorman0773 commented on Dec 5, 2024

    @chorman0773
    ContributorAuthor

    Stabilization Report

    (Retroactive to pFCP)

    Implementation History

    Api Summary

    impl uN{
         pub const fn unbounded_shl(self, rhs: u32) -> Self;
         pub const fn unbounded_shr(self, rhs: u32) -> Self;
    }
    
    impl iN{
        pub const fn unbounded_shl(self, rhs: u32) -> Self;
        pub const fn unbounded_shr(self, rhs: u32) -> Self;
    }
    

    unbounded_shl and unbounded_shr shifts self left or right by rhs as though with unbounded range. If rhs exceeds the bounds of the type, the result is:

    • For unbounded_shl, 0 always
    • For unbounded_shr, 0 for unsigned integers and either 0 or -1 (depending on the sign bit)

    Experience Report

    The Clever-ISA Project Emulator uses the nightly version of these functions to implement same-named functions in a primitive type wrapper. The wrapper functions (particularily unbounded_shr) are then used throughout the emulator to compute a number of different bitmasks.

  9. scottmcm commented on Jan 7, 2025

    @scottmcm
    Member

    Big fan of stabilizing a canonical way to write "shift that actually works" 👍

    (These are the only shifts where shifting by one N times is the same as shifting by N one time.)

  10. added
    final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.
    and removed
    proposed-final-comment-periodProposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off.
    on Feb 11, 2025
  11. rfcbot commented on Feb 11, 2025

    @rfcbot

    🔔 This is now entering its final comment period, as per the review above. 🔔

  12. 1 remaining item

  13. rfcbot commented on Feb 21, 2025

    @rfcbot

    The final comment period, with a disposition to merge, as per the review above, is now complete.

    As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

    This will be merged soon.

  14. added 2 commits that reference this issue on Feb 24, 2025
  15. added a commit that references this issue on Feb 24, 2025
  16. added 2 commits that reference this issue on Mar 11, 2025
  17. added this to the 1.87.0 milestone on Mar 21, 2025
  18. frozolotl commented on May 15, 2025

    @frozolotl

    This issue can be closed, no?

  19. added a commit that references this issue on May 19, 2025
  20. kupiakos commented on Jul 11, 2025

    @kupiakos
    Contributor

    I didn't see saturating_shl/saturating_shr considered among the names. When one writes lhs.saturating_add(rhs), the rhs is unbounded in the same manner - if the result of the operation would be an error, the value of rhs is reduced to its largest value such that an error does not occur. The fully saturated result of shl/shr is 0/!0, like how it's MIN/MAX for overflow. I think it's at least as appropriate a name as wrapping_shl/wrapping_shr.

    • Is "saturating" an appropriate alternative name for "unbounded" here?
    • Considering this is stabilized, is there any value in adding saturating_shl/saturating_shr methods to integers? It'd quickly deprecate unbounded_, which wouldn't be great.
    • Could Shl<usize>/Shr<usize> be implemented on Saturating<T> that is backed by unbounded_shl/unbounded_shr, to mirror impl Shl<usize>/Shr<usize> for Wrapping<T> which forwards to wrapping_?
  21. chorman0773 commented on Jul 11, 2025

    @chorman0773
    ContributorAuthor

    saturating_sh{l,r} were considered offline (also ACP 230 discusses them, but wasn't related to the original ACP). The reason the name wasn't initially proposed is because it's unclear whether it refers to the result or the input. A reasonable behaviour for 1u32.saturating_shl(32) is to yield the value !0 (which is what ACP 230 proposed, incidentally). Saying unbounded is more clear, IMO. One thing that also stands out for unbounded_sh{l,r} is that a valid impl is:

    pub const fn unbounded_shl(mut self, shift: u32) -> Self {
         // pretend `for` loops are const. You can do a while loop instead, this is just visually cleaner
         for _ in 0..shift {
             self <<= 1;
         }
    }
    // Same for `unbounded_shr` and `>>`

    This is not the case for saturating_add et. al.

    Edit; Also looks like 230 was accepted with the behaviour as specified, so there would be both behaviours available, I guess.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-api[DEPRECATED; DO NOT USE]disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions