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

Simplify implementation of align_offset slightly #54534

Merged
merged 1 commit into from
Oct 12, 2018

Conversation

nagisa
Copy link
Member

@nagisa nagisa commented Sep 24, 2018

No description provided.

@rust-highfive
Copy link
Collaborator

r? @shepmaster

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 24, 2018
@shepmaster
Copy link
Member

r? @SimonSapin

@SimonSapin
Copy link
Contributor

I honestly have a hard time following the math in this code. Unfortunately it looks like the reviewers of the previous version in #50319 did too :/

@nagisa
Copy link
Member Author

nagisa commented Sep 25, 2018

I agree that the maths here are fairly complicated. It is fairly easy, however, to see that this PR keeps the code equivalence.

First, consider that the branch removed is if gcd == 1 { ... }. The branch after that, if (p as usize & (gcd - 1) == 0) { … }, if gcd == 1, simplifies as such: if p & 0 == 0 { ... } and thus will be taken whenever the original branch would have been.

To prove the equivalence between the branch bodies consider that gcd = 1 << gcdpow and for it to be 1, gcdpow must be 0.

In that case:

return intrinsics::unchecked_rem(a.wrapping_sub(pmoda).wrapping_mul(mod_inv(smoda, a)), a);

and

let j = a.wrapping_sub(pmoda) >> gcdpow; // same as `a.wrapping_sub(pmoda)`
let k = smoda >> gcdpow; // same as `smoda`
return intrinsics::unchecked_rem(j.wrapping_mul(mod_inv(k, a)), a >> gcdpow); 

are exactly the same.


The rest of the changes are documentation tweaks which I’ve figured as part of writing a blog post about some of the operations involved here (which I’m writing specifically with an intention to make the code here clearer). I’m willing to keep this PR open until I publish the post, if that makes it easier for your conscience to approve this PR.

@nagisa
Copy link
Member Author

nagisa commented Sep 30, 2018

I blogged about the implementation. Hope that makes it easier to review this.

@TimNN
Copy link
Contributor

TimNN commented Oct 9, 2018

Ping from triage @SimonSapin / @rust-lang/libs: This PR requires your review.

@alexcrichton
Copy link
Member

I, like @SimonSapin, have no idea what these functions are doing and find it pretty hard to follow...

This is a lang item though and doesn't really require libs team review, it seems like it's just an implementation detail. @nagisa do you know someone else who's familiar with this who can review?

@nagisa
Copy link
Member Author

nagisa commented Oct 10, 2018

The only reason this is a language item is to make miri understand what kind of function this is (since miri has to handle raw pointers specially). Otherwise, it is just an implementation detail for the currently unstable library function pointer::align_offset, and, I believe, should fall into @rust-lang/libs purview.

Admittedly, this change is so minor that I’d be fine with not landing this if it ends up requiring somebody to spend a significant time in order to understand and review this code. I’ll leave the decision to close this PR up to you.

do you know someone else who's familiar with this who can review?

Alas, the bus factor for this implementation is 2 (me and the person on IRC who collaborated with me in implementing this and whose nick I’ve forgotten by now). I had hoped to rectify this to some extent with my blog post (which I’ve linked in one of the comments above). If you are interested in raising the bus factor (I’m sure all of you have better things to do), I’d be willing to answer any questions as they come up.

Yet, I’d like to point out that understanding the whole algorithm is not necessary to review this PR. I already wrote about what the essence of this specific pull request does here.

I’d like to also point out that this algorithm is tested by a fairly exhaustive test suite. These tests were the basis for accepting the initial code. I’m very confident that these tests would have caught any issues with the changes I’ve made in this PR.

@alexcrichton
Copy link
Member

Ok well it sounds like you're pretty confident in these changes as well as the test coverage, and if it's primarily for an unstable API then it shouldn't be harmful to add in as well.

@bors: r+

@bors
Copy link
Contributor

bors commented Oct 11, 2018

📌 Commit 0b3e5eb has been approved by alexcrichton

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 11, 2018
@RalfJung
Copy link
Member

That's a nice blog post! Might be worth adding a link to it from the code? (Keep in mind though that the post might disappear before the code does.)

@bors
Copy link
Contributor

bors commented Oct 12, 2018

⌛ Testing commit 0b3e5eb with merge 013be4d6d216bfafbd2a8b337ce5a60872a6a8dd...

@bors
Copy link
Contributor

bors commented Oct 12, 2018

💔 Test failed - status-travis

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 12, 2018
@rust-highfive
Copy link
Collaborator

Your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
  Downloading https://files.pythonhosted.org/packages/ac/12/38a00649e6d56d80aeee31dd42343d0795da92a7862b1af7b275f8979613/awscli-1.16.32-py2.py3-none-any.whl (1.4MB)
    0% |▎                               | 10kB 11.7MB/s eta 0:00:01
    1% |▌                               | 20kB 1.8MB/s eta 0:00:01
    2% |▊                               | 30kB 2.2MB/s eta 0:00:01
    3% |█                               | 40kB 2.0MB/s eta 0:00:01
---
travis_time:end:2c62d86e:start=1539304219357809453,finish=1539304219368295719,duration=10486266
travis_fold:end:after_failure.3
travis_fold:start:after_failure.4
travis_time:start:1aba0fd0
$ ln -s . checkout && for CORE in obj/cores/core.*; do EXE=$(echo $CORE | sed 's|obj/cores/core\.[0-9]*\.!checkout!\(.*\)|\1|;y|!|/|'); if [ -f "$EXE" ]; then printf travis_fold":start:crashlog\n\033[31;1m%s\033[0m\n" "$CORE"; gdb --batch -q -c "$CORE" "$EXE" -iex 'set auto-load off' -iex 'dir src/' -iex 'set sysroot .' -ex bt -ex q; echo travis_fold":"end:crashlog; fi; done || true
travis_fold:end:after_failure.4
travis_fold:start:after_failure.5
travis_time:start:039f24f3
travis_time:start:039f24f3
$ cat ./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers || true
cat: ./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers: No such file or directory
travis_fold:end:after_failure.5
travis_fold:start:after_failure.6
travis_time:start:0ab62399
$ dmesg | grep -i kill

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

@RalfJung
Copy link
Member

Looks very spurious to me.

@bors retry

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 12, 2018
@bors
Copy link
Contributor

bors commented Oct 12, 2018

⌛ Testing commit 0b3e5eb with merge 8dc554a...

bors added a commit that referenced this pull request Oct 12, 2018
…hton

Simplify implementation of align_offset slightly
@bors
Copy link
Contributor

bors commented Oct 12, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: alexcrichton
Pushing 8dc554a to master...

@bors bors merged commit 0b3e5eb into rust-lang:master Oct 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants