From 6b66b67f69e2f1934800b372431b5808105dc56c Mon Sep 17 00:00:00 2001 From: Corentin Wallez Date: Wed, 22 Jan 2020 13:05:16 +0000 Subject: [PATCH] Fix compilation of Log2(uint64_t) on MSVC The _BitScanReverse64 intrinsic only exists when compiling for 64bit. Replace it by two calls to _BitScanReverse (the 32bit one) when on 32bit. Bug: Change-Id: Ie294327ec914b0ca4a73732e4b78c1f2a08f100f Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/15321 Commit-Queue: Corentin Wallez Reviewed-by: Kai Ninomiya --- src/common/Math.cpp | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/common/Math.cpp b/src/common/Math.cpp index 2ae977b1e8..4471eb77aa 100644 --- a/src/common/Math.cpp +++ b/src/common/Math.cpp @@ -15,6 +15,7 @@ #include "common/Math.h" #include "common/Assert.h" +#include "common/Platform.h" #include #include @@ -50,28 +51,31 @@ uint32_t Log2(uint32_t value) { uint32_t Log2(uint64_t value) { ASSERT(value != 0); #if defined(DAWN_COMPILER_MSVC) +# if defined(DAWN_PLATFORM_64_BIT) unsigned long firstBitIndex = 0ul; unsigned char ret = _BitScanReverse64(&firstBitIndex, value); ASSERT(ret != 0); return firstBitIndex; -#else +# else // defined(DAWN_PLATFORM_64_BIT) + unsigned long firstBitIndex = 0ul; + if (_BitScanReverse(&firstBitIndex, value >> 32)) { + return firstBitIndex + 32; + } + unsigned char ret = _BitScanReverse(&firstBitIndex, value & 0xFFFFFFFF); + ASSERT(ret != 0); + return firstBitIndex; +# endif // defined(DAWN_PLATFORM_64_BIT) +#else // defined(DAWN_COMPILER_MSVC) return 63 - static_cast(__builtin_clzll(value)); -#endif +#endif // defined(DAWN_COMPILER_MSVC) } uint64_t NextPowerOfTwo(uint64_t n) { -#if defined(DAWN_COMPILER_MSVC) if (n <= 1) { return 1; } - unsigned long firstBitIndex = 0ul; - unsigned char ret = _BitScanReverse64(&firstBitIndex, n - 1); - ASSERT(ret != 0); - return 1ull << (firstBitIndex + 1); -#else - return n <= 1 ? 1 : 1ull << (64 - __builtin_clzll(n - 1)); -#endif + return 1ull << (Log2(n - 1) + 1); } bool IsPowerOfTwo(uint64_t n) {