Skip to content

fix: popcount compiling windows clang-cl#114

Open
jamierpond wants to merge 1 commit into
masterfrom
jp/fix-popcount-msvc
Open

fix: popcount compiling windows clang-cl#114
jamierpond wants to merge 1 commit into
masterfrom
jp/fix-popcount-msvc

Conversation

@jamierpond
Copy link
Copy Markdown
Collaborator

@jamierpond jamierpond commented Jun 4, 2026

Fixes Windows clang-cl compilation.

…ilers

PopcountLogic<LogarithmOfGroupSize, T>::CombiningMask computed
`T(1 << HalvedGroupSize) - 1`, which performs the shift at `int`
precision because `1` is an `int`. For `LogarithmOfGroupSize = 6`,
`HalvedGroupSize` is 32 — so the expression is `1 << 32`, which is UB
per [expr.shift] (shift count not less than width of the promoted type).

GCC happens to accept this in a constant-expression context; Clang and
MSVC /std:c++20 /permissive- correctly reject it ("non-type template
argument is not a constant expression", "shift count 32 >= width of
type 'int' (32 bits)"). The result is that any translation unit that
includes <zoo/swar/SWAR.h> under MSVC with conformance mode fails to
compile, because SWAR.h's unconditional static_asserts on line 589-596
force instantiation of PopcountLogic<6, uint64_t> via logarithmFloor.

Fix: do the shift at `T`'s precision — `T(T(1) << HalvedGroupSize) - 1`.

Add a regression test that directly instantiates PopcountLogic<6, u64>.
popcount<LogNBits>() would have caught this, but on non-MSVC platforms
it routes through PopcountIntrinsic (which uses __builtin_popcountll)
and never touches PopcountLogic<6>, which is why the existing popcount
tests at <1>, <2>, <4>, <5> all passed.
@jamierpond jamierpond requested a review from thecppzoo June 4, 2026 05:45
@jamierpond jamierpond changed the title fix: popcount compiling msvc fix: popcount compiling windows clang-cl Jun 4, 2026
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.

1 participant