Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion inc/zoo/meta/popcount.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct PopcountLogic {
constexpr static int GroupSize = 1 << LogarithmOfGroupSize;
constexpr static int HalvedGroupSize = GroupSize / 2;
constexpr static auto CombiningMask =
BitmaskMaker<T, T(1 << HalvedGroupSize) - 1, GroupSize>::value;
BitmaskMaker<T, T(T(1) << HalvedGroupSize) - 1, GroupSize>::value;
using Recursion = PopcountLogic<LogarithmOfGroupSize - 1, T>;
constexpr static T execute(T input);
};
Expand Down
27 changes: 27 additions & 0 deletions test/swar/BasicOperations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,33 @@ static_assert(0x210 == popcount<1>(0x320));
static_assert(0x4321 == popcount<2>(0xF754));
static_assert(0x50004 == popcount<4>(0x3E001122));

// Regression: PopcountLogic<6, u64>::CombiningMask previously computed
// `T(1 << 32) - 1`. The shift runs at `int` precision because `1` is an
// `int`, so `1 << 32` is UB per [expr.shift] and fails as a constant
// expression under any standards-conformant constexpr evaluator (clang,
// MSVC /std:c++20 /permissive-). GCC happens to accept it, which is why
// the bug was dormant. The fix is `T(T(1) << 32) - 1`. Without it,
// `CombiningMask` fails to be a constant expression and these asserts
// fail to compile.
//
// We reach PopcountLogic<6> directly rather than via popcount<6>(x),
// because on non-MSVC platforms popcount<6> routes through the
// PopcountIntrinsic struct (which uses __builtin_popcountll) and never
// instantiates PopcountLogic<6, u64> — the original code path that
// carried the bug.
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));

static_assert(1 == msbIndex<u64>(1ull<<1));
static_assert(3 == msbIndex<u64>(1ull<<3));
static_assert(5 == msbIndex<u64>(1ull<<5));
Expand Down
Loading