]> git.lizzy.rs Git - rust.git/blob - crates/core_simd/src/masks/to_bitmask.rs
Manually implement for supported lanes
[rust.git] / crates / core_simd / src / masks / to_bitmask.rs
1 use super::{mask_impl, Mask, MaskElement};
2
3 /// Converts masks to and from integer bitmasks.
4 ///
5 /// Each bit of the bitmask corresponds to a mask lane, starting with the LSB.
6 pub trait ToBitMask {
7     /// The integer bitmask type.
8     type BitMask;
9
10     /// Converts a mask to a bitmask.
11     fn to_bitmask(self) -> Self::BitMask;
12
13     /// Converts a bitmask to a mask.
14     fn from_bitmask(bitmask: Self::BitMask) -> Self;
15 }
16
17 /// Converts masks to and from byte array bitmasks.
18 ///
19 /// Each bit of the bitmask corresponds to a mask lane, starting with the LSB of the first byte.
20 pub trait ToBitMaskArray {
21     /// The length of the bitmask array.
22     const BYTES: usize;
23
24     /// Converts a mask to a bitmask.
25     fn to_bitmask_array(self) -> [u8; Self::BYTES];
26
27     /// Converts a bitmask to a mask.
28     fn from_bitmask_array(bitmask: [u8; Self::BYTES]) -> Self;
29 }
30
31 macro_rules! impl_integer_intrinsic {
32     { $(unsafe impl ToBitMask<BitMask=$int:ty> for Mask<_, $lanes:literal>)* } => {
33         $(
34         impl<T: MaskElement> ToBitMask for Mask<T, $lanes> {
35             type BitMask = $int;
36
37             fn to_bitmask(self) -> $int {
38                 unsafe { self.0.to_bitmask_integer() }
39             }
40
41             fn from_bitmask(bitmask: $int) -> Self {
42                 unsafe { Self(mask_impl::Mask::from_bitmask_integer(bitmask)) }
43             }
44         }
45         )*
46     }
47 }
48
49 impl_integer_intrinsic! {
50     unsafe impl ToBitMask<BitMask=u8> for Mask<_, 8>
51     unsafe impl ToBitMask<BitMask=u16> for Mask<_, 16>
52     unsafe impl ToBitMask<BitMask=u32> for Mask<_, 32>
53     unsafe impl ToBitMask<BitMask=u64> for Mask<_, 64>
54 }