Macro std::simd::simd_swizzle

source ·
pub macro simd_swizzle {
    (
        $vector:expr, $index:expr $(,)?
    ) => { ... },
    (
        $first:expr, $second:expr, $index:expr $(,)?
    ) => { ... },
}
🔬This is a nightly-only experimental API. (portable_simd #86656)
Expand description

Constructs a new SIMD vector by copying elements from selected lanes in other vectors.

When swizzling one vector, lanes are selected by a const array of usize, like Swizzle.

When swizzling two vectors, lanes are selected by a const array of Which, like Swizzle2.

Examples

With a single SIMD vector, the const array specifies lane indices in that vector:

let v = u32x4::from_array([10, 11, 12, 13]);

// Keeping the same size
let r: u32x4 = simd_swizzle!(v, [3, 0, 1, 2]);
assert_eq!(r.to_array(), [13, 10, 11, 12]);

// Changing the number of lanes
let r: u32x2 = simd_swizzle!(v, [3, 1]);
assert_eq!(r.to_array(), [13, 11]);
Run

With two input SIMD vectors, the const array uses Which to specify the source of each index:

use Which::{First, Second};
let a = u32x4::from_array([0, 1, 2, 3]);
let b = u32x4::from_array([4, 5, 6, 7]);

// Keeping the same size
let r: u32x4 = 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: u32x2 = simd_swizzle!(a, b, [First(0), Second(0)]);
assert_eq!(r.to_array(), [0, 4]);
Run