From ab76eb30f5d2699bd50282e76271dc8b2eea9cce Mon Sep 17 00:00:00 2001 From: Solomon Ucko Date: Tue, 9 Aug 2022 22:11:56 -0400 Subject: [PATCH] refactor some more --- src/logic.rs | 55 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/src/logic.rs b/src/logic.rs index b53466d..63f813c 100644 --- a/src/logic.rs +++ b/src/logic.rs @@ -89,6 +89,18 @@ macro_rules! logic_op { } } + +macro_rules! inline_heap_to_inline { + ($op:tt; $i:ident, $slice:ident) => { + let mut j = 0u128; + for i in 0..4 { + j <<= 32; + j |= $slice[3 - i] as u128; + } + SmallUint(SmallUintType::Inline($i $op j)) + } +} + macro_rules! inline_heap_to_heap { ($op_assign:tt; $i:ident, $slice:ident) => { let mut retvec = $slice.to_vec(); @@ -107,6 +119,17 @@ macro_rules! inline_heap_to_heap { } } + +macro_rules! heap_heap_create_res_shortest { + ($op:tt; $min:ident, $slice1:ident, $slice2:ident) => { + let mut res = Vec::with_capacity($min); + for l in 0..$min { + res.push($slice1[l] $op $slice2[l]); + } + res + } +} + macro_rules! heap_heap_create_res_longest { ($fun:ident; $slice1:ident, $slice2:ident, $min:ident) => { let mut res = if $slice1.len() > $slice2.len() { @@ -121,6 +144,14 @@ macro_rules! heap_heap_create_res_longest { } } + +macro_rules! heap_heap_return_heap { + ($res:ident) => { + let mut slice = ManuallyDrop::new($res.into_boxed_slice()); + SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), slice.len()))) + } +} + macro_rules! heap_heap_return_any { ($res:ident) => { while $res.len() != 1 && $res[$res.len() - 1] == 0 { @@ -144,21 +175,8 @@ macro_rules! heap_heap_return_any { logic_op! { BitAnd, BitAndAssign, SmallUint, SmallUintType, bitand, bitand_assign; i, j, r, s, slice, slice1, slice2, min, res; - { - let mut j = 0u128; - for i in 0..4 { - j <<= 32; - j |= slice[3 - i] as u128; - } - SmallUint(SmallUintType::Inline(i & j)) - }, - { - let mut res = Vec::with_capacity(min); - for l in 0..min { - res.push(slice1[l] & slice2[l]); - } - res - }, + { inline_heap_to_inline! { &; i, slice } }, + { heap_heap_create_res_shortest! { &; min, slice1, slice2 } }, { heap_heap_return_any! { res } } } @@ -167,10 +185,7 @@ logic_op! { i, j, r, s, slice, slice1, slice2, min, res; { inline_heap_to_heap! { |=; i, slice } }, { heap_heap_create_res_longest! { bitor; slice1, slice2, min }}, - { - let mut slice = ManuallyDrop::new(res.into_boxed_slice()); - SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), slice.len()))) - } + { heap_heap_return_heap! { res } } } logic_op! { @@ -180,3 +195,5 @@ logic_op! { { heap_heap_create_res_longest! { bitxor; slice1, slice2, min }}, { heap_heap_return_any! { res } } } + +