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.

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, falling back to
the common kernel for 64-bit elements. VSX and VXE use vec_popcnt,
which the compiler maps to a single VPOPCNTB/H/W/D or VPOPCT.

Assisted-by: Claude Opus 5 <noreply@anthropic.com>
@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)

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