-
Notifications
You must be signed in to change notification settings - Fork 4
feat(rt): support round-trip transmute bytes <> u64 casts #804
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a131661
feat(rt): support transmute byte casts
Stevengre 25f20ec
fix: make build
Stevengre 9ec997d
fix: make build
Stevengre 880521d
test(prove-rs): add byte endian spec fixtures
Stevengre 08309cb
Update kmir/src/kmir/kdist/mir-semantics/rt/data.md
Stevengre eb6be66
Update kmir/src/kmir/kdist/mir-semantics/rt/data.md
Stevengre b101337
fix: apply suggestions
Stevengre 63d30ae
Merge branch 'master' into jh/int-bytes-cast
dkcumming File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
kmir/src/tests/integration/data/prove-rs/transmute-bytes.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| #![allow(clippy::transmute_bytes_to_bytes)] | ||
| #![allow(clippy::unnecessary_transmute)] | ||
|
|
||
| use std::mem::transmute; | ||
|
|
||
| fn bytes_to_u64(bytes: [u8; 8]) -> u64 { | ||
| let value = unsafe { transmute::<[u8; 8], u64>(bytes) }; | ||
| let roundtrip = unsafe { transmute::<u64, [u8; 8]>(value) }; | ||
| assert_eq!(roundtrip, bytes); | ||
| value | ||
| } | ||
|
|
||
| fn u64_to_bytes(value: u64) -> [u8; 8] { | ||
| let bytes = unsafe { transmute::<u64, [u8; 8]>(value) }; | ||
| let roundtrip = unsafe { transmute::<[u8; 8], u64>(bytes) }; | ||
| assert_eq!(roundtrip, value); | ||
| bytes | ||
| } | ||
|
|
||
| fn main() { | ||
| let bytes = [0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF]; | ||
| let int = bytes_to_u64(bytes); | ||
| assert_eq!(int, 0xEFCD_AB89_6745_2301); | ||
|
|
||
| let roundtrip = u64_to_bytes(int); | ||
| assert_eq!(roundtrip, bytes); | ||
|
|
||
| let value = 0x1020_3040_5060_7080_u64; | ||
| let value_roundtrip = bytes_to_u64(u64_to_bytes(value)); | ||
| assert_eq!(value_roundtrip, value); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 suspect this actually depends on architecture, and we do have access to that information in the configuration via the
MachineInfofieldEndian(link). Not sure if it is an easy inclusion but given we would then want to check it on different endian machines to ensure it works I think it's worth investigating right now. At least a note should be included as this should break on other architectures iiuc.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.
Maybe we just add a
TODO? We have a few more places where we assume little-endian byte order and machine word size 64bit.