-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add bitfield helpers and use them in mstatus
- Loading branch information
Showing
3 changed files
with
46 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/// Insert a new value into a bitfield | ||
/// | ||
/// `value` is masked to `width` bits and inserted into `orig`.` | ||
#[inline] | ||
pub fn bf_insert(orig: usize, bit: usize, width: usize, value: usize) -> usize { | ||
let mask = (1 << width) - 1; | ||
orig & !(mask << bit) | ((value & mask) << bit) | ||
} | ||
|
||
/// Extract a value from a bitfield | ||
/// | ||
/// Extracts `width` bits from bit offset `bit` and returns it shifted to bit 0.s | ||
#[inline] | ||
pub fn bf_extract(orig: usize, bit: usize, width: usize) -> usize { | ||
let mask = (1 << width) - 1; | ||
(orig >> bit) & mask | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters