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;

View File

@ -25,8 +25,10 @@
// The following are not valid for 0
uint32_t ScanForward(uint32_t bits);
uint32_t Log2(uint32_t value);
uint32_t Log2(uint64_t value);
bool IsPowerOfTwo(size_t n);
uint64_t NextPowerOfTwo(uint64_t x);
bool IsPtrAligned(const void* ptr, size_t alignment);
void* AlignVoidPtr(void* ptr, size_t alignment);
bool IsAligned(uint32_t value, size_t alignment);

View File

@ -33,15 +33,18 @@ TEST(Math, ScanForward) {
// Tests for Log2
TEST(Math, Log2) {
// Test extrema
ASSERT_EQ(Log2(1), 0u);
ASSERT_EQ(Log2(0xFFFFFFFF), 31u);
ASSERT_EQ(Log2(1u), 0u);
ASSERT_EQ(Log2(0xFFFFFFFFu), 31u);
ASSERT_EQ(Log2(static_cast<uint64_t>(0xFFFFFFFFFFFFFFFF)), 63u);
// Test boundary between two logs
ASSERT_EQ(Log2(0x80000000), 31u);
ASSERT_EQ(Log2(0x7FFFFFFF), 30u);
ASSERT_EQ(Log2(0x80000000u), 31u);
ASSERT_EQ(Log2(0x7FFFFFFFu), 30u);
ASSERT_EQ(Log2(static_cast<uint64_t>(0x8000000000000000)), 63u);
ASSERT_EQ(Log2(static_cast<uint64_t>(0x7FFFFFFFFFFFFFFF)), 62u);
ASSERT_EQ(Log2(16), 4u);
ASSERT_EQ(Log2(15), 3u);
ASSERT_EQ(Log2(16u), 4u);
ASSERT_EQ(Log2(15u), 3u);
}
// Tests for IsPowerOfTwo
@ -54,6 +57,19 @@ TEST(Math, IsPowerOfTwo) {
ASSERT_FALSE(IsPowerOfTwo(0x8000400));
}
// Tests for NextPowerOfTwo
TEST(Math, NextPowerOfTwo) {
// Test extrema
ASSERT_EQ(NextPowerOfTwo(0), 1ull);
ASSERT_EQ(NextPowerOfTwo(0x7FFFFFFFFFFFFFFF), 0x8000000000000000);
// Test boundary between powers-of-two.
ASSERT_EQ(NextPowerOfTwo(31), 32ull);
ASSERT_EQ(NextPowerOfTwo(33), 64ull);
ASSERT_EQ(NextPowerOfTwo(32), 32ull);
}
// Tests for AlignPtr
TEST(Math, AlignPtr) {
constexpr size_t kTestAlignment = 8;