Compare commits

..

2 Commits

5 changed files with 310 additions and 13 deletions

View File

@ -130,7 +130,14 @@ impl From<SmallUint> for SmallInt {
match s.0 { match s.0 {
SmallUintType::Inline(i) => SmallInt::from(i), SmallUintType::Inline(i) => SmallInt::from(i),
SmallUintType::Heap((r, s)) => { SmallUintType::Heap((r, s)) => {
SmallInt(SmallIntType::Heap((r, isize::try_from(s).unwrap()))) let slice = unsafe { core::slice::from_raw_parts(r, s) };
let mut ret = vec![0; s];
ret.clone_from_slice(slice);
let mut val = ManuallyDrop::new(ret.into_boxed_slice());
SmallInt(SmallIntType::Heap((
val.as_mut_ptr(),
isize::try_from(s).unwrap(),
)))
} }
} }
} }
@ -145,7 +152,11 @@ impl TryFrom<SmallInt> for SmallUint {
SmallIntType::Heap((r, s)) => { SmallIntType::Heap((r, s)) => {
let size = usize::try_from(s).map_err(|_| SmallIntError::ConversionError)?; let size = usize::try_from(s).map_err(|_| SmallIntError::ConversionError)?;
if size > 4 { if size > 4 {
Ok(Self(SmallUintType::Heap((r, size)))) let slice = unsafe { core::slice::from_raw_parts(r, size) };
let mut ret = vec![0; size];
ret.clone_from_slice(slice);
let mut val = ManuallyDrop::new(ret.into_boxed_slice());
Ok(Self(SmallUintType::Heap((val.as_mut_ptr(), size))))
} else { } else {
Ok(Self(SmallUintType::Inline(u128::try_from(value)?))) Ok(Self(SmallUintType::Inline(u128::try_from(value)?)))
} }

View File

@ -277,6 +277,12 @@ fn sub(a: &SmallUint, b: &SmallUint) -> SmallUint {
basic_op!(Sub, sub, SmallUint, sub); basic_op!(Sub, sub, SmallUint, sub);
fn sub_signed(a: &SmallInt, b: &SmallInt) -> SmallInt {
a + (-b.clone())
}
basic_op!(Sub, sub, SmallInt, sub_signed);
// Taken from https://github.com/rust-lang/rust/issues/85532#issuecomment-916309635. Credit to // Taken from https://github.com/rust-lang/rust/issues/85532#issuecomment-916309635. Credit to
// AaronKutch. // AaronKutch.
const fn carrying_mul_u128(lhs: u128, rhs: u128, carry: u128) -> (u128, u128) { const fn carrying_mul_u128(lhs: u128, rhs: u128, carry: u128) -> (u128, u128) {
@ -461,3 +467,44 @@ fn mul(a: &SmallUint, b: &SmallUint) -> SmallUint {
} }
basic_op!(Mul, mul, SmallUint, mul); basic_op!(Mul, mul, SmallUint, mul);
fn mul_signed(a: &SmallInt, b: &SmallInt) -> SmallInt {
let a_sign;
match &a.0 {
SmallIntType::Inline(i) => a_sign = i.signum() as i8,
SmallIntType::Heap((_, s)) => a_sign = s.signum() as i8,
}
let b_sign;
match &b.0 {
SmallIntType::Inline(i) => b_sign = i.signum() as i8,
SmallIntType::Heap((_, s)) => b_sign = s.signum() as i8,
}
match (a_sign, b_sign) {
x if (x.0 >= 0 && x.1 >= 0) => SmallInt::from(
SmallUint::from_smallint_unsigned(a.clone())
* SmallUint::from_smallint_unsigned(b.clone()),
),
x if (x.0 < 0 && x.1 < 0) => SmallInt::from(
SmallUint::from_smallint_unsigned(a.clone())
* SmallUint::from_smallint_unsigned(b.clone()),
),
x if (x.0 >= 0 && x.1 < 0) => -SmallInt::from(
SmallUint::from_smallint_unsigned(a.clone())
* SmallUint::from_smallint_unsigned(b.clone()),
),
x if (x.0 < 0 && x.1 >= 0) => -SmallInt::from(
SmallUint::from_smallint_unsigned(a.clone())
* SmallUint::from_smallint_unsigned(b.clone()),
),
(_, _) => {
panic!("This shouldn't happen. ");
}
}
}
basic_op!(Mul, mul, SmallInt, mul_signed);

View File

@ -1,8 +1,96 @@
use crate::smallint::SmallIntType; use crate::smallint::{SmallIntType, SmallUintType};
use crate::SmallInt; use crate::SmallInt;
use crate::SmallUint; use crate::SmallUint;
use std::cmp::Ordering; use std::cmp::Ordering;
impl PartialEq for SmallUint {
fn eq(&self, other: &SmallUint) -> bool {
match (&self.0, &other.0) {
(SmallUintType::Inline(i), SmallUintType::Inline(j)) => i.eq(j),
(SmallUintType::Heap((r, s)), SmallUintType::Heap((i, j))) => match j.cmp(s) {
Ordering::Greater => false,
Ordering::Less => false,
Ordering::Equal => {
let slice1 = unsafe { core::slice::from_raw_parts(r, *s) };
let slice2 = unsafe { core::slice::from_raw_parts(i, *j) };
for i in 0..*s {
match slice1[s - 1 - i].cmp(&slice2[s - 1 - i]) {
Ordering::Less => return false,
Ordering::Greater => return false,
_ => {}
}
}
true
}
},
(_, _) => false,
}
}
}
impl Eq for SmallUint {}
impl PartialOrd for SmallUint {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
match (&self.0, &other.0) {
(SmallUintType::Inline(i), SmallUintType::Inline(j)) => Some(i.cmp(j)),
(SmallUintType::Inline(_), SmallUintType::Heap((_, _))) => Some(Ordering::Less),
(SmallUintType::Heap((_, _)), SmallUintType::Inline(_)) => Some(Ordering::Greater),
(SmallUintType::Heap((r, s)), SmallUintType::Heap((i, j))) => match j.cmp(s) {
Ordering::Greater => Some(Ordering::Less),
Ordering::Less => Some(Ordering::Greater),
Ordering::Equal => {
let slice1 = unsafe { core::slice::from_raw_parts(r, *s) };
let slice2 = unsafe { core::slice::from_raw_parts(i, *j) };
for i in 0..*s {
match slice1[s - 1 - i].cmp(&slice2[s - 1 - i]) {
Ordering::Less => return Some(Ordering::Less),
Ordering::Greater => return Some(Ordering::Greater),
_ => {}
}
}
Some(Ordering::Equal)
}
},
}
}
}
impl PartialEq for SmallInt {
fn eq(&self, other: &Self) -> bool {
match (&self.0, &other.0) {
(SmallIntType::Inline(i), SmallIntType::Inline(j)) => i.eq(j),
(SmallIntType::Heap((r, s)), SmallIntType::Heap((i, j))) => {
if s.signum() != j.signum() {
return false;
}
match j.cmp(s) {
Ordering::Greater => false,
Ordering::Less => false,
Ordering::Equal => {
let us = usize::try_from(s.abs()).unwrap();
let uj = usize::try_from(j.abs()).unwrap();
let slice1 = unsafe { core::slice::from_raw_parts(r, us) };
let slice2 = unsafe { core::slice::from_raw_parts(i, uj) };
for i in 0..*s {
match slice1[(s - 1 - i) as usize].cmp(&slice2[(s - 1 - i) as usize]) {
Ordering::Less => return false,
Ordering::Greater => return false,
_ => {}
}
}
true
}
}
}
(_, _) => false,
}
}
}
impl Eq for SmallInt {}
impl PartialOrd for SmallInt { impl PartialOrd for SmallInt {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
let a_sign; let a_sign;

View File

@ -1,20 +1,18 @@
use core::mem::ManuallyDrop;
/// 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)]
pub struct SmallInt(pub(crate) SmallIntType); pub struct SmallInt(pub(crate) SmallIntType);
/// An integer-like type that will store small integers up to `u128` inline. Larger integers are /// An integer-like type that will store small integers up to `u128` 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, PartialOrd, Ord)]
pub struct SmallUint(pub(crate) SmallUintType); pub struct SmallUint(pub(crate) SmallUintType);
#[derive(Clone, PartialEq, Eq)]
pub enum SmallIntType { pub enum SmallIntType {
Inline(i128), Inline(i128),
Heap((*mut u32, isize)), Heap((*mut u32, isize)),
} }
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum SmallUintType { pub enum SmallUintType {
Inline(u128), Inline(u128),
Heap((*mut u32, usize)), Heap((*mut u32, usize)),
@ -38,3 +36,34 @@ impl Drop for SmallUint {
} }
} }
} }
impl Clone for SmallUint {
fn clone(&self) -> Self {
match self.0 {
SmallUintType::Inline(i) => Self(SmallUintType::Inline(i)),
SmallUintType::Heap((r, s)) => {
let slice = unsafe { core::slice::from_raw_parts(r, s) };
let mut ret = vec![0; s];
ret.clone_from_slice(slice);
let mut val = ManuallyDrop::new(ret.into_boxed_slice());
SmallUint(SmallUintType::Heap((val.as_mut_ptr(), s)))
}
}
}
}
impl Clone for SmallInt {
fn clone(&self) -> Self {
match self.0 {
SmallIntType::Inline(i) => Self(SmallIntType::Inline(i)),
SmallIntType::Heap((r, s)) => {
let size = usize::try_from(s.abs()).unwrap();
let slice = unsafe { core::slice::from_raw_parts(r, size) };
let mut ret = vec![0; size];
ret.clone_from_slice(slice);
let mut val = ManuallyDrop::new(ret.into_boxed_slice());
SmallInt(SmallIntType::Heap((val.as_mut_ptr(), s)))
}
}
}
}

View File

@ -34,7 +34,51 @@ conversion_tests!(i128, test_i128);
#[test] #[test]
#[cfg(feature = "num-bigint")] #[cfg(feature = "num-bigint")]
fn test_op_add_u_u() { fn test_cmp_u() {
let i = SmallUint::from(30u32);
let k = SmallUint::from(50u32);
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]
#[cfg(feature = "num-bigint")]
fn test_cmp_i() {
let i = SmallInt::from(30u32);
let k = SmallInt::from(50u32);
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]
#[cfg(feature = "num-bigint")]
fn test_op_add_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;
@ -59,7 +103,33 @@ fn test_op_add_u_u() {
#[test] #[test]
#[cfg(feature = "num-bigint")] #[cfg(feature = "num-bigint")]
fn test_op_mul_u_u() { fn test_op_add_i() {
let i = SmallInt::from(u128::MAX);
let k = -SmallInt::from(u128::MAX);
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]
#[cfg(feature = "num-bigint")]
fn test_op_mul_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;
@ -84,7 +154,33 @@ fn test_op_mul_u_u() {
#[test] #[test]
#[cfg(feature = "num-bigint")] #[cfg(feature = "num-bigint")]
fn test_op_sub_u_u() { fn test_op_mul_i() {
let i = -SmallInt::from(u128::MAX);
let k = SmallInt::from(u128::MAX);
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]
#[cfg(feature = "num-bigint")]
fn test_op_sub_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;
@ -109,7 +205,33 @@ fn test_op_sub_u_u() {
#[test] #[test]
#[cfg(feature = "num-bigint")] #[cfg(feature = "num-bigint")]
fn test_op_and_u_u() { fn test_op_sub_i() {
let i = -SmallInt::from(u128::MAX);
let k = SmallInt::from(u128::MAX);
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]
#[cfg(feature = "num-bigint")]
fn test_op_and_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;
@ -137,7 +259,7 @@ fn test_op_and_u_u() {
#[test] #[test]
#[cfg(feature = "num-bigint")] #[cfg(feature = "num-bigint")]
fn test_op_or_u_u() { fn test_op_or_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;
@ -165,7 +287,7 @@ fn test_op_or_u_u() {
#[test] #[test]
#[cfg(feature = "num-bigint")] #[cfg(feature = "num-bigint")]
fn test_op_xor_u_u() { fn test_op_xor_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;