From 570559ae274758d6acf708ac4a2cac65fc1a4b95 Mon Sep 17 00:00:00 2001 From: Solomon Ucko Date: Tue, 9 Aug 2022 21:51:42 -0400 Subject: [PATCH] fix some bugs --- src/logic.rs | 56 ++++++++++++++++++++++++++++++++++----------------- src/ops.rs | 4 ++-- src/pretty.rs | 4 ++-- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/src/logic.rs b/src/logic.rs index e47169d..b53466d 100644 --- a/src/logic.rs +++ b/src/logic.rs @@ -43,8 +43,8 @@ macro_rules! basic_op { macro_rules! logic_op { ( $imp:ident, $imp_assign:ident, $typ:ident, $typ_inner:ident, $fun:ident, $fun_assign:ident; - $i:ident, $j:ident, $r:ident, $s:ident, $slice:ident, $slice1:ident, $slice2:ident, $res:ident; - $inline_heap:tt, $heap_heap:tt + $i:ident, $j:ident, $r:ident, $s:ident, $slice:ident, $slice1:ident, $slice2:ident, $min:ident, $res:ident; + $inline_heap:tt, $heap_heap_create_res:tt, $heap_heap_return:tt ) => { fn $fun(a: &$typ, b: &$typ) -> $typ { match (&a.0, &b.0) { @@ -63,15 +63,12 @@ macro_rules! logic_op { let $slice1 = unsafe { core::slice::from_raw_parts($r, $s) }; let $slice2 = unsafe { core::slice::from_raw_parts($i, $j) }; - let min = std::cmp::min($slice1.len(), $slice2.len()); + let $min = std::cmp::min($slice1.len(), $slice2.len()); - let mut $res = Vec::with_capacity(min); + #[allow(unused_mut)] + let mut $res = $heap_heap_create_res; - for l in 0..min { - $res.push($slice1[l] & $slice2[l]); - } - - $heap_heap + $heap_heap_return } } } @@ -93,7 +90,7 @@ macro_rules! logic_op { } 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 v = $i; @@ -110,7 +107,21 @@ macro_rules! inline_heap_to_heap { } } -macro_rules! heap_heap_to_any { +macro_rules! heap_heap_create_res_longest { + ($fun:ident; $slice1:ident, $slice2:ident, $min:ident) => { + let mut res = if $slice1.len() > $slice2.len() { + $slice1.to_vec() + } else { + $slice2.to_vec() + }; + for l in 0..$min { + res[l] = $slice1[l].$fun($slice2[l]); + } + res + } +} + +macro_rules! heap_heap_return_any { ($res:ident) => { while $res.len() != 1 && $res[$res.len() - 1] == 0 { $res.pop(); @@ -132,7 +143,7 @@ macro_rules! heap_heap_to_any { logic_op! { BitAnd, BitAndAssign, SmallUint, SmallUintType, bitand, bitand_assign; - i, j, r, s, slice, slice1, slice2, res; + i, j, r, s, slice, slice1, slice2, min, res; { let mut j = 0u128; for i in 0..4 { @@ -141,13 +152,21 @@ logic_op! { } SmallUint(SmallUintType::Inline(i & j)) }, - { heap_heap_to_any! { res } } + { + let mut res = Vec::with_capacity(min); + for l in 0..min { + res.push(slice1[l] & slice2[l]); + } + res + }, + { heap_heap_return_any! { res } } } logic_op! { BitOr, BitOrAssign, SmallUint, SmallUintType, bitor, bitor_assign; - i, j, r, s, slice, slice1, slice2, res; - { inline_heap_to_heap! { |=, i, slice } }, + 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()))) @@ -156,7 +175,8 @@ logic_op! { logic_op! { BitXor, BitXorAssign, SmallUint, SmallUintType, bitxor, bitxor_assign; - i, j, r, s, slice, slice1, slice2, res; - { inline_heap_to_heap! { |=, i, slice } }, - { heap_heap_to_any! { res } } + i, j, r, s, slice, slice1, slice2, min, res; + { inline_heap_to_heap! { ^=; i, slice } }, + { heap_heap_create_res_longest! { bitxor; slice1, slice2, min }}, + { heap_heap_return_any! { res } } } diff --git a/src/ops.rs b/src/ops.rs index b2ae78b..8431556 100644 --- a/src/ops.rs +++ b/src/ops.rs @@ -582,7 +582,7 @@ impl MulAssign for SmallInt { } } -fn div(a: &SmallUint, b: &SmallUint) -> SmallUint { +fn div(_a: &SmallUint, _b: &SmallUint) -> SmallUint { todo!() } @@ -600,7 +600,7 @@ impl DivAssign for SmallUint { } } -fn div_signed(a: &SmallInt, b: &SmallInt) -> SmallInt { +fn div_signed(_a: &SmallInt, _b: &SmallInt) -> SmallInt { todo!() } diff --git a/src/pretty.rs b/src/pretty.rs index 5c122c7..66162f2 100644 --- a/src/pretty.rs +++ b/src/pretty.rs @@ -12,7 +12,7 @@ impl core::fmt::Debug for SmallInt { SmallIntType::Inline(i) => { write!(f, "{}", i)?; } - SmallIntType::Heap((r, s)) => { + SmallIntType::Heap((_r, _s)) => { #[cfg(feature = "num-bigint")] write!(f, "{}", BigInt::from(self))?; @@ -30,7 +30,7 @@ impl core::fmt::Debug for SmallUint { SmallUintType::Inline(i) => { write!(f, "{}", i)?; } - SmallUintType::Heap((r, s)) => { + SmallUintType::Heap((_r, _s)) => { #[cfg(feature = "num-bigint")] write!(f, "{}", BigUint::from(self))?;