alignment.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // SPDX-FileCopyrightText: 2014 Jannik Vogel <email@jannikvogel.de>
  2. // SPDX-License-Identifier: CC0-1.0
  3. #pragma once
  4. #include <cstddef>
  5. #include <new>
  6. #include <type_traits>
  7. namespace Common {
  8. template <typename T>
  9. requires std::is_unsigned_v<T>
  10. [[nodiscard]] constexpr T AlignUp(T value, size_t size) {
  11. auto mod{static_cast<T>(value % size)};
  12. value -= mod;
  13. return static_cast<T>(mod == T{0} ? value : value + size);
  14. }
  15. template <typename T>
  16. requires std::is_unsigned_v<T>
  17. [[nodiscard]] constexpr T AlignUpLog2(T value, size_t align_log2) {
  18. return static_cast<T>((value + ((1ULL << align_log2) - 1)) >> align_log2 << align_log2);
  19. }
  20. template <typename T>
  21. requires std::is_unsigned_v<T>
  22. [[nodiscard]] constexpr T AlignDown(T value, size_t size) {
  23. return static_cast<T>(value - value % size);
  24. }
  25. template <typename T>
  26. requires std::is_unsigned_v<T>
  27. [[nodiscard]] constexpr bool Is4KBAligned(T value) {
  28. return (value & 0xFFF) == 0;
  29. }
  30. template <typename T>
  31. requires std::is_unsigned_v<T>
  32. [[nodiscard]] constexpr bool IsWordAligned(T value) {
  33. return (value & 0b11) == 0;
  34. }
  35. template <typename T>
  36. requires std::is_integral_v<T>
  37. [[nodiscard]] constexpr bool IsAligned(T value, size_t alignment) {
  38. using U = typename std::make_unsigned_t<T>;
  39. const U mask = static_cast<U>(alignment - 1);
  40. return (value & mask) == 0;
  41. }
  42. template <typename T, typename U>
  43. requires std::is_integral_v<T>
  44. [[nodiscard]] constexpr T DivideUp(T x, U y) {
  45. return (x + (y - 1)) / y;
  46. }
  47. template <typename T, size_t Align = 16>
  48. class AlignmentAllocator {
  49. public:
  50. using value_type = T;
  51. using size_type = size_t;
  52. using difference_type = ptrdiff_t;
  53. using propagate_on_container_copy_assignment = std::true_type;
  54. using propagate_on_container_move_assignment = std::true_type;
  55. using propagate_on_container_swap = std::true_type;
  56. using is_always_equal = std::false_type;
  57. constexpr AlignmentAllocator() noexcept = default;
  58. template <typename T2>
  59. constexpr AlignmentAllocator(const AlignmentAllocator<T2, Align>&) noexcept {}
  60. [[nodiscard]] T* allocate(size_type n) {
  61. return static_cast<T*>(::operator new (n * sizeof(T), std::align_val_t{Align}));
  62. }
  63. void deallocate(T* p, size_type n) {
  64. ::operator delete (p, n * sizeof(T), std::align_val_t{Align});
  65. }
  66. template <typename T2>
  67. struct rebind {
  68. using other = AlignmentAllocator<T2, Align>;
  69. };
  70. template <typename T2, size_t Align2>
  71. constexpr bool operator==(const AlignmentAllocator<T2, Align2>&) const noexcept {
  72. return std::is_same_v<T, T2> && Align == Align2;
  73. }
  74. };
  75. } // namespace Common