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 <cwallez@chromium.org>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
diff --git a/src/common/Math.cpp b/src/common/Math.cpp
index 2ae977b..4471eb7 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 <algorithm>
#include <cmath>
@@ -50,28 +51,31 @@
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<uint32_t>(__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) {