Skip to content

feat: add per-lane popcount on batch - #1401

Open
DiamonDinoia wants to merge 1 commit into
xtensor-stack:masterfrom
DiamonDinoia:feat/popcount
Open

feat: add per-lane popcount on batch#1401
DiamonDinoia wants to merge 1 commit into
xtensor-stack:masterfrom
DiamonDinoia:feat/popcount

Conversation

@DiamonDinoia

Copy link
Copy Markdown
Contributor

Count the bits set in each element of an integer batch. The common kernel is the SWAR fold; NEON uses CNT with pairwise widening adds, SVE uses svcnt_x, and WASM uses i8x16.popcnt with pairwise widening extends, falling back to the common kernel for 64-bit elements.

I will add kernels in the future too when I have more of this. I need the bitwise operation for a Morton transform library I am writing.

Creating a new file because I plan to add more functions in the future:
countl_zero/countr_zero, bit_reverse, multishift, bit_deposit/bit_extract, bit_permute + bit_permute_constant, bit_matmul, GFNI kernels.

Reviewed by: Claude Opus 5 noreply@anthropic.com

@serge-sans-paille

Copy link
Copy Markdown
Contributor

With the appropriate credits, https://github.com/WojciechMula/sse-popcount seems to provide relevant implementation for arch-specifics

@DiamonDinoia

Copy link
Copy Markdown
Contributor Author

Good point. I also should have listed that the algorithm I implemented I found it on wikipedia.

@serge-sans-paille

Copy link
Copy Markdown
Contributor

See also https://github.com/kimwalisch/libpopcnt/tree/master based on the paper above for an AVX2 implementation you could add to this PR (with proper credit of course)

Count the bits set in each element of an integer batch. The common
kernel is the SWAR fold; x86 uses the PSHUFB nibble lookup from SSSE3
up, NEON uses CNT with pairwise widening adds, SVE uses svcnt_x, and
WASM uses i8x16.popcnt with pairwise widening extends, folding 32-bit
counts with a shift-and-add pair for 64-bit elements. VSX and VXE use
vec_popcnt, which the compiler maps to a single VPOPCNTB/H/W/D or
VPOPCT.

For 64-bit elements the two x86 nibble tables carry a +4 and a -4 bias,
after libpopcnt, so PSADBW yields the byte count and the 8-byte sum in
one instruction. This drops the VPADDB, and measures 1.09x on SSE and
AVX2 and 1.05x on AVX-512.

avx512vnni gains a 32-bit kernel: VPDPBUSD does in one uop what the
VPMADDUBSW and VPMADDWD pair does in two, and the zero accumulator is
free because the register copy is eliminated at rename. This measures
1.14x. The same substitution on 256-bit vectors is neutral, since three
ports serve them, so avxvnni gets no kernel.

The CI job labelled avx512vnni built for knm, which enables
avx5124vnniw rather than avx512vnni and selected the avx512pf arch, so
it covered neither kernel. It now builds for cascadelake.

Assisted-by: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants