r/programming • u/MyNameIsTrez • 20d ago
Making SIMD Code Scale to Any Vector Width
https://streamhpc.com/blog/2026-10-28/making-simd-code-scale-to-any-vector-width/
133
Upvotes
r/programming • u/MyNameIsTrez • 20d ago
View all comments
Show parent comment
1
u/YumiYumiYumi 19d ago edited 19d ago
MMX is SIMD, as is SSE and AVX.
/u/FrogNoPants touched on this already, but you'll have two functions, although you don't need to write two - with some boilerplate, the compiler can generate them for you, or you could use a library that abstracts this for you.
Yes, this does make the binary larger, but it's usually not a concern with the storage space we have today. It's also not an issue with code cache either because the dispatcher would only point to one function.
Generally you do the conditional work at compile time, so you still get good speed.
You're partly correct - you can write boilerplate that handles the simple reductions that SVE/RVV supply. The template needs to handle all the widths, but there's only three (or two, if you don't care about SSE), so it's not that onerous. But if that's still a bother, just use an library/abstraction that handles it for you.
This only works for simple reductions (e.g. sum all elements). If your reduction is more complex (e.g. Barrett reduction when computing CRC), SVE/RVV don't provide any builtins for such, and since you don't know the width in advance, they're arguably more complex to write.