Fix NextPowerOfTwo for 64-bit MSVC
This test was failing consistently on win-msvc-dbg and occasionally
on win-msvc-rel. It's suspected the hardware does not properly
support __lzcnt64 so the function is implemented with _BitScanReverse64.
Bug: dawn:213
Change-Id: I0712f87787aad4aad7233bfb72846ec3dba96239
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/10481
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
Commit-Queue: Austin Eng <enga@chromium.org>
diff --git a/src/common/Math.cpp b/src/common/Math.cpp
index 3258fc3..1eeeb79 100644
--- a/src/common/Math.cpp
+++ b/src/common/Math.cpp
@@ -61,7 +61,14 @@
uint64_t NextPowerOfTwo(uint64_t n) {
#if defined(DAWN_COMPILER_MSVC)
- return n <= 1 ? 1 : 1ull << (64 - __lzcnt64(n - 1));
+ 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