Skip to content

Commit

Permalink
Fix rustfmt errors
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronKutch committed Oct 1, 2019
1 parent 5de5ef9 commit 4a31e0a
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 94 deletions.
71 changes: 37 additions & 34 deletions src/apint/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,41 @@ use crate::{
};
use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};

// Moved here because of a bug in rustfmt. This entire file will be broken up in
// a future commit. assigns `$sum + $sub` to `$target`,
// and assigns `$val + $add` to `$sum`
macro_rules! special1 {
($len:expr, $sum:ident, $sub:ident, $target:ident, $val:expr, $add:ident) => {{
// subtraction (`sub` is the two's complement of some value)
let (temp, mut carry) = $sum[0].carrying_add($sub[0]);
$target[0] = temp;
for i in 1..($len - 1) {
let temp = $sum[i]
.dd()
.wrapping_add($sub[i].dd())
.wrapping_add(carry.dd());
$target[i] = temp.lo();
carry = temp.hi();
}
$target[$len - 1] = $sum[$len - 1]
.wrapping_add($sub[$len - 1])
.wrapping_add(carry);
let (temp, mut carry) = $add[0].carrying_add($val);
$sum[0] = temp;
for i0 in 1..$len {
if carry == Digit::zero() {
for i1 in i0..$len {
$sum[i1] = $add[i1];
break
}
}
let temp = $add[i0].carrying_add(carry);
$sum[i0] = temp.0;
carry = temp.1;
}
}};
}

/// # Basic Arithmetic Operations
///
/// **Note**: unless otherwise noted in the function specific documentation,
Expand Down Expand Up @@ -818,40 +853,6 @@ impl ApInt {
}};
}

// assigns `$sum + $sub` to `$target`,
// and assigns `$val + $add` to `$sum`
macro_rules! special1 {
($len:expr, $sum:ident, $sub:ident, $target:ident, $val:expr, $add:ident) => {{
// subtraction (`sub` is the two's complement of some value)
let (temp, mut carry) = $sum[0].carrying_add($sub[0]);
$target[0] = temp;
for i in 1..($len - 1) {
let temp = $sum[i]
.dd()
.wrapping_add($sub[i].dd())
.wrapping_add(carry.dd());
$target[i] = temp.lo();
carry = temp.hi();
}
$target[$len - 1] = $sum[$len - 1]
.wrapping_add($sub[$len - 1])
.wrapping_add(carry);
let (temp, mut carry) = $add[0].carrying_add($val);
$sum[0] = temp;
for i0 in 1..$len {
if carry == Digit::zero() {
for i1 in i0..$len {
$sum[i1] = $add[i1];
break
}
}
let temp = $add[i0].carrying_add(carry);
$sum[i0] = temp.0;
carry = temp.1;
}
}};
}

// assigns `$sum + $add` to `$sum`
macro_rules! add {
($len:expr, $sum:ident, $add:ident) => {{
Expand Down Expand Up @@ -2662,6 +2663,7 @@ mod tests {
if temp0 != (lhs.clone() & &anti_overflow_mask) {
panic!(
"wrong div\nlhs:{:?}\nactual:{:?}\nrhs:{:?}\nthird:{:?}\\
\
nrem:{:?}\nmul:{:?}\nmul_plus_rem:{:?}\ntemp0:{:?}\ntemp1:\
{:?}",
lhs,
Expand All @@ -2678,6 +2680,7 @@ mod tests {
if temp1 != rem {
panic!(
"wrong rem\nlhs:{:?}\nactual:{:?}\nrhs:{:?}\nthird:{:?}\\
\
nrem:{:?}\nmul:{:?}\nmul_plus_rem:{:?}\ntemp0:{:?}\ntemp1:\
{:?}",
lhs,
Expand Down
58 changes: 29 additions & 29 deletions src/apint/casting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,37 +534,37 @@ mod tests {
ApInt::from_u128(0x0000_0000_FFFF_FFFF_FFFF_FFFF_0000_0000),
ApInt::from_u128(0x0000_0000_0000_0000_FFFF_FFFF_FFFF_FFFF),
ApInt::all_set(BitWidth::w128()),
/* ApInt::zero(width_256),
* ApInt::one(width_256),
* ApInt::repeat_digit(width_256, Digit(1)),
* ApInt::repeat_digit(width_256, Digit(42)),
* ApInt::repeat_digit(width_256, Digit(1337)),
* ApInt::repeat_digit(width_256, 0xFFFF_FFFF_0000_0000),
* ApInt::repeat_digit(width_256, 0x0000_FFFF_FFFF_0000),
* ApInt::repeat_digit(width_256, 0x0000_0000_FFFF_FFFF),
* ApInt::all_set(width_256), */

/* ApInt::zero(width_500),
* ApInt::one(width_500),
* ApInt::repeat_digit(width_500, Digit(1)),
* ApInt::repeat_digit(width_500, Digit(42)),
* ApInt::repeat_digit(width_500, Digit(1337)),
* ApInt::repeat_digit(width_500, 0xFFFF_FFFF_0000_0000),
* ApInt::repeat_digit(width_500, 0x0000_FFFF_FFFF_0000),
* ApInt::repeat_digit(width_500, 0x0000_0000_FFFF_FFFF),
* ApInt::all_set(width_500), */

/* ApInt::zero(width_512),
* ApInt::one(width_512),
* ApInt::repeat_digit(width_512, Digit(1)),
* ApInt::repeat_digit(width_512, Digit(42)),
* ApInt::repeat_digit(width_512, Digit(1337)),
* ApInt::repeat_digit(width_512, 0xFFFF_FFFF_0000_0000),
* ApInt::repeat_digit(width_512, 0x0000_FFFF_FFFF_0000),
* ApInt::repeat_digit(width_512, 0x0000_0000_FFFF_FFFF),
* ApInt::all_set(width_512), */
]
.into_iter()
// ApInt::zero(width_256),
// ApInt::one(width_256),
// ApInt::repeat_digit(width_256, Digit(1)),
// ApInt::repeat_digit(width_256, Digit(42)),
// ApInt::repeat_digit(width_256, Digit(1337)),
// ApInt::repeat_digit(width_256, 0xFFFF_FFFF_0000_0000),
// ApInt::repeat_digit(width_256, 0x0000_FFFF_FFFF_0000),
// ApInt::repeat_digit(width_256, 0x0000_0000_FFFF_FFFF),
// ApInt::all_set(width_256),

// ApInt::zero(width_500),
// ApInt::one(width_500),
// ApInt::repeat_digit(width_500, Digit(1)),
// ApInt::repeat_digit(width_500, Digit(42)),
// ApInt::repeat_digit(width_500, Digit(1337)),
// ApInt::repeat_digit(width_500, 0xFFFF_FFFF_0000_0000),
// ApInt::repeat_digit(width_500, 0x0000_FFFF_FFFF_0000),
// ApInt::repeat_digit(width_500, 0x0000_0000_FFFF_FFFF),
// ApInt::all_set(width_500),

// ApInt::zero(width_512),
// ApInt::one(width_512),
// ApInt::repeat_digit(width_512, Digit(1)),
// ApInt::repeat_digit(width_512, Digit(42)),
// ApInt::repeat_digit(width_512, Digit(1337)),
// ApInt::repeat_digit(width_512, 0xFFFF_FFFF_0000_0000),
// ApInt::repeat_digit(width_512, 0x0000_FFFF_FFFF_0000),
// ApInt::repeat_digit(width_512, 0x0000_0000_FFFF_FFFF),
// ApInt::all_set(width_512),
}

mod clone {
Expand Down
15 changes: 10 additions & 5 deletions src/apint/rand_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ impl ApInt {
assert!(required_digits >= 1);
use rand::distributions::{Distribution, Standard};
let random_digits = Standard.sample_iter(rng).take(required_digits);
// The truncation will be cheap always!
ApInt::from_iter(random_digits)
.expect("We asserted that `required_digits` is at least `1` or greater
so it is safe to assume that `ApInt::from_iter` won't fail.")
.into_truncate(width) // This truncation will be cheap always!
.expect("`BitWidth::required_digits` returns an upper bound for the
number of required digits, so it is safe to truncate.")
.expect(
"We asserted that `required_digits` is at least `1` or greater
so it is safe to assume that `ApInt::from_iter` won't fail.",
)
.into_truncate(width)
.expect(
"`BitWidth::required_digits` returns an upper bound for the
number of required digits, so it is safe to truncate.",
)
}

/// Randomizes the digits of this `ApInt` inplace.
Expand Down
26 changes: 18 additions & 8 deletions src/apint/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,20 @@ impl ApInt {
///
/// ```no_run
/// # use apint::ApInt;
/// let a = ApInt::from_str_radix(10, "42"); // ok
/// let b = ApInt::from_str_radix( 2, "1011011"); // ok (dec. = 91)
/// let c = ApInt::from_str_radix(16, "ffcc00"); // ok (dec. = 16763904)
/// let c = ApInt::from_str_radix(10, "256"); // Error: 256 does not fit within 8 bits!
/// let d = ApInt::from_str_radix( 2, "01020"); // Error: Invalid digit '2' at position 3 for given radix.
/// let e = ApInt::from_str_radix(16, "hello"); // Error: "hello" is not a valid ApInt representation!
///
/// // ok
/// let a = ApInt::from_str_radix(10, "42");
/// // ok (dec. = 91)
/// let b = ApInt::from_str_radix(2, "1011011");
/// // ok (dec. = 16763904)
/// let c = ApInt::from_str_radix(16, "ffcc00");
///
/// // Error: 256 does not fit within 8 bits!
/// let c = ApInt::from_str_radix(10, "256");
/// // Error: Invalid digit '2' at position 3 for given radix.
/// let d = ApInt::from_str_radix(2, "01020");
/// // Error: "hello" is not a valid ApInt representation!
/// let e = ApInt::from_str_radix(16, "hello");
/// ```
pub fn from_str_radix<R, S>(radix: R, input: S) -> Result<ApInt>
where
Expand Down Expand Up @@ -525,12 +533,14 @@ mod tests {
(8, "777_747_666", 0o777_747_666),
(8, "111", 0b001_001_001),
(8, "7_7777_7777_7777_7777_7777", u64::max_value() / 2),
// ( 8, "17_7777_7777_7777_7777_7777", u64::max_value()), // Does not work, yet! Should it work?
// Does not work, yet! Should it work?
// ( 8, "17_7777_7777_7777_7777_7777", u64::max_value()),
(10, "100", 100),
(10, "42", 42),
(10, "1337", 1337),
(10, "5_000_000", 5_000_000),
// (10, "18_446_744_073_709_551_615", u64::max_value()), // Does not work, yet!
// Does not work, yet!
// (10, "18_446_744_073_709_551_615", u64::max_value()),
(16, "100", 0x100),
(16, "42", 0x42),
(16, "1337", 0x1337),
Expand Down
31 changes: 21 additions & 10 deletions src/apint/to_primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,13 @@ impl ApInt {
if actual_width < target_width {
// Sign extend the `i128`. Fill up with `1` up to `128` bits
// starting from the sign bit position.
let b = actual_width.to_usize(); // Number of bits representing the number in x.
let m: i128 = 1 << (b - 1); // Mask can be pre-computed if b is fixed.
result = (result ^ m) - m; // Resulting sign-extended number.

// Number of bits representing the number in x.
let b = actual_width.to_usize();
// Mask can be pre-computed if b is fixed.
let m: i128 = 1 << (b - 1);
// Resulting sign-extended number.
result = (result ^ m) - m;
}

result
Expand Down Expand Up @@ -534,10 +538,13 @@ impl ApInt {
if actual_width < target_width {
// Sign extend the `i128`. Fill up with `1` up to `128` bits
// starting from the sign bit position.
let b = actual_width.to_usize(); // Number of bits representing the number in x.
let m: i128 = 1 << (b - 1); // Mask can be pre-computed if b is fixed.
result = (result ^ m).wrapping_sub(m); // Resulting sign-extended
// number.

// Number of bits representing the number in x.
let b = actual_width.to_usize();
// Mask can be pre-computed if b is fixed.
let m: i128 = 1 << (b - 1);
// Resulting sign-extended number.
result = (result ^ m).wrapping_sub(m);
}

Ok(result)
Expand Down Expand Up @@ -982,9 +989,13 @@ mod tests {
if actual_width < target_width {
// Sign extend the `i128`. Fill up with `1` up to `128` bits
// starting from the sign bit position.
let b = actual_width.to_usize(); // Number of bits representing the number in x.
let m: i128 = 1 << (b - 1); // Mask can be pre-computed if b is fixed.
result = (result ^ m).wrapping_sub(m); // Resulting sign-extended number.

// Number of bits representing the number in x.
let b = actual_width.to_usize();
// Mask can be pre-computed if b is fixed.
let m: i128 = 1 << (b - 1);
// Resulting sign-extended number.
result = (result ^ m).wrapping_sub(m);
}
assert_eq!(apint.try_to_i128(), Ok(result))
} else {
Expand Down
19 changes: 11 additions & 8 deletions src/digit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,14 +449,17 @@ impl Digit {
{
let from = from.into();
self.verify_valid_bitwidth(from)?;

let b = from.to_usize(); // number of bits representing the number in x
let x = self.repr() as i64; // sign extend this b-bit number to r
let m: i64 = 1 << (b - 1); // mask can be pre-computed if b is fixed
// x = x & ((1 << b) - 1); // (Skip this if bits in x above position b are
// already zero.) We don't need this step
// since this condition is an invariant of `Digit`.
let r: i64 = (x ^ m).wrapping_sub(m); // resulting sign-extended number
// number of bits representing the number in x
let b = from.to_usize();
// sign extend this b-bit number to r
let x = self.repr() as i64;
// mask can be pre-computed if b is fixed
// (Skip this if bits in x above position b are already zero.)
// x = x & ((1 << b) - 1);
// We don't need this step since this condition is an invariant of `Digit`.
let m: i64 = 1 << (b - 1);
// resulting sign-extended number
let r: i64 = (x ^ m).wrapping_sub(m);
self.0 = r as u64;
Ok(())
}
Expand Down

0 comments on commit 4a31e0a

Please sign in to comment.