Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

fix: popcount compiling windows clang-cl#114

Open
jamierpond wants to merge 1 commit into
masterthecppzoo/zoo:masterfrom
jp/fix-popcount-msvcthecppzoo/zoo:jp/fix-popcount-msvcCopy head branch name to clipboard
Open

fix: popcount compiling windows clang-cl#114
jamierpond wants to merge 1 commit into
masterthecppzoo/zoo:masterfrom
jp/fix-popcount-msvcthecppzoo/zoo:jp/fix-popcount-msvcCopy head branch name to clipboard

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
Comment on lines +375 to +386
static_assert(meta::PopcountLogic<6, u64>::CombiningMask == 0x0000'0000'FFFF'FFFFull);
static_assert(meta::PopcountLogic<6, u64>::HalvedGroupSize == 32);
static_assert(meta::PopcountLogic<6, u64>::GroupSize == 64);
static_assert(0 == meta::PopcountLogic<6, u64>::execute(0ull));
static_assert(1 == meta::PopcountLogic<6, u64>::execute(1ull));
static_assert(1 == meta::PopcountLogic<6, u64>::execute(1ull << 32));
static_assert(1 == meta::PopcountLogic<6, u64>::execute(1ull << 63));
static_assert(32 == meta::PopcountLogic<6, u64>::execute(0xFFFFFFFFull));
static_assert(32 == meta::PopcountLogic<6, u64>::execute(0xFFFFFFFF00000000ull));
static_assert(32 == meta::PopcountLogic<6, u64>::execute(0xAAAAAAAAAAAAAAAAull));
static_assert(32 == meta::PopcountLogic<6, u64>::execute(0x0123456789ABCDEFull));
static_assert(64 == meta::PopcountLogic<6, u64>::execute(0xFFFFFFFFFFFFFFFFull));
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the type of tests that are useful: they dump a haystack on the needle of wisdom

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

Morty Proxy This is a proxified and sanitized view of the page, visit original site.