sbi_spec/binary/mask_commons.rs
1//! Common SBI mask operations and structures.
2
3/// Check if the implementation can contains the provided `bit`.
4#[inline]
5pub(crate) const fn valid_bit(base: usize, bit: usize) -> bool {
6 if bit < base {
7 // invalid index, under minimum range.
8 false
9 } else if (bit - base) >= usize::BITS as usize {
10 // invalid index, over max range.
11 false
12 } else {
13 true
14 }
15}
16
17/// Check if the implementation contains the provided `bit`.
18///
19/// ## Parameters
20///
21/// - `mask`: bitmask defining the range of bits.
22/// - `base`: the starting bit index. (default: `0`)
23/// - `ignore`: if `base` is equal to this value, ignore the `mask` parameter, and consider all `bit`s set.
24/// - `bit`: the bit index to check for membership in the `mask`.
25#[inline]
26pub(crate) const fn has_bit(mask: usize, base: usize, ignore: usize, bit: usize) -> bool {
27 if base == ignore {
28 // ignore the `mask`, consider all `bit`s as set.
29 true
30 } else if !valid_bit(base, bit) {
31 false
32 } else {
33 // index is in range, check if it is set in the mask.
34 mask & (1 << (bit - base)) != 0
35 }
36}
37
38/// Error of mask modification.
39#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
40pub enum MaskError {
41 /// This mask has been ignored.
42 Ignored,
43 /// Request bit is invalid.
44 InvalidBit,
45}