Skip to content

Fix rotate_{left, right} for multiple of bitsize rotation amounts #15324

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

Merged
merged 1 commit into from
Jul 3, 2014
Merged

Fix rotate_{left, right} for multiple of bitsize rotation amounts #15324

merged 1 commit into from
Jul 3, 2014

Conversation

sneves
Copy link
Contributor

@sneves sneves commented Jul 2, 2014

The current implementation of rotate_left and rotate_right are incorrect when the rotation amount is 0, or a multiple of the input's bitsize. When n = 0, the expression

(self >> n) | (self << ($BITS - n))

results in a shift left by $BITS bits, which is undefined behavior (see #10183), and currently results in a hardcoded -1 value, instead of the original input value. Reducing ($BITS - n) modulo $BITS, simplified to (-n % $BITS), fixes this problem.

@sfackler
Copy link
Member

sfackler commented Jul 2, 2014

Could you add tests for this as well? They should go in libcoretest and there's some macro infrastructure in there to generate cases for all of the integer types.

@sneves
Copy link
Contributor Author

sneves commented Jul 2, 2014

I believe these should cover it.

@sneves
Copy link
Contributor Author

sneves commented Jul 2, 2014

Replaced unsigned negation by subtraction, to appease the compiler.

@alexcrichton
Copy link
Member

Nice catch! Could you squash the commits together as well?

@sneves
Copy link
Contributor Author

sneves commented Jul 2, 2014

OK, done.

bors added a commit that referenced this pull request Jul 3, 2014
The current implementation of `rotate_left` and `rotate_right` are incorrect when the rotation amount is 0, or a multiple of the input's bitsize. When `n = 0`, the expression

    (self >> n) | (self << ($BITS - n))

results in a shift left by `$BITS` bits, which is undefined behavior (see #10183), and currently results in a hardcoded `-1` value, instead of the original input value. Reducing `($BITS - n)` modulo `$BITS`, simplified to `(-n % $BITS)`, fixes this problem.
@bors bors closed this Jul 3, 2014
@bors bors merged commit c0248c0 into rust-lang:master Jul 3, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants