Begin implementing things a bit

main
John Zacarias Jekel 10 months ago
parent 06429de3ae
commit f8910b66ea
  1. 86
      lib/bititer.rs
  2. 212
      lib/bitmanip.rs
  3. 109
      lib/debug_panic.rs
  4. 68
      lib/lib.rs
  5. 114
      lib/primitive_integer.rs

@ -0,0 +1,86 @@
/*
* File: bititer.rs
* Brief: TODO
*
* Copyright: Copyright (C) TODO John Jekel
* See the LICENSE file at the root of the project for licensing info.
*
* TODO longer description
*
*/
/*!
* TODO rustdoc for this file here
*/
/* ------------------------------------------------------------------------------------------------
* Submodules
* --------------------------------------------------------------------------------------------- */
//TODO (includes "mod ..." and "pub mod ...")
/* ------------------------------------------------------------------------------------------------
* Uses
* --------------------------------------------------------------------------------------------- */
//TODO (includes "use ..." and "extern crate ...")
/* ------------------------------------------------------------------------------------------------
* Macros
* --------------------------------------------------------------------------------------------- */
//TODO (also pub(crate) use the_macro statements here too)
/* ------------------------------------------------------------------------------------------------
* Constants
* --------------------------------------------------------------------------------------------- */
//TODO
/* ------------------------------------------------------------------------------------------------
* Static Variables
* --------------------------------------------------------------------------------------------- */
//TODO
/* ------------------------------------------------------------------------------------------------
* Types
* --------------------------------------------------------------------------------------------- */
//TODO includes "type"-defs, structs, enums, unions, etc
/* ------------------------------------------------------------------------------------------------
* Associated Functions and Methods
* --------------------------------------------------------------------------------------------- */
//TODO
/* ------------------------------------------------------------------------------------------------
* Traits And Default Implementations
* --------------------------------------------------------------------------------------------- */
//TODO
/* ------------------------------------------------------------------------------------------------
* Trait Implementations
* --------------------------------------------------------------------------------------------- */
//TODO
/* ------------------------------------------------------------------------------------------------
* Functions
* --------------------------------------------------------------------------------------------- */
//TODO
/* ------------------------------------------------------------------------------------------------
* Tests
* --------------------------------------------------------------------------------------------- */
//TODO
/* ------------------------------------------------------------------------------------------------
* Benchmarks
* --------------------------------------------------------------------------------------------- */
//TODO

@ -0,0 +1,212 @@
/*
* File: bitmanip.rs
* Brief: TODO
*
* Copyright: Copyright (C) TODO John Jekel
* See the LICENSE file at the root of the project for licensing info.
*
* TODO longer description
*
*/
/*!
* TODO rustdoc for this file here
*/
/* ------------------------------------------------------------------------------------------------
* Submodules
* --------------------------------------------------------------------------------------------- */
//TODO (includes "mod ..." and "pub mod ...")
/* ------------------------------------------------------------------------------------------------
* Uses
* --------------------------------------------------------------------------------------------- */
use crate::primitive_integer::PrimitiveInteger;
use crate::debug_panic::debug_panic_if;
use core::ops::RangeBounds;
use core::ops::Bound;
/* ------------------------------------------------------------------------------------------------
* Macros
* --------------------------------------------------------------------------------------------- */
macro_rules! implement_for {
($integer:ty) => {
impl BitManip for $integer {
fn bit(&self, bit_index: Self::BitIndex) -> Self {
debug_panic_if!(bit_index >= Self::BITS, "Attempt to access bit at out of range index {} >= {}", bit_index, Self::BITS);
(self >> bit_index) & 0b1
}
fn bit_bool(&self, bit_index: Self::BitIndex) -> bool {
self.bit(bit_index) == 0b1
}
fn bits(&self, range: impl RangeBounds<Self::BitIndex>) -> Self {
todo!()
}
fn set_bit(&self, bit_index: Self::BitIndex) -> Self {
todo!()
}
fn set_bits(&self, range: impl RangeBounds<Self::BitIndex>) -> Self {
todo!()
}
fn clear_bit(&self, bit_index: Self::BitIndex) -> Self {
todo!()
}
fn clear_bits(&self, range: impl RangeBounds<Self::BitIndex>) -> Self {
todo!()
}
fn toggle_bit(&self, bit_index: Self::BitIndex) -> Self {
todo!()
}
fn toggle_bits(&self, range: impl RangeBounds<Self::BitIndex>) -> Self {
todo!()
}
fn replace_bit(&self, bit_index: Self::BitIndex, bit: impl Into<bool>) -> Self {
todo!()
}
fn replace_bits(&self, range: impl RangeBounds<Self::BitIndex>, bits: Self) -> Self {
todo!()
}
fn bitmask(range: impl RangeBounds<Self::BitIndex>) -> Self {
let leftmost_inclusive = match range.start_bound() {
Bound::Included(&index) => index,
Bound::Excluded(&index) => {
debug_panic_if!(index == 0, "Leftmost bit must be greater than or equal to 0");
index - 1
},
Bound::Unbounded => Self::BITS - 1,
};
let rightmost_inclusive = match range.end_bound() {
Bound::Included(&index) => index,
Bound::Excluded(&index) => {
debug_panic_if!(index == (Self::BITS - 1), "Rightmost bit must be less than or equal to {}", Self::BITS - 1);
index + 1
},
Bound::Unbounded => 0,
};
//TODO support reverse ranges?
debug_panic_if!(leftmost_inclusive < rightmost_inclusive, "Leftmost bit must be greater than or equal to rightmost bit");
((1 << (leftmost_inclusive - rightmost_inclusive + 1)) - 1) << rightmost_inclusive
}
}
}
}
/* ------------------------------------------------------------------------------------------------
* Constants
* --------------------------------------------------------------------------------------------- */
//TODO
/* ------------------------------------------------------------------------------------------------
* Static Variables
* --------------------------------------------------------------------------------------------- */
//TODO
/* ------------------------------------------------------------------------------------------------
* Types
* --------------------------------------------------------------------------------------------- */
//TODO includes "type"-defs, structs, enums, unions, etc
/* ------------------------------------------------------------------------------------------------
* Associated Functions and Methods
* --------------------------------------------------------------------------------------------- */
//TODO
/* ------------------------------------------------------------------------------------------------
* Traits And Default Implementations
* --------------------------------------------------------------------------------------------- */
pub trait BitManip: PrimitiveInteger {
//TODO make all of these const too since we only intend for this to be implemented for integers
//TODO note in the docs we optimize the nice usage of the functions for not using bool because it's useful when chaining multiple functions together
fn bit(&self, bit_index: Self::BitIndex) -> Self;
fn bit_bool(&self, bit_index: Self::BitIndex) -> bool;
fn bits(&self, range: impl RangeBounds<Self::BitIndex>) -> Self;
fn set_bit(&self, bit_index: Self::BitIndex) -> Self;
fn set_bits(&self, range: impl RangeBounds<Self::BitIndex>) -> Self;
fn set_bit_assign(&mut self, bit_index: Self::BitIndex) {
*self = self.set_bit(bit_index);
}
fn set_bits_assign(&mut self, range: impl RangeBounds<Self::BitIndex>) {
*self = self.set_bits(range);
}
fn clear_bit(&self, bit_index: Self::BitIndex) -> Self;
fn clear_bits(&self, range: impl RangeBounds<Self::BitIndex>) -> Self;
fn clear_bit_assign(&mut self, bit_index: Self::BitIndex) {
*self = self.clear_bit(bit_index);
}
fn clear_bits_assign(&mut self, range: impl RangeBounds<Self::BitIndex>) {
*self = self.clear_bits(range);
}
fn toggle_bit(&self, bit_index: Self::BitIndex) -> Self;
fn toggle_bits(&self, range: impl RangeBounds<Self::BitIndex>) -> Self;
fn toggle_bit_assign(&mut self, bit_index: Self::BitIndex) {
*self = self.toggle_bit(bit_index);
}
fn toggle_bits_assign(&mut self, range: impl RangeBounds<Self::BitIndex>) {
*self = self.toggle_bits(range);
}
fn replace_bit(&self, bit_index: Self::BitIndex, value: impl Into<bool>) -> Self;
fn replace_bits(&self, range: impl RangeBounds<Self::BitIndex>, value: Self) -> Self;
fn replace_bit_assign(&mut self, bit_index: Self::BitIndex, value: impl Into<bool>) {
*self = self.replace_bit(bit_index, value);
}
fn replace_bits_assign(&mut self, range: impl RangeBounds<Self::BitIndex>, value: Self) {
*self = self.replace_bits(range, value);
}
fn bitmask(range: impl RangeBounds<Self::BitIndex>) -> Self;
//TODO others
}
/* ------------------------------------------------------------------------------------------------
* Trait Implementations
* --------------------------------------------------------------------------------------------- */
implement_for!(u8);
/* ------------------------------------------------------------------------------------------------
* Functions
* --------------------------------------------------------------------------------------------- */
//TODO
/* ------------------------------------------------------------------------------------------------
* Tests
* --------------------------------------------------------------------------------------------- */
//TODO
/* ------------------------------------------------------------------------------------------------
* Benchmarks
* --------------------------------------------------------------------------------------------- */
//TODO

@ -0,0 +1,109 @@
/*
* File: TODO.rs
* Brief: TODO
*
* Copyright: Copyright (C) TODO John Jekel
* See the LICENSE file at the root of the project for licensing info.
*
* TODO longer description
*
*/
/*!
* TODO rustdoc for this file here
*/
/* ------------------------------------------------------------------------------------------------
* Submodules
* --------------------------------------------------------------------------------------------- */
//TODO (includes "mod ..." and "pub mod ...")
/* ------------------------------------------------------------------------------------------------
* Uses
* --------------------------------------------------------------------------------------------- */
//TODO (includes "use ..." and "extern crate ...")
/* ------------------------------------------------------------------------------------------------
* Macros
* --------------------------------------------------------------------------------------------- */
/*macro_rules! debug_panic {
//TODO
}
*/
macro_rules! debug_panic_if {//Different than debug_assert since it won't print an error like an
//assertion
($cond:expr $(,)?) => {
#[cfg(debug_assertions)]
if $cond {
//debug_panic!();//TODO
panic!();
}
};
($cond:expr, $($arg:tt)+) => {
#[cfg(debug_assertions)]
if $cond {
//debug_panic!($($arg)+);//TODO
panic!($($arg)+);
}
};
}
pub(crate) use debug_panic_if;
/* ------------------------------------------------------------------------------------------------
* Constants
* --------------------------------------------------------------------------------------------- */
//TODO
/* ------------------------------------------------------------------------------------------------
* Static Variables
* --------------------------------------------------------------------------------------------- */
//TODO
/* ------------------------------------------------------------------------------------------------
* Types
* --------------------------------------------------------------------------------------------- */
//TODO includes "type"-defs, structs, enums, unions, etc
/* ------------------------------------------------------------------------------------------------
* Associated Functions and Methods
* --------------------------------------------------------------------------------------------- */
//TODO
/* ------------------------------------------------------------------------------------------------
* Traits And Default Implementations
* --------------------------------------------------------------------------------------------- */
//TODO
/* ------------------------------------------------------------------------------------------------
* Trait Implementations
* --------------------------------------------------------------------------------------------- */
//TODO
/* ------------------------------------------------------------------------------------------------
* Functions
* --------------------------------------------------------------------------------------------- */
//TODO
/* ------------------------------------------------------------------------------------------------
* Tests
* --------------------------------------------------------------------------------------------- */
//TODO
/* ------------------------------------------------------------------------------------------------
* Benchmarks
* --------------------------------------------------------------------------------------------- */
//TODO

@ -1,5 +1,5 @@
/*
* File: lib.rs
* File: bitmanip.rs
* Brief: TODO
*
* Copyright: Copyright (C) 2023 John Jekel
@ -13,17 +13,23 @@
* TODO rustdoc for this file here
*/
#![no_std]
/* ------------------------------------------------------------------------------------------------
* Submodules
* --------------------------------------------------------------------------------------------- */
//TODO
mod bitmanip;
//mod bititer;
mod debug_panic;
mod primitive_integer;
/* ------------------------------------------------------------------------------------------------
* Uses
* --------------------------------------------------------------------------------------------- */
use std::ops::Range;
pub use bitmanip::BitManip;
//pub use bititer::BitIterator;
/* ------------------------------------------------------------------------------------------------
* Macros
@ -59,61 +65,7 @@ use std::ops::Range;
* Traits And Default Implementations
* --------------------------------------------------------------------------------------------- */
trait BitManip: Sized {
type BitIndex;
//TODO make all of these const too since we only intend for this to be implemented for integers
fn bit(&self, bit_index: Self::BitIndex) -> Self;
fn bits(&self, range: Range<Self::BitIndex>) -> Self;
fn set_bit(&self, bit_index: Self::BitIndex) -> Self;
fn set_bits(&self, range: Range<Self::BitIndex>) -> Self;
fn set_bit_assign(&mut self, bit_index: Self::BitIndex) {
*self = self.set_bit(bit_index);
}
fn set_bits_assign(&mut self, range: Range<Self::BitIndex>) {
*self = self.set_bits(range);
}
fn clear_bit(&self, bit_index: Self::BitIndex) -> Self;
fn clear_bits(&self, range: Range<Self::BitIndex>) -> Self;
fn clear_bit_assign(&mut self, bit_index: Self::BitIndex) {
*self = self.clear_bit(bit_index);
}
fn clear_bits_assign(&mut self, range: Range<Self::BitIndex>) {
*self = self.clear_bits(range);
}
fn toggle_bit(&self, bit_index: Self::BitIndex) -> Self;
fn toggle_bits(&self, range: Range<Self::BitIndex>) -> Self;
fn toggle_bit_assign(&mut self, bit_index: Self::BitIndex) {
*self = self.toggle_bit(bit_index);
}
fn toggle_bits_assign(&mut self, range: Range<Self::BitIndex>) {
*self = self.toggle_bits(range);
}
fn replace_bit(&self, bit_index: Self::BitIndex, value: impl Into<bool>) -> Self;
fn replace_bits(&self, range: Range<Self::BitIndex>, value: Self) -> Self;
fn replace_bit_assign(&mut self, bit_index: Self::BitIndex, value: impl Into<bool>) {
*self = self.replace_bit(bit_index, value);
}
fn replace_bits_assign(&mut self, range: Range<Self::BitIndex>, value: Self) {
*self = self.replace_bits(range, value);
}
fn bitmask(range: Range<Self::BitIndex>) -> Self;
//TODO others
}
/*trait BitManipBool: BitManip {
/*const*/ fn bit(&self, bit: Self) -> bool {
BitManip::bit(self, bit) == 1
}
//TODO others
}*/
//TODO
/* ------------------------------------------------------------------------------------------------
* Trait Implementations

@ -0,0 +1,114 @@
/*
* File: TODO.rs
* Brief: TODO
*
* Copyright: Copyright (C) TODO John Jekel
* See the LICENSE file at the root of the project for licensing info.
*
* TODO longer description
*
*/
/*!
* TODO rustdoc for this file here
*/
/* ------------------------------------------------------------------------------------------------
* Submodules
* --------------------------------------------------------------------------------------------- */
//TODO (includes "mod ..." and "pub mod ...")
/* ------------------------------------------------------------------------------------------------
* Uses
* --------------------------------------------------------------------------------------------- */
use core::ops::*;
/* ------------------------------------------------------------------------------------------------
* Macros
* --------------------------------------------------------------------------------------------- */
macro_rules! implement_for {
($integer:ty) => {
impl PrimitiveInteger for $integer {
type BitIndex = u32;//$integer;//To be consistent with BITS
const BITS: u32 = <$integer>::BITS;
}
}
}
/* ------------------------------------------------------------------------------------------------
* Constants
* --------------------------------------------------------------------------------------------- */
//TODO
/* ------------------------------------------------------------------------------------------------
* Static Variables
* --------------------------------------------------------------------------------------------- */
//TODO
/* ------------------------------------------------------------------------------------------------
* Types
* --------------------------------------------------------------------------------------------- */
//TODO includes "type"-defs, structs, enums, unions, etc
/* ------------------------------------------------------------------------------------------------
* Associated Functions and Methods
* --------------------------------------------------------------------------------------------- */
//TODO
/* ------------------------------------------------------------------------------------------------
* Traits And Default Implementations
* --------------------------------------------------------------------------------------------- */
//NOTE: Only includes the features BitManip needs
pub trait PrimitiveInteger:
Sized + Add<Self> + Sub<Self> + Shl<Self> + Shr<Self> + BitAnd<Self> + BitOr<Self> +
BitXor<Self> + Not + Copy + Clone + Ord + Eq
{
type BitIndex: PrimitiveInteger/* = Self*/;//Just to allow for flexibility in the future (for a nicer api, this is always u32 to be consistent with BITS)
const BITS: u32;
}
/* ------------------------------------------------------------------------------------------------
* Trait Implementations
* --------------------------------------------------------------------------------------------- */
implement_for!(u8);
implement_for!(u16);
implement_for!(u32);
implement_for!(u64);
implement_for!(u128);
implement_for!(usize);
implement_for!(i8);
implement_for!(i16);
implement_for!(i32);
implement_for!(i64);
implement_for!(i128);
implement_for!(isize);
/* ------------------------------------------------------------------------------------------------
* Functions
* --------------------------------------------------------------------------------------------- */
//TODO
/* ------------------------------------------------------------------------------------------------
* Tests
* --------------------------------------------------------------------------------------------- */
//TODO
/* ------------------------------------------------------------------------------------------------
* Benchmarks
* --------------------------------------------------------------------------------------------- */
//TODO
Loading…
Cancel
Save