summaryrefslogtreecommitdiff
path: root/src/common/alignment.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/alignment.h')
-rw-r--r--src/common/alignment.h28
1 files changed, 20 insertions, 8 deletions
diff --git a/src/common/alignment.h b/src/common/alignment.h
index 32d796ffa..8570c7d3c 100644
--- a/src/common/alignment.h
+++ b/src/common/alignment.h
@@ -9,41 +9,48 @@
9namespace Common { 9namespace Common {
10 10
11template <typename T> 11template <typename T>
12requires std::is_unsigned_v<T>[[nodiscard]] constexpr T AlignUp(T value, size_t size) { 12requires std::is_unsigned_v<T>
13[[nodiscard]] constexpr T AlignUp(T value, size_t size) {
13 auto mod{static_cast<T>(value % size)}; 14 auto mod{static_cast<T>(value % size)};
14 value -= mod; 15 value -= mod;
15 return static_cast<T>(mod == T{0} ? value : value + size); 16 return static_cast<T>(mod == T{0} ? value : value + size);
16} 17}
17 18
18template <typename T> 19template <typename T>
19requires std::is_unsigned_v<T>[[nodiscard]] constexpr T AlignUpLog2(T value, size_t align_log2) { 20requires std::is_unsigned_v<T>
21[[nodiscard]] constexpr T AlignUpLog2(T value, size_t align_log2) {
20 return static_cast<T>((value + ((1ULL << align_log2) - 1)) >> align_log2 << align_log2); 22 return static_cast<T>((value + ((1ULL << align_log2) - 1)) >> align_log2 << align_log2);
21} 23}
22 24
23template <typename T> 25template <typename T>
24requires std::is_unsigned_v<T>[[nodiscard]] constexpr T AlignDown(T value, size_t size) { 26requires std::is_unsigned_v<T>
27[[nodiscard]] constexpr T AlignDown(T value, size_t size) {
25 return static_cast<T>(value - value % size); 28 return static_cast<T>(value - value % size);
26} 29}
27 30
28template <typename T> 31template <typename T>
29requires std::is_unsigned_v<T>[[nodiscard]] constexpr bool Is4KBAligned(T value) { 32requires std::is_unsigned_v<T>
33[[nodiscard]] constexpr bool Is4KBAligned(T value) {
30 return (value & 0xFFF) == 0; 34 return (value & 0xFFF) == 0;
31} 35}
32 36
33template <typename T> 37template <typename T>
34requires std::is_unsigned_v<T>[[nodiscard]] constexpr bool IsWordAligned(T value) { 38requires std::is_unsigned_v<T>
39[[nodiscard]] constexpr bool IsWordAligned(T value) {
35 return (value & 0b11) == 0; 40 return (value & 0b11) == 0;
36} 41}
37 42
38template <typename T> 43template <typename T>
39requires std::is_integral_v<T>[[nodiscard]] constexpr bool IsAligned(T value, size_t alignment) { 44requires std::is_integral_v<T>
45[[nodiscard]] constexpr bool IsAligned(T value, size_t alignment) {
40 using U = typename std::make_unsigned_t<T>; 46 using U = typename std::make_unsigned_t<T>;
41 const U mask = static_cast<U>(alignment - 1); 47 const U mask = static_cast<U>(alignment - 1);
42 return (value & mask) == 0; 48 return (value & mask) == 0;
43} 49}
44 50
45template <typename T, typename U> 51template <typename T, typename U>
46requires std::is_integral_v<T>[[nodiscard]] constexpr T DivideUp(T x, U y) { 52requires std::is_integral_v<T>
53[[nodiscard]] constexpr T DivideUp(T x, U y) {
47 return (x + (y - 1)) / y; 54 return (x + (y - 1)) / y;
48} 55}
49 56
@@ -57,7 +64,7 @@ public:
57 using propagate_on_container_copy_assignment = std::true_type; 64 using propagate_on_container_copy_assignment = std::true_type;
58 using propagate_on_container_move_assignment = std::true_type; 65 using propagate_on_container_move_assignment = std::true_type;
59 using propagate_on_container_swap = std::true_type; 66 using propagate_on_container_swap = std::true_type;
60 using is_always_equal = std::true_type; 67 using is_always_equal = std::false_type;
61 68
62 constexpr AlignmentAllocator() noexcept = default; 69 constexpr AlignmentAllocator() noexcept = default;
63 70
@@ -76,6 +83,11 @@ public:
76 struct rebind { 83 struct rebind {
77 using other = AlignmentAllocator<T2, Align>; 84 using other = AlignmentAllocator<T2, Align>;
78 }; 85 };
86
87 template <typename T2, size_t Align2>
88 constexpr bool operator==(const AlignmentAllocator<T2, Align2>&) const noexcept {
89 return std::is_same_v<T, T2> && Align == Align2;
90 }
79}; 91};
80 92
81} // namespace Common 93} // namespace Common