diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/system/align.h | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/system/align.h')
-rw-r--r-- | util/system/align.h | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/util/system/align.h b/util/system/align.h new file mode 100644 index 0000000000..ea0bbc5b46 --- /dev/null +++ b/util/system/align.h @@ -0,0 +1,49 @@ +#pragma once + +#include "yassert.h" +#include "defaults.h" +#include <util/generic/bitops.h> + +template <class T> +static inline T AlignDown(T len, T align) noexcept { + Y_ASSERT(IsPowerOf2(align)); // align should be power of 2 + return len & ~(align - 1); +} + +template <class T> +static inline T AlignUp(T len, T align) noexcept { + const T alignedResult = AlignDown(len + (align - 1), align); + Y_ASSERT(alignedResult >= len); // check for overflow + return alignedResult; +} + +template <class T> +static inline T AlignUpSpace(T len, T align) noexcept { + Y_ASSERT(IsPowerOf2(align)); // align should be power of 2 + return ((T)0 - len) & (align - 1); // AlignUp(len, align) - len; +} + +template <class T> +static inline T* AlignUp(T* ptr, size_t align) noexcept { + return (T*)AlignUp((uintptr_t)ptr, align); +} + +template <class T> +static inline T* AlignDown(T* ptr, size_t align) noexcept { + return (T*)AlignDown((uintptr_t)ptr, align); +} + +template <class T> +static inline T AlignUp(T t) noexcept { + return AlignUp(t, (size_t)PLATFORM_DATA_ALIGN); +} + +template <class T> +static inline T AlignDown(T t) noexcept { + return AlignDown(t, (size_t)PLATFORM_DATA_ALIGN); +} + +template <class T> +static inline T Align(T t) noexcept { + return AlignUp(t); +} |