r/cpp • • Apr 08 '26

Optimize norm gathering asm in C++

https://blog.serenedb.com/norm-gathering

We replaced SereneDB's AVX2 gather intrinsics with pure C++ and it actually beat intrinsics on x86_64.

The trick is combining сlang's auto-vectorization for dense data with #pragma unroll to let out-of-order execution handle sparse data. The post covers the assembly breakdown and some compiler traps that can make your code slower.

Happy to answer any questions below!

If you enjoy C++ systems optimization star us we appreciate your support!

25 Upvotes

3 comments sorted by

View all comments

Show parent comment

2

u/MBkkt Apr 08 '26

Sounds interesting, I think there're two options:

  1. You can try to contribute your approach into micro benchmark and I will measure and update post
  2. I will try it later

From my experience avx512 sometimes have great instructions (and register size doesn't really matter), and the only bad thing about it, that not all processors support them.

Anyway, thanks for suggestions!

2

u/Successful_Yam_9023 Apr 10 '26

I tried this, using a complicated unroll strategy to be able to extract some ILP (otherwise this suffers from a horrible loop-carried dependency) and managing to do both the "gather" and zero-extension in one instruction (masked vpermw) which I thought was neat, but it still wasn't good. Not horrible, but not good.