modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo/manual/
Other
23.32k stars 2.59k forks source link

[Feature Request] Introduce `rotate_bits_[left|right]` for dynamic shifts #3657

Open soraros opened 1 month ago

soraros commented 1 month ago

Review Mojo's priorities

What is your request?

As title. They currently have signature

fn rotate_bits_right[shift: Int](x: T) -> T

Let's add

fn rotate_bits_right(x: T, shift: T) -> T

What is your motivation for this change?

So people don't hand roll their own implementation and end up causing UB like in #3652.

Any other details?

N/A

soraros commented 1 month ago

We should probably remove support for negative shift as well, so the implementation aligns with the docstring.

CC: @JoeLoser, as I recall you mentioning something similar in a previous PR.