forked from MightyPork/crsn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.4 KiB
65 lines
1.4 KiB
//! Mask applied to a data source or destination
|
|
|
|
use crate::error::AsmError;
|
|
|
|
/// Bit mask to apply to a value
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
|
|
pub struct Mask {
|
|
/// Length of the selected bit slice
|
|
len: u8,
|
|
/// Offset of the selected bit slice from bit zero
|
|
offset: u8,
|
|
}
|
|
|
|
impl Default for Mask {
|
|
fn default() -> Self {
|
|
Mask {
|
|
len: 64,
|
|
offset: 0,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Mask {
|
|
pub const BYTE: Mask = Mask {
|
|
len: 8,
|
|
offset: 0,
|
|
};
|
|
pub const HALF_WORD: Mask = Mask {
|
|
len: 16,
|
|
offset: 0,
|
|
};
|
|
pub const WORD: Mask = Mask {
|
|
len: 32,
|
|
offset: 0,
|
|
};
|
|
pub const DOUBLE_WORD: Mask = Mask {
|
|
len: 64,
|
|
offset: 0,
|
|
};
|
|
pub const FULL: Mask = Self::DOUBLE_WORD;
|
|
|
|
pub fn new(len: u8, offset: u8) -> Result<Self, AsmError> {
|
|
if len == 0 || offset >= 64 {
|
|
// create the invalid mask to display it in the error
|
|
return Err(AsmError::BadMask(Mask {
|
|
len,
|
|
offset,
|
|
}));
|
|
}
|
|
|
|
Ok(Self {
|
|
len: len.min(64 - offset),
|
|
offset,
|
|
})
|
|
}
|
|
|
|
/// Get a binary mask representing the span
|
|
pub fn as_bitmask(self) -> u64 {
|
|
((1 << self.len) - 1) << self.offset
|
|
}
|
|
|
|
pub fn is_default(self) -> bool {
|
|
self == Self::default()
|
|
}
|
|
}
|
|
|