improve ops.rs, rename BigUint::from_smallint_unsigned to SmallInt::unsigned_abs, etc.

This commit is contained in:
Solomon Ucko 2022-11-18 00:24:13 -05:00
parent c392ba8633
commit 5bea89295d
4 changed files with 103 additions and 111 deletions

View File

@ -98,10 +98,10 @@ impl From<u128> for SmallInt {
} }
} }
impl TryFrom<SmallInt> for u128 { impl TryFrom<&SmallInt> for u128 {
type Error = SmallIntError; type Error = SmallIntError;
fn try_from(s: SmallInt) -> Result<Self, Self::Error> { fn try_from(s: &SmallInt) -> Result<Self, Self::Error> {
match s.0 { match s.0 {
SmallIntType::Inline(i) => { SmallIntType::Inline(i) => {
u128::try_from(i).map_err(|_| SmallIntError::ConversionError) u128::try_from(i).map_err(|_| SmallIntError::ConversionError)
@ -125,6 +125,14 @@ impl TryFrom<SmallInt> for u128 {
} }
} }
impl TryFrom<SmallInt> for u128 {
type Error = SmallIntError;
fn try_from(value: SmallInt) -> Result<Self, Self::Error> {
Self::try_from(&value)
}
}
impl From<SmallUint> for SmallInt { impl From<SmallUint> for SmallInt {
fn from(s: SmallUint) -> Self { fn from(s: SmallUint) -> Self {
match s.0 { match s.0 {
@ -143,10 +151,10 @@ impl From<SmallUint> for SmallInt {
} }
} }
impl TryFrom<SmallInt> for SmallUint { impl TryFrom<&SmallInt> for SmallUint {
type Error = SmallIntError; type Error = SmallIntError;
fn try_from(value: SmallInt) -> Result<Self, Self::Error> { fn try_from(value: &SmallInt) -> Result<Self, Self::Error> {
match value.0 { match value.0 {
SmallIntType::Inline(i) => Self::try_from(i), SmallIntType::Inline(i) => Self::try_from(i),
SmallIntType::Heap((r, s)) => { SmallIntType::Heap((r, s)) => {
@ -165,11 +173,19 @@ impl TryFrom<SmallInt> for SmallUint {
} }
} }
impl SmallUint { impl TryFrom<SmallInt> for SmallUint {
/// Converts a `SmallInt` into a `SmallUint` and drops the sign instead of throwing an error. type Error = SmallIntError;
pub fn from_smallint_unsigned(value: SmallInt) -> Self {
match value.0 { fn try_from(value: SmallInt) -> Result<Self, Self::Error> {
SmallIntType::Inline(i) => Self::from(i.unsigned_abs()), Self::try_from(&value)
}
}
impl SmallInt {
/// Returns the absolute value of the `SmallInt` as a `SmallUint`.
pub fn unsigned_abs(&self) -> SmallUint {
match self.0 {
SmallIntType::Inline(i) => SmallUint::from(i.unsigned_abs()),
SmallIntType::Heap((r, s)) => { SmallIntType::Heap((r, s)) => {
let size = s.unsigned_abs(); let size = s.unsigned_abs();
if size > 4 { if size > 4 {
@ -177,11 +193,11 @@ impl SmallUint {
let mut ret = vec![0; size]; let mut ret = vec![0; size];
ret.clone_from_slice(slice); ret.clone_from_slice(slice);
let mut val = ManuallyDrop::new(ret.into_boxed_slice()); let mut val = ManuallyDrop::new(ret.into_boxed_slice());
Self(SmallUintType::Heap((val.as_mut_ptr(), size))) SmallUint(SmallUintType::Heap((val.as_mut_ptr(), size)))
} else if s >= 0 { } else if s >= 0 {
Self(SmallUintType::Inline(u128::try_from(value).unwrap())) SmallUint(SmallUintType::Inline(u128::try_from(self).unwrap()))
} else { } else {
Self(SmallUintType::Inline(u128::try_from(-value).unwrap())) SmallUint(SmallUintType::Inline(u128::try_from(-self).unwrap()))
} }
} }
} }

View File

@ -4,10 +4,10 @@ use core::mem::ManuallyDrop;
use core::ops::{Add, Div, Mul, Neg, Sub}; use core::ops::{Add, Div, Mul, Neg, Sub};
use core::ops::{AddAssign, DivAssign, MulAssign, SubAssign}; use core::ops::{AddAssign, DivAssign, MulAssign, SubAssign};
impl Neg for SmallInt { impl Neg for &SmallInt {
type Output = Self; type Output = SmallInt;
fn neg(self) -> Self::Output { fn neg(self) -> SmallInt {
match self.0 { match self.0 {
SmallIntType::Inline(i) => { SmallIntType::Inline(i) => {
if let Some(n) = i.checked_neg() { if let Some(n) = i.checked_neg() {
@ -30,6 +30,14 @@ impl Neg for SmallInt {
} }
} }
impl Neg for SmallInt {
type Output = SmallInt;
fn neg(self) -> SmallInt {
(&self).neg()
}
}
macro_rules! basic_op { macro_rules! basic_op {
($imp:ident, $lower:ident, $typ:ty, $fun:ident) => { ($imp:ident, $lower:ident, $typ:ty, $fun:ident) => {
impl<'a, 'b> $imp<&'a $typ> for &'b $typ { impl<'a, 'b> $imp<&'a $typ> for &'b $typ {
@ -178,17 +186,17 @@ fn add_signed(a: &SmallInt, b: &SmallInt) -> SmallInt {
match (a_sign, b_sign) { match (a_sign, b_sign) {
x if (x.0 >= 0 && x.1 >= 0) => SmallInt::from( x if (x.0 >= 0 && x.1 >= 0) => SmallInt::from(
SmallUint::from_smallint_unsigned(a.clone()) a.unsigned_abs()
+ SmallUint::from_smallint_unsigned(b.clone()), + b.unsigned_abs(),
), ),
x if (x.0 < 0 && x.1 < 0) => -SmallInt::from( x if (x.0 < 0 && x.1 < 0) => -SmallInt::from(
SmallUint::from_smallint_unsigned(a.clone()) a.unsigned_abs()
+ SmallUint::from_smallint_unsigned(b.clone()), + b.unsigned_abs(),
), ),
x if (x.0 >= 0 && x.1 < 0) => { x if (x.0 >= 0 && x.1 < 0) => {
let s = SmallUint::from_smallint_unsigned(a.clone()); let s = a.unsigned_abs();
let b = SmallUint::from_smallint_unsigned(b.clone()); let b = b.unsigned_abs();
if b <= s { if b <= s {
SmallInt::from(s - b) SmallInt::from(s - b)
} else { } else {
@ -197,8 +205,8 @@ fn add_signed(a: &SmallInt, b: &SmallInt) -> SmallInt {
} }
x if (x.0 < 0 && x.1 >= 0) => { x if (x.0 < 0 && x.1 >= 0) => {
let s = SmallUint::from_smallint_unsigned(a.clone()); let s = a.unsigned_abs();
let b = SmallUint::from_smallint_unsigned(b.clone()); let b = b.unsigned_abs();
if s <= b { if s <= b {
SmallInt::from(b - s) SmallInt::from(b - s)
} else { } else {
@ -552,22 +560,22 @@ fn mul_signed(a: &SmallInt, b: &SmallInt) -> SmallInt {
match (a_sign, b_sign) { match (a_sign, b_sign) {
x if (x.0 >= 0 && x.1 >= 0) => SmallInt::from( x if (x.0 >= 0 && x.1 >= 0) => SmallInt::from(
SmallUint::from_smallint_unsigned(a.clone()) a.unsigned_abs()
* SmallUint::from_smallint_unsigned(b.clone()), * b.unsigned_abs(),
), ),
x if (x.0 < 0 && x.1 < 0) => SmallInt::from( x if (x.0 < 0 && x.1 < 0) => SmallInt::from(
SmallUint::from_smallint_unsigned(a.clone()) a.unsigned_abs()
* SmallUint::from_smallint_unsigned(b.clone()), * b.unsigned_abs(),
), ),
x if (x.0 >= 0 && x.1 < 0) => -SmallInt::from( x if (x.0 >= 0 && x.1 < 0) => -SmallInt::from(
SmallUint::from_smallint_unsigned(a.clone()) a.unsigned_abs()
* SmallUint::from_smallint_unsigned(b.clone()), * b.unsigned_abs(),
), ),
x if (x.0 < 0 && x.1 >= 0) => -SmallInt::from( x if (x.0 < 0 && x.1 >= 0) => -SmallInt::from(
SmallUint::from_smallint_unsigned(a.clone()) a.unsigned_abs()
* SmallUint::from_smallint_unsigned(b.clone()), * b.unsigned_abs(),
), ),
(_, _) => { (_, _) => {

View File

@ -6,23 +6,12 @@ use std::cmp::Ordering;
impl PartialEq for SmallUint { impl PartialEq for SmallUint {
fn eq(&self, other: &SmallUint) -> bool { fn eq(&self, other: &SmallUint) -> bool {
match (&self.0, &other.0) { match (&self.0, &other.0) {
(SmallUintType::Inline(i), SmallUintType::Inline(j)) => i.eq(j), (SmallUintType::Inline(i), SmallUintType::Inline(j)) => i == j,
(SmallUintType::Heap((r, s)), SmallUintType::Heap((i, j))) => match j.cmp(s) { (SmallUintType::Heap((p, s)), SmallUintType::Heap((q, t))) => {
Ordering::Greater => false, let slice1 = unsafe { core::slice::from_raw_parts(*p, *s) };
Ordering::Less => false, let slice2 = unsafe { core::slice::from_raw_parts(*q, *t) };
Ordering::Equal => { slice1 == slice2
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, (_, _) => false,
} }
} }
@ -32,57 +21,45 @@ impl Eq for SmallUint {}
impl PartialOrd for SmallUint { impl PartialOrd for SmallUint {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for SmallUint {
fn cmp(&self, other: &Self) -> Ordering {
match (&self.0, &other.0) { match (&self.0, &other.0) {
(SmallUintType::Inline(i), SmallUintType::Inline(j)) => Some(i.cmp(j)), (SmallUintType::Inline(i), SmallUintType::Inline(j)) => i.cmp(j),
(SmallUintType::Inline(_), SmallUintType::Heap((_, _))) => Some(Ordering::Less), (SmallUintType::Inline(_), SmallUintType::Heap((_, _))) => Ordering::Less,
(SmallUintType::Heap((_, _)), SmallUintType::Inline(_)) => Some(Ordering::Greater), (SmallUintType::Heap((_, _)), SmallUintType::Inline(_)) => Ordering::Greater,
(SmallUintType::Heap((r, s)), SmallUintType::Heap((i, j))) => match j.cmp(s) { (SmallUintType::Heap((p, s)), SmallUintType::Heap((q, t))) => match s.cmp(t) {
Ordering::Greater => Some(Ordering::Less),
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(*p, *s) };
let slice2 = unsafe { core::slice::from_raw_parts(*i, *j) }; let slice2 = unsafe { core::slice::from_raw_parts(*q, *t) };
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::Equal => {}
Ordering::Greater => return Some(Ordering::Greater), o => return o,
_ => {}
} }
} }
Some(Ordering::Equal) Ordering::Equal
}
o => o,
} }
},
} }
} }
} }
impl PartialEq for SmallInt { impl PartialEq for SmallInt {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &SmallInt) -> bool {
match (&self.0, &other.0) { match (&self.0, &other.0) {
(SmallIntType::Inline(i), SmallIntType::Inline(j)) => i.eq(j), (SmallIntType::Inline(i), SmallIntType::Inline(j)) => i == j,
(SmallIntType::Heap((r, s)), SmallIntType::Heap((i, j))) => { (SmallIntType::Heap((p, s)), SmallIntType::Heap((q, t))) => {
if s.signum() != j.signum() { if s != t { // need to compare signs, at minimum
return false; return false;
} }
let slice1 = unsafe { core::slice::from_raw_parts(*p, s.unsigned_abs()) };
match j.cmp(s) { let slice2 = unsafe { core::slice::from_raw_parts(*q, t.unsigned_abs()) };
Ordering::Greater => false, slice1 == slice2
Ordering::Less => false,
Ordering::Equal => {
let us = s.unsigned_abs();
let uj = j.unsigned_abs();
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, (_, _) => false,
} }
@ -93,36 +70,27 @@ 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; Some(self.cmp(other))
match &self.0 {
SmallIntType::Inline(i) => a_sign = i.signum() as i8,
SmallIntType::Heap((_, s)) => a_sign = s.signum() as i8,
}
let b_sign;
match &other.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) => Some(Ordering::Greater),
x if (x.0 < 0 && x.1 >= 0) => Some(Ordering::Less),
x if (x.0 >= 0 && x.1 >= 0) => SmallUint::from_smallint_unsigned(self.clone())
.partial_cmp(&SmallUint::from_smallint_unsigned(other.clone())),
x if (x.0 < 0 && x.1 < 0) => SmallUint::from_smallint_unsigned(other.clone())
.partial_cmp(&SmallUint::from_smallint_unsigned(self.clone())),
(_, _) => None,
}
} }
} }
impl Ord for SmallInt { impl Ord for SmallInt {
fn cmp(&self, other: &Self) -> Ordering { fn cmp(&self, other: &Self) -> Ordering {
self.partial_cmp(other).expect("This should not happen.") let a_sign = match &self.0 {
SmallIntType::Inline(i) => i.cmp(&0),
SmallIntType::Heap((_, s)) => s.cmp(&0),
};
let b_sign = match &other.0 {
SmallIntType::Inline(j) => j.cmp(&0),
SmallIntType::Heap((_, t)) => t.cmp(&0),
};
match a_sign.cmp(&b_sign) {
Ordering::Equal => match a_sign {
Ordering::Less => other.unsigned_abs().cmp(&self.unsigned_abs()),
Ordering::Equal => Ordering::Equal,
Ordering::Greater => self.unsigned_abs().cmp(&other.unsigned_abs()),
},
o => o,
}
} }
} }

View File

@ -260,7 +260,7 @@ fn test_op_neg() {
#[cfg(feature = "num-bigint")] #[cfg(feature = "num-bigint")]
fn test_conversion_sign_drop() { fn test_conversion_sign_drop() {
run_tests_i_1( run_tests_i_1(
|i| BigUint::from(&SmallUint::from_smallint_unsigned(i)), |i| BigUint::from(&SmallInt::unsigned_abs(&i)),
|i| i.magnitude().clone() |i| i.magnitude().clone()
); );
} }