Skip to content

Commit

Permalink
add i2powm1
Browse files Browse the repository at this point in the history
  • Loading branch information
ngtkana committed Nov 29, 2024
1 parent 57adb45 commit 848dbd4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
10 changes: 5 additions & 5 deletions libs/lg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ macro_rules! __lg_internal {
#[macro_export]
macro_rules! table {
($vec2:expr) => {
eprintln!(
eprint!(
"{}",
$crate::vec2($crate::remove_ampersand(stringify!($vec2)), $vec2)
);
Expand All @@ -74,7 +74,7 @@ macro_rules! table {
#[macro_export]
macro_rules! vmap {
($map:expr) => {
eprintln!(
eprint!(
"{}",
$crate::vmap($crate::remove_ampersand(stringify!($map)), $map)
);
Expand All @@ -84,7 +84,7 @@ macro_rules! vmap {
#[macro_export]
macro_rules! hmap {
($map:expr) => {
eprintln!(
eprint!(
"{}",
$crate::hmap($crate::remove_ampersand(stringify!($map)), $map)
);
Expand Down Expand Up @@ -115,7 +115,7 @@ macro_rules! vvec {
vecs.push((name.to_owned(), values))
}
)+
eprintln!("{}", $crate::vvec(&vecs));
eprint!("{}", $crate::vvec(&vecs));
};
}

Expand Down Expand Up @@ -143,7 +143,7 @@ macro_rules! hvec {
vecs.push((name.to_owned(), values))
}
)+
eprintln!("{}", $crate::hvec(&vecs));
eprint!("{}", $crate::hvec(&vecs));
};
}

Expand Down
15 changes: 15 additions & 0 deletions libs/riff/src/bitmask_operations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use crate::Unsigned;

/// Returns $2^n - 1$.
///
/// # Examples
/// ```
/// use crate::i2powm1;
/// assert_eq!(i2powm1::<u32>(0), 0);
/// assert_eq!(i2powm1::<u32>(1), 1);
/// assert_eq!(i2powm1::<u32>(2), 3);
/// assert_eq!(i2powm1::<u32>(3), 7);
/// ```
pub fn i2powm1<T: Unsigned>(n: u32) -> T {
T::MAX >> (T::BITS - n)
}
2 changes: 2 additions & 0 deletions libs/riff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
mod binary_search;
mod bitmask_iterators;
mod bitmask_operations;
mod change_min_max;
mod numeric_traits;
mod pop_if;

pub use binary_search::BinarySearch;
pub use bitmask_iterators::bitmask_combinations;
pub use bitmask_iterators::bitmask_subsets;
pub use bitmask_operations::i2powm1;
pub use change_min_max::ChangeMinMax;
pub use numeric_traits::Unsigned;
pub use pop_if::PopIf;
4 changes: 4 additions & 0 deletions libs/riff/src/numeric_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub trait Unsigned:
+ ShrAssign<u32>
+ Not<Output = Self>
{
const BITS: u32;
const MAX: Self;
fn zero() -> Self;
fn one() -> Self;
fn wrapping_neg(self) -> Self;
Expand All @@ -59,6 +61,8 @@ pub trait Unsigned:
macro_rules! impl_unsigned {
($($T:ty),* $(,)?) => {$(
impl Unsigned for $T {
const BITS: u32 = <$T>::BITS;
const MAX: Self = <$T>::MAX;
fn zero() -> Self { 0 }
fn one() -> Self { 1 }
fn wrapping_neg(self) -> Self { self.wrapping_neg() }
Expand Down

0 comments on commit 848dbd4

Please sign in to comment.