This commit is contained in:
EvilMuffinHa 2022-08-06 16:40:19 -04:00
parent 64ad3189bf
commit cf9c8b0b78
8 changed files with 109 additions and 152 deletions

View File

@ -79,5 +79,3 @@ impl From<&SmallUint> for BigUint {
} }
} }
} }

View File

@ -1,7 +1,7 @@
use crate::SmallInt;
use crate::SmallUint;
use crate::SmallIntError;
use crate::smallint::{SmallIntType, SmallUintType}; use crate::smallint::{SmallIntType, SmallUintType};
use crate::SmallInt;
use crate::SmallIntError;
use crate::SmallUint;
use core::mem::ManuallyDrop; use core::mem::ManuallyDrop;
macro_rules! int_impl { macro_rules! int_impl {

View File

@ -19,8 +19,8 @@ mod error;
pub use error::SmallIntError; pub use error::SmallIntError;
mod convert; mod convert;
mod ops;
mod logic; mod logic;
mod ops;
mod bigint; mod bigint;

View File

@ -1,74 +1,63 @@
use crate::SmallUint;
use crate::smallint::SmallUintType; use crate::smallint::SmallUintType;
use core::ops::{BitAnd, BitOr, BitXor}; use crate::SmallUint;
use core::mem::ManuallyDrop; use core::mem::ManuallyDrop;
use core::ops::{BitAnd, BitOr, BitXor};
macro_rules! basic_op { macro_rules! basic_op {
($imp:ident, $typ:ty, $fun:ident) => { ($imp:ident, $typ:ty, $fun:ident) => {
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 {
$fun(self, rhs) $fun(self, rhs)
} }
} }
impl<'a> $imp<$typ> for &'a $typ { impl<'a> $imp<$typ> for &'a $typ {
type Output = $typ; type Output = $typ;
fn $fun(self, rhs: $typ) -> Self::Output { fn $fun(self, rhs: $typ) -> Self::Output {
self.$fun(&rhs) self.$fun(&rhs)
} }
} }
impl<'a> $imp<&'a $typ> for $typ { impl<'a> $imp<&'a $typ> for $typ {
type Output = $typ; type Output = $typ;
fn $fun(self, rhs: &$typ) -> Self::Output { fn $fun(self, rhs: &$typ) -> Self::Output {
(&self).$fun(rhs) (&self).$fun(rhs)
} }
} }
impl $imp<$typ> for $typ { impl $imp<$typ> for $typ {
type Output = $typ; type Output = $typ;
fn $fun(self, rhs: $typ) -> Self::Output { fn $fun(self, rhs: $typ) -> Self::Output {
(&self).$fun(&rhs) (&self).$fun(&rhs)
} }
} }
} };
} }
fn bitand(a: &SmallUint, b: &SmallUint) -> SmallUint { fn bitand(a: &SmallUint, b: &SmallUint) -> SmallUint {
match (&a.0, &b.0) { match (&a.0, &b.0) {
(&SmallUintType::Inline(i), &SmallUintType::Inline(j)) => { (&SmallUintType::Inline(i), &SmallUintType::Inline(j)) => {
SmallUint(SmallUintType::Inline(i & j)) SmallUint(SmallUintType::Inline(i & j))
}, }
(&SmallUintType::Inline(i), &SmallUintType::Heap((r, s))) | (&SmallUintType::Heap((r, s)), &SmallUintType::Inline(i)) => { (&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 slice = unsafe { core::slice::from_raw_parts(r, s) };
let mut j = 0u128; let mut j = 0u128;
for i in 0..4 { for i in 0..4 {
j <<= 32; j <<= 32;
j |= slice[3 - i] as u128; j |= slice[3 - i] as u128;
} }
SmallUint(SmallUintType::Inline(i & j)) SmallUint(SmallUintType::Inline(i & j))
}, }
(&SmallUintType::Heap((r, s)), &SmallUintType::Heap((i, j))) => { (&SmallUintType::Heap((r, s)), &SmallUintType::Heap((i, j))) => {
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) };
@ -105,8 +94,9 @@ fn bitor(a: &SmallUint, b: &SmallUint) -> SmallUint {
match (&a.0, &b.0) { match (&a.0, &b.0) {
(&SmallUintType::Inline(i), &SmallUintType::Inline(j)) => { (&SmallUintType::Inline(i), &SmallUintType::Inline(j)) => {
SmallUint(SmallUintType::Inline(i | j)) SmallUint(SmallUintType::Inline(i | j))
}, }
(&SmallUintType::Inline(i), &SmallUintType::Heap((r, s))) | (&SmallUintType::Heap((r, s)), &SmallUintType::Inline(i)) => { (&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 slice = unsafe { core::slice::from_raw_parts(r, s) };
let mut retvec = slice.to_vec(); let mut retvec = slice.to_vec();
@ -122,11 +112,9 @@ fn bitor(a: &SmallUint, b: &SmallUint) -> SmallUint {
let mut retslice = ManuallyDrop::new(retvec.into_boxed_slice()); let mut retslice = ManuallyDrop::new(retvec.into_boxed_slice());
SmallUint(SmallUintType::Heap((retslice.as_mut_ptr(), retslice.len()))) SmallUint(SmallUintType::Heap((retslice.as_mut_ptr(), retslice.len())))
} }
(&SmallUintType::Heap((r, s)), &SmallUintType::Heap((i, j))) => { (&SmallUintType::Heap((r, s)), &SmallUintType::Heap((i, j))) => {
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) };
@ -146,9 +134,7 @@ fn bitor(a: &SmallUint, b: &SmallUint) -> SmallUint {
let mut slice = ManuallyDrop::new(retvec.into_boxed_slice()); let mut slice = ManuallyDrop::new(retvec.into_boxed_slice());
SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), slice.len()))) SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), slice.len())))
} }
} }
} }
@ -159,7 +145,8 @@ fn bitxor(a: &SmallUint, b: &SmallUint) -> SmallUint {
(&SmallUintType::Inline(i), &SmallUintType::Inline(j)) => { (&SmallUintType::Inline(i), &SmallUintType::Inline(j)) => {
SmallUint(SmallUintType::Inline(i ^ j)) SmallUint(SmallUintType::Inline(i ^ j))
} }
(&SmallUintType::Inline(i), &SmallUintType::Heap((r, s))) | (&SmallUintType::Heap((r, s)), &SmallUintType::Inline(i)) => { (&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 slice = unsafe { core::slice::from_raw_parts(r, s) };
let mut retvec = slice.to_vec(); let mut retvec = slice.to_vec();
@ -175,12 +162,9 @@ fn bitxor(a: &SmallUint, b: &SmallUint) -> SmallUint {
let mut retslice = ManuallyDrop::new(retvec.into_boxed_slice()); let mut retslice = ManuallyDrop::new(retvec.into_boxed_slice());
SmallUint(SmallUintType::Heap((retslice.as_mut_ptr(), retslice.len()))) SmallUint(SmallUintType::Heap((retslice.as_mut_ptr(), retslice.len())))
}
},
(&SmallUintType::Heap((r, s)), &SmallUintType::Heap((i, j))) => { (&SmallUintType::Heap((r, s)), &SmallUintType::Heap((i, j))) => {
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) };

View File

@ -1,57 +1,45 @@
use crate::SmallUint;
use crate::smallint::SmallUintType; use crate::smallint::SmallUintType;
use core::ops::{Add, Sub, Mul}; use crate::SmallUint;
use core::mem::ManuallyDrop; use core::mem::ManuallyDrop;
use core::ops::{Add, Mul, Sub};
macro_rules! basic_op { macro_rules! basic_op {
($imp:ident, $typ:ty, $fun:ident) => { ($imp:ident, $typ:ty, $fun:ident) => {
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 {
$fun(self, rhs) $fun(self, rhs)
} }
} }
impl<'a> $imp<$typ> for &'a $typ { impl<'a> $imp<$typ> for &'a $typ {
type Output = $typ; type Output = $typ;
fn $fun(self, rhs: $typ) -> Self::Output { fn $fun(self, rhs: $typ) -> Self::Output {
self.$fun(&rhs) self.$fun(&rhs)
} }
} }
impl<'a> $imp<&'a $typ> for $typ { impl<'a> $imp<&'a $typ> for $typ {
type Output = $typ; type Output = $typ;
fn $fun(self, rhs: &$typ) -> Self::Output { fn $fun(self, rhs: &$typ) -> Self::Output {
(&self).$fun(rhs) (&self).$fun(rhs)
} }
} }
impl $imp<$typ> for $typ { impl $imp<$typ> for $typ {
type Output = $typ; type Output = $typ;
fn $fun(self, rhs: $typ) -> Self::Output { fn $fun(self, rhs: $typ) -> Self::Output {
(&self).$fun(&rhs) (&self).$fun(&rhs)
} }
} }
} };
} }
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();
@ -60,24 +48,14 @@ fn add_two_slices(slice1: &[u32], slice2: &[u32]) -> Vec<u32> {
let mut carry = false; let mut carry = false;
for t in 0..larger { for t in 0..larger {
let value1 = if t < s { slice1[t] } else { 0 };
let value1 = if t < s { let value2 = if t < j { slice2[t] } else { 0 };
slice1[t]
} else {
0
};
let value2 = if t < j {
slice2[t]
} else {
0
};
let (val, overflow) = value1.overflowing_add(value2); let (val, overflow) = value1.overflowing_add(value2);
let (cval, coverflow) = val.overflowing_add(carry as u32); let (cval, coverflow) = val.overflowing_add(carry as u32);
res.push(cval); res.push(cval);
carry = overflow | coverflow; carry = overflow | coverflow;
} }
if carry { if carry {
@ -89,32 +67,28 @@ fn add_two_slices(slice1: &[u32], slice2: &[u32]) -> Vec<u32> {
res res
} }
fn add(a: &SmallUint, b: &SmallUint) -> SmallUint { fn add(a: &SmallUint, b: &SmallUint) -> SmallUint {
match (&a.0, &b.0) { match (&a.0, &b.0) {
(&SmallUintType::Inline(i), &SmallUintType::Inline(j)) => { (&SmallUintType::Inline(i), &SmallUintType::Inline(j)) => match i.overflowing_add(j) {
match i.overflowing_add(j) { (t, false) => SmallUint(SmallUintType::Inline(t)),
(t, false) => SmallUint(SmallUintType::Inline(t)), (t, true) => {
(t, true) => { let mut res = [0, 0, 0, 0, 1];
let mut res = [0, 0, 0, 0, 1];
let mut v = t; let mut v = t;
#[allow(clippy::needless_range_loop)] #[allow(clippy::needless_range_loop)]
for r in 0..4 { for r in 0..4 {
res[r] = v as u32; res[r] = v as u32;
v >>= 32;
}
let mut slice = ManuallyDrop::new(<Box<[u32]>>::from(res));
SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), 5)))
v >>= 32;
} }
let mut slice = ManuallyDrop::new(<Box<[u32]>>::from(res));
SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), 5)))
} }
}, },
(&SmallUintType::Heap((r, s)), &SmallUintType::Inline(i)) | (&SmallUintType::Inline(i), &SmallUintType::Heap((r, s))) => { (&SmallUintType::Heap((r, s)), &SmallUintType::Inline(i))
| (&SmallUintType::Inline(i), &SmallUintType::Heap((r, s))) => {
let slice1 = unsafe { core::slice::from_raw_parts(r, s) }; let slice1 = unsafe { core::slice::from_raw_parts(r, s) };
let mut res = [0, 0, 0, 0]; let mut res = [0, 0, 0, 0];
@ -133,11 +107,8 @@ fn add(a: &SmallUint, b: &SmallUint) -> SmallUint {
let mut slice = ManuallyDrop::new(result.into_boxed_slice()); let mut slice = ManuallyDrop::new(result.into_boxed_slice());
SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), size))) SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), size)))
}
},
(&SmallUintType::Heap((r, s)), &SmallUintType::Heap((i, j))) => { (&SmallUintType::Heap((r, s)), &SmallUintType::Heap((i, j))) => {
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) };
@ -147,7 +118,6 @@ fn add(a: &SmallUint, b: &SmallUint) -> SmallUint {
let mut slice = ManuallyDrop::new(res.into_boxed_slice()); let mut slice = ManuallyDrop::new(res.into_boxed_slice());
SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), size))) SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), size)))
} }
} }
} }
@ -168,11 +138,7 @@ fn sub_two_slices(slice1: &[u32], slice2: &[u32]) -> Vec<u32> {
for i in 0..b { for i in 0..b {
let mut value1 = slice1[i]; let mut value1 = slice1[i];
let value2 = if i < s { let value2 = if i < s { slice2[i] } else { 0 };
slice2[i]
} else {
0
};
if borrow { if borrow {
let (temp, b) = value1.overflowing_sub(1); let (temp, b) = value1.overflowing_sub(1);
@ -186,7 +152,6 @@ fn sub_two_slices(slice1: &[u32], slice2: &[u32]) -> Vec<u32> {
let val = value1.wrapping_sub(value2); let val = value1.wrapping_sub(value2);
res.push(val); res.push(val);
} }
if borrow { if borrow {
@ -194,11 +159,9 @@ fn sub_two_slices(slice1: &[u32], slice2: &[u32]) -> Vec<u32> {
} }
res res
} }
fn sub(a: &SmallUint, b: &SmallUint) -> SmallUint { fn sub(a: &SmallUint, b: &SmallUint) -> SmallUint {
match (&a.0, &b.0) { match (&a.0, &b.0) {
(&SmallUintType::Inline(i), &SmallUintType::Inline(j)) => { (&SmallUintType::Inline(i), &SmallUintType::Inline(j)) => {
if let (t, false) = i.overflowing_sub(j) { if let (t, false) = i.overflowing_sub(j) {
@ -206,7 +169,7 @@ fn sub(a: &SmallUint, b: &SmallUint) -> SmallUint {
} else { } else {
panic!("First number is smaller than second. "); panic!("First number is smaller than second. ");
} }
}, }
(&SmallUintType::Heap((r, s)), &SmallUintType::Inline(i)) => { (&SmallUintType::Heap((r, s)), &SmallUintType::Inline(i)) => {
let slice1 = unsafe { core::slice::from_raw_parts(r, s) }; let slice1 = unsafe { core::slice::from_raw_parts(r, s) };
@ -226,15 +189,11 @@ fn sub(a: &SmallUint, b: &SmallUint) -> SmallUint {
let mut slice = ManuallyDrop::new(result.into_boxed_slice()); let mut slice = ManuallyDrop::new(result.into_boxed_slice());
SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), size))) SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), size)))
}
},
(&SmallUintType::Inline(_), &SmallUintType::Heap((_, _))) => { (&SmallUintType::Inline(_), &SmallUintType::Heap((_, _))) => {
panic!("First number is smaller than second. "); panic!("First number is smaller than second. ");
}
},
(&SmallUintType::Heap((r, s)), &SmallUintType::Heap((i, j))) => { (&SmallUintType::Heap((r, s)), &SmallUintType::Heap((i, j))) => {
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) };
@ -244,7 +203,6 @@ fn sub(a: &SmallUint, b: &SmallUint) -> SmallUint {
let mut slice = ManuallyDrop::new(res.into_boxed_slice()); let mut slice = ManuallyDrop::new(res.into_boxed_slice());
SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), size))) SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), size)))
} }
} }
} }
@ -288,13 +246,11 @@ const fn carrying_mul_u128(lhs: u128, rhs: u128, carry: u128) -> (u128, u128) {
} }
fn mul_two_slices(slice1: &[u32], slice2: &[u32]) -> Vec<u32> { fn mul_two_slices(slice1: &[u32], slice2: &[u32]) -> Vec<u32> {
// https://en.wikipedia.org/wiki/Karatsuba_algorithm // https://en.wikipedia.org/wiki/Karatsuba_algorithm
let l1 = slice1.len(); let l1 = slice1.len();
let l2 = slice2.len(); let l2 = slice2.len();
if l1 == 0 || l2 == 0 { if l1 == 0 || l2 == 0 {
return vec![]; return vec![];
} else if l1 == 1 { } else if l1 == 1 {
@ -308,8 +264,7 @@ fn mul_two_slices(slice1: &[u32], slice2: &[u32]) -> Vec<u32> {
let m = r as u32; let m = r as u32;
overflow = (r >> 32) as u32; overflow = (r >> 32) as u32;
res.push(m); res.push(m);
}
}
if overflow != 0 { if overflow != 0 {
res.push(overflow); res.push(overflow);
@ -317,7 +272,6 @@ fn mul_two_slices(slice1: &[u32], slice2: &[u32]) -> Vec<u32> {
return res; return res;
} else if l2 == 1 { } else if l2 == 1 {
let mut overflow = 0; let mut overflow = 0;
let mut res: Vec<u32> = Vec::with_capacity(l2 + 1); let mut res: Vec<u32> = Vec::with_capacity(l2 + 1);
@ -328,7 +282,6 @@ fn mul_two_slices(slice1: &[u32], slice2: &[u32]) -> Vec<u32> {
let m = r as u32; let m = r as u32;
overflow = (r >> 32) as u32; overflow = (r >> 32) as u32;
res.push(m); res.push(m);
} }
if overflow != 0 { if overflow != 0 {
@ -356,8 +309,6 @@ fn mul_two_slices(slice1: &[u32], slice2: &[u32]) -> Vec<u32> {
op0.reverse(); op0.reverse();
let mut op1 = sub_two_slices(&sub_two_slices(&z1, &z2), &z0); let mut op1 = sub_two_slices(&sub_two_slices(&z1, &z2), &z0);
op1.reverse(); op1.reverse();
@ -367,10 +318,8 @@ fn mul_two_slices(slice1: &[u32], slice2: &[u32]) -> Vec<u32> {
op1.reverse(); op1.reverse();
add_two_slices(&add_two_slices(&op0, &op1), &z0) add_two_slices(&add_two_slices(&op0, &op1), &z0)
} }
fn mul(a: &SmallUint, b: &SmallUint) -> SmallUint { fn mul(a: &SmallUint, b: &SmallUint) -> SmallUint {
match (&a.0, &b.0) { match (&a.0, &b.0) {
(&SmallUintType::Inline(i), &SmallUintType::Inline(j)) => { (&SmallUintType::Inline(i), &SmallUintType::Inline(j)) => {
@ -403,12 +352,12 @@ fn mul(a: &SmallUint, b: &SmallUint) -> SmallUint {
let mut slice = ManuallyDrop::new(res.into_boxed_slice()); let mut slice = ManuallyDrop::new(res.into_boxed_slice());
SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), size))) SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), size)))
} }
} }
}, }
(&SmallUintType::Heap((r, s)), &SmallUintType::Inline(i)) | (&SmallUintType::Inline(i), &SmallUintType::Heap((r, s))) => { (&SmallUintType::Heap((r, s)), &SmallUintType::Inline(i))
| (&SmallUintType::Inline(i), &SmallUintType::Heap((r, s))) => {
let slice1 = unsafe { core::slice::from_raw_parts(r, s) }; let slice1 = unsafe { core::slice::from_raw_parts(r, s) };
let mut res = [0, 0, 0, 0]; let mut res = [0, 0, 0, 0];
@ -427,12 +376,9 @@ fn mul(a: &SmallUint, b: &SmallUint) -> SmallUint {
let mut slice = ManuallyDrop::new(result.into_boxed_slice()); let mut slice = ManuallyDrop::new(result.into_boxed_slice());
SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), size))) SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), size)))
}
},
(&SmallUintType::Heap((r, s)), &SmallUintType::Heap((i, j))) => { (&SmallUintType::Heap((r, s)), &SmallUintType::Heap((i, j))) => {
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) };
@ -442,10 +388,8 @@ fn mul(a: &SmallUint, b: &SmallUint) -> SmallUint {
let mut slice = ManuallyDrop::new(res.into_boxed_slice()); let mut slice = ManuallyDrop::new(res.into_boxed_slice());
SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), size))) SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), size)))
} }
} }
} }
basic_op!(Mul, SmallUint, mul); basic_op!(Mul, SmallUint, mul);

View File

@ -1,11 +1,9 @@
use crate::SmallInt; use crate::SmallInt;
use crate::SmallUint; use crate::SmallUint;
#[cfg(feature = "num-bigint")]
#[cfg(feature= "num-bigint")]
use num_bigint::{BigInt, BigUint}; use num_bigint::{BigInt, BigUint};
#[cfg(feature = "num-bigint")] #[cfg(feature = "num-bigint")]
impl core::fmt::Debug for SmallInt { impl core::fmt::Debug for SmallInt {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@ -19,4 +17,3 @@ impl core::fmt::Debug for SmallUint {
write!(f, "{}", BigUint::from(self)) write!(f, "{}", BigUint::from(self))
} }
} }

View File

@ -1,4 +1,3 @@
/// An integer-like type that will store small integers up to `i128` inline. Larger integers are /// An integer-like type that will store small integers up to `i128` inline. Larger integers are
/// represented as a slice to a sequence of base 2<sup>32</sup> digits represented as a `*mut u32`. /// represented as a slice to a sequence of base 2<sup>32</sup> digits represented as a `*mut u32`.
#[derive(Clone, PartialEq, Eq)] #[derive(Clone, PartialEq, Eq)]
@ -21,7 +20,6 @@ pub enum SmallUintType {
Heap((*mut u32, usize)), Heap((*mut u32, usize)),
} }
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 {
@ -40,4 +38,3 @@ impl Drop for SmallUint {
} }
} }
} }

View File

@ -4,7 +4,7 @@ use crate::SmallInt;
use crate::SmallUint; use crate::SmallUint;
#[cfg(feature = "num-bigint")] #[cfg(feature = "num-bigint")]
use num_bigint::{BigInt, Sign, BigUint}; use num_bigint::{BigInt, BigUint, Sign};
macro_rules! conversion_tests { macro_rules! conversion_tests {
($t:ty, $i:ident) => { ($t:ty, $i:ident) => {
@ -43,12 +43,18 @@ fn test_op_add_u_u() {
let i = SmallUint::from(u128::MAX); let i = SmallUint::from(u128::MAX);
let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81])); let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81]));
let q = i + k; let q = i + k;
assert_eq!(BigUint::from(&q), BigUint::new(vec![5, 4, 9, 3, 1, 81]) + u128::MAX); 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 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 k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81]));
let q = i + k; 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])); assert_eq!(
BigUint::from(&q),
BigUint::new(vec![3, 9, 8, 3, 1]) + BigUint::new(vec![5, 4, 9, 3, 1, 81])
);
} }
#[test] #[test]
@ -59,17 +65,21 @@ fn test_op_mul_u_u() {
let q = i * k; let q = i * k;
assert_eq!(BigUint::from(&q), BigUint::from(u128::MAX) * u128::MAX); assert_eq!(BigUint::from(&q), BigUint::from(u128::MAX) * u128::MAX);
let i = SmallUint::from(u32::MAX); let i = SmallUint::from(u32::MAX);
let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81])); let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81]));
let q = i * k; let q = i * k;
assert_eq!(BigUint::from(&q), BigUint::new(vec![5, 4, 9, 3, 1, 81]) * u32::MAX); 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 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 k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81]));
let q = i * k; 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])); assert_eq!(
BigUint::from(&q),
BigUint::new(vec![3, 9, 8, 3, 1]) * BigUint::new(vec![5, 4, 9, 3, 1, 81])
);
} }
#[test] #[test]
@ -83,54 +93,74 @@ fn test_op_sub_u_u() {
let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81])); let k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81]));
let i = SmallUint::from(u128::MAX); let i = SmallUint::from(u128::MAX);
let q = k - i; let q = k - i;
assert_eq!(BigUint::from(&q), BigUint::new(vec![5, 4, 9, 3, 1, 81]) - u128::MAX); 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 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 i = SmallUint::from(&BigUint::new(vec![3, 9, 8, 3, 1]));
let q = k - i; 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])); 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_and_u_u() { fn test_op_and_u_u() {
let i = SmallUint::from(u128::MAX); let i = SmallUint::from(u128::MAX);
let k = SmallUint::from(u128::MAX); let k = SmallUint::from(u128::MAX);
let q = i & k; let q = i & k;
assert_eq!(BigUint::from(&q), BigUint::from(u128::MAX) & BigUint::from(u128::MAX)); 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 k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81]));
let i = SmallUint::from(u128::MAX); let i = SmallUint::from(u128::MAX);
let q = k & i; let q = k & i;
assert_eq!(BigUint::from(&q), BigUint::new(vec![5, 4, 9, 3]) & BigUint::from(u128::MAX)); assert_eq!(
BigUint::from(&q),
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 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 i = SmallUint::from(&BigUint::new(vec![3, 9, 8, 3, 1]));
let q = k & i; 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])); 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_u() { fn test_op_or_u_u() {
let i = SmallUint::from(u128::MAX); let i = SmallUint::from(u128::MAX);
let k = SmallUint::from(u128::MAX); let k = SmallUint::from(u128::MAX);
let q = i | k; let q = i | k;
assert_eq!(BigUint::from(&q), BigUint::from(u128::MAX) | BigUint::from(u128::MAX)); 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 k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81]));
let i = SmallUint::from(u128::MAX); let i = SmallUint::from(u128::MAX);
let q = k | i; let q = k | i;
assert_eq!(BigUint::from(&q), BigUint::new(vec![5, 4, 9, 3, 1, 81]) | BigUint::from(u128::MAX)); 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 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 i = SmallUint::from(&BigUint::new(vec![3, 9, 8, 3, 1]));
let q = k | i; 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])); assert_eq!(
BigUint::from(&q),
BigUint::new(vec![5, 4, 9, 3, 1, 81]) | BigUint::new(vec![3, 9, 8, 3, 1])
);
} }
#[test] #[test]
@ -139,21 +169,28 @@ fn test_op_xor_u_u() {
let i = SmallUint::from(u128::MAX); let i = SmallUint::from(u128::MAX);
let k = SmallUint::from(u128::MAX); let k = SmallUint::from(u128::MAX);
let q = i ^ k; let q = i ^ k;
assert_eq!(BigUint::from(&q), BigUint::from(u128::MAX) ^ BigUint::from(u128::MAX)); 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 k = SmallUint::from(&BigUint::new(vec![5, 4, 9, 3, 1, 81]));
let i = SmallUint::from(u128::MAX); let i = SmallUint::from(u128::MAX);
let q = k ^ i; let q = k ^ i;
assert_eq!(BigUint::from(&q), BigUint::new(vec![5, 4, 9, 3, 1, 81]) ^ BigUint::from(u128::MAX)); 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 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 i = SmallUint::from(&BigUint::new(vec![3, 9, 8, 3, 1]));
let q = k ^ i; 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])); 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_bigint() { fn test_bigint() {