Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/gpgmm/common/BuddyMemoryAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace gpgmm {
GPGMM_RETURN_ERROR_IF(!ValidateRequest(request));

// Round allocation size to nearest power-of-two.
const uint64_t allocationSize = NextPowerOfTwo(request.SizeInBytes);
const uint64_t allocationSize = UpperPowerOfTwo(request.SizeInBytes);

// Request cannot exceed memory size.
GPGMM_RETURN_ERROR_IF(allocationSize > mMemorySize);
Expand Down
2 changes: 1 addition & 1 deletion src/gpgmm/common/SlabMemoryAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ namespace gpgmm {
slabSize *= 2;
}

slabSize = NextPowerOfTwo(slabSize);
slabSize = UpperPowerOfTwo(slabSize);

// If the larger slab excceeds available memory, re-use a slab instead.
// Otherwise, creating a larger slab will page-out smaller slabs.
Expand Down
8 changes: 4 additions & 4 deletions src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -728,16 +728,16 @@ namespace gpgmm::d3d12 {
case ALLOCATOR_ALGORITHM_BUDDY_SYSTEM: {
// System and memory size must be aligned at creation-time.
return std::make_unique<BuddyMemoryAllocator>(
/*systemSize*/ PrevPowerOfTwo(mMaxResourceHeapSize),
/*memorySize*/ NextPowerOfTwo(memorySize),
/*systemSize*/ LowerPowerOfTwo(mMaxResourceHeapSize),
/*memorySize*/ UpperPowerOfTwo(memorySize),
/*memoryAlignment*/ memoryAlignment,
/*memoryAllocator*/ std::move(underlyingAllocator));
}
case ALLOCATOR_ALGORITHM_SLAB: {
// Min slab size is always equal to the memory size because the
// slab allocator aligns the slab size at allocate-time.
return std::make_unique<SlabCacheAllocator>(
/*maxSlabSize*/ PrevPowerOfTwo(mMaxResourceHeapSize),
/*maxSlabSize*/ LowerPowerOfTwo(mMaxResourceHeapSize),
/*minSlabSize*/ memorySize,
/*slabAlignment*/ memoryAlignment,
/*slabFragmentationLimit*/ memoryFragmentationLimit,
Expand Down Expand Up @@ -1170,7 +1170,7 @@ namespace gpgmm::d3d12 {
// Only constant buffers must be 256B aligned.
request.Alignment = (initialResourceState == D3D12_RESOURCE_STATE_GENERIC_READ)
? D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT
: NextPowerOfTwo(newResourceDesc.Width);
: UpperPowerOfTwo(newResourceDesc.Width);
} else {
request.Alignment = resourceDescriptor.Alignment;
}
Expand Down
4 changes: 2 additions & 2 deletions src/gpgmm/utils/Math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ namespace gpgmm {
#endif // defined(GPGMM_COMPILER_MSVC)
}

uint64_t NextPowerOfTwo(uint64_t number) {
uint64_t UpperPowerOfTwo(uint64_t number) {
if (number <= 1) {
return 1;
}

return 1ull << (Log2(number - 1) + 1);
}

uint64_t PrevPowerOfTwo(uint64_t number) {
uint64_t LowerPowerOfTwo(uint64_t number) {
ASSERT(number != 0);
return 1ull << Log2(number);
}
Expand Down
4 changes: 2 additions & 2 deletions src/gpgmm/utils/Math.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ namespace gpgmm {
uint32_t ScanForward(uint32_t bits);
uint32_t Log2(uint32_t number);
uint32_t Log2(uint64_t number);
uint64_t PrevPowerOfTwo(uint64_t number);
uint64_t LowerPowerOfTwo(uint64_t number);

bool IsPowerOfTwo(uint64_t number);
uint64_t NextPowerOfTwo(uint64_t number);
uint64_t UpperPowerOfTwo(uint64_t number);
bool IsAligned(uint64_t number, size_t multiple);

// Aligns number to the nearest power-of-two multiple.
Expand Down
14 changes: 7 additions & 7 deletions src/tests/unittests/MathTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ TEST(MathTests, IsPowerOfTwo) {
EXPECT_FALSE(IsPowerOfTwo(3u));
}

TEST(MathTests, PrevPowerOfTwo) {
TEST(MathTests, LowerPowerOfTwo) {
// Check number from POT.
EXPECT_EQ(PrevPowerOfTwo(1u), 1u);
EXPECT_EQ(PrevPowerOfTwo(2u), 2u);
EXPECT_EQ(LowerPowerOfTwo(1u), 1u);
EXPECT_EQ(LowerPowerOfTwo(2u), 2u);

// Check number from NPOT.
EXPECT_EQ(PrevPowerOfTwo(3u), 2u);
EXPECT_EQ(PrevPowerOfTwo(9u), 8u);
EXPECT_EQ(PrevPowerOfTwo(15u), 8u);
EXPECT_EQ(LowerPowerOfTwo(3u), 2u);
EXPECT_EQ(LowerPowerOfTwo(9u), 8u);
EXPECT_EQ(LowerPowerOfTwo(15u), 8u);

EXPECT_EQ(PrevPowerOfTwo((2ull << 31) + 1), 2ull << 31);
EXPECT_EQ(LowerPowerOfTwo((2ull << 31) + 1), 2ull << 31);
}

TEST(MathTests, IsAligned) {
Expand Down