csp icon indicating copy to clipboard operation
csp copied to clipboard

Make bit counting operations constexpr on Windows using C++20 features

Open AdamGlustein opened this issue 1 year ago • 0 comments

For Windows build support in https://github.com/Point72/csp/pull/229, we remove the constexpr specifier from the clz/ffs bit counting operations as MSVC's _BitScanForward and _BitScanReverse compiler intrinsics are not constexpr, unlike gcc's __builtin_clz and __builtin_ffs. It also leads to a macro being defined to control whether the functions are marked constexpr or not in DynamicBitset.

However, using a combination of std::bit_width in the new <bits> header and std::is_constant_evaluation we can make these functions constexpr for Windows too by only using the (faster) compiler intrinsics at runtime, not compile-time. It should make things a bit simpler and also ensures the same functions are constexpr across all platforms.

AdamGlustein avatar May 21 '24 14:05 AdamGlustein