1
0
Fork 0

Common: remove a mod from AlignUp (#5441)

In cases where the size is not a known constant when inlining, AlignUp<std::size_t> currently generates two 64-bit div instructions.
This generates one div and a cmov which is significantly cheaper.
This commit is contained in:
Marshall Mohror 2020-07-07 16:39:23 -05:00 committed by GitHub
parent 835efd321e
commit bd7ee8c315
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 1 deletions

View File

@ -10,7 +10,9 @@ namespace Common {
template <typename T> template <typename T>
constexpr T AlignUp(T value, std::size_t size) { constexpr T AlignUp(T value, std::size_t size) {
static_assert(std::is_unsigned_v<T>, "T must be an unsigned value."); static_assert(std::is_unsigned_v<T>, "T must be an unsigned value.");
return static_cast<T>(value + (size - value % size) % size); auto mod{value % size};
value -= mod;
return static_cast<T>(mod == T{0} ? value : value + size);
} }
template <typename T> template <typename T>