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

Add functions for reversing the bit pattern in an integer #48573

Merged
merged 3 commits into from
Mar 6, 2018

Conversation

Amanieu
Copy link
Member

@Amanieu Amanieu commented Feb 27, 2018

I'm reviving PR #32798 now that the LLVM issues have been resolved.

This adds the bitreverse intrinsic and adds a reverse_bits function to all integer types.

@rust-highfive
Copy link
Collaborator

r? @sfackler

(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 Feb 27, 2018
@Amanieu Amanieu force-pushed the bitreverse2 branch 2 times, most recently from 9467b30 to 616ea71 Compare February 27, 2018 06:51
@pitdicker
Copy link
Contributor

Should this also support i128/u128/isize/usize?

#[cfg(not(stage0))]
#[inline]
pub fn reverse_bits(self) -> Self {
(self as $UnsignedT).swap_bytes() as Self
Copy link
Member

Choose a reason for hiding this comment

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

Should this be reverse_bits?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops.

@Amanieu
Copy link
Member Author

Amanieu commented Feb 27, 2018

Should this also support i128/u128/isize/usize?

Yes these should all be supported. (I fixed the missing intrinsic for i128)

/// # Examples
///
/// Please note that this example is shared between integer types.
/// Which explains why `i16` is used here.
Copy link
Member

Choose a reason for hiding this comment

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

@GuillaumeGomez @QuietMisdreavus I feel like I've recently heard something about us being able to generate dedicated examples with the right types for primitives...

Copy link
Member

Choose a reason for hiding this comment

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

Yep we did here.

Copy link
Member Author

Choose a reason for hiding this comment

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

I just copied the docs for swap_bytes and made minor adjustments.

Copy link
Member

Choose a reason for hiding this comment

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

Well, in this case it is very complicated to be able to write a fully generic example.

assert_eq!(bitreverse(0x0ABBCC0Eu32), 0x7033DD50);
assert_eq!(bitreverse(0x0ABBCC0Ei32), 0x7033DD50);
assert_eq!(bitreverse(0x0122334455667708u64), 0x10EE66AA22CC4480);
assert_eq!(bitreverse(0x0122334455667708i64), 0x10EE66AA22CC4480);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a test for u/i128? It would not surprise me at all if some LLVM backends have trouble with this intrinsic on 128 bit integers, so it would be good to have at least some test to demonstrate that it works at all. (And if it works at all, it's probably reasonably robust given how LLVM is structured.)

@Amanieu
Copy link
Member Author

Amanieu commented Feb 28, 2018

All comments have been addressed. I will create a tracking issue when this is approved.

@pietroalbini
Copy link
Member

Ping from the release team @sfackler! This PR needs your review.

@sfackler
Copy link
Member

sfackler commented Mar 5, 2018

There were some questions about use cases in the original PR - where would these functions be used?

@Amanieu
Copy link
Member Author

Amanieu commented Mar 5, 2018

I need these because I am emulating an architecture that has a bit reverse instruction. I would like to be able to use native bit-reverse instructions if they are available.

AFAIK LLVM won't optimize normal code into a native bit-reverse instruction, you need to use the intrinsic for that.

@sfackler
Copy link
Member

sfackler commented Mar 6, 2018

LGTM on the API side, but I'm not super familiar with trans - @alexcrichton does that side of things look okay to you?

@alexcrichton
Copy link
Member

Looks good! Want to open a tracking issue and this can be r+'d?

@Amanieu
Copy link
Member Author

Amanieu commented Mar 6, 2018

Tracking issue created: #48763

@sfackler
Copy link
Member

sfackler commented Mar 6, 2018

@bors r+

@bors
Copy link
Contributor

bors commented Mar 6, 2018

📌 Commit 88aec91 has been approved by sfackler

@bors bors removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 6, 2018
@bors bors added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Mar 6, 2018
kennytm added a commit to kennytm/rust that referenced this pull request Mar 6, 2018
Add functions for reversing the bit pattern in an integer

I'm reviving PR rust-lang#32798 now that the LLVM issues have been resolved.

> This adds the bitreverse intrinsic and adds a reverse_bits function to all integer types.
bors added a commit that referenced this pull request Mar 6, 2018
Rollup of 14 pull requests

- Successful merges: #48403, #48432, #48546, #48573, #48590, #48657, #48727, #48732, #48753, #48754, #48761, #48474, #48507, #47463
- Failed merges:
@alexcrichton alexcrichton merged commit 88aec91 into rust-lang:master Mar 6, 2018
@gnzlbg
Copy link
Contributor

gnzlbg commented Mar 14, 2018

@Amanieu aren't these exposed by stdsimd already? If not, could you fill an issue there mentioning which instructions you were missing for which architecture?

@gnzlbg
Copy link
Contributor

gnzlbg commented Mar 14, 2018

I've opened an issue on stdsimd to start migrating the intrinsics that were already using llvm's bitreverse intrinsic to use the rustc intrinsic.

@Amanieu
Copy link
Member Author

Amanieu commented Mar 14, 2018

They are only exposed as vendor-specific intrinsics in stdsimd. I wanted to add a generic solution that worked on all architectures.

@gnzlbg
Copy link
Contributor

gnzlbg commented Mar 14, 2018

@Amanieu makes sense, just keep in mind that the performance of LLVM's generic bitreverse solution can vary greatly depending on the types involved, your target architecture, and what the rest of your code does around it: https://bugs.llvm.org/show_bug.cgi?id=31810

If your target has a bit reverse instruction (only ARMv8 as far as I know) then everything should be fine.

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.