diff options
Diffstat (limited to 'src/common/alignment.h')
| -rw-r--r-- | src/common/alignment.h | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/common/alignment.h b/src/common/alignment.h new file mode 100644 index 000000000..b77da4a92 --- /dev/null +++ b/src/common/alignment.h | |||
| @@ -0,0 +1,22 @@ | |||
| 1 | // This file is under the public domain. | ||
| 2 | |||
| 3 | #pragma once | ||
| 4 | |||
| 5 | #include <cstddef> | ||
| 6 | #include <type_traits> | ||
| 7 | |||
| 8 | namespace Common { | ||
| 9 | |||
| 10 | template <typename T> | ||
| 11 | constexpr T AlignUp(T value, size_t size) { | ||
| 12 | static_assert(std::is_unsigned<T>::value, "T must be an unsigned value."); | ||
| 13 | return static_cast<T>(value + (size - value % size) % size); | ||
| 14 | } | ||
| 15 | |||
| 16 | template <typename T> | ||
| 17 | constexpr T AlignDown(T value, size_t size) { | ||
| 18 | static_assert(std::is_unsigned<T>::value, "T must be an unsigned value."); | ||
| 19 | return static_cast<T>(value - value % size); | ||
| 20 | } | ||
| 21 | |||
| 22 | } // namespace Common | ||