3 ms·
I guess you probably posted this as some sort of joke, but people seem to be taking you seriously, so I am going to take the bait and respond seriously. I am r
by kragen 11y ago
I guess you probably posted this as some sort of joke, but people seem to be taking you seriously, so I am going to take the bait and respond seriously.
I am really at a loss as to how the current SIMD.js API for shuffles could possibly be fast, or how using vector arguments to specify the permutation would make it slower.
No hardware instruction set has an instruction that takes 10 or 18 separate operands the way the SIMD.js API does. NEON has VTBL and VTBX, SSE2 doesn't have shuffling instructions at all (unless you count the rather pathetic SHUFPD, which packs two double-precision values into its output), and SSSE3 adds PSHUFB. All of them use vector arguments to provide the indices for shuffling.
To compile a SIMD.Int8x16.shuffle call into an efficient PSHUFB, the compiler is going to have to figure out whether the s15:0 arguments are drawn from an existing vector and additionally that they all necessarily index into only one of the vector arguments. VTBL is more powerful, but it still needs the indices to be in a vector. So if the compiler isn't able to figure out that you were extracting the indices from a vector, it has to reconstitute that vector in order to invoke VTBL.
- sunfish 11y agoSIMD.js's shuffle instructions closely resemble LLVM's shuffle instructions, and LLVM also require shuffle indices to be constant, so this isn't restriction isn't unique to SIMD.js. Also, x86's shuffle situation is much more involved than your post here acknowledges; it includes shufps, shufpd, movlhps, movlhpd, movhlps, movhlpd, unpcklps, unpcklpd, unpckhps, unpckhpd, movss, movsd, and more, and all of these are effectively constant shuffle masks. I agree that there's more we can do here, and there are some interesting questions if we want to abstract over pshufb and VTBL and so on. Fortunately, SIMD.js is an evolving API and we can add things to it.
- kragen 11y agoOh, I didn't realize SIMD.js required the shuffle indices to be constant in order to generate efficient code. In that case, sure, you can generate efficient code and take advantage of the panoply of special-purpose shunting instructions you mention, and writing and reading the SIMD.js code is even going to be easier than SSE. Thank you for correcting my understanding!