-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
libcore: fix compilation on 16bit target (MSP430). #40832
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @alexcrichton (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for reporting the problem! I assume the issue is with shifting a usize
by 17 when usize
consists of 16 bits only.
Instead of fixing this by writing a custom fn break_patterns
under a #[cfg(...)]
, could you please just copy this implementation? This one shouldn't suffer from the problem as it shifts u64
s rather than usize
s.
@stjepang, but this change converts len to u64, and computes |
@pftbest Indeed - What do you think, does it look good now? |
I did some testing and on my machine the new version is 3 times slower than original:
I didn't test the Also this new version gives different results compared to original, and I don't know enough about the algorithm to confirm that it is OK and won't introduce any regressions. |
Interesting. Can you share the tests? I'd like to investigate why performance suffers and why the results are different. Btw, you can find me on IRC if you wish. |
@stjepang here is my crude test https://gist.github.com/pftbest/b15f39b866e70bd9f6ea0ed84aef9fb0 It is also possible to see that something is wrong just by looking at assembly code: |
That's totally okay, don't worry. So, the purpose of I slightly changed the algorithm so that it's more random. Now it picks three independent random indices instead of just one. Also, the range of indices is Yes, the function is slightly slower now, but we should be better off overall because produced indices are more random, which should speed up the sort by creating more balanced partitions. When benchmarking the whole sort algorithm, I don't see any difference in performance. This function is rarely called anyways, and all this fiddling with randomness is not too important... All that matters is that we swap the 3 indices in the middle with some other reasonably random indices. :) |
Select 3 random points instead of just 1. Also the code now compiles on 16bit architectures.
Updated PR to reflect changes from |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
📌 Commit fda8e15 has been approved by |
libcore: fix compilation on 16bit target (MSP430). Since PR rust-lang#40601 has been merged, libcore no longer compiles on MSP430. The reason is this code in `break_patterns`: ```rust let mut random = len; random ^= random << 13; random ^= random >> 17; random ^= random << 5; random &= modulus - 1; ``` It assumes that `len` is at least a 32 bit integer. As a workaround replace `break_patterns` with an empty function for 16bit targets. cc @stjepang cc @alexcrichton
src/libcore/slice/sort.rs
Outdated
// we first take it modulo a power of two, and then decrease by `len` until it fits | ||
// into the range `[0, len - 1]`. | ||
let mut other = gen_usize() & (modulus - 1); | ||
while other >= len { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stjepang Can't this just be a conditional instead of a loop? As it stands now, you're guaranteed other < 2*len
upon entering the loop, so the body runs either 0 or 1 times and no more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that is true. :) It can be a single conditional.
`other` is guaranteed to be less than `2 * len`.
I've replaced the loop with conditional, please review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I approve! :)
@bors r=stjepang |
📌 Commit b909364 has been approved by |
libcore: fix compilation on 16bit target (MSP430). Since PR rust-lang#40601 has been merged, libcore no longer compiles on MSP430. The reason is this code in `break_patterns`: ```rust let mut random = len; random ^= random << 13; random ^= random >> 17; random ^= random << 5; random &= modulus - 1; ``` It assumes that `len` is at least a 32 bit integer. As a workaround replace `break_patterns` with an empty function for 16bit targets. cc @stjepang cc @alexcrichton
Since PR #40601 has been merged, libcore no longer compiles on MSP430.
The reason is this code in
break_patterns
:It assumes that
len
is at least a 32 bit integer.As a workaround replace
break_patterns
with an empty function for 16bit targets.cc @stjepang
cc @alexcrichton