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

Make some std::intrinsics const fns #51171

Merged
merged 5 commits into from
Jun 1, 2018

Conversation

faern
Copy link
Contributor

@faern faern commented May 29, 2018

Making some rustc intrinsics (ctpop, cttz, ctlz and bswap) const fns.

This is a pre-step to being able to make swap_bytes, to_be and from_be constant functions. That in itself could be ergonomic and useful. But even better is that it would allow Ipv4Addr::new etc becoming const fns as well. Which might be really useful since I find it quite common to want to define them as constants.

r? @oli-obk

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @oli-obk (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.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 29, 2018
@leonardo-m
Copy link

Eventually I'd like log/sin/cos too const.

Copy link
Contributor

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are inherently unstable, so there's nothing wrong with making these const except if they were somehow exposed by macros.

($method:ident) => ({
use rustc_target::abi::Integer;
let (bits, defined) = match kind {
Primitive::Int(Integer::I8, true) => ((bytes as i8).$method() as u128, 8),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eddyb I know you dislike these kinds of matches. We could probably do all the ops on u128 and modify the result depending on the type's bitwidth, but that feels fragile

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let extra = 128 - bits;

  • ctpop(x) = x.count_ones()
  • ctlz(x) = x.leading_zeros() - extra
  • cttz(x) = (x << extra).trailing_zeros() - extra
  • bswap(x) = (x << extra).swap_bytes()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree the code can probably be written a bit nicer. Looking into it. Will push as soon as I have the time.

@oli-obk
Copy link
Contributor

oli-obk commented May 29, 2018

@leonardo-m I suggest you open a PR for those intrinsics then ;)

@eddyb
Copy link
Member

eddyb commented May 29, 2018

Eventually I'd like log/sin/cos too const.

We can't do that because we don't have an soft-float (rustc_apfloat) implementation of them.

@oli-obk
Copy link
Contributor

oli-obk commented May 31, 2018

@bors r+

@bors
Copy link
Contributor

bors commented May 31, 2018

📌 Commit 3fefa96 has been approved by oli-obk

@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 May 31, 2018
@bors
Copy link
Contributor

bors commented May 31, 2018

🔒 Merge conflict

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels May 31, 2018
@@ -596,34 +596,17 @@ fn numeric_intrinsic<'tcx>(
bytes: u128,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If anything, I'd call these bits, not bytes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I agree. Matches better with the field same in Scalar as well. Pushed a rename.

@faern faern force-pushed the const-bswap-ctpop-cttz-ctlz branch from 3fefa96 to 0d7d5e9 Compare May 31, 2018 17:19
@faern
Copy link
Contributor Author

faern commented May 31, 2018

Fixed the merge conflict by rebasing.

@oli-obk
Copy link
Contributor

oli-obk commented May 31, 2018

@bors r+

@bors
Copy link
Contributor

bors commented May 31, 2018

📌 Commit 97a0d46 has been approved by oli-obk

@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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 31, 2018
@bors
Copy link
Contributor

bors commented Jun 1, 2018

⌛ Testing commit 97a0d46 with merge 63cd4a3...

bors added a commit that referenced this pull request Jun 1, 2018
Make some std::intrinsics `const fn`s

Making some rustc intrinsics (`ctpop`, `cttz`, `ctlz` and `bswap`) `const fn`s.

This is a pre-step to being able to make `swap_bytes`, `to_be` and `from_be` constant functions. That in itself could be ergonomic and useful. But even better is that it would allow `Ipv4Addr::new` etc becoming `const fn`s as well. Which might be really useful since I find it quite common to want to define them as constants.

r? @oli-obk
@bors
Copy link
Contributor

bors commented Jun 1, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: oli-obk
Pushing 63cd4a3 to master...

@bors bors merged commit 97a0d46 into rust-lang:master Jun 1, 2018
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request Jun 4, 2018
const fn integer operations

A follow up to rust-lang#51171
Fixes rust-lang#51267

Makes a lot of the integer methods (`swap_bytes`, `count_ones` etc) `const fn`s. See rust-lang#51267 for a discussion about why this is wanted and the solution used.
@faern faern deleted the const-bswap-ctpop-cttz-ctlz branch January 31, 2019 19:50
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.

6 participants