mirror of
https://gitlab.com/artofrev/smallint.git
synced 2024-12-04 17:11:38 -05:00
fmt
This commit is contained in:
parent
64ad3189bf
commit
cf9c8b0b78
|
@ -79,5 +79,3 @@ impl From<&SmallUint> for BigUint {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use crate::SmallInt;
|
||||
use crate::SmallUint;
|
||||
use crate::SmallIntError;
|
||||
use crate::smallint::{SmallIntType, SmallUintType};
|
||||
use crate::SmallInt;
|
||||
use crate::SmallIntError;
|
||||
use crate::SmallUint;
|
||||
use core::mem::ManuallyDrop;
|
||||
|
||||
macro_rules! int_impl {
|
||||
|
|
|
@ -19,8 +19,8 @@ mod error;
|
|||
pub use error::SmallIntError;
|
||||
|
||||
mod convert;
|
||||
mod ops;
|
||||
mod logic;
|
||||
mod ops;
|
||||
|
||||
mod bigint;
|
||||
|
||||
|
|
42
src/logic.rs
42
src/logic.rs
|
@ -1,74 +1,63 @@
|
|||
use crate::SmallUint;
|
||||
use crate::smallint::SmallUintType;
|
||||
use core::ops::{BitAnd, BitOr, BitXor};
|
||||
use crate::SmallUint;
|
||||
use core::mem::ManuallyDrop;
|
||||
use core::ops::{BitAnd, BitOr, BitXor};
|
||||
|
||||
macro_rules! basic_op {
|
||||
($imp:ident, $typ:ty, $fun:ident) => {
|
||||
|
||||
impl<'a, 'b> $imp<&'a $typ> for &'b $typ {
|
||||
|
||||
type Output = $typ;
|
||||
|
||||
fn $fun(self, rhs: &$typ) -> Self::Output {
|
||||
$fun(self, rhs)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl<'a> $imp<$typ> for &'a $typ {
|
||||
|
||||
type Output = $typ;
|
||||
|
||||
fn $fun(self, rhs: $typ) -> Self::Output {
|
||||
self.$fun(&rhs)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl<'a> $imp<&'a $typ> for $typ {
|
||||
|
||||
type Output = $typ;
|
||||
|
||||
fn $fun(self, rhs: &$typ) -> Self::Output {
|
||||
(&self).$fun(rhs)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
impl $imp<$typ> for $typ {
|
||||
|
||||
type Output = $typ;
|
||||
|
||||
fn $fun(self, rhs: $typ) -> Self::Output {
|
||||
(&self).$fun(&rhs)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
fn bitand(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)) => {
|
||||
(&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 j = 0u128;
|
||||
for i in 0..4 {
|
||||
j <<= 32;
|
||||
|
||||
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) };
|
||||
|
||||
|
@ -105,8 +94,9 @@ 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)) => {
|
||||
}
|
||||
(&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();
|
||||
|
@ -122,11 +112,9 @@ fn bitor(a: &SmallUint, b: &SmallUint) -> SmallUint {
|
|||
let mut retslice = ManuallyDrop::new(retvec.into_boxed_slice());
|
||||
|
||||
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) };
|
||||
|
||||
|
@ -146,9 +134,7 @@ fn bitor(a: &SmallUint, b: &SmallUint) -> SmallUint {
|
|||
let mut slice = ManuallyDrop::new(retvec.into_boxed_slice());
|
||||
|
||||
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)) => {
|
||||
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 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());
|
||||
|
||||
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) };
|
||||
|
||||
|
|
114
src/ops.rs
114
src/ops.rs
|
@ -1,57 +1,45 @@
|
|||
use crate::SmallUint;
|
||||
use crate::smallint::SmallUintType;
|
||||
use core::ops::{Add, Sub, Mul};
|
||||
use crate::SmallUint;
|
||||
use core::mem::ManuallyDrop;
|
||||
use core::ops::{Add, Mul, Sub};
|
||||
|
||||
macro_rules! basic_op {
|
||||
($imp:ident, $typ:ty, $fun:ident) => {
|
||||
|
||||
impl<'a, 'b> $imp<&'a $typ> for &'b $typ {
|
||||
|
||||
type Output = $typ;
|
||||
|
||||
fn $fun(self, rhs: &$typ) -> Self::Output {
|
||||
$fun(self, rhs)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl<'a> $imp<$typ> for &'a $typ {
|
||||
|
||||
type Output = $typ;
|
||||
|
||||
fn $fun(self, rhs: $typ) -> Self::Output {
|
||||
self.$fun(&rhs)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl<'a> $imp<&'a $typ> for $typ {
|
||||
|
||||
type Output = $typ;
|
||||
|
||||
fn $fun(self, rhs: &$typ) -> Self::Output {
|
||||
(&self).$fun(rhs)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
impl $imp<$typ> for $typ {
|
||||
|
||||
type Output = $typ;
|
||||
|
||||
fn $fun(self, rhs: $typ) -> Self::Output {
|
||||
(&self).$fun(&rhs)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
fn add_two_slices(slice1: &[u32], slice2: &[u32]) -> Vec<u32> {
|
||||
|
||||
let s = slice1.len();
|
||||
let j = slice2.len();
|
||||
|
||||
|
@ -60,24 +48,14 @@ fn add_two_slices(slice1: &[u32], slice2: &[u32]) -> Vec<u32> {
|
|||
let mut carry = false;
|
||||
|
||||
for t in 0..larger {
|
||||
let value1 = if t < s { slice1[t] } else { 0 };
|
||||
|
||||
let value1 = if t < s {
|
||||
slice1[t]
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
let value2 = if t < j {
|
||||
slice2[t]
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let value2 = if t < j { slice2[t] } else { 0 };
|
||||
|
||||
let (val, overflow) = value1.overflowing_add(value2);
|
||||
let (cval, coverflow) = val.overflowing_add(carry as u32);
|
||||
res.push(cval);
|
||||
carry = overflow | coverflow;
|
||||
|
||||
}
|
||||
|
||||
if carry {
|
||||
|
@ -89,32 +67,28 @@ fn add_two_slices(slice1: &[u32], slice2: &[u32]) -> Vec<u32> {
|
|||
res
|
||||
}
|
||||
|
||||
|
||||
fn add(a: &SmallUint, b: &SmallUint) -> SmallUint {
|
||||
|
||||
match (&a.0, &b.0) {
|
||||
(&SmallUintType::Inline(i), &SmallUintType::Inline(j)) => {
|
||||
match i.overflowing_add(j) {
|
||||
(t, false) => SmallUint(SmallUintType::Inline(t)),
|
||||
(t, true) => {
|
||||
let mut res = [0, 0, 0, 0, 1];
|
||||
(&SmallUintType::Inline(i), &SmallUintType::Inline(j)) => match i.overflowing_add(j) {
|
||||
(t, false) => SmallUint(SmallUintType::Inline(t)),
|
||||
(t, true) => {
|
||||
let mut res = [0, 0, 0, 0, 1];
|
||||
|
||||
let mut v = t;
|
||||
#[allow(clippy::needless_range_loop)]
|
||||
for r in 0..4 {
|
||||
res[r] = v as u32;
|
||||
|
||||
v >>= 32;
|
||||
}
|
||||
|
||||
let mut slice = ManuallyDrop::new(<Box<[u32]>>::from(res));
|
||||
|
||||
SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), 5)))
|
||||
let mut v = t;
|
||||
#[allow(clippy::needless_range_loop)]
|
||||
for r in 0..4 {
|
||||
res[r] = v as u32;
|
||||
|
||||
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 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());
|
||||
|
||||
SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), size)))
|
||||
|
||||
|
||||
},
|
||||
}
|
||||
(&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) };
|
||||
|
||||
|
@ -147,7 +118,6 @@ fn add(a: &SmallUint, b: &SmallUint) -> SmallUint {
|
|||
let mut slice = ManuallyDrop::new(res.into_boxed_slice());
|
||||
|
||||
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 {
|
||||
let mut value1 = slice1[i];
|
||||
|
||||
let value2 = if i < s {
|
||||
slice2[i]
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let value2 = if i < s { slice2[i] } else { 0 };
|
||||
|
||||
if borrow {
|
||||
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);
|
||||
res.push(val);
|
||||
|
||||
}
|
||||
|
||||
if borrow {
|
||||
|
@ -194,11 +159,9 @@ fn sub_two_slices(slice1: &[u32], slice2: &[u32]) -> Vec<u32> {
|
|||
}
|
||||
|
||||
res
|
||||
|
||||
}
|
||||
|
||||
fn sub(a: &SmallUint, b: &SmallUint) -> SmallUint {
|
||||
|
||||
match (&a.0, &b.0) {
|
||||
(&SmallUintType::Inline(i), &SmallUintType::Inline(j)) => {
|
||||
if let (t, false) = i.overflowing_sub(j) {
|
||||
|
@ -206,7 +169,7 @@ fn sub(a: &SmallUint, b: &SmallUint) -> SmallUint {
|
|||
} else {
|
||||
panic!("First number is smaller than second. ");
|
||||
}
|
||||
},
|
||||
}
|
||||
(&SmallUintType::Heap((r, s)), &SmallUintType::Inline(i)) => {
|
||||
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());
|
||||
|
||||
SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), size)))
|
||||
|
||||
|
||||
},
|
||||
}
|
||||
(&SmallUintType::Inline(_), &SmallUintType::Heap((_, _))) => {
|
||||
panic!("First number is smaller than second. ");
|
||||
|
||||
},
|
||||
}
|
||||
(&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) };
|
||||
|
||||
|
@ -244,7 +203,6 @@ fn sub(a: &SmallUint, b: &SmallUint) -> SmallUint {
|
|||
let mut slice = ManuallyDrop::new(res.into_boxed_slice());
|
||||
|
||||
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> {
|
||||
|
||||
// https://en.wikipedia.org/wiki/Karatsuba_algorithm
|
||||
|
||||
let l1 = slice1.len();
|
||||
let l2 = slice2.len();
|
||||
|
||||
|
||||
if l1 == 0 || l2 == 0 {
|
||||
return vec![];
|
||||
} else if l1 == 1 {
|
||||
|
@ -308,7 +264,6 @@ fn mul_two_slices(slice1: &[u32], slice2: &[u32]) -> Vec<u32> {
|
|||
let m = r as u32;
|
||||
overflow = (r >> 32) as u32;
|
||||
res.push(m);
|
||||
|
||||
}
|
||||
|
||||
if overflow != 0 {
|
||||
|
@ -317,7 +272,6 @@ fn mul_two_slices(slice1: &[u32], slice2: &[u32]) -> Vec<u32> {
|
|||
|
||||
return res;
|
||||
} else if l2 == 1 {
|
||||
|
||||
let mut overflow = 0;
|
||||
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;
|
||||
overflow = (r >> 32) as u32;
|
||||
res.push(m);
|
||||
|
||||
}
|
||||
|
||||
if overflow != 0 {
|
||||
|
@ -356,8 +309,6 @@ fn mul_two_slices(slice1: &[u32], slice2: &[u32]) -> Vec<u32> {
|
|||
|
||||
op0.reverse();
|
||||
|
||||
|
||||
|
||||
let mut op1 = sub_two_slices(&sub_two_slices(&z1, &z2), &z0);
|
||||
|
||||
op1.reverse();
|
||||
|
@ -367,10 +318,8 @@ fn mul_two_slices(slice1: &[u32], slice2: &[u32]) -> Vec<u32> {
|
|||
op1.reverse();
|
||||
|
||||
add_two_slices(&add_two_slices(&op0, &op1), &z0)
|
||||
|
||||
}
|
||||
|
||||
|
||||
fn mul(a: &SmallUint, b: &SmallUint) -> SmallUint {
|
||||
match (&a.0, &b.0) {
|
||||
(&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());
|
||||
|
||||
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 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());
|
||||
|
||||
SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), size)))
|
||||
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
(&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) };
|
||||
|
||||
|
@ -442,10 +388,8 @@ fn mul(a: &SmallUint, b: &SmallUint) -> SmallUint {
|
|||
let mut slice = ManuallyDrop::new(res.into_boxed_slice());
|
||||
|
||||
SmallUint(SmallUintType::Heap((slice.as_mut_ptr(), size)))
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
basic_op!(Mul, SmallUint, mul);
|
||||
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
use crate::SmallInt;
|
||||
use crate::SmallUint;
|
||||
|
||||
|
||||
#[cfg(feature= "num-bigint")]
|
||||
#[cfg(feature = "num-bigint")]
|
||||
use num_bigint::{BigInt, BigUint};
|
||||
|
||||
|
||||
#[cfg(feature = "num-bigint")]
|
||||
impl core::fmt::Debug for SmallInt {
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
/// 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`.
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
|
@ -21,7 +20,6 @@ pub enum SmallUintType {
|
|||
Heap((*mut u32, usize)),
|
||||
}
|
||||
|
||||
|
||||
impl Drop for SmallInt {
|
||||
fn drop(&mut self) {
|
||||
if let Self(SmallIntType::Heap((r, s))) = self {
|
||||
|
@ -40,4 +38,3 @@ impl Drop for SmallUint {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
85
src/tests.rs
85
src/tests.rs
|
@ -4,7 +4,7 @@ use crate::SmallInt;
|
|||
use crate::SmallUint;
|
||||
|
||||
#[cfg(feature = "num-bigint")]
|
||||
use num_bigint::{BigInt, Sign, BigUint};
|
||||
use num_bigint::{BigInt, BigUint, Sign};
|
||||
|
||||
macro_rules! conversion_tests {
|
||||
($t:ty, $i:ident) => {
|
||||
|
@ -43,12 +43,18 @@ fn test_op_add_u_u() {
|
|||
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);
|
||||
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]));
|
||||
assert_eq!(
|
||||
BigUint::from(&q),
|
||||
BigUint::new(vec![3, 9, 8, 3, 1]) + BigUint::new(vec![5, 4, 9, 3, 1, 81])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -59,17 +65,21 @@ fn test_op_mul_u_u() {
|
|||
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);
|
||||
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]));
|
||||
|
||||
assert_eq!(
|
||||
BigUint::from(&q),
|
||||
BigUint::new(vec![3, 9, 8, 3, 1]) * BigUint::new(vec![5, 4, 9, 3, 1, 81])
|
||||
);
|
||||
}
|
||||
|
||||
#[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 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);
|
||||
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]));
|
||||
|
||||
assert_eq!(
|
||||
BigUint::from(&q),
|
||||
BigUint::new(vec![5, 4, 9, 3, 1, 81]) - BigUint::new(vec![3, 9, 8, 3, 1])
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "num-bigint")]
|
||||
fn test_op_and_u_u() {
|
||||
let i = SmallUint::from(u128::MAX);
|
||||
let k = SmallUint::from(u128::MAX);
|
||||
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 i = SmallUint::from(u128::MAX);
|
||||
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 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]));
|
||||
assert_eq!(
|
||||
BigUint::from(&q),
|
||||
BigUint::new(vec![5, 4, 9, 3, 1, 81]) & BigUint::new(vec![3, 9, 8, 3, 1])
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "num-bigint")]
|
||||
fn test_op_or_u_u() {
|
||||
let i = SmallUint::from(u128::MAX);
|
||||
let k = SmallUint::from(u128::MAX);
|
||||
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 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));
|
||||
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]));
|
||||
|
||||
assert_eq!(
|
||||
BigUint::from(&q),
|
||||
BigUint::new(vec![5, 4, 9, 3, 1, 81]) | BigUint::new(vec![3, 9, 8, 3, 1])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -139,21 +169,28 @@ fn test_op_xor_u_u() {
|
|||
let i = SmallUint::from(u128::MAX);
|
||||
let k = SmallUint::from(u128::MAX);
|
||||
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 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));
|
||||
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]));
|
||||
|
||||
assert_eq!(
|
||||
BigUint::from(&q),
|
||||
BigUint::new(vec![5, 4, 9, 3, 1, 81]) ^ BigUint::new(vec![3, 9, 8, 3, 1])
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "num-bigint")]
|
||||
fn test_bigint() {
|
||||
|
|
Loading…
Reference in New Issue
Block a user