]> git.lizzy.rs Git - rust.git/blob - crates/core_simd/src/first.rs
Begin reducing mask API
[rust.git] / crates / core_simd / src / first.rs
1 /// Implements common traits on the specified vector `$name`, holding multiple `$lanes` of `$type`.
2 macro_rules! impl_vector {
3     { $name:ident, $type:ty } => {
4         impl<const LANES: usize> $name<LANES> where Self: crate::LanesAtMost32 {
5             /// Construct a SIMD vector by setting all lanes to the given value.
6             pub const fn splat(value: $type) -> Self {
7                 Self([value; LANES])
8             }
9
10             /// Returns a slice containing the entire SIMD vector.
11             pub const fn as_slice(&self) -> &[$type] {
12                 &self.0
13             }
14
15             /// Returns a mutable slice containing the entire SIMD vector.
16             pub fn as_mut_slice(&mut self) -> &mut [$type] {
17                 &mut self.0
18             }
19
20             /// Converts an array to a SIMD vector.
21             pub const fn from_array(array: [$type; LANES]) -> Self {
22                 Self(array)
23             }
24
25             /// Converts a SIMD vector to an array.
26             pub const fn to_array(self) -> [$type; LANES] {
27                 // workaround for rust-lang/rust#80108
28                 // TODO fix this
29                 #[cfg(target_arch = "wasm32")]
30                 {
31                     let mut arr = [self.0[0]; LANES];
32                     let mut i = 0;
33                     while i < LANES {
34                         arr[i] = self.0[i];
35                         i += 1;
36                     }
37                     arr
38                 }
39
40                 #[cfg(not(target_arch = "wasm32"))]
41                 {
42                     self.0
43                 }
44             }
45         }
46
47         impl<const LANES: usize> Copy for $name<LANES> where Self: crate::LanesAtMost32 {}
48
49         impl<const LANES: usize> Clone for $name<LANES> where Self: crate::LanesAtMost32 {
50             #[inline]
51             fn clone(&self) -> Self {
52                 *self
53             }
54         }
55
56         impl<const LANES: usize> Default for $name<LANES> where Self: crate::LanesAtMost32 {
57             #[inline]
58             fn default() -> Self {
59                 Self::splat(<$type>::default())
60             }
61         }
62
63         impl<const LANES: usize> PartialEq for $name<LANES> where Self: crate::LanesAtMost32 {
64             #[inline]
65             fn eq(&self, other: &Self) -> bool {
66                 // TODO use SIMD equality
67                 self.to_array() == other.to_array()
68             }
69         }
70
71         impl<const LANES: usize> PartialOrd for $name<LANES> where Self: crate::LanesAtMost32 {
72             #[inline]
73             fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
74                 // TODO use SIMD equalitya
75                 self.to_array().partial_cmp(other.as_ref())
76             }
77         }
78
79         // array references
80         impl<const LANES: usize> AsRef<[$type; LANES]> for $name<LANES> where Self: crate::LanesAtMost32 {
81             #[inline]
82             fn as_ref(&self) -> &[$type; LANES] {
83                 &self.0
84             }
85         }
86
87         impl<const LANES: usize> AsMut<[$type; LANES]> for $name<LANES> where Self: crate::LanesAtMost32 {
88             #[inline]
89             fn as_mut(&mut self) -> &mut [$type; LANES] {
90                 &mut self.0
91             }
92         }
93
94         // slice references
95         impl<const LANES: usize> AsRef<[$type]> for $name<LANES> where Self: crate::LanesAtMost32 {
96             #[inline]
97             fn as_ref(&self) -> &[$type] {
98                 &self.0
99             }
100         }
101
102         impl<const LANES: usize> AsMut<[$type]> for $name<LANES> where Self: crate::LanesAtMost32 {
103             #[inline]
104             fn as_mut(&mut self) -> &mut [$type] {
105                 &mut self.0
106             }
107         }
108
109         // vector/array conversion
110         impl<const LANES: usize> From<[$type; LANES]> for $name<LANES> where Self: crate::LanesAtMost32 {
111             fn from(array: [$type; LANES]) -> Self {
112                 Self(array)
113             }
114         }
115
116         impl <const LANES: usize> From<$name<LANES>> for [$type; LANES] where $name<LANES>: crate::LanesAtMost32 {
117             fn from(vector: $name<LANES>) -> Self {
118                 vector.to_array()
119             }
120         }
121
122         impl_shuffle_2pow_lanes!{ $name }
123     }
124 }