Simplify utils::align() a bit

std::is_unsigned implies std::is_integral I believe.
This commit is contained in:
Nekotekina 2021-02-19 16:18:09 +03:00
parent 73d45f3bf0
commit 9dc238187f

View File

@ -366,24 +366,24 @@ namespace utils
}
// Align to power of 2
template <typename T, typename = std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value>>
constexpr T align(T value, ullong align)
template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
constexpr T align(T value, std::type_identity_t<T> align)
{
return static_cast<T>((value + (align - 1)) & (0 - align));
return static_cast<T>((value + (align - 1)) & (T{0} - align));
}
// General purpose aligned division, the result is rounded up not truncated
template <typename T, typename = std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value>>
template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
constexpr T aligned_div(T value, std::type_identity_t<T> align)
{
return static_cast<T>(value / align + T{!!(value % align)});
}
// General purpose aligned division, the result is rounded to nearest
template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
constexpr T rounded_div(T value, std::type_identity_t<T> align)
{
if constexpr (std::is_unsigned<T>::value)
if constexpr (std::is_unsigned_v<T>)
{
return static_cast<T>(value / align + T{(value % align) > (align / 2)});
}