Open
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PopcountLogic<LogarithmOfGroupSize, T>::CombiningMask computed
T(1 << HalvedGroupSize) - 1, which performs the shift atintprecision because
1is anint. ForLogarithmOfGroupSize = 6,HalvedGroupSizeis 32 — so the expression is1 << 32, which is UBper [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.