Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add truncate_* operations on BigInt and BigUint #202

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/bigint.rs
Expand Up @@ -29,6 +29,7 @@ mod bits;
mod convert;
mod power;
mod shift;
mod truncate;

#[cfg(any(feature = "quickcheck", feature = "arbitrary"))]
mod arbitrary;
Expand Down
17 changes: 17 additions & 0 deletions src/bigint/truncate.rs
@@ -0,0 +1,17 @@
use num_traits::{WrappingNeg, Zero};

use super::{BigUint, BigInt, Sign};
use crate::biguint::TruncateFrom;

impl BigInt {
/// Returns the input reduced modulo 2 to the power of the size of the target type.
/// This is analogous to the behavior of `as` conversion on integral types.
#[inline]
pub fn truncate<T: TruncateFrom<BigUint> + Zero + WrappingNeg>(&self) -> T {
match self.sign {
Sign::Minus => self.data.truncate::<T>().wrapping_neg(),
Sign::NoSign => T::zero(),
Sign::Plus => self.data.truncate(),
}
}
}
2 changes: 2 additions & 0 deletions src/biguint.rs
Expand Up @@ -24,6 +24,7 @@ mod iter;
mod monty;
mod power;
mod shift;
mod truncate;

#[cfg(any(feature = "quickcheck", feature = "arbitrary"))]
mod arbitrary;
Expand All @@ -33,6 +34,7 @@ mod serde;

pub(crate) use self::convert::to_str_radix_reversed;
pub use self::iter::{U32Digits, U64Digits};
pub use truncate::TruncateFrom;

/// A big unsigned integer type.
pub struct BigUint {
Expand Down
76 changes: 76 additions & 0 deletions src/biguint/truncate.rs
@@ -0,0 +1,76 @@
use super::{big_digit, BigUint};

pub trait TruncateFrom<S> {
/// Returns the input reduced modulo 2 to the power of the size of the target type.
/// This is analogous to the behavior of `as` conversion on integral types.
fn truncate_from(_: &S) -> Self;
}

impl BigUint {
/// Returns the input reduced modulo 2 to the power of the size of the target type.
/// This is analogous to the behavior of `as` conversion on integral types.
#[inline]
pub fn truncate<T: TruncateFrom<Self>>(&self) -> T {
TruncateFrom::truncate_from(self)
}
}

macro_rules! impl_truncate_large {
($T:ty, $size:expr) => {
impl TruncateFrom<BigUint> for $T {
#[inline]
fn truncate_from(n: &BigUint) -> Self {
let mut ret = 0;
let mut bits = 0;

for i in n.data.iter() {
if bits >= $size {
break;
}

ret += Self::from(*i) << bits;
bits += big_digit::BITS;
}
ret
}
}
};
}

macro_rules! impl_truncate_from {
($from:ty: $($T:ty),* $(,)*) => {
$(
impl TruncateFrom<BigUint> for $T {
#[inline]
fn truncate_from(n: &BigUint) -> Self {
n.truncate::<$from>() as Self
}
}
)*
};
}

#[cfg(not(u64_digit))]
impl_truncate_large!(u32, 32);
impl_truncate_large!(u64, 64);
impl_truncate_large!(u128, 128);

#[cfg(not(u64_digit))]
impl_truncate_from! { u32: u16, u8 }

#[cfg(u64_digit)]
impl_truncate_from! { u64: u32, u16, u8 }

#[cfg(target_pointer_width = "64")]
impl_truncate_from!(u64: usize);
#[cfg(target_pointer_width = "32")]
impl_truncate_from!(u32: usize);
#[cfg(target_pointer_width = "16")]
impl_truncate_from!(u16: usize);

impl_truncate_from!(u128: i128);
impl_truncate_from!(usize: isize);
impl_truncate_from!(u64: i64);
impl_truncate_from!(u32: i32);
impl_truncate_from!(u16: i16);
impl_truncate_from!(u8: i8);
39 changes: 39 additions & 0 deletions tests/bigint.rs
Expand Up @@ -1404,3 +1404,42 @@ fn test_set_bit() {
x.set_bit(0, false);
assert_eq!(x, BigInt::from_biguint(Minus, BigUint::one() << 200));
}

#[test]
fn test_truncate() {
let x = BigInt::from(0xfedcba9012345678_u64);
assert_eq!(x.truncate_u8(), 0x78);
assert_eq!(x.truncate_u16(), 0x5678);
assert_eq!(x.truncate_u32(), 0x12345678);
assert_eq!(x.truncate_u64(), 0xfedcba9012345678);
assert_eq!(x.truncate_u128(), 0xfedcba9012345678);
assert_eq!(x.truncate_i8(), 0x78);
assert_eq!(x.truncate_i16(), 0x5678);
assert_eq!(x.truncate_i32(), 0x12345678);
assert_eq!(x.truncate_i64(), -0x123456fedcba988);
assert_eq!(x.truncate_i128(), 0xfedcba9012345678);

let x = BigInt::zero();
assert_eq!(x.truncate_u8(), 0);
assert_eq!(x.truncate_u16(), 0);
assert_eq!(x.truncate_u32(), 0);
assert_eq!(x.truncate_u64(), 0);
assert_eq!(x.truncate_u128(), 0);
assert_eq!(x.truncate_i8(), 0);
assert_eq!(x.truncate_i16(), 0);
assert_eq!(x.truncate_i32(), 0);
assert_eq!(x.truncate_i64(), 0);
assert_eq!(x.truncate_i128(), 0);

let x = -BigInt::from(500);
assert_eq!(x.truncate_u8(), 0x0c);
assert_eq!(x.truncate_u16(), 0xfe0c);
assert_eq!(x.truncate_u32(), 0xfffffe0c);
assert_eq!(x.truncate_u64(), 0xfffffffffffffe0c);
assert_eq!(x.truncate_u128(), 0xfffffffffffffffffffffffffffffe0c);
assert_eq!(x.truncate_i8(), 0x0c);
assert_eq!(x.truncate_i16(), -500);
assert_eq!(x.truncate_i32(), -500);
assert_eq!(x.truncate_i64(), -500);
assert_eq!(x.truncate_i128(), -500);
}
27 changes: 27 additions & 0 deletions tests/biguint.rs
Expand Up @@ -1838,3 +1838,30 @@ fn test_set_bit() {
x.set_bit(1, false);
assert_eq!(x, BigUint::zero());
}

#[test]
fn test_truncate() {
let x = BigUint::from(0xfedcba9012345678_u64);
assert_eq!(x.truncate_u8(), 0x78);
assert_eq!(x.truncate_u16(), 0x5678);
assert_eq!(x.truncate_u32(), 0x12345678);
assert_eq!(x.truncate_u64(), 0xfedcba9012345678);
assert_eq!(x.truncate_u128(), 0xfedcba9012345678);
assert_eq!(x.truncate_i8(), 0x78);
assert_eq!(x.truncate_i16(), 0x5678);
assert_eq!(x.truncate_i32(), 0x12345678);
assert_eq!(x.truncate_i64(), -0x123456fedcba988);
assert_eq!(x.truncate_i128(), 0xfedcba9012345678);

let x = BigUint::new(vec![0xffffffff, 0xfedcba90, 1, 1]);
assert_eq!(x.truncate_u8(), 0xff);
assert_eq!(x.truncate_u16(), 0xffff);
assert_eq!(x.truncate_u32(), 0xffffffff);
assert_eq!(x.truncate_u64(), 0xfedcba90ffffffff);
assert_eq!(x.truncate_u128(), 0x100000001fedcba90ffffffff);
assert_eq!(x.truncate_i8(), -1);
assert_eq!(x.truncate_i16(), -1);
assert_eq!(x.truncate_i32(), -1);
assert_eq!(x.truncate_i64(), -0x0123456f00000001);
assert_eq!(x.truncate_i128(), 0x100000001fedcba90ffffffff);
}