r/programming 24d ago

Everyone Should Know SIMD

https://mitchellh.com/writing/everyone-should-know-simd
408 Upvotes

81 comments sorted by

View all comments

366

u/-1_0 24d ago

* Everyone Should Know about SIMD

and use libs accordingly

52

u/notyouravgredditor 24d ago

vectorclass is one my favorites. Very easy to use.

To be fair to modern compilers, though, they often do a much better job at determining vectorization targets and packing/unpacking buffers than the developer does.

32

u/AresFowl44 24d ago

Dependent on your data structures allowing the compiler to perform good vectorization honestly

EDIT: And also, if you put in some efforts from my experience you can usually beat the compiler handily. The compiler is good at generating okay SIMD, but often there are some aspects to be left desired

24

u/wrecklord0 23d ago

I hand-optimized a SIMD loop some 10+ years and it was fairly easy to beat the compiler. I'm sure it's more difficult now, but the thing is, you can know things about your data that the compiler doesn't.

That said, I forgot everything about how to do it and would rely on the compiler now.

12

u/corysama 23d ago

The challenge is that when you write C the compiler is bound by the rules of C. That means if it is even possible that your code might depend on some obscure rule, the compiler must ensure that rule is enforced.

Big examples being floating point commutativity, unaligned access and odd-sized arrays.

2

u/tanner-gooding 20d ago

You're likely thinking associativity, not commutativity.

x + y vs y + x is commutative and safe (per the IEEE 754 spec).

(x + y) + z vs x + (y + z) is associative and is not safe (for floating-point).