Support bit operations

This commit is contained in:
jaby 2022-09-20 22:10:17 +02:00
parent e79df423b0
commit 090d0d09a1
3 changed files with 33 additions and 0 deletions

View File

@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
paste = "*"

View File

@ -0,0 +1,30 @@
pub struct BitRange {
pub start: usize,
pub len: usize,
}
impl BitRange {
pub const fn from_to(start: usize, end: usize) -> BitRange {
BitRange{start, len: (end - start + 1)}
}
}
macro_rules! create_bit_functions {
($type_val:ty) => {
paste::item! {
fn [< get_mask_ $type_val >](range: &BitRange) -> $type_val {
(1 << range.len) - 1
}
pub fn [< clear_value_ $type_val >](dst: $type_val, range: &BitRange) -> $type_val {
dst & !([< get_mask_ $type_val >](range) << (range.start as $type_val))
}
pub fn [< set_value_ $type_val >](dst: $type_val, value: $type_val, range: &BitRange) -> $type_val {
[< clear_value_ $type_val >](dst, range) | ((value & [< get_mask_ $type_val >](range)) << range.start)
}
}
};
}
create_bit_functions!(u16);

View File

@ -1,5 +1,7 @@
use std::{boxed::Box, io::{Read, Write}, path::PathBuf};
pub mod bits;
pub type Output = Box<dyn Write>;
pub type Input = Box<dyn Read>;