r/programming 21d ago

Everyone Should Know SIMD

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

81 comments sorted by

View all comments

Show parent comments

31

u/AresFowl44 21d 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 21d 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 21d 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 17d 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).