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/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.