#pragma once #include "yassert.h" #include "defaults.h" #include template static inline T AlignDown(T len, T align) noexcept { Y_ASSERT(IsPowerOf2(align)); // align should be power of 2 return len & ~(align - 1); } template 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 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 static inline T* AlignUp(T* ptr, size_t align) noexcept { return (T*)AlignUp((uintptr_t)ptr, align); } template static inline T* AlignDown(T* ptr, size_t align) noexcept { return (T*)AlignDown((uintptr_t)ptr, align); } template static inline T AlignUp(T t) noexcept { return AlignUp(t, (size_t)PLATFORM_DATA_ALIGN); } template static inline T AlignDown(T t) noexcept { return AlignDown(t, (size_t)PLATFORM_DATA_ALIGN); } template static inline T Align(T t) noexcept { return AlignUp(t); }