1 use crate::simd::intrinsics;
2 use crate::simd::{LaneCount, Simd, SimdElement, SupportedLaneCount};
4 /// Constructs a new vector by selecting values from the lanes of the source vector or vectors to use.
6 /// When swizzling one vector, the indices of the result vector are indicated by a `const` array
7 /// of `usize`, like [`Swizzle`].
8 /// When swizzling two vectors, the indices are indicated by a `const` array of [`Which`], like
12 /// ## One source vector
14 /// # #![feature(portable_simd)]
15 /// # #[cfg(feature = "std")] use core_simd::{Simd, simd_swizzle};
16 /// # #[cfg(not(feature = "std"))] use core::simd::{Simd, simd_swizzle};
17 /// let v = Simd::<f32, 4>::from_array([0., 1., 2., 3.]);
19 /// // Keeping the same size
20 /// let r = simd_swizzle!(v, [3, 0, 1, 2]);
21 /// assert_eq!(r.to_array(), [3., 0., 1., 2.]);
23 /// // Changing the number of lanes
24 /// let r = simd_swizzle!(v, [3, 1]);
25 /// assert_eq!(r.to_array(), [3., 1.]);
28 /// ## Two source vectors
30 /// # #![feature(portable_simd)]
31 /// # #[cfg(feature = "std")] use core_simd::{Simd, simd_swizzle, Which};
32 /// # #[cfg(not(feature = "std"))] use core::simd::{Simd, simd_swizzle, Which};
34 /// let a = Simd::<f32, 4>::from_array([0., 1., 2., 3.]);
35 /// let b = Simd::<f32, 4>::from_array([4., 5., 6., 7.]);
37 /// // Keeping the same size
38 /// let r = simd_swizzle!(a, b, [First(0), First(1), Second(2), Second(3)]);
39 /// assert_eq!(r.to_array(), [0., 1., 6., 7.]);
41 /// // Changing the number of lanes
42 /// let r = simd_swizzle!(a, b, [First(0), Second(0)]);
43 /// assert_eq!(r.to_array(), [0., 4.]);
45 #[allow(unused_macros)]
46 pub macro simd_swizzle {
48 $vector:expr, $index:expr $(,)?
51 use $crate::simd::Swizzle;
53 impl<const LANES: usize> Swizzle<LANES, {$index.len()}> for Impl {
54 const INDEX: [usize; {$index.len()}] = $index;
56 Impl::swizzle($vector)
60 $first:expr, $second:expr, $index:expr $(,)?
63 use $crate::simd::{Which, Swizzle2};
65 impl<const LANES: usize> Swizzle2<LANES, {$index.len()}> for Impl {
66 const INDEX: [Which; {$index.len()}] = $index;
68 Impl::swizzle2($first, $second)
73 /// An index into one of two vectors.
74 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
76 /// Indexes the first vector.
78 /// Indexes the second vector.
82 /// Create a vector from the elements of another vector.
83 pub trait Swizzle<const INPUT_LANES: usize, const OUTPUT_LANES: usize> {
84 /// Map from the lanes of the input vector to the output vector.
85 const INDEX: [usize; OUTPUT_LANES];
87 /// Create a new vector from the lanes of `vector`.
89 /// Lane `i` of the output is `vector[Self::INDEX[i]]`.
90 fn swizzle<T>(vector: Simd<T, INPUT_LANES>) -> Simd<T, OUTPUT_LANES>
93 LaneCount<INPUT_LANES>: SupportedLaneCount,
94 LaneCount<OUTPUT_LANES>: SupportedLaneCount,
96 unsafe { intrinsics::simd_shuffle(vector, vector, Self::INDEX_IMPL) }
100 /// Create a vector from the elements of two other vectors.
101 pub trait Swizzle2<const INPUT_LANES: usize, const OUTPUT_LANES: usize> {
102 /// Map from the lanes of the input vectors to the output vector
103 const INDEX: [Which; OUTPUT_LANES];
105 /// Create a new vector from the lanes of `first` and `second`.
107 /// Lane `i` is `first[j]` when `Self::INDEX[i]` is `First(j)`, or `second[j]` when it is
110 first: Simd<T, INPUT_LANES>,
111 second: Simd<T, INPUT_LANES>,
112 ) -> Simd<T, OUTPUT_LANES>
115 LaneCount<INPUT_LANES>: SupportedLaneCount,
116 LaneCount<OUTPUT_LANES>: SupportedLaneCount,
118 unsafe { intrinsics::simd_shuffle(first, second, Self::INDEX_IMPL) }
122 /// The `simd_shuffle` intrinsic expects `u32`, so do error checking and conversion here.
123 /// This trait hides `INDEX_IMPL` from the public API.
124 trait SwizzleImpl<const INPUT_LANES: usize, const OUTPUT_LANES: usize> {
125 const INDEX_IMPL: [u32; OUTPUT_LANES];
128 impl<T, const INPUT_LANES: usize, const OUTPUT_LANES: usize> SwizzleImpl<INPUT_LANES, OUTPUT_LANES>
131 T: Swizzle<INPUT_LANES, OUTPUT_LANES> + ?Sized,
133 const INDEX_IMPL: [u32; OUTPUT_LANES] = {
134 let mut output = [0; OUTPUT_LANES];
136 while i < OUTPUT_LANES {
137 let index = Self::INDEX[i];
138 assert!(index as u32 as usize == index);
139 assert!(index < INPUT_LANES, "source lane exceeds input lane count",);
140 output[i] = index as u32;
147 /// The `simd_shuffle` intrinsic expects `u32`, so do error checking and conversion here.
148 /// This trait hides `INDEX_IMPL` from the public API.
149 trait Swizzle2Impl<const INPUT_LANES: usize, const OUTPUT_LANES: usize> {
150 const INDEX_IMPL: [u32; OUTPUT_LANES];
153 impl<T, const INPUT_LANES: usize, const OUTPUT_LANES: usize> Swizzle2Impl<INPUT_LANES, OUTPUT_LANES>
156 T: Swizzle2<INPUT_LANES, OUTPUT_LANES> + ?Sized,
158 const INDEX_IMPL: [u32; OUTPUT_LANES] = {
159 let mut output = [0; OUTPUT_LANES];
161 while i < OUTPUT_LANES {
162 let (offset, index) = match Self::INDEX[i] {
163 Which::First(index) => (false, index),
164 Which::Second(index) => (true, index),
166 assert!(index < INPUT_LANES, "source lane exceeds input lane count",);
168 // lanes are indexed by the first vector, then second vector
169 let index = if offset { index + INPUT_LANES } else { index };
170 assert!(index as u32 as usize == index);
171 output[i] = index as u32;
178 impl<T, const LANES: usize> Simd<T, LANES>
181 LaneCount<LANES>: SupportedLaneCount,
183 /// Reverse the order of the lanes in the vector.
185 pub fn reverse(self) -> Self {
186 const fn reverse_index<const LANES: usize>() -> [usize; LANES] {
187 let mut index = [0; LANES];
190 index[i] = LANES - i - 1;
198 impl<const LANES: usize> Swizzle<LANES, LANES> for Reverse {
199 const INDEX: [usize; LANES] = reverse_index::<LANES>();
202 Reverse::swizzle(self)
205 /// Rotates the vector such that the first `OFFSET` elements of the slice move to the end
206 /// while the last `LANES - OFFSET` elements move to the front. After calling `rotate_lanes_left`,
207 /// the element previously in lane `OFFSET` will become the first element in the slice.
209 pub fn rotate_lanes_left<const OFFSET: usize>(self) -> Self {
210 const fn rotate_index<const OFFSET: usize, const LANES: usize>() -> [usize; LANES] {
211 let offset = OFFSET % LANES;
212 let mut index = [0; LANES];
215 index[i] = (i + offset) % LANES;
221 struct Rotate<const OFFSET: usize>;
223 impl<const OFFSET: usize, const LANES: usize> Swizzle<LANES, LANES> for Rotate<OFFSET> {
224 const INDEX: [usize; LANES] = rotate_index::<OFFSET, LANES>();
227 Rotate::<OFFSET>::swizzle(self)
230 /// Rotates the vector such that the first `LANES - OFFSET` elements of the vector move to
231 /// the end while the last `OFFSET` elements move to the front. After calling `rotate_lanes_right`,
232 /// the element previously at index `LANES - OFFSET` will become the first element in the slice.
234 pub fn rotate_lanes_right<const OFFSET: usize>(self) -> Self {
235 const fn rotate_index<const OFFSET: usize, const LANES: usize>() -> [usize; LANES] {
236 let offset = LANES - OFFSET % LANES;
237 let mut index = [0; LANES];
240 index[i] = (i + offset) % LANES;
246 struct Rotate<const OFFSET: usize>;
248 impl<const OFFSET: usize, const LANES: usize> Swizzle<LANES, LANES> for Rotate<OFFSET> {
249 const INDEX: [usize; LANES] = rotate_index::<OFFSET, LANES>();
252 Rotate::<OFFSET>::swizzle(self)
255 /// Interleave two vectors.
257 /// Produces two vectors with lanes taken alternately from `self` and `other`.
259 /// The first result contains the first `LANES / 2` lanes from `self` and `other`,
260 /// alternating, starting with the first lane of `self`.
262 /// The second result contains the last `LANES / 2` lanes from `self` and `other`,
263 /// alternating, starting with the lane `LANES / 2` from the start of `self`.
266 /// #![feature(portable_simd)]
267 /// # #[cfg(feature = "std")] use core_simd::Simd;
268 /// # #[cfg(not(feature = "std"))] use core::simd::Simd;
269 /// let a = Simd::from_array([0, 1, 2, 3]);
270 /// let b = Simd::from_array([4, 5, 6, 7]);
271 /// let (x, y) = a.interleave(b);
272 /// assert_eq!(x.to_array(), [0, 4, 1, 5]);
273 /// assert_eq!(y.to_array(), [2, 6, 3, 7]);
276 pub fn interleave(self, other: Self) -> (Self, Self) {
277 const fn lo<const LANES: usize>() -> [Which; LANES] {
278 let mut idx = [Which::First(0); LANES];
282 idx[i] = if i % 2 == 0 {
285 Which::Second(offset)
291 const fn hi<const LANES: usize>() -> [Which; LANES] {
292 let mut idx = [Which::First(0); LANES];
295 let offset = (LANES + i) / 2;
296 idx[i] = if i % 2 == 0 {
299 Which::Second(offset)
309 impl<const LANES: usize> Swizzle2<LANES, LANES> for Lo {
310 const INDEX: [Which; LANES] = lo::<LANES>();
313 impl<const LANES: usize> Swizzle2<LANES, LANES> for Hi {
314 const INDEX: [Which; LANES] = hi::<LANES>();
317 (Lo::swizzle2(self, other), Hi::swizzle2(self, other))
320 /// Deinterleave two vectors.
322 /// The first result takes every other lane of `self` and then `other`, starting with
325 /// The second result takes every other lane of `self` and then `other`, starting with
329 /// #![feature(portable_simd)]
330 /// # #[cfg(feature = "std")] use core_simd::Simd;
331 /// # #[cfg(not(feature = "std"))] use core::simd::Simd;
332 /// let a = Simd::from_array([0, 4, 1, 5]);
333 /// let b = Simd::from_array([2, 6, 3, 7]);
334 /// let (x, y) = a.deinterleave(b);
335 /// assert_eq!(x.to_array(), [0, 1, 2, 3]);
336 /// assert_eq!(y.to_array(), [4, 5, 6, 7]);
339 pub fn deinterleave(self, other: Self) -> (Self, Self) {
340 const fn even<const LANES: usize>() -> [Which; LANES] {
341 let mut idx = [Which::First(0); LANES];
343 while i < LANES / 2 {
344 idx[i] = Which::First(2 * i);
345 idx[i + LANES / 2] = Which::Second(2 * i);
350 const fn odd<const LANES: usize>() -> [Which; LANES] {
351 let mut idx = [Which::First(0); LANES];
353 while i < LANES / 2 {
354 idx[i] = Which::First(2 * i + 1);
355 idx[i + LANES / 2] = Which::Second(2 * i + 1);
364 impl<const LANES: usize> Swizzle2<LANES, LANES> for Even {
365 const INDEX: [Which; LANES] = even::<LANES>();
368 impl<const LANES: usize> Swizzle2<LANES, LANES> for Odd {
369 const INDEX: [Which; LANES] = odd::<LANES>();
372 (Even::swizzle2(self, other), Odd::swizzle2(self, other))