From 20ccc38f0fad309d4e830af7334d5478a224155a Mon Sep 17 00:00:00 2001 From: Merry Date: Thu, 7 Jul 2022 22:57:21 +0100 Subject: [PATCH] BitUtils: Implement CountTrailingZeros --- Source/Core/Common/BitUtils.h | 50 +++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/Source/Core/Common/BitUtils.h b/Source/Core/Common/BitUtils.h index 1934809e21..bd0801491d 100644 --- a/Source/Core/Common/BitUtils.h +++ b/Source/Core/Common/BitUtils.h @@ -411,6 +411,56 @@ constexpr int CountLeadingZeros(uint32_t value) #endif } +template +constexpr int CountTrailingZerosConst(T value) +{ + int result = sizeof(T) * 8; + while (value) + { + result--; + value <<= 1; + } + return result; +} + +constexpr int CountTrailingZeros(uint64_t value) +{ +#if defined(__GNUC__) + return value ? __builtin_ctzll(value) : 64; +#elif defined(_MSC_VER) + if (std::is_constant_evaluated()) + { + return CountTrailingZerosConst(value); + } + else + { + unsigned long index = 0; + return _BitScanForward64(&index, value) ? index : 64; + } +#else + return CountTrailingZerosConst(value); +#endif +} + +constexpr int CountTrailingZeros(uint32_t value) +{ +#if defined(__GNUC__) + return value ? __builtin_ctz(value) : 32; +#elif defined(_MSC_VER) + if (std::is_constant_evaluated()) + { + return CountTrailingZerosConst(value); + } + else + { + unsigned long index = 0; + return _BitScanForward(&index, value) ? index : 32; + } +#else + return CountLeadingZerosConst(value); +#endif +} + #undef CONSTEXPR_FROM_INTRINSIC template