simd_shuffle -> simd_swizzle

This commit is contained in:
Caleb Zulawski 2021-09-23 00:28:07 +00:00 committed by Jubilee
parent 98e4fcae5a
commit 37797d9c0a
3 changed files with 36 additions and 24 deletions

View File

@ -169,16 +169,16 @@ pub fn simd_inv4x4(m: Matrix4x4) -> Option<Matrix4x4> {
const SHUFFLE13: [Which; 4] = [First(1), First(3), Second(1), Second(3)];
const SHUFFLE23: [Which; 4] = [First(2), First(3), Second(2), Second(3)];
let tmp = simd_shuffle!(m_0, m_1, SHUFFLE01);
let row1 = simd_shuffle!(m_2, m_3, SHUFFLE01);
let tmp = simd_swizzle!(m_0, m_1, SHUFFLE01);
let row1 = simd_swizzle!(m_2, m_3, SHUFFLE01);
let row0 = simd_shuffle!(tmp, row1, SHUFFLE02);
let row1 = simd_shuffle!(row1, tmp, SHUFFLE13);
let row0 = simd_swizzle!(tmp, row1, SHUFFLE02);
let row1 = simd_swizzle!(row1, tmp, SHUFFLE13);
let tmp = simd_shuffle!(m_0, m_1, SHUFFLE23);
let row3 = simd_shuffle!(m_2, m_3, SHUFFLE23);
let row2 = simd_shuffle!(tmp, row3, SHUFFLE02);
let row3 = simd_shuffle!(row3, tmp, SHUFFLE13);
let tmp = simd_swizzle!(m_0, m_1, SHUFFLE23);
let row3 = simd_swizzle!(m_2, m_3, SHUFFLE23);
let row2 = simd_swizzle!(tmp, row3, SHUFFLE02);
let row3 = simd_swizzle!(row3, tmp, SHUFFLE13);
let tmp = (row2 * row3).reverse().rotate_right::<2>();
let minor0 = row1 * tmp;

View File

@ -18,5 +18,5 @@
#[path = "mod.rs"]
mod core_simd;
use self::core_simd::simd;
pub use self::core_simd::simd;
pub use simd::*;

View File

@ -5,43 +5,55 @@ use crate::{LaneCount, Simd, SimdElement, SupportedLaneCount};
///
/// A new vector is constructed by specifying the the lanes of the source vector or vectors to use.
///
/// When shuffling one vector, the indices of the result vector are indicated by a `const` array
/// When swizzling one vector, the indices of the result vector are indicated by a `const` array
/// of `usize`, like [`Swizzle`].
/// When shuffling two vectors, the indices are indicated by a `const` array of [`Which`], like
/// When swizzling two vectors, the indices are indicated by a `const` array of [`Which`], like
/// [`Swizzle2`].
///
/// # Examples
/// ## One source vector
/// ```
/// # #![feature(portable_simd)]
/// # use core_simd::{Simd, simd_shuffle};
/// # use core_simd::{Simd, simd_swizzle};
/// let v = Simd::<f32, 4>::from_array([0., 1., 2., 3.]);
/// let v = simd_shuffle!(v, [3, 0, 1, 2]);
/// assert_eq!(v.to_array(), [3., 0., 1., 2.]);
///
/// // Keeping the same size
/// let r = simd_swizzle!(v, [3, 0, 1, 2]);
/// assert_eq!(r.to_array(), [3., 0., 1., 2.]);
///
/// // Changing the number of lanes
/// let r = simd_swizzle!(v, [3, 1]);
/// assert_eq!(r.to_array(), [3., 1.]);
/// ```
///
/// ## Two source vectors
/// ```
/// # #![feature(portable_simd)]
/// # use core_simd::{Simd, simd_shuffle, Which};
/// # use core_simd::{Simd, simd_swizzle, Which};
/// use Which::*;
/// let a = Simd::<f32, 4>::from_array([0., 1., 2., 3.]);
/// let b = Simd::<f32, 4>::from_array([4., 5., 6., 7.]);
/// let v = simd_shuffle!(a, b, [First(0), First(1), Second(2), Second(3)]);
/// assert_eq!(v.to_array(), [0., 1., 6., 7.]);
///
/// // Keeping the same size
/// let r = simd_swizzle!(a, b, [First(0), First(1), Second(2), Second(3)]);
/// assert_eq!(r.to_array(), [0., 1., 6., 7.]);
///
/// // Changing the number of lanes
/// let r = simd_swizzle!(a, b, [First(0), Second(0)]);
/// assert_eq!(r.to_array(), [0., 4.]);
/// ```
#[macro_export]
macro_rules! simd_shuffle {
macro_rules! simd_swizzle {
{
$vector:expr, $index:expr $(,)?
} => {
{
use $crate::simd::Swizzle;
struct Shuffle;
impl Swizzle<{$index.len()}, {$index.len()}> for Shuffle {
struct SwizzleImpl;
impl<const LANES: usize> Swizzle<LANES, {$index.len()}> for SwizzleImpl {
const INDEX: [usize; {$index.len()}] = $index;
}
Shuffle::swizzle($vector)
SwizzleImpl::swizzle($vector)
}
};
{
@ -49,11 +61,11 @@ macro_rules! simd_shuffle {
} => {
{
use $crate::simd::{Which, Swizzle2};
struct Shuffle;
impl Swizzle2<{$index.len()}, {$index.len()}> for Shuffle {
struct SwizzleImpl;
impl<const LANES: usize> Swizzle2<LANES, {$index.len()}> for SwizzleImpl {
const INDEX: [Which; {$index.len()}] = $index;
}
Shuffle::swizzle2($first, $second)
SwizzleImpl::swizzle2($first, $second)
}
}
}