bit_arch 0.1.1

High-level zero-cost bit manipulation
Documentation
mod traits;

use traits::BitIntrinsics;

/// Extracts bits of `x` masked by `mask` into lower contiguous bits of output.
#[must_use]
#[inline(always)]
pub fn extract<T: BitIntrinsics>(x: T, mask: T) -> T {
    T::extract(x, mask)
}

/// Scatters (deposits) lower bits of `x` into all set bits of `mask`.
///
/// This is the inverse of `extract`.
#[must_use]
#[inline(always)]
pub fn deposit<T: BitIntrinsics>(x: T, mask: T) -> T {
    T::deposit(x, mask)
}

/// Zeros all bits of `x` above the specified index.
#[must_use]
#[inline(always)]
pub fn zero_upper<T: BitIntrinsics>(x: T, index: u32) -> T {
    T::zero_upper(x, index)
}

/// Counts trailing zero bits of `x`.
#[must_use]
#[inline(always)]
pub fn trailing_zeros<T: BitIntrinsics>(x: T) -> T {
    T::trailing_zeros(x)
}

/// Mask from trailing zeros of x. Replaces `!x & x.wrapping_sub(1)`.
#[must_use]
#[inline(always)]
pub fn trailing_zero_mask<T: BitIntrinsics>(x: T) -> T {
    T::trailing_zero_mask(x)
}

/// Inverse mask from trailing ones of `x`. Replaces `!x | x.wrapping_add(1)`.
#[must_use]
#[inline(always)]
pub fn trailing_one_mask<T: BitIntrinsics>(x: T) -> T {
    T::trailing_one_mask(x)
}

/// Extracts and clears the lowest set bit from `x`. Replaces `x & (x - 1)`.
#[must_use]
#[inline(always)]
pub fn clear_lowest_set<T: BitIntrinsics>(x: T) -> T {
    T::clear_lowest_set(x)
}

/// Clears the least significant set bit and sets the rest. Replaces `!x | x.wrapping_sub(1)`.
#[must_use]
#[inline(always)]
pub fn complement_lowest_set<T: BitIntrinsics>(x: T) -> T {
    T::complement_lowest_set(x)
}

/// Extracts a bitfield at index `start` of size `len` of `x``.
#[must_use]
#[inline(always)]
pub fn extract_bitfield<T: BitIntrinsics>(x: T, start: u32, len: u32) -> T {
    T::extract_bitfield(x, start, len)
}

/// Sets all bits below the least significant one. Replaces `x | x.wrapping_sub(1)`.
#[must_use]
#[inline(always)]
pub fn from_lowest_set<T: BitIntrinsics>(x: T) -> T {
    T::from_lowest_set(x)
}

/// Performs a bitwise logical `!a & b`.
#[must_use]
#[inline(always)]
pub fn inverted_and<T: BitIntrinsics>(a: T, b: T) -> T {
    T::inverted_and(a, b)
}

/// Extracts and isolates the lowest clear bit from `x``. Replaces `x | !x.wrapping_add(1)`.
#[must_use]
#[inline(always)]
pub fn isolate_lowest_clear<T: BitIntrinsics>(x: T) -> T {
    T::isolate_lowest_clear(x)
}

/// Counts leading zero bits of `x`.
#[must_use]
#[inline(always)]
pub fn leading_zeros<T: BitIntrinsics>(x: T) -> T {
    T::leading_zeros(x)
}

/// Sets the least significant zero bit and clears the rest. Replaces `!x & x.wrapping_add(1)`.
#[must_use]
#[inline(always)]
pub fn set_lowest_clear<T: BitIntrinsics>(x: T) -> T {
    T::set_lowest_clear(x)
}

/// Sets the least significant zero bit from `x`. Replaces `x | x.wrapping_add(1)`.
#[must_use]
#[inline(always)]
pub fn set_lowest_zero<T: BitIntrinsics>(x: T) -> T {
    T::set_lowest_zero(x)
}

/// Mask up to the lowest set bit from `x`. Replaces `x ^ (x - 1)`.
#[must_use]
#[inline(always)]
pub fn through_lowest_set<T: BitIntrinsics>(x: T) -> T {
    T::through_lowest_set(x)
}

/// Counts all one bits of `x`.
#[must_use]
#[inline(always)]
pub fn count_ones<T: BitIntrinsics>(x: T) -> T::Half {
    T::count_ones(x)
}