x86-64 SIMD popcnt

David Sparks sparks05 at proton.me
Tue Oct 13 05:15:29 CEST 2026


Of course, *after* writing that code, I did a bit of web searching
and turned up some significant work in that area.

https://arxiv.org/abs/1611.07612
Faster Population Counts Using AVX2 Instructions
(Wojciech Mula, Nathan Kurz, Daniel Lemire)

https://arxiv.org/abs/2412.16370
Faster Positional-Population Counts for AVX2, AVX-512, and ASIMD
(Robert Clausecker, Daniel Lemire, Florian Schintke)

The latter points out that the AVX-512 vpternlogd instruction is
gold for implementing full adders.

The one thing I disagree with the latter paper about is the
handling of the tail.  They go to some trouble to add the
first 15 SIMD words to 4-bit accumulators before the main 16-fold
main loop.  Then throw away that minor performance gain with messy
tail handling.

Better is to have simplified addition trees for 1, 2, 4, and 8
SIMD words, which are selected based on bits of the length modulo
16, then the main loop can execute at full speed.

You can either break out of the main loop early and handle the last
SIMD word specially, or you can just fetch the last word early and use it to initialize the loop.  (Merging the masked words if start%16 >= end%16 may or may not be a win.)  This is fine if the data is cache-resident,
but the non-sequential access will cost a cache miss if it's not.


More information about the gmp-bugs mailing list