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 u64/u128 atom exports #127

Merged
merged 9 commits into from
Nov 17, 2023
Merged
39 changes: 39 additions & 0 deletions rust/ares/src/noun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,25 @@ impl IndirectAtom {
UBig::from_le_bytes_stack(stack, self.as_bytes())
}

pub unsafe fn as_u64(self) -> Result<u64> {
if self.size() == 1 {
Ok(*(self.data_pointer()))
} else {
Err(Error::NotRepresentable)
}
}

/* SoftFloat-compatible ordered pair of 64-bit words */
Copy link
Collaborator

Choose a reason for hiding this comment

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

doc comment please

pub unsafe fn as_u64_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)
}
}

/** Ensure that the size does not contain any trailing 0 words */
pub unsafe fn normalize(&mut self) -> &Self {
let mut index = self.size() - 1;
Expand Down Expand Up @@ -738,6 +757,26 @@ impl Atom {
}
}

pub fn as_u64(self) -> Result<u64> {
if self.is_direct() {
Ok(unsafe { self.direct.data() })
} else {
unsafe { self.indirect.as_u64() }
}
}

/* SoftFloat-compatible ordered pair of 64-bit words */
pub unsafe fn as_u64_pair(self) -> Result<[u64; 2]> {
eamsden marked this conversation as resolved.
Show resolved Hide resolved
if self.is_direct() {
let u128_array = &mut [0u64; 2];
u128_array[0] = 0x0_u64;
u128_array[1] = self.as_direct()?.data();
eamsden marked this conversation as resolved.
Show resolved Hide resolved
Ok(*u128_array)
} else {
unsafe { self.indirect.as_u64_pair() }
}
}

pub fn as_bitslice(&self) -> &BitSlice<u64, Lsb0> {
if self.is_indirect() {
unsafe { self.indirect.as_bitslice() }
Expand Down