Skip to content

Fix UB in PopcountLogic on MSVC #113

Open
jamierpond wants to merge 1 commit intomasterfrom
jp/popcount-msvc-bug
Open

Fix UB in PopcountLogic on MSVC #113
jamierpond wants to merge 1 commit intomasterfrom
jp/popcount-msvc-bug

Conversation

@jamierpond
Copy link
Copy Markdown
Collaborator

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() 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.

…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 April 20, 2026 17:16
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