simde icon indicating copy to clipboard operation
simde copied to clipboard

wasm fallback for _mm_shuffle_epi8()?

Open liquidaty opened this issue 3 years ago • 2 comments

Hi, I'm looking for a wasm fallback for _mm_shuffle_epi8() (using emscripten, for my case at least)

I see from https://emscripten.org/docs/porting/simd.html that it should be as simple as "emulated with a SIMD swizzle+and+const". However, not being particularly versed in SIMD operations, I'm not sure how to translate this into actual code.

I see that wasm/simd128.h has something similar in simde_wasm_i8x16_swizzle(), but it doesn't seem quite the same as _mm_shuffle_epi8().

Any suggestions as to how to exactly emulate _mm_shuffle_epi8() in wasm?

(BTW have the same question for _mm_alignr_epi8(), but will try to resolve this first before posting as a separate issue)

liquidaty avatar Sep 08 '22 23:09 liquidaty

_mm_shuffle_epi8() zeros when index >u 0x7F. wasm_i8x16_swizzle() zeros when index >u 0x0F.

so you need to clear those three bits of the shuffle index.

_mm_shuffle_epi8(a, b) -> wasm_i8x16_swizzle(a, wasm_v128_and(b, wasm_i8x16_splat(0x8F)));

aqrit avatar Sep 10 '22 21:09 aqrit

Will try it out thank you!

liquidaty avatar Sep 11 '22 18:09 liquidaty

@liquidaty can you send a PR with your WASM optimized implementation of _mm_shuffle_epi8?

mr-c avatar Apr 01 '23 09:04 mr-c

I implemented this in a different body of code than simd-everywhere but here is the code I used. Note, this basically is exactly what @aqrit suggested and it seems to work for my purposes, but I certainly have not tested enough to make any representation myself as to its equivalence in results for all conceivable inputs to _mm_shuffle_epi8.

#ifdef __EMSCRIPTEN__

/* x_mm_shuffle_epi8 is the wasm version of mm_shuffle_epi8 */

static really_inline m128 x_mm_shuffle_epi8(m128 a, m128 b) {
  return wasm_i8x16_swizzle(a, wasm_v128_and(b, wasm_i8x16_splat(0x8F)));
}

liquidaty avatar Apr 01 '23 16:04 liquidaty