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 {
10 /// Returns a slice containing the entire SIMD vector.
11 pub const fn as_slice(&self) -> &[$type] {
15 /// Returns a mutable slice containing the entire SIMD vector.
16 pub fn as_mut_slice(&mut self) -> &mut [$type] {
20 /// Converts an array to a SIMD vector.
21 pub const fn from_array(array: [$type; LANES]) -> Self {
25 /// Converts a SIMD vector to an array.
26 pub const fn to_array(self) -> [$type; LANES] {
27 // workaround for rust-lang/rust#80108
29 #[cfg(target_arch = "wasm32")]
31 let mut arr = [self.0[0]; LANES];
40 #[cfg(not(target_arch = "wasm32"))]
47 impl<const LANES: usize> Copy for $name<LANES> where Self: crate::LanesAtMost32 {}
49 impl<const LANES: usize> Clone for $name<LANES> where Self: crate::LanesAtMost32 {
51 fn clone(&self) -> Self {
56 impl<const LANES: usize> Default for $name<LANES> where Self: crate::LanesAtMost32 {
58 fn default() -> Self {
59 Self::splat(<$type>::default())
63 impl<const LANES: usize> PartialEq for $name<LANES> where Self: crate::LanesAtMost32 {
65 fn eq(&self, other: &Self) -> bool {
66 // TODO use SIMD equality
67 self.to_array() == other.to_array()
71 impl<const LANES: usize> PartialOrd for $name<LANES> where Self: crate::LanesAtMost32 {
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())
80 impl<const LANES: usize> AsRef<[$type; LANES]> for $name<LANES> where Self: crate::LanesAtMost32 {
82 fn as_ref(&self) -> &[$type; LANES] {
87 impl<const LANES: usize> AsMut<[$type; LANES]> for $name<LANES> where Self: crate::LanesAtMost32 {
89 fn as_mut(&mut self) -> &mut [$type; LANES] {
95 impl<const LANES: usize> AsRef<[$type]> for $name<LANES> where Self: crate::LanesAtMost32 {
97 fn as_ref(&self) -> &[$type] {
102 impl<const LANES: usize> AsMut<[$type]> for $name<LANES> where Self: crate::LanesAtMost32 {
104 fn as_mut(&mut self) -> &mut [$type] {
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 {
116 impl <const LANES: usize> From<$name<LANES>> for [$type; LANES] where $name<LANES>: crate::LanesAtMost32 {
117 fn from(vector: $name<LANES>) -> Self {
122 impl_shuffle_2pow_lanes!{ $name }