1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use errors::{Result};
use digit;

/// Represents a bit position within an `ApInt`.
/// 
/// This utility might become useful later, for example
/// when we reduce the range of valid bit widths for some
/// optimization oportunities.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct BitPos(usize);

/// A `DigitPos` represents the integer offset at which to find
/// a `Digit` within an `ApInt` instance.
pub type DigitPos = usize;

impl BitPos {
	/// Returns the `usize` representation of this `BitPos`.
	#[inline]
	pub fn to_usize(self) -> usize {
		self.0
	}

	/// Returns a `BitPos` representing the given bit position.
	/// 
	/// # Errors
	/// 
	/// - This operation cannot fail but may do so in future version of this library.
	#[inline]
	pub fn new(pos: usize) -> Result<BitPos> {
		Ok(BitPos(pos))
	}

	/// Converts this `BitPos` into its associated `BitPos` that is usable to operate
	/// on `Digit` instances.
	#[inline]
	pub(crate) fn to_pos_within_digit(self) -> BitPos {
		BitPos(self.0 % digit::BITS)
	}

	/// Splits this `BitPos` that may range over several `Digit`s within an `ApInt`
	/// into the associated `Digit` offset and its `Digit`-relative bit position.
	#[inline]
	pub(crate) fn to_digit_and_bit_pos(self) -> (DigitPos, BitPos) {
		let digit_pos = DigitPos::from(self.0 / digit::BITS);
		let bit_pos = BitPos::from(self.0 % digit::BITS);
		(digit_pos, bit_pos)
	}
}

impl From<usize> for BitPos {
	#[inline]
	fn from(pos: usize) -> BitPos {
		BitPos(pos)
	}
}