-
Notifications
You must be signed in to change notification settings - Fork 17
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 u64/u128 atom exports #127
Conversation
Necessary for floating-point; nice for conversions from direct atoms.
Couldn't find the original |
rust/ares/src/noun.rs
Outdated
pub unsafe fn as_u128_pair(self) -> Result<[u64; 2]> { | ||
if self.size() <= 2 { | ||
let u128_array = &mut [0u64; 2]; | ||
u128_array.copy_from_slice(&(self.as_slice()[0..2])); | ||
Ok(*u128_array) | ||
} else { | ||
Err(Error::NotRepresentable) | ||
} | ||
} |
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.
Rust has a native u128 type: https://doc.rust-lang.org/std/primitive.u128.html
You could make a 16-element byte array, copy into it from atom.as_bytes()
, and then call u128::from_le_bytes() on the array to get the atom.
pub unsafe fn as_u128_pair(self) -> Result<[u64; 2]> { | |
if self.size() <= 2 { | |
let u128_array = &mut [0u64; 2]; | |
u128_array.copy_from_slice(&(self.as_slice()[0..2])); | |
Ok(*u128_array) | |
} else { | |
Err(Error::NotRepresentable) | |
} | |
} | |
pub unsafe fn as_u128(self) -> Result<u128> { | |
if self.size() <= 2 { | |
let mut u128_array = [0u8; 16]; | |
let self_bytes = self.as_bytes(); | |
u128_array[0..self.as_bytes()].copy_from_slice(self_bytes); | |
Ok(u128::from_le_bytes(u128_array) | |
} else { | |
Err(Error::NotRepresentable) | |
} | |
} |
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.
It's a pair of u64 to match the SoftFloat interface.
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.
It's not a generic u128, it's to solve a particular use case from urcrypt.
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.
urcrypt urfloat, whatever we end up calling it
rust/ares/src/noun.rs
Outdated
} | ||
} | ||
|
||
/* SoftFloat-compatible ordered pair of 64-bit words */ |
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.
doc comment please
i think it's worth double checking here. I don't know anything about the softfloat crate in use, but vere's equivalent jets are clearly little-endian: https://github.com/urbit/vere/blob/629ac3698383be5c93324b5ed6355f4aac87dad9/pkg/noun/jets/e/rq.c#L57-L75 |
I'll check before we merge and comment here when that's done. |
This test passes as written:
|
indicates the direct atom pair case is swapped; correcting now. @joemfb |
Marking as ready to merge. @eamsden |
@sigilante I would like @joemfb's review on this before merging. I'm not presently in a state where I can be confident in my own judgement of the code or the tests. |
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.
this will do
Necessary for floating-point; nice for conversions from direct atoms.
In particular, I'd like this expedited to complete
last
in the parsing jets.