mirror of
https://gitlab.com/artofrev/smallint.git
synced 2024-12-04 17:11:38 -05:00
fix add
This commit is contained in:
parent
2b09c240c4
commit
021454c438
26
src/lib.rs
26
src/lib.rs
|
@ -126,8 +126,6 @@ impl Add<SmallUint> for SmallUint {
|
||||||
match i.overflowing_add(j) {
|
match i.overflowing_add(j) {
|
||||||
(t, false) => Self(SmallUintType::Inline(t)),
|
(t, false) => Self(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;
|
||||||
|
@ -138,7 +136,9 @@ impl Add<SmallUint> for SmallUint {
|
||||||
v >>= 32;
|
v >>= 32;
|
||||||
}
|
}
|
||||||
|
|
||||||
Self(SmallUintType::Heap((<Box<[u32]>>::from(res).as_mut_ptr(), 5)))
|
let mut slice = ManuallyDrop::new(<Box<[u32]>>::from(res));
|
||||||
|
|
||||||
|
Self(SmallUintType::Heap((slice.as_mut_ptr(), 5)))
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -146,7 +146,7 @@ impl Add<SmallUint> for SmallUint {
|
||||||
(&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, 1];
|
let mut res = [0, 0, 0, 0];
|
||||||
|
|
||||||
let mut v = i;
|
let mut v = i;
|
||||||
#[allow(clippy::needless_range_loop)]
|
#[allow(clippy::needless_range_loop)]
|
||||||
|
@ -155,6 +155,7 @@ impl Add<SmallUint> for SmallUint {
|
||||||
|
|
||||||
v >>= 32;
|
v >>= 32;
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = add_two_slices(slice1, &res[..]);
|
let result = add_two_slices(slice1, &res[..]);
|
||||||
let size = result.len();
|
let size = result.len();
|
||||||
|
|
||||||
|
@ -181,6 +182,7 @@ impl Add<SmallUint> for SmallUint {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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 {
|
||||||
|
@ -429,7 +431,7 @@ mod conversion_tests {
|
||||||
use crate::SmallUint;
|
use crate::SmallUint;
|
||||||
|
|
||||||
#[cfg(feature = "num-bigint")]
|
#[cfg(feature = "num-bigint")]
|
||||||
use num_bigint::{BigInt, Sign, BigUint, };
|
use num_bigint::{BigInt, Sign, BigUint};
|
||||||
|
|
||||||
macro_rules! conversion_tests {
|
macro_rules! conversion_tests {
|
||||||
($t:ty, $i:ident) => {
|
($t:ty, $i:ident) => {
|
||||||
|
@ -461,11 +463,19 @@ mod conversion_tests {
|
||||||
#[cfg(feature = "num-bigint")]
|
#[cfg(feature = "num-bigint")]
|
||||||
fn test_op_add_u_u() {
|
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, 2, 1, 5]));
|
let k = SmallUint::from(u128::MAX);
|
||||||
let q = i + k;
|
let q = i + k;
|
||||||
println!("{:?}", BigUint::from(&q));
|
assert_eq!(BigUint::from(&q), BigUint::from(u128::MAX) + u128::MAX);
|
||||||
println!("{:?}", BigUint::new(vec![5, 4, 9, 3, 2, 1, 5]) + 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]));
|
||||||
|
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]) + BigUint::new(vec![5, 4, 9, 3, 1, 81]));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user