common/div_ceil: Return numerator type

Fixes instances where DivCeil(u32, u64) would surprisingly return u64,
instead of the more natural u32.
This commit is contained in:
ReinUsesLisp 2021-01-09 03:14:18 -03:00
parent 8eea7c1176
commit c190586597

View File

@ -11,16 +11,16 @@ namespace Common {
/// Ceiled integer division. /// Ceiled integer division.
template <typename N, typename D> template <typename N, typename D>
requires std::is_integral_v<N>&& std::is_unsigned_v<D>[[nodiscard]] constexpr auto DivCeil( requires std::is_integral_v<N>&& std::is_unsigned_v<D>[[nodiscard]] constexpr N DivCeil(N number,
N number, D divisor) { D divisor) {
return (static_cast<D>(number) + divisor - 1) / divisor; return static_cast<N>((static_cast<D>(number) + divisor - 1) / divisor);
} }
/// Ceiled integer division with logarithmic divisor in base 2 /// Ceiled integer division with logarithmic divisor in base 2
template <typename N, typename D> template <typename N, typename D>
requires std::is_integral_v<N>&& std::is_unsigned_v<D>[[nodiscard]] constexpr auto DivCeilLog2( requires std::is_integral_v<N>&& std::is_unsigned_v<D>[[nodiscard]] constexpr N DivCeilLog2(
N value, D alignment_log2) { N value, D alignment_log2) {
return (static_cast<D>(value) + (D(1) << alignment_log2) - 1) >> alignment_log2; return static_cast<N>((static_cast<D>(value) + (D(1) << alignment_log2) - 1) >> alignment_log2);
} }
} // namespace Common } // namespace Common