WebAssembly / simd

Branch of the spec repo scoped to discussion of SIMD in WebAssembly
Other
527 stars 43 forks source link

Variable indices `shuffle2` op? #488

Closed ghost closed 2 years ago

ghost commented 3 years ago

I would like to suggest adding shuffle2, with variable indices. I have difficulties in assemblyscript, where there are no constexpr arguments and no constexpr arguments are expected (because of this, I can't write SIMD classes).

i8x16.shuffle2(a: v128, b: v128, s: v128) -> v128

def S.shuffle2(a, b, s):
    result = S.New()
    for i in range(S.Lanes):
        if s[i] < S.lanes:
            result[i] = a[s[i]]
        else:
            result[i] = b[s[i] - S.lanes]
    return result
Maratyszcza commented 3 years ago

i8x16.swizzle produce zero when index is out-of-bounds, so you can do two-vector dynamic shuffle with

v128.or(i8x16.swizzle(a, s),
        i8x16.swizzle(b, i8x16.sub(s, i8x16.splat(16))))