simd-everywhere / simde

Implementations of SIMD instruction sets for systems which don't natively support them.
https://simd-everywhere.github.io/blog/
MIT License
2.39k stars 248 forks source link

wasm fallback for _mm_alignr_epi8()? #968

Open liquidaty opened 2 years ago

liquidaty commented 2 years ago

Hi,

Could anyone help me to construct a wasm fallback for _mm_alignr_epi8() (using emscripten)?

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

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

aqrit commented 2 years ago

_mm_alignr_epi8() would map to wasm_i8x16_shuffle().

If for some reason you need a dynamic variable shift by bytes then that can be emulated using wasm_i8x16_swizzle().

mr-c commented 1 year ago

@liquidaty Did you see https://github.com/emscripten-core/emscripten/blame/9eff02bc816c50ab0e3b70a3bd5b72a8dc2893a2/system/include/compat/tmmintrin.h#L34-L37 ?

aqrit commented 1 year ago

at the least v8/turbofan should lower a wasm_i8x16_shuffle in this instance to 1 instruction on both SSSE3 and NEON: https://github.com/zeux/wasm-simd/blob/master/Shuffles.md#concats

However, I'm having trouble getting the disassembly to prove this...

mr-c commented 1 year ago

@aqrit If you use emscripten to compile the tests (perhaps by using the SIMDe development container) then you can run wasm2wat tests/x86/ssse3-native-c.wasm to get an assembly dump. It can be interesting to compare that to the non-native version at tests/x86/ssse3-emul-c.wasm. If you have diffoscope installed, then you can make the diff in one command: diffoscope tests/x86/ssse3-{emul,native}-c.wasm.

Note for using the docker container: To make it easier to examine the artifacts, use the PERSISTENT_BUILD_DIR option so you can inspect the build directory on your host system.