refactor some more

This commit is contained in:
Solomon Ucko 2022-08-09 22:11:56 -04:00
parent 570559ae27
commit ab76eb30f5

View File

@ -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 { macro_rules! inline_heap_to_heap {
($op_assign:tt; $i:ident, $slice:ident) => { ($op_assign:tt; $i:ident, $slice:ident) => {
let mut retvec = $slice.to_vec(); 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 { macro_rules! heap_heap_create_res_longest {
($fun:ident; $slice1:ident, $slice2:ident, $min:ident) => { ($fun:ident; $slice1:ident, $slice2:ident, $min:ident) => {
let mut res = if $slice1.len() > $slice2.len() { 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 { macro_rules! heap_heap_return_any {
($res:ident) => { ($res:ident) => {
while $res.len() != 1 && $res[$res.len() - 1] == 0 { while $res.len() != 1 && $res[$res.len() - 1] == 0 {
@ -144,21 +175,8 @@ macro_rules! heap_heap_return_any {
logic_op! { logic_op! {
BitAnd, BitAndAssign, SmallUint, SmallUintType, bitand, bitand_assign; BitAnd, BitAndAssign, SmallUint, SmallUintType, bitand, bitand_assign;
i, j, r, s, slice, slice1, slice2, min, res; i, j, r, s, slice, slice1, slice2, min, res;
{ { inline_heap_to_inline! { &; i, slice } },
let mut j = 0u128; { heap_heap_create_res_shortest! { &; min, slice1, slice2 } },
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
},
{ heap_heap_return_any! { res } } { heap_heap_return_any! { res } }
} }
@ -167,10 +185,7 @@ logic_op! {
i, j, r, s, slice, slice1, slice2, min, res; i, j, r, s, slice, slice1, slice2, min, res;
{ inline_heap_to_heap! { |=; i, slice } }, { inline_heap_to_heap! { |=; i, slice } },
{ heap_heap_create_res_longest! { bitor; slice1, slice2, min }}, { heap_heap_create_res_longest! { bitor; slice1, slice2, min }},
{ { heap_heap_return_heap! { res } }
let mut slice = ManuallyDrop::new(res.into_boxed_slice());
SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), slice.len())))
}
} }
logic_op! { logic_op! {
@ -180,3 +195,5 @@ logic_op! {
{ heap_heap_create_res_longest! { bitxor; slice1, slice2, min }}, { heap_heap_create_res_longest! { bitxor; slice1, slice2, min }},
{ heap_heap_return_any! { res } } { heap_heap_return_any! { res } }
} }