Skip to content

Commit

Permalink
Fix rustfmt errors
Browse files Browse the repository at this point in the history
The wierd change done in arithmetic.rs is to satisfy a rustfmt off-by-one bug
  • Loading branch information
AaronKutch committed Dec 8, 2019
1 parent 6f43afc commit 4320db5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 55 deletions.
10 changes: 6 additions & 4 deletions src/apint/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,19 +845,19 @@ 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) => {{
($len:expr, $sum:ident, $sub:ident, $targ: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;
$targ[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();
$targ[i] = temp.lo();
carry = temp.hi();
}
$target[$len - 1] = $sum[$len - 1]
$targ[$len - 1] = $sum[$len - 1]
.wrapping_add($sub[$len - 1])
.wrapping_add(carry);
let (temp, mut carry) = $add[0].carrying_add($val);
Expand Down Expand Up @@ -2689,6 +2689,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 @@ -2705,6 +2706,7 @@ mod tests {
if temp1 != rem {
panic!(
"wrong rem\nlhs:{:?}\nactual:{:?}\nrhs:{:?}\nthird:{:?}\\
\
nrem:{:?}\nmul:{:?}\nmul_plus_rem:{:?}\ntemp0:{:?}\ntemp1:\
{:?}",
lhs,
Expand Down
29 changes: 0 additions & 29 deletions src/apint/casting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,35 +540,6 @@ 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()
}
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 @@ -37,12 +37,17 @@ impl ApInt {
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
24 changes: 16 additions & 8 deletions src/apint/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,18 @@ 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 @@ -540,12 +546,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
30 changes: 21 additions & 9 deletions src/apint/to_primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,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 @@ -537,9 +541,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 @@ -984,9 +992,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

0 comments on commit 4320db5

Please sign in to comment.