Add IsAligned helper for values

This commit is contained in:
Austin Eng 2017-08-17 14:02:16 -04:00 committed by Austin Eng
parent d37ec9af92
commit ae48c95306
4 changed files with 42 additions and 7 deletions

View File

@ -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<uint32_t*>(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<uint32_t*>(currentPtr);

View File

@ -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<size_t>(ptr) & (alignment - 1)) == 0;
@ -61,6 +61,14 @@ void* AlignVoidPtr(void* ptr, size_t alignment) {
return reinterpret_cast<void*>((reinterpret_cast<size_t>(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<uint32_t>(alignment);
return (value & (alignment32 - 1)) == 0;
}
uint32_t Align(uint32_t value, size_t alignment) {
ASSERT(alignment <= UINT32_MAX);
ASSERT(IsPowerOfTwo(alignment));

View File

@ -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<typename T>

View File

@ -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));
}
}