div_ceil.h 785 B

1234567891011121314151617181920212223242526
  1. // Copyright 2020 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <cstddef>
  6. #include <type_traits>
  7. namespace Common {
  8. /// Ceiled integer division.
  9. template <typename N, typename D>
  10. requires std::is_integral_v<N>&& std::is_unsigned_v<D>[[nodiscard]] constexpr auto DivCeil(
  11. N number, D divisor) {
  12. return (static_cast<D>(number) + divisor - 1) / divisor;
  13. }
  14. /// Ceiled integer division with logarithmic divisor in base 2
  15. template <typename N, typename D>
  16. requires std::is_integral_v<N>&& std::is_unsigned_v<D>[[nodiscard]] constexpr auto DivCeilLog2(
  17. N value, D alignment_log2) {
  18. return (static_cast<D>(value) + (D(1) << alignment_log2) - 1) >> alignment_log2;
  19. }
  20. } // namespace Common