Skip to content

Commit

Permalink
use rustfmt 1.4.8
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronKutch committed Sep 23, 2019
1 parent 37a90fc commit b4ed7ab
Show file tree
Hide file tree
Showing 23 changed files with 1,189 additions and 1,173 deletions.
560 changes: 316 additions & 244 deletions src/apint/arithmetic.rs

Large diffs are not rendered by default.

40 changes: 15 additions & 25 deletions src/apint/bitwise.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::{
apint::{
utils::{
DataAccess,
DataAccessMut,
},
utils::{DataAccess, DataAccessMut},
ApInt,
},
bitpos::BitPos,
Expand All @@ -12,21 +9,10 @@ use crate::{
digit::Bit,
errors::Result,
traits::Width,
utils::{
forward_mut_impl,
try_forward_bin_mut_impl,
},
utils::{forward_mut_impl, try_forward_bin_mut_impl},
};

use std::ops::{
BitAnd,
BitAndAssign,
BitOr,
BitOrAssign,
BitXor,
BitXorAssign,
Not,
};
use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not};

/// # Bitwise Operations
impl ApInt {
Expand Down Expand Up @@ -176,8 +162,8 @@ impl ApInt {
///
/// # Note
///
/// - If the bit at the given position was `0` it will be `1`
/// after this operation and vice versa.
/// - If the bit at the given position was `0` it will be `1` after this
/// operation and vice versa.
///
/// # Errors
///
Expand Down Expand Up @@ -260,9 +246,10 @@ impl ApInt {
///
/// # Note
///
/// - If the sign bit was `0` it will be `1` after this operation and vice versa.
/// - Depending on the interpretation of the `ApInt` this
/// operation changes its signedness.
/// - If the sign bit was `0` it will be `1` after this operation and vice
/// versa.
/// - Depending on the interpretation of the `ApInt` this operation changes
/// its signedness.
pub fn flip_sign_bit(&mut self) {
let sign_bit_pos = self.width().sign_bit_pos();
self.flip_bit_at(sign_bit_pos).expect(
Expand All @@ -282,7 +269,8 @@ impl ApInt {
.sum::<usize>()
}

/// Returns the number of zeros in the binary representation of this `ApInt`.
/// Returns the number of zeros in the binary representation of this
/// `ApInt`.
pub fn count_zeros(&self) -> usize {
let zeros = self
.as_digit_slice()
Expand All @@ -295,7 +283,8 @@ impl ApInt {
zeros - (digit::BITS - self.width().excess_bits().unwrap_or(digit::BITS))
}

/// Returns the number of leading zeros in the binary representation of this `ApInt`.
/// Returns the number of leading zeros in the binary representation of this
/// `ApInt`.
pub fn leading_zeros(&self) -> usize {
let mut zeros = 0;
for d in self.as_digit_slice().iter().rev() {
Expand All @@ -308,7 +297,8 @@ impl ApInt {
zeros - (digit::BITS - self.width().excess_bits().unwrap_or(digit::BITS))
}

/// Returns the number of trailing zeros in the binary representation of this `ApInt`.
/// Returns the number of trailing zeros in the binary representation of
/// this `ApInt`.
pub fn trailing_zeros(&self) -> usize {
let mut zeros = 0;
for d in self.as_digit_slice() {
Expand Down
134 changes: 62 additions & 72 deletions src/apint/casting.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
use crate::{
apint::ApInt,
errors::{
Error,
Result,
},
errors::{Error, Result},
};

use crate::{
bitwidth::BitWidth,
digit::Bit,
storage::Storage,
traits::Width,
utils::{
forward_bin_mut_impl,
try_forward_bin_mut_impl,
},
utils::{forward_bin_mut_impl, try_forward_bin_mut_impl},
};

impl Clone for ApInt {
Expand Down Expand Up @@ -155,8 +149,8 @@ impl ApInt {
return Error::truncation_bitwidth_too_large(target_width, actual_width)
.with_annotation(format!(
"Cannot truncate `ApInt` with a width of {:?}
to an `ApInt` with a width of {:?} bits. \
Do you mean to extend the instance instead?",
to an `ApInt` with a width of {:?} bits. Do you mean to extend \
the instance instead?",
actual_width.to_usize(),
target_width.to_usize()
))
Expand All @@ -176,8 +170,8 @@ impl ApInt {
// The same applies to all bit widths that require the same
// amount of digits for their representation.
let excess_width = target_width.excess_bits().expect(
"We already filtered cases where `excess_bits` may return `None` \
by requiring that `self.width() > target_width`.",
"We already filtered cases where `excess_bits` may return `None` by \
requiring that `self.width() > target_width`.",
);
self.most_significant_digit_mut()
.truncate_to(excess_width)
Expand Down Expand Up @@ -257,8 +251,8 @@ impl ApInt {
if target_width < actual_width {
return Error::extension_bitwidth_too_small(target_width, actual_width)
.with_annotation(format!(
"Cannot zero-extend bit-width of {:?} to {:?} bits. \
Do you mean to truncate the instance instead?",
"Cannot zero-extend bit-width of {:?} to {:?} bits. Do you mean to \
truncate the instance instead?",
actual_width, target_width
))
.into()
Expand Down Expand Up @@ -343,8 +337,8 @@ impl ApInt {
if target_width < actual_width {
return Error::extension_bitwidth_too_small(target_width, actual_width)
.with_annotation(format!(
"Cannot sign-extend bit-width of {:?} to {:?} bits. \
Do you mean to truncate the instance instead?",
"Cannot sign-extend bit-width of {:?} to {:?} bits. Do you mean to \
truncate the instance instead?",
actual_width, target_width
))
.into()
Expand Down Expand Up @@ -387,7 +381,8 @@ impl ApInt {
assert!(target_req_digits > actual_req_digits);
let additional_digits = target_req_digits - actual_req_digits;

// Fill most-significant-digit of `self` with `1` starting from its most-significant bit.
// Fill most-significant-digit of `self` with `1` starting from its
// most-significant bit.
if let Some(excess_width) = actual_width.excess_width() {
self.most_significant_digit_mut()
.sign_extend_from(excess_width)?;
Expand Down Expand Up @@ -444,11 +439,9 @@ impl ApInt {
///
/// This operation will forward to
///
/// - [`truncate`](struct.ApInt.html#method.truncate)
/// if `target_width` is less than or equal to the width of
/// the given `ApInt`
/// - [`zero_extend`](struct.ApInt.html#method.zero_extend)
/// otherwise
/// - [`truncate`](struct.ApInt.html#method.truncate) if `target_width` is
/// less than or equal to the width of the given `ApInt`
/// - [`zero_extend`](struct.ApInt.html#method.zero_extend) otherwise
pub fn zero_resize<W>(&mut self, target_width: W)
where
W: Into<BitWidth>,
Expand All @@ -458,13 +451,13 @@ impl ApInt {

if target_width <= actual_width {
self.truncate(target_width).expect(
"It was asserted that `target_width` is \
a valid truncation `BitWidth` in this context.",
"It was asserted that `target_width` is a valid truncation `BitWidth` \
in this context.",
)
} else {
self.zero_extend(target_width).expect(
"It was asserted that `target_width` is \
a valid zero-extension `BitWidth` in this context.",
"It was asserted that `target_width` is a valid zero-extension \
`BitWidth` in this context.",
)
}
}
Expand All @@ -475,11 +468,9 @@ impl ApInt {
///
/// This operation will forward to
///
/// - [`truncate`](struct.ApInt.html#method.truncate)
/// if `target_width` is less than or equal to the width of
/// the given `ApInt`
/// - [`sign_extend`](struct.ApInt.html#method.sign_extend)
/// otherwise
/// - [`truncate`](struct.ApInt.html#method.truncate) if `target_width` is
/// less than or equal to the width of the given `ApInt`
/// - [`sign_extend`](struct.ApInt.html#method.sign_extend) otherwise
pub fn sign_resize<W>(&mut self, target_width: W)
where
W: Into<BitWidth>,
Expand All @@ -489,13 +480,13 @@ impl ApInt {

if target_width <= actual_width {
self.truncate(target_width).expect(
"It was asserted that `target_width` is \
a valid truncation `BitWidth` in this context.",
"It was asserted that `target_width` is a valid truncation `BitWidth` \
in this context.",
)
} else {
self.sign_extend(target_width).expect(
"It was asserted that `target_width` is \
a valid sign-extension `BitWidth` in this context.",
"It was asserted that `target_width` is a valid sign-extension \
`BitWidth` in this context.",
)
}
}
Expand Down Expand Up @@ -545,52 +536,54 @@ 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 {
use super::*;

/// Test Clone impl of `ApInt`.
///
/// Invariants between the origin `ApInt` `o` and for the cloned `c` are:
/// Invariants between the origin `ApInt` `o` and for the cloned `c`
/// are:
///
/// - `o` and `c` have same bit widths
/// - If `o` is heap-allocated then `c` is, too and vice versa for stack.
/// - If `o` is heap-allocated then `c` is, too and vice versa for
/// stack.
/// - `o` and `c` have an equal amount of digits and the values of their
/// digits is equal and in the same order.
/// - Memory addresses of `c` and `o` won't overlap. (No aliasing!)
/// This is enforced by safe Rust.
/// - Memory addresses of `c` and `o` won't overlap. (No aliasing!) This
/// is enforced by safe Rust.
#[test]
fn clone() {
for apint in test_apints() {
Expand Down Expand Up @@ -741,10 +734,7 @@ mod tests {

#[test]
fn regression_issue15() {
use std::{
i128,
i64,
};
use std::{i128, i64};
{
let input = ApInt::from_i64(i64::MIN)
.into_sign_extend(BitWidth::new(128).unwrap())
Expand Down
Loading

0 comments on commit b4ed7ab

Please sign in to comment.