-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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 fn
s
#51171
Conversation
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. |
Eventually I'd like log/sin/cos too const. |
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.
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), |
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.
@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
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.
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()
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 agree the code can probably be written a bit nicer. Looking into it. Will push as soon as I have the time.
@leonardo-m I suggest you open a PR for those intrinsics then ;) |
We can't do that because we don't have an soft-float ( |
@bors r+ |
📌 Commit 3fefa96 has been approved by |
🔒 Merge conflict |
@@ -596,34 +596,17 @@ fn numeric_intrinsic<'tcx>( | |||
bytes: u128, |
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.
If anything, I'd call these bits
, not bytes
.
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.
Ok. I agree. Matches better with the field same in Scalar
as well. Pushed a rename.
3fefa96
to
0d7d5e9
Compare
Fixed the merge conflict by rebasing. |
@bors r+ |
📌 Commit 97a0d46 has been approved by |
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
☀️ Test successful - status-appveyor, status-travis |
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.
Making some rustc intrinsics (
ctpop
,cttz
,ctlz
andbswap
)const fn
s.This is a pre-step to being able to make
swap_bytes
,to_be
andfrom_be
constant functions. That in itself could be ergonomic and useful. But even better is that it would allowIpv4Addr::new
etc becomingconst 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