Skip to main content

BitMask

Struct BitMask 

Source
#[repr(transparent)]
pub struct BitMask<const N: usize>(pub u64);
Expand description

Bit-packed predicate mask for exactly N SIMD lanes.

The inner u64 stores one bit per lane: bit i = lane i is active. Invariant: (self.0 >> N) == 0 (high bits always clear).

N must satisfy N <= 64. Violations are caught by a const assertion in ALL_ACTIVE.

Tuple Fields§

§0: u64

Implementations§

Source§

impl<const N: usize> BitMask<N>

Source

pub const ALL_ACTIVE: BitMask<N>

All N lanes active.

§Panics (compile-time)

Panics at compile time if N > 64.

Source

pub const NONE_ACTIVE: BitMask<N>

No lanes active.

Source

pub const fn leading_k(k: usize) -> BitMask<N>

First k lanes active, rest inactive. Clamps k to N.

This is a const fn so it can be used in const contexts.

§Examples
use hermes_simd_core::mask::BitMask;
assert_eq!(BitMask::<8>::leading_k(5).0, 0b00011111);
assert_eq!(BitMask::<8>::leading_k(0).0, 0);
assert_eq!(BitMask::<8>::leading_k(8).0, 0xFF);
Source

pub fn from_bools(bits: &[bool]) -> BitMask<N>

Build a mask from a bool slice of length N.

Bit i is set if bits[i] is true.

§Panics

Panics in debug mode if bits.len() != N.

Source

pub fn popcount(self) -> u32

Number of active (set) lanes.

Source

pub fn is_all_active(self) -> bool

Returns true if all N lanes are active.

Source

pub fn is_none_active(self) -> bool

Returns true if no lanes are active.

Source

pub fn is_lane_active(self, i: usize) -> bool

Returns true if lane i is active.

§Panics

Panics in debug mode if i >= N.

Source

pub fn and(self, other: BitMask<N>) -> BitMask<N>

Bitwise AND of two masks.

Source

pub fn or(self, other: BitMask<N>) -> BitMask<N>

Bitwise OR of two masks.

Source

pub fn to_bools(self) -> [bool; N]
where [bool; N]: Sized,

Expand this mask to a [bool; N] array.

Useful for scalar fallbacks and debugging. Not performance-critical.

Source§

impl<const N: usize> BitMask<N>

Source

pub unsafe fn to_native_mask<T, Arch>(self) -> <Arch as SimdKernel<T>>::Mask
where T: NumericElement, Arch: SimdKernel<T>,

Convert this BitMask<N> to the native hardware mask type for Arch.

Delegates to SimdKernel::mask_from_bitmask using the inner u64 value. Zero runtime cost: the compiler inlines this into a single instruction on AVX-512 (KMOV), a vector comparison + blend mask on AVX2, or a bool-array copy on scalar backends.

§Safety

Processor must support the target feature of Arch.

§Example
use hermes_simd_core::mask::BitMask;
use hermes_simd_core::kernel::SimdKernel;
use hermes_simd_intrinsics::Scalar;

let bm = BitMask::<4>::leading_k(3);
// SAFETY: `Scalar` has no target-feature precondition.
let native: <Scalar as SimdKernel<f32>>::Mask =
    unsafe { bm.to_native_mask::<f32, Scalar>() };

assert_eq!(native, [true, true, true, false]);
Source§

impl<const N: usize> BitMask<N>

Source

pub fn active_lanes(self) -> BitMaskIter<N>

Convenience method to iterate active lane indices without consuming.

Equivalent to (*self).into_iter() since BitMask<N>: Copy.

Trait Implementations§

Source§

impl<const N: usize> Archive for BitMask<N>
where u64: Archive,

Source§

type Archived = ArchivedBitMask<N>

The archived representation of this type. Read more
Source§

type Resolver = BitMaskResolver<N>

The resolver for this type. It must contain all the additional information from serializing needed to make the archived type from the normal type.
Source§

fn resolve( &self, resolver: <BitMask<N> as Archive>::Resolver, out: Place<<BitMask<N> as Archive>::Archived>, )

Creates the archived version of this value at the given position and writes it to the given output. Read more
Source§

const COPY_OPTIMIZATION: CopyOptimization<Self> = _

An optimization flag that allows the bytes of this type to be copied directly to a writer instead of calling serialize. Read more
Source§

impl<const N: usize> BitAnd for BitMask<N>

Source§

type Output = BitMask<N>

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: BitMask<N>) -> BitMask<N>

Performs the & operation. Read more
Source§

impl<const N: usize> BitOr for BitMask<N>

Source§

type Output = BitMask<N>

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: BitMask<N>) -> BitMask<N>

Performs the | operation. Read more
Source§

impl<const N: usize> Clone for BitMask<N>

Source§

fn clone(&self) -> BitMask<N>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const N: usize> Copy for BitMask<N>

Source§

impl<const N: usize> Debug for BitMask<N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<const N: usize> Default for BitMask<N>

Source§

fn default() -> BitMask<N>

Returns the “default value” for a type. Read more
Source§

impl<__D, const N: usize> Deserialize<BitMask<N>, __D> for <BitMask<N> as Archive>::Archived
where __D: Fallible + ?Sized, u64: Archive, <u64 as Archive>::Archived: Deserialize<u64, __D>,

Source§

fn deserialize( &self, deserializer: &mut __D, ) -> Result<BitMask<N>, <__D as Fallible>::Error>

Deserializes using the given deserializer
Source§

impl<const N: usize> Eq for BitMask<N>

Source§

impl<const N: usize> Hash for BitMask<N>

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<const N: usize> IntoIterator for BitMask<N>

Source§

fn into_iter(self) -> BitMaskIter<N>

Iterate over active lane indices in ascending order.

§Example
use hermes_simd_core::mask::BitMask;

let mask = BitMask::<8>::from_bools(&[true, false, true, false, true, false, false, false]);
let indices: Vec<usize> = mask.into_iter().collect();

assert_eq!(indices, vec![0, 2, 4]);
Source§

type Item = usize

The type of the elements being iterated over.
Source§

type IntoIter = BitMaskIter<N>

Which kind of iterator are we turning this into?
Source§

impl<const N: usize> Not for BitMask<N>

Source§

type Output = BitMask<N>

The resulting type after applying the ! operator.
Source§

fn not(self) -> BitMask<N>

Performs the unary ! operation. Read more
Source§

impl<const N: usize> PartialEq for BitMask<N>

Source§

fn eq(&self, other: &BitMask<N>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<__S, const N: usize> Serialize<__S> for BitMask<N>
where __S: Fallible + ?Sized, u64: Serialize<__S>,

Source§

fn serialize( &self, serializer: &mut __S, ) -> Result<<BitMask<N> as Archive>::Resolver, <__S as Fallible>::Error>

Writes the dependencies for the object and returns a resolver that can create the archived type.
Source§

impl<const N: usize> StructuralPartialEq for BitMask<N>

Auto Trait Implementations§

§

impl<const N: usize> Freeze for BitMask<N>

§

impl<const N: usize> RefUnwindSafe for BitMask<N>

§

impl<const N: usize> Send for BitMask<N>

§

impl<const N: usize> Sync for BitMask<N>

§

impl<const N: usize> Unpin for BitMask<N>

§

impl<const N: usize> UnsafeUnpin for BitMask<N>

§

impl<const N: usize> UnwindSafe for BitMask<N>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> ArchivePointee for T

Source§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
Source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
Source§

impl<T> ArchiveUnsized for T
where T: Archive,

Source§

type Archived = <T as Archive>::Archived

The archived counterpart of this type. Unlike Archive, it may be unsized. Read more
Source§

fn archived_metadata( &self, ) -> <<T as ArchiveUnsized>::Archived as ArchivePointee>::ArchivedMetadata

Creates the archived version of the metadata for this value.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CastTo for T
where T: Copy,

Source§

fn cast_to<U>(self) -> U
where U: CastFrom<Self>,

Cast self to type U.
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> LayoutRaw for T

Source§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Returns the layout of the type.
Source§

impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
where T: SharedNiching<N1, N2>, N1: Niching<T>, N2: Niching<T>,

Source§

unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool

Returns whether the given value has been niched. Read more
Source§

fn resolve_niched(out: Place<NichedOption<T, N1>>)

Writes data to out indicating that a T is niched.
Source§

impl<T> Pointee for T

Source§

type Metadata = ()

The metadata type for pointers and references to this type.
Source§

impl<T, S> SerializeUnsized<S> for T
where T: Serialize<S>, S: Fallible + Writer + ?Sized,

Source§

fn serialize_unsized( &self, serializer: &mut S, ) -> Result<usize, <S as Fallible>::Error>

Writes the object and returns the position of the archived type.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.