Common: Add DivideRoundingUp function

Many callers of AlignUp are dividing immediately after aligning. Let's
add a helper function for this so callers don't have to specify the
alignment size twice.
This commit is contained in:
JosJuice committed 2026-08-22 11:52:22 +02:00
1 parent 82f7ec5390
commit f93a8a995b
10 files changed
+30 -22

No files matched your search

+7
View File
@@ -22,4 +22,11 @@ constexpr T AlignUp(T value, size_t size)
return AlignDown<T>(static_cast<T>(value + (size - 1)), size);
}
template <typename T>
constexpr T DivideRoundingUp(T numerator, size_t denominator)
{
static_assert(std::is_unsigned<T>(), "T must be an unsigned value.");
return static_cast<T>((numerator + (denominator - 1)) / denominator);
}
} // namespace Common
@@ -58,7 +58,7 @@ void LazyMemoryRegionBitSet::SetBits(size_t bits_start, size_t bits_end)
if (bits_start % 32 != 0)
m_pointer[bits_start / 32] |= extra_first_word;
const size_t start_index = Common::AlignUp(bits_start, 32) / 32;
const size_t start_index = Common::DivideRoundingUp(bits_start, 32);
const size_t end_index = bits_end / 32;
std::memset(m_pointer + start_index, 0xFF, (end_index - start_index) * 4);
@@ -85,7 +85,7 @@ void LazyMemoryRegionBitSet::ClearBits(size_t bits_start, size_t bits_end)
if (bits_start % 32 != 0)
m_pointer[bits_start / 32] &= extra_first_word;
const size_t start_index = Common::AlignUp(bits_start, 32) / 32;
const size_t start_index = Common::DivideRoundingUp(bits_start, 32);
const size_t end_index = bits_end / 32;
std::memset(m_pointer + start_index, 0x00, (end_index - start_index) * sizeof(u32));
+1 -1
View File
@@ -693,7 +693,7 @@ static u64 ComputeUsedClusters(const File::FSTEntry& parent_entry)
if (entry.isDirectory)
clusters += ComputeUsedClusters(entry);
else
clusters += Common::AlignUp(entry.size, CLUSTER_SIZE) / CLUSTER_SIZE;
clusters += Common::DivideRoundingUp(entry.size, CLUSTER_SIZE);
}
return clusters;
}
@@ -183,8 +183,7 @@ void JitBaseBlockCache::FinalizeBlock(JitBlock& block, bool block_link,
for (auto [range_start, range_end] : block.physical_addresses)
{
valid_block.SetBits(range_start / VALID_BLOCK_CACHE_LINE_SIZE,
Common::AlignUp(range_end, VALID_BLOCK_CACHE_LINE_SIZE) /
VALID_BLOCK_CACHE_LINE_SIZE);
Common::DivideRoundingUp(range_end, VALID_BLOCK_CACHE_LINE_SIZE));
for (u32 i = range_start & BLOCK_RANGE_MAP_MASK; i < range_end; i += BLOCK_RANGE_SIZE)
block_range_map[i].insert(&block);
+3 -1
View File
@@ -792,7 +792,9 @@ void DirectoryBlobReader::SetPartitionHeader(DirectoryBlobPartition* partition,
}
constexpr u32 PARTITION_HEADER_SIZE = 0x1c;
const u64 data_size = Common::AlignUp(partition->GetDataSize(), 0x7c00) / 0x7c00 * 0x8000;
const u64 data_size =
Common::DivideRoundingUp(partition->GetDataSize(), VolumeWii::BLOCK_DATA_SIZE) *
VolumeWii::BLOCK_TOTAL_SIZE;
std::vector<u8> partition_header(PARTITION_HEADER_SIZE);
Write32(static_cast<u32>(tmd_size), 0x0, &partition_header);
Write32(TMD_OFFSET >> 2, 0x4, &partition_header);
+8 -8
View File
@@ -641,8 +641,8 @@ WIARVZFileReader<RVZ>::Chunk::Chunk(File::DirectIOFile* file, u64 offset_in_file
m_rvz_packed_size(rvz_packed_size), m_data_offset(data_offset)
{
constexpr size_t MAX_SIZE_PER_EXCEPTION_LIST =
Common::AlignUp(VolumeWii::BLOCK_HEADER_SIZE, Common::SHA1::DIGEST_LEN) /
Common::SHA1::DIGEST_LEN * VolumeWii::BLOCKS_PER_GROUP * sizeof(HashExceptionEntry) +
Common::DivideRoundingUp(VolumeWii::BLOCK_HEADER_SIZE, Common::SHA1::DIGEST_LEN) *
VolumeWii::BLOCKS_PER_GROUP * sizeof(HashExceptionEntry) +
sizeof(u16);
m_out_bytes_allocated_for_exceptions =
@@ -909,7 +909,7 @@ void WIARVZFileReader<RVZ>::AddRawDataEntry(u64 offset, u64 size, int chunk_size
return;
const u32 group_index = *total_groups;
const u32 groups = static_cast<u32>(Common::AlignUp(size, chunk_size) / chunk_size);
const u32 groups = static_cast<u32>(Common::DivideRoundingUp(size, chunk_size));
*total_groups += groups;
data_entries->emplace_back(raw_data_entries->size());
@@ -924,7 +924,7 @@ typename WIARVZFileReader<RVZ>::PartitionDataEntry WIARVZFileReader<RVZ>::Create
{
const u32 group_index = *total_groups;
const u64 rounded_size = Common::AlignDown(size, VolumeWii::BLOCK_TOTAL_SIZE);
const u32 groups = static_cast<u32>(Common::AlignUp(rounded_size, chunk_size) / chunk_size);
const u32 groups = static_cast<u32>(Common::DivideRoundingUp(rounded_size, chunk_size));
*total_groups += groups;
data_entries->emplace_back(partition_entries.size(), index);
@@ -1334,8 +1334,8 @@ WIARVZFileReader<RVZ>::ProcessAndCompress(CompressThreadState* state, CompressPa
auto aes_context = Common::AES::CreateContextDecrypt(partition_entry.partition_key.data());
const u64 groups = Common::AlignUp(parameters.data.size(), VolumeWii::GROUP_TOTAL_SIZE) /
VolumeWii::GROUP_TOTAL_SIZE;
const u64 groups =
Common::DivideRoundingUp(parameters.data.size(), VolumeWii::GROUP_TOTAL_SIZE);
ASSERT(parameters.data.size() % VolumeWii::BLOCK_TOTAL_SIZE == 0);
const u64 blocks = parameters.data.size() / VolumeWii::BLOCK_TOTAL_SIZE;
@@ -1344,7 +1344,7 @@ WIARVZFileReader<RVZ>::ProcessAndCompress(CompressThreadState* state, CompressPa
exception_lists_per_chunk * VolumeWii::BLOCKS_PER_GROUP :
VolumeWii::BLOCKS_PER_GROUP / chunks_per_wii_group;
const u64 chunks = Common::AlignUp(blocks, blocks_per_chunk) / blocks_per_chunk;
const u64 chunks = Common::DivideRoundingUp(blocks, blocks_per_chunk);
const u64 in_data_per_chunk = blocks_per_chunk * VolumeWii::BLOCK_TOTAL_SIZE;
const u64 out_data_per_chunk = blocks_per_chunk * VolumeWii::BLOCK_DATA_SIZE;
@@ -1932,7 +1932,7 @@ WIARVZFileReader<RVZ>::Convert(BlobReader* infile, const VolumeDisc* infile_volu
data_offset_in_partition += bytes_to_read;
}
groups_processed += Common::AlignUp(bytes_to_read, chunk_size) / chunk_size;
groups_processed += Common::DivideRoundingUp(bytes_to_read, chunk_size);
}
ASSERT(data_size == 0);
+4 -4
View File
@@ -1370,16 +1370,16 @@ void MenuBar::CheckNAND()
const QString user_cluster_message =
tr("The user-accessible part of your NAND contains %1 blocks (%2 KiB) "
"of data, out of an allowed maximum of %3 blocks (%4 KiB).")
.arg(Common::AlignUp(result.used_clusters_user, IOS::HLE::FS::CLUSTERS_PER_BLOCK) /
IOS::HLE::FS::CLUSTERS_PER_BLOCK)
.arg(Common::DivideRoundingUp(result.used_clusters_user,
IOS::HLE::FS::CLUSTERS_PER_BLOCK))
.arg((result.used_clusters_user * IOS::HLE::FS::CLUSTER_SIZE) / 1024)
.arg(IOS::HLE::FS::USER_CLUSTERS / IOS::HLE::FS::CLUSTERS_PER_BLOCK)
.arg((IOS::HLE::FS::USER_CLUSTERS * IOS::HLE::FS::CLUSTER_SIZE) / 1024);
const QString system_cluster_message =
tr("The system-reserved part of your NAND contains %1 blocks (%2 KiB) "
"of data, out of an allowed maximum of %3 blocks (%4 KiB).")
.arg(Common::AlignUp(result.used_clusters_system, IOS::HLE::FS::CLUSTERS_PER_BLOCK) /
IOS::HLE::FS::CLUSTERS_PER_BLOCK)
.arg(Common::DivideRoundingUp(result.used_clusters_system,
IOS::HLE::FS::CLUSTERS_PER_BLOCK))
.arg((result.used_clusters_system * IOS::HLE::FS::CLUSTER_SIZE) / 1024)
.arg(IOS::HLE::FS::SYSTEM_CLUSTERS / IOS::HLE::FS::CLUSTERS_PER_BLOCK)
.arg((IOS::HLE::FS::SYSTEM_CLUSTERS * IOS::HLE::FS::CLUSTER_SIZE) / 1024);
@@ -138,8 +138,8 @@ std::unique_ptr<VKShader> VKShader::CreateFromSource(ShaderStage stage, std::str
std::unique_ptr<VKShader> VKShader::CreateFromBinary(ShaderStage stage, const void* data,
size_t length, std::string_view name)
{
const size_t size_in_words = Common::AlignUp(length, sizeof(ShaderCompiler::SPIRVCodeType)) /
sizeof(ShaderCompiler::SPIRVCodeType);
const size_t size_in_words =
Common::DivideRoundingUp(length, sizeof(ShaderCompiler::SPIRVCodeType));
ShaderCompiler::SPIRVCodeVector spv(size_in_words);
if (length > 0)
std::memcpy(spv.data(), data, length);
@@ -378,7 +378,7 @@ void VKTexture::Load(u32 level, u32 width, u32 height, u32 row_length, const u8*
// that lie outside of the texture's dimensions.
const u32 upload_alignment = static_cast<u32>(g_vulkan_context->GetBufferImageGranularity());
const u32 block_size = GetBlockSizeForFormat(GetFormat());
const u32 num_rows = Common::AlignUp(height, block_size) / block_size;
const u32 num_rows = Common::DivideRoundingUp(height, block_size);
const u32 source_pitch = CalculateStrideForFormat(m_config.format, row_length);
const u32 upload_size = source_pitch * num_rows;
std::unique_ptr<StagingBuffer> temp_buffer;
@@ -166,7 +166,7 @@ struct DDSLoadInfo
static constexpr u32 GetBlockCount(u32 extent, u32 block_size)
{
return std::max(Common::AlignUp(extent, block_size) / block_size, 1u);
return std::max(Common::DivideRoundingUp(extent, block_size), 1u);
}
static u32 CalculateMipCount(u32 width, u32 height)