Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: OverflowingShl #299

Open
tower120 opened this issue Nov 22, 2023 · 2 comments
Open

Feature request: OverflowingShl #299

tower120 opened this issue Nov 22, 2023 · 2 comments

Comments

@tower120
Copy link

tower120 commented Nov 22, 2023

I have need in overflowing_shl. Is there a reason, why there is no trait for it?

P.S. Well, actually I need something like saturating_shl, but since there is no such thing in Rust, overflowing_shl is the closest one.

@cuviper
Copy link
Member

cuviper commented Nov 28, 2023

For most such things, it's just because nobody needed it yet.

However, note that shifting is a bit weird compared to other checked/overflowing/wrapping ops, because overflowing_shl and overflowing_shr will overflow the shift value, not the result. Is that what you're actually looking for?

See also rust-lang/libs-team#230

@tower120
Copy link
Author

tower120 commented Nov 29, 2023

I am aware of overflowing_shl behavior in Rust. I personally need behavior, where shifting left too far results in zero (like in old x86 shl instructions).

And the thing is, this:

    let (res, ok) = 1usize.overflowing_shl(n);
    res * (!ok as usize)

is faster then this:

     1usize.checked_shl(n).unwrap_or(0)

https://rust.godbolt.org/z/3a6q5Wazf

UPDATE: there were error in example

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

No branches or pull requests

2 participants