mirror of
https://gitlab.com/artofrev/smallint.git
synced 2024-12-22 14:32:44 -05:00
Compare commits
No commits in common. "81739a131108fefccbc8e8afaf60a8621fde3e07" and "1fe105f58c067ed90aac8a7acbf0e5cec0a427ab" have entirely different histories.
81739a1311
...
1fe105f58c
|
@ -16,7 +16,3 @@ num-bigint = { version = "0.4.3", optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
num-bigint = { version = "0.4.3" }
|
num-bigint = { version = "0.4.3" }
|
||||||
|
|
||||||
|
|
||||||
[profile.test]
|
|
||||||
opt-level = 2
|
|
||||||
|
|
|
@ -37,7 +37,7 @@ impl From<&SmallInt> for BigInt {
|
||||||
match s.0 {
|
match s.0 {
|
||||||
SmallIntType::Inline(i) => Self::from(i),
|
SmallIntType::Inline(i) => Self::from(i),
|
||||||
SmallIntType::Heap((r, s)) => {
|
SmallIntType::Heap((r, s)) => {
|
||||||
let size = s.unsigned_abs();
|
let size = usize::try_from(s.abs()).unwrap();
|
||||||
let sign = s.signum();
|
let sign = s.signum();
|
||||||
let slice = unsafe { core::slice::from_raw_parts(r, size) };
|
let slice = unsafe { core::slice::from_raw_parts(r, size) };
|
||||||
let bs = match sign {
|
let bs = match sign {
|
||||||
|
|
|
@ -109,7 +109,7 @@ impl TryFrom<SmallInt> for u128 {
|
||||||
SmallIntType::Heap((r, s)) => {
|
SmallIntType::Heap((r, s)) => {
|
||||||
let mut ret: u128 = 0;
|
let mut ret: u128 = 0;
|
||||||
let mut bits = 0;
|
let mut bits = 0;
|
||||||
let size = s.unsigned_abs();
|
let size = usize::try_from(s.abs()).unwrap();
|
||||||
let slice = unsafe { core::slice::from_raw_parts(r, size) };
|
let slice = unsafe { core::slice::from_raw_parts(r, size) };
|
||||||
for i in slice {
|
for i in slice {
|
||||||
if bits >= 128 {
|
if bits >= 128 {
|
||||||
|
@ -169,9 +169,9 @@ impl SmallUint {
|
||||||
/// Converts a `SmallInt` into a `SmallUint` and drops the sign instead of throwing an error.
|
/// Converts a `SmallInt` into a `SmallUint` and drops the sign instead of throwing an error.
|
||||||
pub fn from_smallint_unsigned(value: SmallInt) -> Self {
|
pub fn from_smallint_unsigned(value: SmallInt) -> Self {
|
||||||
match value.0 {
|
match value.0 {
|
||||||
SmallIntType::Inline(i) => Self::from(i.unsigned_abs()),
|
SmallIntType::Inline(i) => Self::try_from(i.abs()).unwrap(),
|
||||||
SmallIntType::Heap((r, s)) => {
|
SmallIntType::Heap((r, s)) => {
|
||||||
let size = s.unsigned_abs();
|
let size = usize::try_from(s.abs()).unwrap();
|
||||||
if size > 4 {
|
if size > 4 {
|
||||||
let slice = unsafe { core::slice::from_raw_parts(r, size) };
|
let slice = unsafe { core::slice::from_raw_parts(r, size) };
|
||||||
let mut ret = vec![0; size];
|
let mut ret = vec![0; size];
|
||||||
|
|
401
src/logic.rs
401
src/logic.rs
|
@ -1,35 +1,16 @@
|
||||||
use crate::ops::{add_two_slices, sub_two_slices};
|
use crate::smallint::SmallUintType;
|
||||||
use crate::smallint::{SmallIntType, SmallUintType};
|
use crate::SmallUint;
|
||||||
use crate::{SmallInt, SmallUint};
|
|
||||||
use core::cmp::Ordering;
|
|
||||||
use core::mem::ManuallyDrop;
|
use core::mem::ManuallyDrop;
|
||||||
use core::ops::{BitAnd, BitOr, BitXor};
|
use core::ops::{BitAnd, BitOr, BitXor};
|
||||||
use core::ops::{BitAndAssign, BitOrAssign, BitXorAssign};
|
use core::ops::{BitAndAssign, BitOrAssign, BitXorAssign};
|
||||||
|
|
||||||
macro_rules! logic_op {
|
macro_rules! basic_op {
|
||||||
(
|
($imp:ident, $typ:ty, $fun:ident) => {
|
||||||
$imp:ident, $imp_assign:ident, $typ:ident, $typ_inner:ident, $fun:ident, $fun_assign:ident;
|
|
||||||
$i:ident, $j:ident, $p:ident, $q:ident, $s:ident, $t:ident;
|
|
||||||
$inline_heap:tt, $heap_heap:tt
|
|
||||||
) => {
|
|
||||||
impl<'a, 'b> $imp<&'a $typ> for &'b $typ {
|
impl<'a, 'b> $imp<&'a $typ> for &'b $typ {
|
||||||
type Output = $typ;
|
type Output = $typ;
|
||||||
|
|
||||||
fn $fun(self, rhs: &$typ) -> Self::Output {
|
fn $fun(self, rhs: &$typ) -> Self::Output {
|
||||||
match (&self.0, &rhs.0) {
|
$fun(self, rhs)
|
||||||
(&$typ_inner::Inline($i), &$typ_inner::Inline($j)) => {
|
|
||||||
$typ($typ_inner::Inline($i.$fun($j)))
|
|
||||||
}
|
|
||||||
|
|
||||||
(&$typ_inner::Inline($i), &$typ_inner::Heap(($p, $s)))
|
|
||||||
| (&$typ_inner::Heap(($p, $s)), &$typ_inner::Inline($i)) => {
|
|
||||||
$inline_heap
|
|
||||||
}
|
|
||||||
|
|
||||||
(&$typ_inner::Heap(($p, $s)), &$typ_inner::Heap(($q, $t))) => {
|
|
||||||
$heap_heap
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,280 +37,204 @@ macro_rules! logic_op {
|
||||||
(&self).$fun(&rhs)
|
(&self).$fun(&rhs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> $imp_assign<&'a $typ> for $typ {
|
|
||||||
fn $fun_assign(&mut self, rhs: &'a $typ) {
|
|
||||||
*self = (&*self).$fun(rhs);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl $imp_assign<$typ> for $typ {
|
|
||||||
fn $fun_assign(&mut self, rhs: $typ) {
|
|
||||||
*self = (&*self).$fun(rhs);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
(
|
|
||||||
$imp:ident, $imp_assign:ident, $typ:ident, $typ_inner:ident, $fun:ident, $fun_assign:ident;
|
|
||||||
$i:ident, $j:ident, $p:ident, $q:ident, $s:ident, $t:ident, $slice:ident, $slice1:ident, $slice2:ident, $min:ident, $res:ident;
|
|
||||||
$inline_heap_inner:tt, $heap_heap_create_res:tt, $heap_heap_return:tt
|
|
||||||
) => {
|
|
||||||
logic_op! {
|
|
||||||
$imp, $imp_assign, $typ, $typ_inner, $fun, $fun_assign;
|
|
||||||
$i, $j, $p, $q, $s, $t;
|
|
||||||
{
|
|
||||||
let $slice = unsafe { core::slice::from_raw_parts($p, $s) };
|
|
||||||
|
|
||||||
$inline_heap_inner
|
|
||||||
},
|
|
||||||
{
|
|
||||||
let $slice1 = unsafe { core::slice::from_raw_parts($p, $s) };
|
|
||||||
let $slice2 = unsafe { core::slice::from_raw_parts($q, $t) };
|
|
||||||
|
|
||||||
let $min = std::cmp::min($slice1.len(), $slice2.len());
|
|
||||||
|
|
||||||
#[allow(unused_mut)]
|
|
||||||
let mut $res = $heap_heap_create_res;
|
|
||||||
|
|
||||||
$heap_heap_return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn bitand(a: &SmallUint, b: &SmallUint) -> SmallUint {
|
||||||
|
match (&a.0, &b.0) {
|
||||||
|
(&SmallUintType::Inline(i), &SmallUintType::Inline(j)) => {
|
||||||
|
SmallUint(SmallUintType::Inline(i & j))
|
||||||
|
}
|
||||||
|
|
||||||
macro_rules! heap_to_inline {
|
(&SmallUintType::Inline(i), &SmallUintType::Heap((r, s)))
|
||||||
($typ_inline:ident; $slice:ident) => {
|
| (&SmallUintType::Heap((r, s)), &SmallUintType::Inline(i)) => {
|
||||||
let mut j = 0;
|
let slice = unsafe { core::slice::from_raw_parts(r, s) };
|
||||||
|
let mut j = 0u128;
|
||||||
for i in 0..4 {
|
for i in 0..4 {
|
||||||
j <<= 32;
|
j <<= 32;
|
||||||
j |= $slice[3 - i] as $typ_inline;
|
|
||||||
|
j |= slice[3 - i] as u128;
|
||||||
|
}
|
||||||
|
SmallUint(SmallUintType::Inline(i & j))
|
||||||
|
}
|
||||||
|
|
||||||
|
(&SmallUintType::Heap((r, s)), &SmallUintType::Heap((i, j))) => {
|
||||||
|
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 mut res = Vec::with_capacity(min);
|
||||||
|
|
||||||
|
for l in 0..min {
|
||||||
|
res.push(slice1[l] & slice2[l]);
|
||||||
|
}
|
||||||
|
|
||||||
|
while res.len() != 1 && res[res.len() - 1] == 0 {
|
||||||
|
res.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
if res.len() <= 4 {
|
||||||
|
let mut r = 0u128;
|
||||||
|
for t in 0..res.len() {
|
||||||
|
r <<= 32;
|
||||||
|
r |= res[res.len() - 1 - t] as u128;
|
||||||
|
}
|
||||||
|
SmallUint(SmallUintType::Inline(r))
|
||||||
|
} else {
|
||||||
|
let mut slice = ManuallyDrop::new(res.into_boxed_slice());
|
||||||
|
SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), slice.len())))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
j
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! inline_heap_to_inline {
|
basic_op!(BitAnd, SmallUint, bitand);
|
||||||
($fun:ident, $typ:ident, $typ_inner:ident, $typ_inline:ident; $i:ident, $slice:ident) => {
|
|
||||||
let j = { heap_to_inline! { $typ_inline; $slice } };
|
impl<'a> BitAndAssign<&'a SmallUint> for SmallUint {
|
||||||
$typ($typ_inner::Inline($i.$fun(j)))
|
fn bitand_assign(&mut self, rhs: &'a SmallUint) {
|
||||||
|
*self = self.clone() & rhs;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! inline_heap_to_heap {
|
impl BitAndAssign<SmallUint> for SmallUint {
|
||||||
($fun_assign:ident, $typ:ident, $typ_inner:ident; $i:ident, $slice:ident) => {
|
fn bitand_assign(&mut self, rhs: SmallUint) {
|
||||||
let mut retvec = $slice.to_vec();
|
*self = self.clone() & rhs;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut v = $i;
|
fn bitor(a: &SmallUint, b: &SmallUint) -> SmallUint {
|
||||||
|
match (&a.0, &b.0) {
|
||||||
|
(&SmallUintType::Inline(i), &SmallUintType::Inline(j)) => {
|
||||||
|
SmallUint(SmallUintType::Inline(i | j))
|
||||||
|
}
|
||||||
|
(&SmallUintType::Inline(i), &SmallUintType::Heap((r, s)))
|
||||||
|
| (&SmallUintType::Heap((r, s)), &SmallUintType::Inline(i)) => {
|
||||||
|
let slice = unsafe { core::slice::from_raw_parts(r, s) };
|
||||||
|
|
||||||
|
let mut retvec = slice.to_vec();
|
||||||
|
|
||||||
|
let mut v = i;
|
||||||
#[allow(clippy::needless_range_loop)]
|
#[allow(clippy::needless_range_loop)]
|
||||||
for r in 0..4 {
|
for r in 0..4 {
|
||||||
retvec[r].$fun_assign(v as u32);
|
retvec[r] |= v as u32;
|
||||||
|
|
||||||
v >>= 32;
|
v >>= 32;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut retslice = ManuallyDrop::new(retvec.into_boxed_slice());
|
let mut retslice = ManuallyDrop::new(retvec.into_boxed_slice());
|
||||||
|
|
||||||
$typ($typ_inner::Heap((retslice.as_mut_ptr(), retslice.len())))
|
SmallUint(SmallUintType::Heap((retslice.as_mut_ptr(), retslice.len())))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
(&SmallUintType::Heap((r, s)), &SmallUintType::Heap((i, j))) => {
|
||||||
|
let slice1 = unsafe { core::slice::from_raw_parts(r, s) };
|
||||||
|
let slice2 = unsafe { core::slice::from_raw_parts(i, j) };
|
||||||
|
|
||||||
macro_rules! heap_heap_create_res_shortest {
|
let m = std::cmp::min(slice1.len(), slice2.len());
|
||||||
($fun:ident; $slice1:ident, $slice2:ident, $min:ident) => {
|
|
||||||
let mut res = Vec::with_capacity($min);
|
|
||||||
for l in 0..$min {
|
|
||||||
res.push($slice1[l].$fun($slice2[l]));
|
|
||||||
}
|
|
||||||
res
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! heap_heap_create_res_longest {
|
let mut retvec;
|
||||||
($fun:ident; $slice1:ident, $slice2:ident, $min:ident) => {
|
if slice1.len() > slice2.len() {
|
||||||
let mut res = if $slice1.len() > $slice2.len() {
|
retvec = slice1.to_vec();
|
||||||
$slice1.to_vec()
|
|
||||||
} else {
|
} else {
|
||||||
$slice2.to_vec()
|
retvec = slice2.to_vec();
|
||||||
};
|
}
|
||||||
for l in 0..$min {
|
|
||||||
res[l] = $slice1[l].$fun($slice2[l]);
|
for t in 0..m {
|
||||||
|
retvec[t] = slice1[t] | slice2[t];
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut slice = ManuallyDrop::new(retvec.into_boxed_slice());
|
||||||
|
|
||||||
|
SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), slice.len())))
|
||||||
}
|
}
|
||||||
res
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
basic_op!(BitOr, SmallUint, bitor);
|
||||||
|
|
||||||
macro_rules! heap_heap_return_heap_inner {
|
impl<'a> BitOrAssign<&'a SmallUint> for SmallUint {
|
||||||
($typ:ident, $typ_inner:ident; $res:ident) => {
|
fn bitor_assign(&mut self, rhs: &'a SmallUint) {
|
||||||
let mut slice = ManuallyDrop::new($res);
|
*self = self.clone() | rhs;
|
||||||
$typ($typ_inner::Heap((slice.as_mut_ptr(), slice.len().try_into().unwrap())))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! heap_heap_return_heap_inner_neg {
|
impl BitOrAssign<SmallUint> for SmallUint {
|
||||||
($typ:ident, $typ_inner:ident; $res:ident) => {
|
fn bitor_assign(&mut self, rhs: SmallUint) {
|
||||||
let mut slice = ManuallyDrop::new($res);
|
*self = self.clone() | rhs;
|
||||||
$typ($typ_inner::Heap((slice.as_mut_ptr(), -isize::try_from(slice.len()).unwrap())))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! heap_heap_return_heap {
|
fn bitxor(a: &SmallUint, b: &SmallUint) -> SmallUint {
|
||||||
($typ:ident, $typ_inner:ident; $res:ident) => {
|
match (&a.0, &b.0) {
|
||||||
let res = $res.into_boxed_slice();
|
(&SmallUintType::Inline(i), &SmallUintType::Inline(j)) => {
|
||||||
heap_heap_return_heap_inner! { $typ, $typ_inner; res }
|
SmallUint(SmallUintType::Inline(i ^ j))
|
||||||
}
|
}
|
||||||
}
|
(&SmallUintType::Inline(i), &SmallUintType::Heap((r, s)))
|
||||||
|
| (&SmallUintType::Heap((r, s)), &SmallUintType::Inline(i)) => {
|
||||||
|
let slice = unsafe { core::slice::from_raw_parts(r, s) };
|
||||||
|
|
||||||
macro_rules! heap_heap_return_heap_neg {
|
let mut retvec = slice.to_vec();
|
||||||
($typ:ident, $typ_inner:ident; $res:ident) => {
|
|
||||||
let res = $res.into_boxed_slice();
|
|
||||||
heap_heap_return_heap_inner_neg! { $typ, $typ_inner; res }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! heap_heap_return_any {
|
let mut v = i;
|
||||||
($typ:ident, $typ_inner:ident, $typ_inline:ident; $res:ident) => {
|
#[allow(clippy::needless_range_loop)]
|
||||||
while $res.len() != 1 && $res[$res.len() - 1] == 0 {
|
for r in 0..4 {
|
||||||
$res.pop();
|
retvec[r] ^= v as u32;
|
||||||
|
|
||||||
|
v >>= 32;
|
||||||
}
|
}
|
||||||
|
|
||||||
if $res.len() <= 4 {
|
let mut retslice = ManuallyDrop::new(retvec.into_boxed_slice());
|
||||||
let mut r = 0;
|
|
||||||
for t in 0..$res.len() {
|
SmallUint(SmallUintType::Heap((retslice.as_mut_ptr(), retslice.len())))
|
||||||
|
}
|
||||||
|
|
||||||
|
(&SmallUintType::Heap((r, s)), &SmallUintType::Heap((i, j))) => {
|
||||||
|
let slice1 = unsafe { core::slice::from_raw_parts(r, s) };
|
||||||
|
let slice2 = unsafe { core::slice::from_raw_parts(i, j) };
|
||||||
|
|
||||||
|
let m = std::cmp::min(slice1.len(), slice2.len());
|
||||||
|
|
||||||
|
let mut res;
|
||||||
|
if slice1.len() > slice2.len() {
|
||||||
|
res = slice1.to_vec();
|
||||||
|
} else {
|
||||||
|
res = slice2.to_vec();
|
||||||
|
}
|
||||||
|
|
||||||
|
for t in 0..m {
|
||||||
|
res[t] = slice1[t] ^ slice2[t];
|
||||||
|
}
|
||||||
|
|
||||||
|
while res.len() != 1 && res[res.len() - 1] == 0 {
|
||||||
|
res.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
if res.len() <= 4 {
|
||||||
|
let mut r = 0u128;
|
||||||
|
for t in 0..res.len() {
|
||||||
r <<= 32;
|
r <<= 32;
|
||||||
r |= $res[$res.len() - 1 - t] as $typ_inline;
|
r |= res[res.len() - 1 - t] as u128;
|
||||||
}
|
}
|
||||||
$typ($typ_inner::Inline(r))
|
SmallUint(SmallUintType::Inline(r))
|
||||||
} else {
|
} else {
|
||||||
let mut slice = ManuallyDrop::new($res.into_boxed_slice());
|
let mut slice = ManuallyDrop::new(res.into_boxed_slice());
|
||||||
$typ($typ_inner::Heap((slice.as_mut_ptr(), slice.len().try_into().unwrap())))
|
SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), slice.len())))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logic_op! {
|
basic_op!(BitXor, SmallUint, bitxor);
|
||||||
BitAnd, BitAndAssign, SmallUint, SmallUintType, bitand, bitand_assign;
|
|
||||||
i, j, p, q, s, t, slice, slice1, slice2, min, res;
|
|
||||||
{ inline_heap_to_inline! { bitand, SmallUint, SmallUintType, u128; i, slice } },
|
|
||||||
{ heap_heap_create_res_shortest! { bitand; slice1, slice2, min } },
|
|
||||||
{ heap_heap_return_any! { SmallUint, SmallUintType, u128; res } }
|
|
||||||
}
|
|
||||||
|
|
||||||
logic_op! {
|
impl<'a> BitXorAssign<&'a SmallUint> for SmallUint {
|
||||||
BitOr, BitOrAssign, SmallUint, SmallUintType, bitor, bitor_assign;
|
fn bitxor_assign(&mut self, rhs: &'a SmallUint) {
|
||||||
i, j, p, q, s, t, slice, slice1, slice2, min, res;
|
*self = self.clone() ^ rhs;
|
||||||
{ inline_heap_to_heap! { bitor_assign, SmallUint, SmallUintType; i, slice } },
|
}
|
||||||
{ heap_heap_create_res_longest! { bitor; slice1, slice2, min }},
|
}
|
||||||
{ heap_heap_return_heap! { SmallUint, SmallUintType; res } }
|
|
||||||
}
|
impl BitXorAssign<SmallUint> for SmallUint {
|
||||||
|
fn bitxor_assign(&mut self, rhs: SmallUint) {
|
||||||
logic_op! {
|
*self = self.clone() ^ rhs;
|
||||||
BitXor, BitXorAssign, SmallUint, SmallUintType, bitxor, bitxor_assign;
|
|
||||||
i, j, p, q, s, t, slice, slice1, slice2, min, res;
|
|
||||||
{ inline_heap_to_heap! { bitxor_assign, SmallUint, SmallUintType; i, slice } },
|
|
||||||
{ heap_heap_create_res_longest! { bitxor; slice1, slice2, min }},
|
|
||||||
{ heap_heap_return_any! { SmallUint, SmallUintType, u128; res } }
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
fn bitor_two_slices(slice1: &[u32], slice2: &[u32]) -> Vec<u32> {
|
|
||||||
let mut result = if slice1.len() > slice2.len() {
|
|
||||||
slice1.to_vec()
|
|
||||||
} else {
|
|
||||||
slice2.to_vec()
|
|
||||||
};
|
|
||||||
for l in 0..std::cmp::min(slice1.len(), slice2.len()) {
|
|
||||||
result[l] = slice1[l] | slice2[l];
|
|
||||||
}
|
|
||||||
result
|
|
||||||
}
|
|
||||||
|
|
||||||
fn bitand_two_slices_mixed_sign(positive: &[u32], negative: &[u32]) -> Vec<u32> {
|
|
||||||
// a & (-b) = (a | (b - 1)) + 1 - b
|
|
||||||
// 0 is not negative, so we can ignore it
|
|
||||||
let sub1 = sub_two_slices(negative, &[1]);
|
|
||||||
let or = bitor_two_slices(positive, &sub1);
|
|
||||||
let add = add_two_slices(&or, &[1]);
|
|
||||||
let sub2 = sub_two_slices(&add, negative);
|
|
||||||
sub2
|
|
||||||
}
|
|
||||||
|
|
||||||
logic_op! {
|
|
||||||
BitAnd, BitAndAssign, SmallInt, SmallIntType, bitand, bitand_assign;
|
|
||||||
i, j, p, q, s, t;
|
|
||||||
{
|
|
||||||
let slice = unsafe { core::slice::from_raw_parts(p, s.unsigned_abs()) };
|
|
||||||
match (i.cmp(&0), s.cmp(&0)) {
|
|
||||||
(Ordering::Equal, _) => SmallInt(SmallIntType::Inline(0)),
|
|
||||||
(Ordering::Greater, Ordering::Greater) => {
|
|
||||||
let j = { heap_to_inline! { i128; slice } };
|
|
||||||
SmallInt(SmallIntType::Inline(i & j))
|
|
||||||
},
|
|
||||||
(Ordering::Greater, Ordering::Less) => {
|
|
||||||
let j = { heap_to_inline! { i128; slice } };
|
|
||||||
SmallInt(SmallIntType::Inline(i & -j))
|
|
||||||
},
|
|
||||||
(Ordering::Less, Ordering::Greater) => {
|
|
||||||
let mut res = <Box<[u32]>>::from(slice);
|
|
||||||
let mut inline = i;
|
|
||||||
for idx in 0..4 {
|
|
||||||
res[idx] &= inline as u32;
|
|
||||||
inline >>= 32;
|
|
||||||
}
|
|
||||||
heap_heap_return_heap_inner! { SmallInt, SmallIntType; res }
|
|
||||||
},
|
|
||||||
(Ordering::Less, Ordering::Less) => {
|
|
||||||
let mut res = <Box<[u32]>>::from(slice);
|
|
||||||
let j = { heap_to_inline! { i128; slice } };
|
|
||||||
let lo = (i & j.wrapping_neg()).wrapping_neg();
|
|
||||||
let mut lo_remaining = lo;
|
|
||||||
for idx in 0..4 {
|
|
||||||
res[idx] = lo_remaining as u32;
|
|
||||||
lo_remaining >>= 32;
|
|
||||||
}
|
|
||||||
|
|
||||||
if lo == 0 && j != 0 {
|
|
||||||
let res2 = add_two_slices(&res, &[0, 0, 0, 0, 1]);
|
|
||||||
heap_heap_return_heap_neg! { SmallInt, SmallIntType; res2 }
|
|
||||||
} else {
|
|
||||||
heap_heap_return_heap_inner_neg! { SmallInt, SmallIntType; res }
|
|
||||||
}
|
|
||||||
},
|
|
||||||
(_, Ordering::Equal) => unreachable!("0 must be inline"),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
let slice1 = unsafe { core::slice::from_raw_parts(p, s.unsigned_abs()) };
|
|
||||||
let slice2 = unsafe { core::slice::from_raw_parts(q, t.unsigned_abs()) };
|
|
||||||
match (s.cmp(&0), t.cmp(&0)) {
|
|
||||||
(Ordering::Greater, Ordering::Greater) => {
|
|
||||||
let min = std::cmp::min(slice1.len(), slice2.len());
|
|
||||||
|
|
||||||
#[allow(unused_mut)]
|
|
||||||
let mut res = { heap_heap_create_res_shortest! { bitand; slice1, slice2, min } };
|
|
||||||
|
|
||||||
heap_heap_return_heap! { SmallInt, SmallIntType; res }
|
|
||||||
},
|
|
||||||
(Ordering::Greater, Ordering::Less) => {
|
|
||||||
let res = bitand_two_slices_mixed_sign(slice1, slice2);
|
|
||||||
heap_heap_return_heap! { SmallInt, SmallIntType; res }
|
|
||||||
},
|
|
||||||
(Ordering::Less, Ordering::Greater) => {
|
|
||||||
let res = bitand_two_slices_mixed_sign(slice2, slice1);
|
|
||||||
heap_heap_return_heap! { SmallInt, SmallIntType; res }
|
|
||||||
},
|
|
||||||
(Ordering::Less, Ordering::Less) => {
|
|
||||||
// (-a) & (-b) = -(((a-1) | (b-1)) + 1)
|
|
||||||
// 0 is not negative, so we can ignore it
|
|
||||||
let sub1 = sub_two_slices(slice1, &[1]);
|
|
||||||
let sub2 = sub_two_slices(slice2, &[1]);
|
|
||||||
let tmp = bitor_two_slices(&sub1, &sub2);
|
|
||||||
let res = add_two_slices(&tmp, &[1]);
|
|
||||||
heap_heap_return_heap_neg! { SmallInt, SmallIntType; res }
|
|
||||||
},
|
|
||||||
(Ordering::Equal, _) | (_, Ordering::Equal) => unreachable!("0 must be inline"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
20
src/ops.rs
20
src/ops.rs
|
@ -9,17 +9,9 @@ impl Neg for SmallInt {
|
||||||
|
|
||||||
fn neg(self) -> Self::Output {
|
fn neg(self) -> Self::Output {
|
||||||
match self.0 {
|
match self.0 {
|
||||||
SmallIntType::Inline(i) => {
|
SmallIntType::Inline(i) => SmallInt(SmallIntType::Inline(-i)),
|
||||||
if let Some(n) = i.checked_neg() {
|
|
||||||
SmallInt(SmallIntType::Inline(n))
|
|
||||||
} else {
|
|
||||||
// -i128::MIN = i128::MAX + 1 = 0x8000...
|
|
||||||
let mut val = ManuallyDrop::new(Box::new([0, 0, 0, 0x80_00_00_00]));
|
|
||||||
SmallInt(SmallIntType::Heap((val.as_mut_ptr(), 4)))
|
|
||||||
}
|
|
||||||
},
|
|
||||||
SmallIntType::Heap((r, s)) => {
|
SmallIntType::Heap((r, s)) => {
|
||||||
let size = s.unsigned_abs();
|
let size = usize::try_from(s.abs()).unwrap();
|
||||||
let slice = unsafe { core::slice::from_raw_parts(r, size) };
|
let slice = unsafe { core::slice::from_raw_parts(r, size) };
|
||||||
let mut ret = vec![0; size];
|
let mut ret = vec![0; size];
|
||||||
ret.clone_from_slice(slice);
|
ret.clone_from_slice(slice);
|
||||||
|
@ -66,7 +58,7 @@ macro_rules! basic_op {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add_two_slices(slice1: &[u32], slice2: &[u32]) -> Vec<u32> {
|
fn add_two_slices(slice1: &[u32], slice2: &[u32]) -> Vec<u32> {
|
||||||
let s = slice1.len();
|
let s = slice1.len();
|
||||||
let j = slice2.len();
|
let j = slice2.len();
|
||||||
|
|
||||||
|
@ -225,7 +217,7 @@ impl AddAssign<SmallInt> for SmallInt {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn sub_two_slices(slice1: &[u32], slice2: &[u32]) -> Vec<u32> {
|
fn sub_two_slices(slice1: &[u32], slice2: &[u32]) -> Vec<u32> {
|
||||||
let b = slice1.len();
|
let b = slice1.len();
|
||||||
let s = slice2.len();
|
let s = slice2.len();
|
||||||
|
|
||||||
|
@ -590,7 +582,7 @@ impl MulAssign<SmallInt> for SmallInt {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn div(_a: &SmallUint, _b: &SmallUint) -> SmallUint {
|
fn div(a: &SmallUint, b: &SmallUint) -> SmallUint {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -608,7 +600,7 @@ impl DivAssign<SmallUint> for SmallUint {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn div_signed(_a: &SmallInt, _b: &SmallInt) -> SmallInt {
|
fn div_signed(a: &SmallInt, b: &SmallInt) -> SmallInt {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,8 +40,8 @@ impl PartialOrd for SmallUint {
|
||||||
Ordering::Greater => Some(Ordering::Less),
|
Ordering::Greater => Some(Ordering::Less),
|
||||||
Ordering::Less => Some(Ordering::Greater),
|
Ordering::Less => Some(Ordering::Greater),
|
||||||
Ordering::Equal => {
|
Ordering::Equal => {
|
||||||
let slice1 = unsafe { core::slice::from_raw_parts(*r, *s) };
|
let slice1 = unsafe { core::slice::from_raw_parts(r, *s) };
|
||||||
let slice2 = unsafe { core::slice::from_raw_parts(*i, *j) };
|
let slice2 = unsafe { core::slice::from_raw_parts(i, *j) };
|
||||||
for i in 0..*s {
|
for i in 0..*s {
|
||||||
match slice1[s - 1 - i].cmp(&slice2[s - 1 - i]) {
|
match slice1[s - 1 - i].cmp(&slice2[s - 1 - i]) {
|
||||||
Ordering::Less => return Some(Ordering::Less),
|
Ordering::Less => return Some(Ordering::Less),
|
||||||
|
@ -69,8 +69,8 @@ impl PartialEq for SmallInt {
|
||||||
Ordering::Greater => false,
|
Ordering::Greater => false,
|
||||||
Ordering::Less => false,
|
Ordering::Less => false,
|
||||||
Ordering::Equal => {
|
Ordering::Equal => {
|
||||||
let us = s.unsigned_abs();
|
let us = usize::try_from(s.abs()).unwrap();
|
||||||
let uj = j.unsigned_abs();
|
let uj = usize::try_from(j.abs()).unwrap();
|
||||||
let slice1 = unsafe { core::slice::from_raw_parts(r, us) };
|
let slice1 = unsafe { core::slice::from_raw_parts(r, us) };
|
||||||
let slice2 = unsafe { core::slice::from_raw_parts(i, uj) };
|
let slice2 = unsafe { core::slice::from_raw_parts(i, uj) };
|
||||||
for i in 0..*s {
|
for i in 0..*s {
|
||||||
|
|
|
@ -12,7 +12,7 @@ impl core::fmt::Debug for SmallInt {
|
||||||
SmallIntType::Inline(i) => {
|
SmallIntType::Inline(i) => {
|
||||||
write!(f, "{}", i)?;
|
write!(f, "{}", i)?;
|
||||||
}
|
}
|
||||||
SmallIntType::Heap((_r, _s)) => {
|
SmallIntType::Heap((r, s)) => {
|
||||||
#[cfg(feature = "num-bigint")]
|
#[cfg(feature = "num-bigint")]
|
||||||
write!(f, "{}", BigInt::from(self))?;
|
write!(f, "{}", BigInt::from(self))?;
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ impl core::fmt::Debug for SmallUint {
|
||||||
SmallUintType::Inline(i) => {
|
SmallUintType::Inline(i) => {
|
||||||
write!(f, "{}", i)?;
|
write!(f, "{}", i)?;
|
||||||
}
|
}
|
||||||
SmallUintType::Heap((_r, _s)) => {
|
SmallUintType::Heap((r, s)) => {
|
||||||
#[cfg(feature = "num-bigint")]
|
#[cfg(feature = "num-bigint")]
|
||||||
write!(f, "{}", BigUint::from(self))?;
|
write!(f, "{}", BigUint::from(self))?;
|
||||||
|
|
||||||
|
@ -107,7 +107,8 @@ impl core::fmt::UpperHex for SmallInt {
|
||||||
_ => panic!("This should not happen."),
|
_ => panic!("This should not happen."),
|
||||||
};
|
};
|
||||||
|
|
||||||
let slice = unsafe { core::slice::from_raw_parts(r, s.unsigned_abs()) };
|
let slice =
|
||||||
|
unsafe { core::slice::from_raw_parts(r, usize::try_from(s.abs()).unwrap()) };
|
||||||
let mut iter = slice.iter().rev();
|
let mut iter = slice.iter().rev();
|
||||||
if let Some(i) = iter.next() {
|
if let Some(i) = iter.next() {
|
||||||
write!(f, "{}0x{:X}", sign, i)?;
|
write!(f, "{}0x{:X}", sign, i)?;
|
||||||
|
@ -136,7 +137,8 @@ impl core::fmt::LowerHex for SmallInt {
|
||||||
_ => panic!("This should not happen."),
|
_ => panic!("This should not happen."),
|
||||||
};
|
};
|
||||||
|
|
||||||
let slice = unsafe { core::slice::from_raw_parts(r, s.unsigned_abs()) };
|
let slice =
|
||||||
|
unsafe { core::slice::from_raw_parts(r, usize::try_from(s.abs()).unwrap()) };
|
||||||
let mut iter = slice.iter().rev();
|
let mut iter = slice.iter().rev();
|
||||||
if let Some(i) = iter.next() {
|
if let Some(i) = iter.next() {
|
||||||
write!(f, "{}0x{:x}", sign, i)?;
|
write!(f, "{}0x{:x}", sign, i)?;
|
||||||
|
|
|
@ -22,7 +22,7 @@ pub enum SmallUintType {
|
||||||
impl Drop for SmallInt {
|
impl Drop for SmallInt {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if let Self(SmallIntType::Heap((r, s))) = self {
|
if let Self(SmallIntType::Heap((r, s))) = self {
|
||||||
let size = s.unsigned_abs();
|
let size = usize::try_from(s.abs()).unwrap();
|
||||||
let slice = unsafe { core::slice::from_raw_parts_mut(*r, size) };
|
let slice = unsafe { core::slice::from_raw_parts_mut(*r, size) };
|
||||||
unsafe { std::mem::drop(Box::from_raw(slice)) }
|
unsafe { std::mem::drop(Box::from_raw(slice)) }
|
||||||
}
|
}
|
||||||
|
@ -58,7 +58,7 @@ impl Clone for SmallInt {
|
||||||
match self.0 {
|
match self.0 {
|
||||||
SmallIntType::Inline(i) => Self(SmallIntType::Inline(i)),
|
SmallIntType::Inline(i) => Self(SmallIntType::Inline(i)),
|
||||||
SmallIntType::Heap((r, s)) => {
|
SmallIntType::Heap((r, s)) => {
|
||||||
let size = s.unsigned_abs();
|
let size = usize::try_from(s.abs()).unwrap();
|
||||||
let slice = unsafe { core::slice::from_raw_parts(r, size) };
|
let slice = unsafe { core::slice::from_raw_parts(r, size) };
|
||||||
let mut ret = vec![0; size];
|
let mut ret = vec![0; size];
|
||||||
ret.clone_from_slice(slice);
|
ret.clone_from_slice(slice);
|
||||||
|
@ -86,7 +86,7 @@ impl Hash for SmallInt {
|
||||||
match self.0 {
|
match self.0 {
|
||||||
SmallIntType::Inline(i) => i.hash(state),
|
SmallIntType::Inline(i) => i.hash(state),
|
||||||
SmallIntType::Heap((r, s)) => {
|
SmallIntType::Heap((r, s)) => {
|
||||||
let size = s.unsigned_abs();
|
let size = usize::try_from(s.abs()).unwrap();
|
||||||
let slice = unsafe { core::slice::from_raw_parts(r, size) };
|
let slice = unsafe { core::slice::from_raw_parts(r, size) };
|
||||||
slice.hash(state);
|
slice.hash(state);
|
||||||
}
|
}
|
||||||
|
|
359
src/tests.rs
359
src/tests.rs
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
use crate::SmallInt;
|
use crate::SmallInt;
|
||||||
use crate::SmallUint;
|
use crate::SmallUint;
|
||||||
use core::fmt::Debug;
|
|
||||||
|
|
||||||
#[cfg(feature = "num-bigint")]
|
#[cfg(feature = "num-bigint")]
|
||||||
use num_bigint::{BigInt, BigUint, Sign};
|
use num_bigint::{BigInt, BigUint, Sign};
|
||||||
|
@ -33,213 +32,305 @@ conversion_tests!(i64, test_i64);
|
||||||
conversion_tests!(u128, test_u128);
|
conversion_tests!(u128, test_u128);
|
||||||
conversion_tests!(i128, test_i128);
|
conversion_tests!(i128, test_i128);
|
||||||
|
|
||||||
#[cfg(feature = "num-bigint")]
|
|
||||||
fn run_tests_u_inner(test: impl Fn(BigUint)) {
|
|
||||||
for i in 0u8..=7 {
|
|
||||||
test(BigUint::from(i));
|
|
||||||
}
|
|
||||||
for shift_scale in 1..=16 {
|
|
||||||
for shift_offset in -2..=2 {
|
|
||||||
let shift: i16 = (shift_scale * 32) + shift_offset;
|
|
||||||
test(BigUint::from(1u8) << shift);
|
|
||||||
for offset in 1u8..=7 {
|
|
||||||
test((BigUint::from(1u8) << shift) + BigUint::from(offset));
|
|
||||||
test((BigUint::from(1u8) << shift) - BigUint::from(offset));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
test(BigUint::new(vec![3, 9, 8, 3, 1]));
|
|
||||||
test(BigUint::new(vec![5, 4, 9, 3, 1, 81]));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
#[cfg(feature = "num-bigint")]
|
|
||||||
fn run_tests_u_1<T: Eq + Debug>(small: impl Fn(SmallUint) -> T, big: impl Fn(&BigUint) -> T) {
|
|
||||||
run_tests_u_inner(|i| {
|
|
||||||
let small_result = small(SmallUint::from(&i));
|
|
||||||
let big_result = big(&i);
|
|
||||||
assert_eq!(
|
|
||||||
small_result,
|
|
||||||
big_result,
|
|
||||||
"{:#x}", i
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "num-bigint")]
|
|
||||||
fn run_tests_u_2<T: Eq + Debug>(
|
|
||||||
small: impl Fn(SmallUint, SmallUint) -> T,
|
|
||||||
big: impl Fn(&BigUint, &BigUint) -> T
|
|
||||||
) {
|
|
||||||
run_tests_u_inner(|i| run_tests_u_inner(|k| {
|
|
||||||
let small_result = small(SmallUint::from(&i), SmallUint::from(&k));
|
|
||||||
let big_result = big(&i, &k);
|
|
||||||
assert_eq!(
|
|
||||||
small_result,
|
|
||||||
big_result,
|
|
||||||
"{:#x} {:#x}", i, k
|
|
||||||
);
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "num-bigint")]
|
|
||||||
fn run_tests_i_inner(test: impl Fn(BigInt)) {
|
|
||||||
run_tests_u_inner(|value| {
|
|
||||||
test(BigInt::from_biguint(Sign::Plus, value.clone()));
|
|
||||||
test(BigInt::from_biguint(Sign::Minus, value));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "num-bigint")]
|
|
||||||
fn run_tests_i_1<T: Eq + Debug>(small: impl Fn(SmallInt) -> T, big: impl Fn(&BigInt) -> T) {
|
|
||||||
run_tests_i_inner(|i| {
|
|
||||||
let small_result = small(SmallInt::from(&i));
|
|
||||||
let big_result = big(&i);
|
|
||||||
assert_eq!(
|
|
||||||
small_result,
|
|
||||||
big_result,
|
|
||||||
"{:#x}", i
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "num-bigint")]
|
|
||||||
fn run_tests_i_2<T: Eq + Debug>(
|
|
||||||
small: impl Fn(SmallInt, SmallInt) -> T,
|
|
||||||
big: impl Fn(&BigInt, &BigInt) -> T
|
|
||||||
) {
|
|
||||||
run_tests_i_inner(|i| run_tests_i_inner(|k| {
|
|
||||||
let small_result = small(SmallInt::from(&i), SmallInt::from(&k));
|
|
||||||
let big_result = big(&i, &k);
|
|
||||||
assert_eq!(
|
|
||||||
small_result,
|
|
||||||
big_result,
|
|
||||||
"{:#x} {:#x}", i, k
|
|
||||||
);
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "num-bigint")]
|
#[cfg(feature = "num-bigint")]
|
||||||
fn test_cmp_u() {
|
fn test_cmp_u() {
|
||||||
run_tests_u_2(
|
let i = SmallUint::from(30u32);
|
||||||
|i, k| i.partial_cmp(&k),
|
let k = SmallUint::from(50u32);
|
||||||
|i, k| i.partial_cmp(&k),
|
assert!(i < k);
|
||||||
);
|
|
||||||
|
let i = SmallUint::from(u128::MAX);
|
||||||
|
let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81]));
|
||||||
|
assert!(i < k);
|
||||||
|
|
||||||
|
let i = SmallUint::from(&BigUint::new(vec![3, 9, 8, 3, 1]));
|
||||||
|
let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81]));
|
||||||
|
assert!(i < k);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "num-bigint")]
|
#[cfg(feature = "num-bigint")]
|
||||||
fn test_cmp_i() {
|
fn test_cmp_i() {
|
||||||
run_tests_i_2(
|
let i = SmallInt::from(30u32);
|
||||||
|i, k| i.partial_cmp(&k),
|
let k = SmallInt::from(50u32);
|
||||||
|i, k| i.partial_cmp(&k),
|
assert!(i < k);
|
||||||
);
|
|
||||||
|
let i = SmallInt::from(u128::MAX);
|
||||||
|
let k = SmallInt::from(&BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81]));
|
||||||
|
assert!(i < k);
|
||||||
|
|
||||||
|
let i = SmallInt::from(&BigInt::new(Sign::Plus, vec![3, 9, 8, 3, 1]));
|
||||||
|
let k = SmallInt::from(&BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81]));
|
||||||
|
assert!(i < k);
|
||||||
|
|
||||||
|
let i = SmallInt::from(30u32);
|
||||||
|
let k = -SmallInt::from(50u32);
|
||||||
|
assert!(k < i);
|
||||||
|
|
||||||
|
let i = SmallInt::from(u128::MAX);
|
||||||
|
let k = -SmallInt::from(&BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81]));
|
||||||
|
assert!(k < i);
|
||||||
|
|
||||||
|
let i = SmallInt::from(&BigInt::new(Sign::Plus, vec![3, 9, 8, 3, 1]));
|
||||||
|
let k = -SmallInt::from(&BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81]));
|
||||||
|
assert!(k < i);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "num-bigint")]
|
#[cfg(feature = "num-bigint")]
|
||||||
fn test_op_add_u() {
|
fn test_op_add_u() {
|
||||||
run_tests_u_2(
|
let i = SmallUint::from(u128::MAX);
|
||||||
|i, k| BigUint::from(&(i + k)),
|
let k = SmallUint::from(u128::MAX);
|
||||||
|i, k| i + k,
|
let q = i + k;
|
||||||
|
assert_eq!(BigUint::from(&q), BigUint::from(u128::MAX) + u128::MAX);
|
||||||
|
|
||||||
|
let i = SmallUint::from(u128::MAX);
|
||||||
|
let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81]));
|
||||||
|
let q = i + k;
|
||||||
|
assert_eq!(
|
||||||
|
BigUint::from(&q),
|
||||||
|
BigUint::new(vec![5, 4, 9, 3, 1, 81]) + u128::MAX
|
||||||
|
);
|
||||||
|
|
||||||
|
let i = SmallUint::from(&BigUint::new(vec![3, 9, 8, 3, 1]));
|
||||||
|
let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81]));
|
||||||
|
let q = i + k;
|
||||||
|
assert_eq!(
|
||||||
|
BigUint::from(&q),
|
||||||
|
BigUint::new(vec![3, 9, 8, 3, 1]) + BigUint::new(vec![5, 4, 9, 3, 1, 81])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "num-bigint")]
|
#[cfg(feature = "num-bigint")]
|
||||||
fn test_op_add_i() {
|
fn test_op_add_i() {
|
||||||
run_tests_i_2(
|
let i = SmallInt::from(u128::MAX);
|
||||||
|i, k| BigInt::from(&(i + k)),
|
let k = -SmallInt::from(u128::MAX);
|
||||||
|i, k| i + k,
|
let q = i + k;
|
||||||
|
assert_eq!(BigInt::from(&q), BigInt::from(0));
|
||||||
|
|
||||||
|
let i = SmallInt::from(u128::MAX);
|
||||||
|
let k = -SmallInt::from(&BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81]));
|
||||||
|
let q = i + k;
|
||||||
|
assert_eq!(
|
||||||
|
BigInt::from(&q),
|
||||||
|
u128::MAX - BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81])
|
||||||
|
);
|
||||||
|
|
||||||
|
let i = -SmallInt::from(&BigInt::new(Sign::Plus, vec![3, 9, 8, 3, 1]));
|
||||||
|
let k = SmallInt::from(&BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81]));
|
||||||
|
let q = i + k;
|
||||||
|
assert_eq!(
|
||||||
|
BigInt::from(&q),
|
||||||
|
BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81])
|
||||||
|
- BigInt::new(Sign::Plus, vec![3, 9, 8, 3, 1])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "num-bigint")]
|
#[cfg(feature = "num-bigint")]
|
||||||
fn test_op_mul_u() {
|
fn test_op_mul_u() {
|
||||||
run_tests_u_2(
|
let i = SmallUint::from(u128::MAX);
|
||||||
|i, k| BigUint::from(&(i * k)),
|
let k = SmallUint::from(u128::MAX);
|
||||||
|i, k| i * k,
|
let q = i * k;
|
||||||
|
assert_eq!(BigUint::from(&q), BigUint::from(u128::MAX) * u128::MAX);
|
||||||
|
|
||||||
|
let i = SmallUint::from(u32::MAX);
|
||||||
|
let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81]));
|
||||||
|
let q = i * k;
|
||||||
|
assert_eq!(
|
||||||
|
BigUint::from(&q),
|
||||||
|
BigUint::new(vec![5, 4, 9, 3, 1, 81]) * u32::MAX
|
||||||
|
);
|
||||||
|
|
||||||
|
let i = SmallUint::from(&BigUint::new(vec![3, 9, 8, 3, 1]));
|
||||||
|
let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81]));
|
||||||
|
let q = i * k;
|
||||||
|
assert_eq!(
|
||||||
|
BigUint::from(&q),
|
||||||
|
BigUint::new(vec![3, 9, 8, 3, 1]) * BigUint::new(vec![5, 4, 9, 3, 1, 81])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "num-bigint")]
|
#[cfg(feature = "num-bigint")]
|
||||||
fn test_op_mul_i() {
|
fn test_op_mul_i() {
|
||||||
run_tests_i_2(
|
let i = -SmallInt::from(u128::MAX);
|
||||||
|i, k| BigInt::from(&(i * k)),
|
let k = SmallInt::from(u128::MAX);
|
||||||
|i, k| i * k,
|
let q = i * k;
|
||||||
|
assert_eq!(BigInt::from(&q), -BigInt::from(u128::MAX) * u128::MAX);
|
||||||
|
|
||||||
|
let i = -SmallInt::from(u32::MAX);
|
||||||
|
let k = -SmallInt::from(&BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81]));
|
||||||
|
let q = i * k;
|
||||||
|
assert_eq!(
|
||||||
|
BigInt::from(&q),
|
||||||
|
BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81]) * u32::MAX
|
||||||
|
);
|
||||||
|
|
||||||
|
let i = -SmallInt::from(&BigInt::new(Sign::Plus, vec![3, 9, 8, 3, 1]));
|
||||||
|
let k = SmallInt::from(&BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81]));
|
||||||
|
let q = i * k;
|
||||||
|
assert_eq!(
|
||||||
|
BigInt::from(&q),
|
||||||
|
BigInt::new(Sign::Plus, vec![3, 9, 8, 3, 1])
|
||||||
|
* -BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "num-bigint")]
|
#[cfg(feature = "num-bigint")]
|
||||||
fn test_op_sub_u() {
|
fn test_op_sub_u() {
|
||||||
run_tests_u_2(
|
let i = SmallUint::from(u128::MAX);
|
||||||
|i, k| (i >= k).then(|| BigUint::from(&(i - k))),
|
let k = SmallUint::from(u128::MAX);
|
||||||
|i, k| (i >= k).then(|| i - k),
|
let q = i - k;
|
||||||
|
assert_eq!(BigUint::from(&q), BigUint::from(u128::MAX) - u128::MAX);
|
||||||
|
|
||||||
|
let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81]));
|
||||||
|
let i = SmallUint::from(u128::MAX);
|
||||||
|
let q = k - i;
|
||||||
|
assert_eq!(
|
||||||
|
BigUint::from(&q),
|
||||||
|
BigUint::new(vec![5, 4, 9, 3, 1, 81]) - u128::MAX
|
||||||
|
);
|
||||||
|
|
||||||
|
let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81]));
|
||||||
|
let i = SmallUint::from(&BigUint::new(vec![3, 9, 8, 3, 1]));
|
||||||
|
let q = k - i;
|
||||||
|
assert_eq!(
|
||||||
|
BigUint::from(&q),
|
||||||
|
BigUint::new(vec![5, 4, 9, 3, 1, 81]) - BigUint::new(vec![3, 9, 8, 3, 1])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "num-bigint")]
|
#[cfg(feature = "num-bigint")]
|
||||||
fn test_op_sub_i() {
|
fn test_op_sub_i() {
|
||||||
run_tests_i_2(
|
let i = -SmallInt::from(u128::MAX);
|
||||||
|i, k| BigInt::from(&(i - k)),
|
let k = SmallInt::from(u128::MAX);
|
||||||
|i, k| i - k,
|
let q = i - k;
|
||||||
|
assert_eq!(BigInt::from(&q), -BigInt::from(u128::MAX) - u128::MAX);
|
||||||
|
|
||||||
|
let k = SmallInt::from(&BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81]));
|
||||||
|
let i = -SmallInt::from(u128::MAX);
|
||||||
|
let q = k - i;
|
||||||
|
assert_eq!(
|
||||||
|
BigInt::from(&q),
|
||||||
|
BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81]) + u128::MAX
|
||||||
|
);
|
||||||
|
|
||||||
|
let k = -SmallInt::from(&BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81]));
|
||||||
|
let i = SmallInt::from(&BigInt::new(Sign::Plus, vec![3, 9, 8, 3, 1]));
|
||||||
|
let q = k - i;
|
||||||
|
assert_eq!(
|
||||||
|
BigInt::from(&q),
|
||||||
|
-(BigInt::new(Sign::Plus, vec![5, 4, 9, 3, 1, 81])
|
||||||
|
+ BigInt::new(Sign::Plus, vec![3, 9, 8, 3, 1]))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "num-bigint")]
|
#[cfg(feature = "num-bigint")]
|
||||||
fn test_op_and_u() {
|
fn test_op_and_u() {
|
||||||
run_tests_u_2(
|
let i = SmallUint::from(u128::MAX);
|
||||||
|i, k| BigUint::from(&(i & k)),
|
let k = SmallUint::from(u128::MAX);
|
||||||
|i, k| i & k,
|
let q = i & k;
|
||||||
|
assert_eq!(
|
||||||
|
BigUint::from(&q),
|
||||||
|
BigUint::from(u128::MAX) & BigUint::from(u128::MAX)
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81]));
|
||||||
#[cfg(feature = "num-bigint")]
|
let i = SmallUint::from(u128::MAX);
|
||||||
fn test_op_and_i() {
|
let q = k & i;
|
||||||
run_tests_i_2(
|
assert_eq!(
|
||||||
|i, k| BigInt::from(&(i & k)),
|
BigUint::from(&q),
|
||||||
|i, k| i & k,
|
BigUint::new(vec![5, 4, 9, 3]) & BigUint::from(u128::MAX)
|
||||||
|
);
|
||||||
|
|
||||||
|
let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81]));
|
||||||
|
let i = SmallUint::from(&BigUint::new(vec![3, 9, 8, 3, 1]));
|
||||||
|
let q = k & i;
|
||||||
|
assert_eq!(
|
||||||
|
BigUint::from(&q),
|
||||||
|
BigUint::new(vec![5, 4, 9, 3, 1, 81]) & BigUint::new(vec![3, 9, 8, 3, 1])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "num-bigint")]
|
#[cfg(feature = "num-bigint")]
|
||||||
fn test_op_or_u() {
|
fn test_op_or_u() {
|
||||||
run_tests_u_2(
|
let i = SmallUint::from(u128::MAX);
|
||||||
|i, k| BigUint::from(&(i | k)),
|
let k = SmallUint::from(u128::MAX);
|
||||||
|i, k| i | k,
|
let q = i | k;
|
||||||
|
assert_eq!(
|
||||||
|
BigUint::from(&q),
|
||||||
|
BigUint::from(u128::MAX) | BigUint::from(u128::MAX)
|
||||||
|
);
|
||||||
|
|
||||||
|
let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81]));
|
||||||
|
let i = SmallUint::from(u128::MAX);
|
||||||
|
let q = k | i;
|
||||||
|
assert_eq!(
|
||||||
|
BigUint::from(&q),
|
||||||
|
BigUint::new(vec![5, 4, 9, 3, 1, 81]) | BigUint::from(u128::MAX)
|
||||||
|
);
|
||||||
|
|
||||||
|
let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81]));
|
||||||
|
let i = SmallUint::from(&BigUint::new(vec![3, 9, 8, 3, 1]));
|
||||||
|
let q = k | i;
|
||||||
|
assert_eq!(
|
||||||
|
BigUint::from(&q),
|
||||||
|
BigUint::new(vec![5, 4, 9, 3, 1, 81]) | BigUint::new(vec![3, 9, 8, 3, 1])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "num-bigint")]
|
#[cfg(feature = "num-bigint")]
|
||||||
fn test_op_xor_u() {
|
fn test_op_xor_u() {
|
||||||
run_tests_u_2(
|
let i = SmallUint::from(u128::MAX);
|
||||||
|i, k| BigUint::from(&(i ^ k)),
|
let k = SmallUint::from(u128::MAX);
|
||||||
|i, k| i ^ k,
|
let q = i ^ k;
|
||||||
|
assert_eq!(
|
||||||
|
BigUint::from(&q),
|
||||||
|
BigUint::from(u128::MAX) ^ BigUint::from(u128::MAX)
|
||||||
|
);
|
||||||
|
|
||||||
|
let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81]));
|
||||||
|
let i = SmallUint::from(u128::MAX);
|
||||||
|
let q = k ^ i;
|
||||||
|
assert_eq!(
|
||||||
|
BigUint::from(&q),
|
||||||
|
BigUint::new(vec![5, 4, 9, 3, 1, 81]) ^ BigUint::from(u128::MAX)
|
||||||
|
);
|
||||||
|
|
||||||
|
let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81]));
|
||||||
|
let i = SmallUint::from(&BigUint::new(vec![3, 9, 8, 3, 1]));
|
||||||
|
let q = k ^ i;
|
||||||
|
assert_eq!(
|
||||||
|
BigUint::from(&q),
|
||||||
|
BigUint::new(vec![5, 4, 9, 3, 1, 81]) ^ BigUint::new(vec![3, 9, 8, 3, 1])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "num-bigint")]
|
#[cfg(feature = "num-bigint")]
|
||||||
fn test_op_neg() {
|
fn test_op_neg() {
|
||||||
run_tests_i_1(|i| BigInt::from(&-i), |i| -i.clone());
|
let i = SmallInt::from(u128::MAX);
|
||||||
|
let q = -i;
|
||||||
|
assert_eq!(BigInt::from(&q), -BigInt::from(u128::MAX));
|
||||||
|
|
||||||
|
let i = SmallInt::from(i64::MIN);
|
||||||
|
let q = -i;
|
||||||
|
assert_eq!(BigInt::from(&q), -BigInt::from(i64::MIN));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "num-bigint")]
|
#[cfg(feature = "num-bigint")]
|
||||||
fn test_conversion_sign_drop() {
|
fn test_conversion_sign_drop() {
|
||||||
run_tests_i_1(
|
let si = SmallInt::from(i128::MIN + 1);
|
||||||
|i| BigUint::from(&SmallUint::from_smallint_unsigned(i)),
|
let i = SmallUint::from_smallint_unsigned(si);
|
||||||
|i| i.magnitude().clone()
|
assert_eq!(BigUint::from(&i), BigUint::from(-(i128::MIN + 1) as u128))
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user