From bd7ee8c315760b3e91a490254370bd3ccfb24bdc Mon Sep 17 00:00:00 2001 From: Marshall Mohror Date: Tue, 7 Jul 2020 16:39:23 -0500 Subject: [PATCH] Common: remove a mod from AlignUp (#5441) In cases where the size is not a known constant when inlining, AlignUp currently generates two 64-bit div instructions. This generates one div and a cmov which is significantly cheaper. --- src/common/alignment.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/common/alignment.h b/src/common/alignment.h index 225770fab..92dec59c1 100644 --- a/src/common/alignment.h +++ b/src/common/alignment.h @@ -10,7 +10,9 @@ namespace Common { template constexpr T AlignUp(T value, std::size_t size) { static_assert(std::is_unsigned_v, "T must be an unsigned value."); - return static_cast(value + (size - value % size) % size); + auto mod{value % size}; + value -= mod; + return static_cast(mod == T{0} ? value : value + size); } template