diff --git a/src/backend/CommandAllocator.cpp b/src/backend/CommandAllocator.cpp index 6ef87ad5d7..e665d196cb 100644 --- a/src/backend/CommandAllocator.cpp +++ b/src/backend/CommandAllocator.cpp @@ -154,7 +154,7 @@ namespace backend { CommandBlocks&& CommandAllocator::AcquireBlocks() { ASSERT(currentPtr != nullptr && endPtr != nullptr); - ASSERT(IsAligned(currentPtr, alignof(uint32_t))); + ASSERT(IsPtrAligned(currentPtr, alignof(uint32_t))); ASSERT(currentPtr + sizeof(uint32_t) <= endPtr); *reinterpret_cast(currentPtr) = EndOfBlock; @@ -169,7 +169,7 @@ namespace backend { ASSERT(commandId != EndOfBlock); // It should always be possible to allocate one id, for EndOfBlock tagging, - ASSERT(IsAligned(currentPtr, alignof(uint32_t))); + ASSERT(IsPtrAligned(currentPtr, alignof(uint32_t))); ASSERT(currentPtr + sizeof(uint32_t) <= endPtr); uint32_t* idAlloc = reinterpret_cast(currentPtr); diff --git a/src/common/Math.cpp b/src/common/Math.cpp index 1552c877ad..3632630b89 100644 --- a/src/common/Math.cpp +++ b/src/common/Math.cpp @@ -49,7 +49,7 @@ bool IsPowerOfTwo(size_t n) { return (n & (n - 1)) == 0; } -bool IsAligned(const void* ptr, size_t alignment) { +bool IsPtrAligned(const void* ptr, size_t alignment) { ASSERT(IsPowerOfTwo(alignment)); ASSERT(alignment != 0); return (reinterpret_cast(ptr) & (alignment - 1)) == 0; @@ -61,6 +61,14 @@ void* AlignVoidPtr(void* ptr, size_t alignment) { return reinterpret_cast((reinterpret_cast(ptr) + (alignment - 1)) & ~(alignment - 1)); } +bool IsAligned(uint32_t value, size_t alignment) { + ASSERT(alignment <= UINT32_MAX); + ASSERT(IsPowerOfTwo(alignment)); + ASSERT(alignment != 0); + uint32_t alignment32 = static_cast(alignment); + return (value & (alignment32 - 1)) == 0; +} + uint32_t Align(uint32_t value, size_t alignment) { ASSERT(alignment <= UINT32_MAX); ASSERT(IsPowerOfTwo(alignment)); diff --git a/src/common/Math.h b/src/common/Math.h index 486b8a4261..c71b531987 100644 --- a/src/common/Math.h +++ b/src/common/Math.h @@ -23,8 +23,9 @@ uint32_t ScanForward(uint32_t bits); uint32_t Log2(uint32_t value); bool IsPowerOfTwo(size_t n); -bool IsAligned(const void* ptr, size_t alignment); +bool IsPtrAligned(const void* ptr, size_t alignment); void* AlignVoidPtr(void* ptr, size_t alignment); +bool IsAligned(uint32_t value, size_t alignment); uint32_t Align(uint32_t value, size_t alignment); template diff --git a/src/tests/unittests/MathTests.cpp b/src/tests/unittests/MathTests.cpp index 569d0311e8..bdb361360d 100644 --- a/src/tests/unittests/MathTests.cpp +++ b/src/tests/unittests/MathTests.cpp @@ -94,8 +94,8 @@ TEST(Math, Align) { } } -// Tests for IsAligned -TEST(Math, IsAligned) { +// Tests for IsPtrAligned +TEST(Math, IsPtrAligned) { constexpr size_t kTestAlignment = 8; char buffer[kTestAlignment * 4]; @@ -104,6 +104,32 @@ TEST(Math, IsAligned) { char* unaligned = &buffer[i]; char* aligned = AlignPtr(unaligned, kTestAlignment); - ASSERT_EQ(IsAligned(unaligned, kTestAlignment), unaligned == aligned); + ASSERT_EQ(IsPtrAligned(unaligned, kTestAlignment), unaligned == aligned); + } +} + +// Tests for IsAligned +TEST(Math, IsAligned) { + // 0 is aligned + ASSERT_TRUE(IsAligned(0, 4)); + ASSERT_TRUE(IsAligned(0, 256)); + ASSERT_TRUE(IsAligned(0, 512)); + + // Multiples are aligned + ASSERT_TRUE(IsAligned(8, 8)); + ASSERT_TRUE(IsAligned(16, 8)); + ASSERT_TRUE(IsAligned(24, 8)); + ASSERT_TRUE(IsAligned(256, 256)); + ASSERT_TRUE(IsAligned(512, 256)); + ASSERT_TRUE(IsAligned(768, 256)); + + // Alignment with 1 is always aligned + for (uint32_t i = 0; i < 128; ++i) { + ASSERT_TRUE(IsAligned(i, 1)); + } + + // Everything in the range (align, 2*align) is not aligned + for (uint32_t i = 1; i < 64; ++i) { + ASSERT_FALSE(IsAligned(64 + i, 64)); } }