simd_swizzle in std::simd - Rust (original) (raw)

pub macro simd_swizzle {
    ($vector:expr, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi><mi>n</mi><mi>d</mi><mi>e</mi><mi>x</mi><mo>:</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi></mrow><annotation encoding="application/x-tex">index:expr </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">in</span><span class="mord mathnormal">d</span><span class="mord mathnormal">e</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">:</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">e</span><span class="mord mathnormal">x</span><span class="mord mathnormal">p</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span></span></span></span>(,)?) => { ... },
    ($first:expr, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>e</mi><mi>c</mi><mi>o</mi><mi>n</mi><mi>d</mi><mo>:</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">second:expr, </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">seco</span><span class="mord mathnormal">n</span><span class="mord mathnormal">d</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">:</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">e</span><span class="mord mathnormal">x</span><span class="mord mathnormal">p</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mpunct">,</span></span></span></span>index:expr $(,)?) => { ... },
}

🔬This is a nightly-only experimental API. (portable_simd #86656)

Expand description

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

When swizzling one vector, elements are selected like Swizzle::swizzle.

When swizzling two vectors, elements are selected like Swizzle::concat_swizzle.

§Examples

With a single SIMD vector, the const array specifies element 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 elements
let r: u32x2 = simd_swizzle!(v, [3, 1]);
assert_eq!(r.to_array(), [13, 11]);

With two input SIMD vectors, the const array specifies element indices in the concatenation of those vectors:

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, [0, 1, 6, 7]);
assert_eq!(r.to_array(), [0, 1, 6, 7]);

// Changing the number of elements
let r: u32x2 = simd_swizzle!(a, b, [0, 4]);
assert_eq!(r.to_array(), [0, 4]);