Skip to main content

simply_simd/mask/
element.rs

1use std::marker::PhantomData;
2use std::ops::*;
3
4use crate::architectures::interface::{Arch, SimdAllBitsImpl, *};
5use crate::mask::Mask;
6use crate::register::Simd;
7use crate::simd_types::*;
8
9impl<T: SimdElement, A: Arch> Mask<T, A> {
10    #[inline(always)]
11    pub(crate) fn new(data: A::Mask) -> Self {
12        Self {
13            data,
14            _marker: PhantomData,
15        }
16    }
17
18    #[inline(always)]
19    pub fn raw_cast<S: SimdElement>(self) -> Mask<S, A> {
20        Mask::new(self.data)
21    }
22
23
24    #[inline(always)]
25    pub fn all_false(self) -> bool {
26        unsafe { self.data.all_zero() }
27    }
28
29    // TODO: Support other bit_sizes
30    #[inline(always)]
31    pub fn first_n_true(n: u32) -> Mask<T, A> {
32        let iota = Simd::iota(0u32);
33        let n_vec = Simd::splat(n);
34        n_vec.simd_gt(iota).raw_cast()
35    }
36
37    #[inline(always)]
38    pub fn first_n_false(n: u32) -> Mask<T, A> {
39        let iota = Simd::iota(1u32);
40        let n_vec = Simd::splat(n);
41        iota.simd_gt(n_vec).raw_cast()
42    }
43}
44
45impl<T: SimdElement, A: Arch> BitAnd for Mask<T, A> {
46    type Output = Self;
47    #[inline(always)]
48    fn bitand(self, rhs: Self) -> Self {
49        unsafe { Self::new(self.data.and(rhs.data)) }
50    }
51}
52
53impl<T: SimdElement, A: Arch> BitOr for Mask<T, A> {
54    type Output = Self;
55    #[inline(always)]
56    fn bitor(self, rhs: Self) -> Self {
57        unsafe { Self::new(self.data.or(rhs.data)) }
58    }
59}
60
61impl<T: SimdElement, A: Arch> BitXor for Mask<T, A> {
62    type Output = Self;
63    #[inline(always)]
64    fn bitxor(self, rhs: Self) -> Self {
65        unsafe { Self::new(self.data.xor(rhs.data)) }
66    }
67}
68
69impl<T: SimdElement, A: Arch> Not for Mask<T, A> {
70    type Output = Self;
71    #[inline(always)]
72    fn not(self) -> Self {
73        unsafe { Self::new(self.data.not()) }
74    }
75}
76
77impl<T: SimdElement, A: Arch> Mask<T, A> {
78    #[inline(always)]
79    pub fn andnot(self, rhs: Self) -> Self {
80        unsafe { Self::new(self.data.and_not(rhs.data)) }
81    }
82
83    // TODO: Add 16 bit select.
84    pub fn select(self, true_values: Simd<T, A>, false_values: Simd<T, A>) -> Simd<T, A> {
85        unsafe {
86            match T::BIT_SIZE {
87                BitSize::Size64 => {
88                    Simd::new(self.data.vblend_64(true_values.data, false_values.data))
89                }
90                BitSize::Size32 => {
91                    Simd::new(self.data.vblend_32(true_values.data, false_values.data))
92                }
93                BitSize::Size8 => {
94                    Simd::new(self.data.vblend_8(true_values.data, false_values.data))
95                }
96                _ => panic!("Select for 16 bit types not implemented yet!"),
97            }
98        }
99    }
100}
101
102impl<T: SimdElement, A: Arch> Mask<T, A> {
103    pub fn to_bits(self) -> u64 {
104        unsafe {
105            match T::BIT_SIZE {
106                BitSize::Size64 => self.data.to_bits_64(),
107                BitSize::Size32 => self.data.to_bits_32(),
108                BitSize::Size8 => self.data.to_bits_8(),
109                _ => unreachable!(), // TODO: Add to_bits_16.
110            }
111        }
112    }
113
114    pub fn from_bits(bitmask: u64) -> Self {
115        unsafe {
116            match T::BIT_SIZE {
117                BitSize::Size64 => Self::new(A::Mask::from_bits_64(bitmask)),
118                BitSize::Size32 => Self::new(A::Mask::from_bits_32(bitmask)),
119                BitSize::Size16 => Self::new(A::Mask::from_bits_16(bitmask)),
120                BitSize::Size8 => Self::new(A::Mask::from_bits_8(bitmask)),
121            }
122        }
123    }
124}