utils/simd/
scalar.rs

1//! Scalar vector implementations.
2
3// #[inline(always)] significantly improves the performance of dev builds
4#![allow(clippy::inline_always)]
5
6use std::ops::{Add, BitAnd, BitOr, BitXor, Not};
7
8/// The name of this backend.
9pub const SIMD_BACKEND: &str = "scalar";
10
11/// Scalar vector with a single [u32] lane.
12#[derive(Clone, Copy)]
13#[repr(transparent)]
14pub struct U32Vector(u32);
15
16impl From<[u32; 1]> for U32Vector {
17    #[inline]
18    fn from(value: [u32; 1]) -> Self {
19        U32Vector(value[0])
20    }
21}
22
23impl From<U32Vector> for [u32; 1] {
24    #[inline]
25    fn from(value: U32Vector) -> Self {
26        [value.0]
27    }
28}
29
30impl Add for U32Vector {
31    type Output = Self;
32
33    #[inline(always)]
34    fn add(self, rhs: Self) -> Self::Output {
35        Self(self.0.wrapping_add(rhs.0))
36    }
37}
38
39impl BitAnd for U32Vector {
40    type Output = Self;
41
42    #[inline(always)]
43    fn bitand(self, rhs: Self) -> Self::Output {
44        Self(self.0 & rhs.0)
45    }
46}
47
48impl BitOr for U32Vector {
49    type Output = Self;
50
51    #[inline(always)]
52    fn bitor(self, rhs: Self) -> Self::Output {
53        Self(self.0 | rhs.0)
54    }
55}
56
57impl BitXor for U32Vector {
58    type Output = Self;
59
60    #[inline(always)]
61    fn bitxor(self, rhs: Self) -> Self::Output {
62        Self(self.0 ^ rhs.0)
63    }
64}
65
66impl Not for U32Vector {
67    type Output = Self;
68
69    #[inline(always)]
70    fn not(self) -> Self::Output {
71        Self(!self.0)
72    }
73}
74
75impl U32Vector {
76    pub const LANES: usize = 1;
77
78    #[inline(always)]
79    #[must_use]
80    pub fn andnot(self, rhs: Self) -> Self {
81        Self(self.0 & !rhs.0)
82    }
83
84    #[inline(always)]
85    #[must_use]
86    pub fn splat(v: u32) -> Self {
87        Self(v)
88    }
89
90    #[inline(always)]
91    #[must_use]
92    pub fn rotate_left(self, n: u32) -> Self {
93        Self(self.0.rotate_left(n))
94    }
95}