sonic_simd/
traits.rs

1use std::ops::{BitAnd, BitOr, BitOrAssign};
2
3/// Portbal SIMD traits
4pub trait Simd: Sized {
5    const LANES: usize;
6
7    type Element;
8    type Mask: Mask;
9
10    /// # Safety
11    unsafe fn from_slice_unaligned_unchecked(slice: &[u8]) -> Self {
12        debug_assert!(slice.len() >= Self::LANES);
13        Self::loadu(slice.as_ptr())
14    }
15
16    /// # Safety
17    unsafe fn write_to_slice_unaligned_unchecked(&self, slice: &mut [u8]) {
18        debug_assert!(slice.len() >= Self::LANES);
19        self.storeu(slice.as_mut_ptr());
20    }
21
22    /// # Safety
23    unsafe fn loadu(ptr: *const u8) -> Self;
24
25    /// # Safety
26    unsafe fn storeu(&self, ptr: *mut u8);
27
28    fn eq(&self, rhs: &Self) -> Self::Mask;
29
30    fn splat(elem: Self::Element) -> Self;
31
32    /// greater than
33    fn gt(&self, rhs: &Self) -> Self::Mask;
34
35    /// less or equal
36    fn le(&self, rhs: &Self) -> Self::Mask;
37}
38
39/// Portbal SIMD mask traits
40pub trait Mask: Sized + BitOr<Self> + BitOrAssign + BitAnd<Self> {
41    type Element;
42    type BitMask: BitMask;
43
44    fn bitmask(self) -> Self::BitMask;
45
46    fn splat(b: bool) -> Self;
47}
48
49/// Trait for the bitmask of a vector Mask.
50pub trait BitMask {
51    /// Total bits in the bitmask.
52    const LEN: usize;
53
54    /// get the offset of the first `1` bit.
55    fn first_offset(&self) -> usize;
56
57    /// check if this bitmask is before the other bitmask.
58    fn before(&self, rhs: &Self) -> bool;
59
60    /// convert bitmask as little endian
61    fn as_little_endian(&self) -> Self;
62
63    /// whether all bits are zero.
64    fn all_zero(&self) -> bool;
65
66    /// clear high n bits.
67    fn clear_high_bits(&self, n: usize) -> Self;
68}