Repository navigation
Fix data packet sign extension (phases below -65536 wrap positive) #1
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bmazin
wants to merge
1
commit into
main
Choose a base branch
from
fix-packet-sign-extension
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -617,8 +617,7 @@ pub struct DataPacket { | |
|
|
||
| impl SerializePacket for DataPacket { | ||
| fn serialize(&self) -> [u8; 8] { | ||
| let mut packet = 0xff00_0000_0000_0000u64; | ||
| packet |= (self.x as u64).wrapping_shl(64 - 10); | ||
| let mut packet = (self.x as u64).wrapping_shl(64 - 10); | ||
| packet |= (self.y as u64).wrapping_shl(64 - 10 - 10); | ||
| packet |= (self.timestamp as u64 & 0x1ff).wrapping_shl(64 - 10 - 10 - 9); | ||
| packet |= (self.phase as u64 & 0x3ffff).wrapping_shl(64 - 10 - 10 - 9 - 18); | ||
|
|
@@ -685,9 +684,10 @@ fn parse_data(input: &mut Stream<'_>) -> ModalResult<DataPacket> { | |
| take(10usize).verify(|i| *i < 256), | ||
| take(10usize).verify(|i| *i < 256), | ||
| take(9usize), | ||
| // Use an arithmetic right shift to sign extend these | ||
| take(18usize).map(|input: i32| input.wrapping_shl(32 - 17).wrapping_shr(32 - 17)), | ||
| take(17usize).map(|input: i32| input.wrapping_shl(32 - 16).wrapping_shr(32 - 16)), | ||
| // Phase and baseline are 18- and 17-bit two's complement fields; | ||
| // shift each field's sign bit into bit 31, then arithmetic-shift back | ||
| take(18usize).map(|input: i32| input.wrapping_shl(32 - 18).wrapping_shr(32 - 18)), | ||
| take(17usize).map(|input: i32| input.wrapping_shl(32 - 17).wrapping_shr(32 - 17)), | ||
| )) | ||
| .parse_next(input) | ||
| .map_err(|_| ErrMode::<ContextError>::Cut(ContextError::new()))?; | ||
|
|
@@ -1226,6 +1226,16 @@ mod tests { | |
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_data_full_width_sign_extension() { | ||
| // Values outside the 17-bit phase / 16-bit baseline range, including a real | ||
| // MEC 850 nm photon (phase -100212), must not wrap to the opposite sign | ||
| for (phase, baseline) in [(-100212, -9286), (-131072, -65536), (131071, 65535), (-65537, -32769)] { | ||
| let packet = DataPacket { x: 99, y: 44, timestamp: 131, phase, baseline }; | ||
| assert_eq!(parse_data(&mut stream(&packet.serialize())), Ok(packet)); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_header() { | ||
| let mut input_header = stream(&[0xff, 0xe8, 0x76, 0x0b, 0x46, 0x72, 0x2c, 0xfb]); | ||
|
|
@@ -1254,7 +1264,9 @@ mod tests { | |
| }) | ||
| ); | ||
|
|
||
| assert_eq!(input_data, stream(&[])) | ||
| assert_eq!(input_data, stream(&[])); | ||
| let packet = DataPacket { x: 99, y: 44, timestamp: 131, phase: -34148, baseline: -4334 }; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be a seperate test as serialization and deserialization are very nearly orthogonal as implemented here |
||
| assert_eq!(packet.serialize(), [0x18, 0xc2, 0xc4, 0x1e, 0xf5, 0x39, 0xef, 0x12]); | ||
| } | ||
|
|
||
| #[test] | ||
|
|
||
Oops, something went wrong.
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.
something a little less verbose like
test_sign_extension