Resource Management 1: Math ops

Support 64-bit log2 and power-of-two alignment.

BUG=dawn:27

Change-Id: I2d254e5dda9626a6e26017b0d8e33f5db4c9298d
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/9224
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Bryan Bernhart <bryan.bernhart@intel.com>
This commit is contained in:
Bryan Bernhart
2019-07-22 21:42:18 +00:00
committed by Commit Bot service account
parent 0e338ff986
commit 40793232d8
3 changed files with 44 additions and 6 deletions

View File

@@ -47,6 +47,26 @@ uint32_t Log2(uint32_t value) {
#endif
}
uint32_t Log2(uint64_t value) {
ASSERT(value != 0);
#if defined(DAWN_COMPILER_MSVC)
unsigned long firstBitIndex = 0ul;
unsigned char ret = _BitScanReverse64(&firstBitIndex, value);
ASSERT(ret != 0);
return firstBitIndex;
#else
return 63 - static_cast<uint32_t>(__builtin_clzll(value));
#endif
}
uint64_t NextPowerOfTwo(uint64_t x) {
#if defined(DAWN_COMPILER_MSVC)
return x == 1 ? 1 : 1ull << (64 - __lzcnt64(x - 1));
#else
return x == 1 ? 1 : 1ull << (64 - __builtin_clzll(x - 1));
#endif
}
bool IsPowerOfTwo(size_t n) {
ASSERT(n != 0);
return (n & (n - 1)) == 0;