From 804ac35870081c2d573c2db523dfabeacd370f6b Mon Sep 17 00:00:00 2001 From: Sigilante <57601680+sigilante@users.noreply.github.com> Date: Wed, 15 Nov 2023 15:29:02 -0600 Subject: [PATCH 1/9] Add u64/u128 atom exports Necessary for floating-point; nice for conversions from direct atoms. --- rust/ares/src/noun.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/rust/ares/src/noun.rs b/rust/ares/src/noun.rs index c151126..57a49d8 100644 --- a/rust/ares/src/noun.rs +++ b/rust/ares/src/noun.rs @@ -738,6 +738,29 @@ impl Atom { } } + pub fn as_u64(self) -> Result { + if self.is_direct() { + Ok(unsafe { self.direct.data() }) + } else { + unsafe { + self.indirect.as_u64() + } + } + } + + pub unsafe fn as_u128_pair(self) -> Result<[u64; 2]> { + if self.is_direct() { + let mut u128_array = &mut [0u64; 2]; + u128_array[0] = 0x0 as u64; + u128_array[1] = self.as_direct()?.data() as u64; + Ok(unsafe { *u128_array } ) + } else { + unsafe { + self.indirect.as_u128_pair() + } + } + } + pub fn as_bitslice(&self) -> &BitSlice { if self.is_indirect() { unsafe { self.indirect.as_bitslice() } @@ -807,6 +830,24 @@ impl Atom { } } + pub unsafe fn as_u64(self) -> Result { + if self.size() == 1 { + Ok(*(self.data_pointer())) + } else { + Err(Error::NotRepresentable) + } + } + + pub unsafe fn as_u128_pair(self) -> Result<[u64; 2]> { + if self.size() <= 2 { + let mut u128_array = &mut [0u64; 2]; + u128_array.copy_from_slice(&(self.as_slice()[0..2])); + Ok(unsafe { *u128_array } ) + } else { + Err(Error::NotRepresentable) + } + } + pub fn as_noun(self) -> Noun { Noun { atom: self } } From 1f0fec0f93537614a18c137d20a27e141517b655 Mon Sep 17 00:00:00 2001 From: Sigilante <57601680+sigilante@users.noreply.github.com> Date: Wed, 15 Nov 2023 15:36:12 -0600 Subject: [PATCH 2/9] Satisfy linter --- rust/ares/src/noun.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/rust/ares/src/noun.rs b/rust/ares/src/noun.rs index 57a49d8..d533965 100644 --- a/rust/ares/src/noun.rs +++ b/rust/ares/src/noun.rs @@ -742,9 +742,7 @@ impl Atom { if self.is_direct() { Ok(unsafe { self.direct.data() }) } else { - unsafe { - self.indirect.as_u64() - } + unsafe { self.indirect.as_u64() } } } @@ -842,7 +840,7 @@ impl Atom { if self.size() <= 2 { let mut u128_array = &mut [0u64; 2]; u128_array.copy_from_slice(&(self.as_slice()[0..2])); - Ok(unsafe { *u128_array } ) + Ok(unsafe { *u128_array }) } else { Err(Error::NotRepresentable) } From 7debba4ad7d0dd3dc244191dd5ccc8261c5fb48c Mon Sep 17 00:00:00 2001 From: Sigilante <57601680+sigilante@users.noreply.github.com> Date: Wed, 15 Nov 2023 15:37:33 -0600 Subject: [PATCH 3/9] Satisfy linter --- rust/ares/src/noun.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/rust/ares/src/noun.rs b/rust/ares/src/noun.rs index d533965..c68ef8f 100644 --- a/rust/ares/src/noun.rs +++ b/rust/ares/src/noun.rs @@ -751,14 +751,12 @@ impl Atom { let mut u128_array = &mut [0u64; 2]; u128_array[0] = 0x0 as u64; u128_array[1] = self.as_direct()?.data() as u64; - Ok(unsafe { *u128_array } ) + Ok(unsafe { *u128_array }) } else { - unsafe { - self.indirect.as_u128_pair() - } + unsafe { self.indirect.as_u128_pair() } } } - + pub fn as_bitslice(&self) -> &BitSlice { if self.is_indirect() { unsafe { self.indirect.as_bitslice() } From 1ae6303cbf40a11c6017a714617a049de9144ad8 Mon Sep 17 00:00:00 2001 From: Sigilante <57601680+sigilante@users.noreply.github.com> Date: Wed, 15 Nov 2023 15:41:22 -0600 Subject: [PATCH 4/9] Move u64/u128 to IndirectAtom as well --- rust/ares/src/noun.rs | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/rust/ares/src/noun.rs b/rust/ares/src/noun.rs index c68ef8f..b667527 100644 --- a/rust/ares/src/noun.rs +++ b/rust/ares/src/noun.rs @@ -464,6 +464,24 @@ impl IndirectAtom { UBig::from_le_bytes_stack(stack, self.as_bytes()) } + pub unsafe fn as_u64(self) -> Result { + if self.size() == 1 { + Ok(*(self.data_pointer())) + } else { + Err(Error::NotRepresentable) + } + } + + pub unsafe fn as_u128_pair(self) -> Result<[u64; 2]> { + if self.size() <= 2 { + let mut u128_array = &mut [0u64; 2]; + u128_array.copy_from_slice(&(self.as_slice()[0..2])); + Ok(unsafe { *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; @@ -826,24 +844,6 @@ impl Atom { } } - pub unsafe fn as_u64(self) -> Result { - if self.size() == 1 { - Ok(*(self.data_pointer())) - } else { - Err(Error::NotRepresentable) - } - } - - pub unsafe fn as_u128_pair(self) -> Result<[u64; 2]> { - if self.size() <= 2 { - let mut u128_array = &mut [0u64; 2]; - u128_array.copy_from_slice(&(self.as_slice()[0..2])); - Ok(unsafe { *u128_array }) - } else { - Err(Error::NotRepresentable) - } - } - pub fn as_noun(self) -> Noun { Noun { atom: self } } From 44ba80cd33309102f183dd510e6ae8e5d93e1d26 Mon Sep 17 00:00:00 2001 From: Sigilante <57601680+sigilante@users.noreply.github.com> Date: Wed, 15 Nov 2023 15:44:20 -0600 Subject: [PATCH 5/9] Fix minor unsafe blocks --- rust/ares/src/noun.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/rust/ares/src/noun.rs b/rust/ares/src/noun.rs index b667527..54e5f39 100644 --- a/rust/ares/src/noun.rs +++ b/rust/ares/src/noun.rs @@ -474,9 +474,9 @@ impl IndirectAtom { pub unsafe fn as_u128_pair(self) -> Result<[u64; 2]> { if self.size() <= 2 { - let mut u128_array = &mut [0u64; 2]; + let u128_array = &mut [0u64; 2]; u128_array.copy_from_slice(&(self.as_slice()[0..2])); - Ok(unsafe { *u128_array }) + Ok(*u128_array) } else { Err(Error::NotRepresentable) } @@ -766,10 +766,10 @@ impl Atom { pub unsafe fn as_u128_pair(self) -> Result<[u64; 2]> { if self.is_direct() { - let mut u128_array = &mut [0u64; 2]; - u128_array[0] = 0x0 as u64; - u128_array[1] = self.as_direct()?.data() as u64; - Ok(unsafe { *u128_array }) + let u128_array = &mut [0u64; 2]; + u128_array[0] = 0x0_u64; + u128_array[1] = self.as_direct()?.data(); + Ok(*u128_array) } else { unsafe { self.indirect.as_u128_pair() } } From a2749a71cb815aa47785117321f4753b4088ed28 Mon Sep 17 00:00:00 2001 From: Sigilante <57601680+sigilante@users.noreply.github.com> Date: Wed, 15 Nov 2023 16:12:26 -0600 Subject: [PATCH 6/9] Rename u128_pair to u64_pair and clarify SoftFloat intent --- rust/ares/src/noun.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/rust/ares/src/noun.rs b/rust/ares/src/noun.rs index 54e5f39..1ae9fcc 100644 --- a/rust/ares/src/noun.rs +++ b/rust/ares/src/noun.rs @@ -472,7 +472,8 @@ impl IndirectAtom { } } - pub unsafe fn as_u128_pair(self) -> Result<[u64; 2]> { + /* SoftFloat-compatible ordered pair of 64-bit words */ + 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])); @@ -764,14 +765,15 @@ impl Atom { } } - pub unsafe fn as_u128_pair(self) -> Result<[u64; 2]> { + /* SoftFloat-compatible ordered pair of 64-bit words */ + pub unsafe fn as_u64_pair(self) -> Result<[u64; 2]> { if self.is_direct() { let u128_array = &mut [0u64; 2]; u128_array[0] = 0x0_u64; u128_array[1] = self.as_direct()?.data(); Ok(*u128_array) } else { - unsafe { self.indirect.as_u128_pair() } + unsafe { self.indirect.as_u64_pair() } } } From 0cb6cb3063e1cf467e7d1bd86eedba24eb7acbdf Mon Sep 17 00:00:00 2001 From: Sigilante <57601680+sigilante@users.noreply.github.com> Date: Wed, 15 Nov 2023 16:30:19 -0600 Subject: [PATCH 7/9] Switch to doc comment format. --- rust/ares/src/noun.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rust/ares/src/noun.rs b/rust/ares/src/noun.rs index 1ae9fcc..a97746c 100644 --- a/rust/ares/src/noun.rs +++ b/rust/ares/src/noun.rs @@ -472,7 +472,7 @@ impl IndirectAtom { } } - /* SoftFloat-compatible ordered pair of 64-bit words */ + /** Produce a SoftFloat-compatible ordered pair of 64-bit words */ pub unsafe fn as_u64_pair(self) -> Result<[u64; 2]> { if self.size() <= 2 { let u128_array = &mut [0u64; 2]; @@ -765,7 +765,7 @@ impl Atom { } } - /* SoftFloat-compatible ordered pair of 64-bit words */ + /** Produce a SoftFloat-compatible ordered pair of 64-bit words */ pub unsafe fn as_u64_pair(self) -> Result<[u64; 2]> { if self.is_direct() { let u128_array = &mut [0u64; 2]; From 379bd2279a1f9484e4b6c56a3923e17038f776a5 Mon Sep 17 00:00:00 2001 From: Sigilante <57601680+sigilante@users.noreply.github.com> Date: Fri, 17 Nov 2023 10:40:41 -0600 Subject: [PATCH 8/9] Mark manually-checked pair as safe. --- rust/ares/src/noun.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/ares/src/noun.rs b/rust/ares/src/noun.rs index a97746c..5e3282b 100644 --- a/rust/ares/src/noun.rs +++ b/rust/ares/src/noun.rs @@ -473,7 +473,7 @@ impl IndirectAtom { } /** Produce a SoftFloat-compatible ordered pair of 64-bit words */ - pub unsafe fn as_u64_pair(self) -> Result<[u64; 2]> { + pub 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])); From 15a8745097827612a58bdf9b485b5198d4866bab Mon Sep 17 00:00:00 2001 From: Sigilante <57601680+sigilante@users.noreply.github.com> Date: Fri, 17 Nov 2023 10:46:58 -0600 Subject: [PATCH 9/9] Swap high.low bytes in pair --- rust/ares/src/noun.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rust/ares/src/noun.rs b/rust/ares/src/noun.rs index 5e3282b..664474a 100644 --- a/rust/ares/src/noun.rs +++ b/rust/ares/src/noun.rs @@ -769,8 +769,8 @@ impl Atom { pub unsafe fn as_u64_pair(self) -> Result<[u64; 2]> { if self.is_direct() { let u128_array = &mut [0u64; 2]; - u128_array[0] = 0x0_u64; - u128_array[1] = self.as_direct()?.data(); + u128_array[0] = self.as_direct()?.data(); + u128_array[1] = 0x0_u64; Ok(*u128_array) } else { unsafe { self.indirect.as_u64_pair() }