From 7cb859d3a0f4a4131c6c2936e4686d976336eebb Mon Sep 17 00:00:00 2001 From: Adam Sawicki Date: Mon, 6 Jul 2020 17:39:20 +0200 Subject: [PATCH] Improvement in VmaAlignUp, VmaAlignDown --- src/vk_mem_alloc.h | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/src/vk_mem_alloc.h b/src/vk_mem_alloc.h index 893c6b2..a880c10 100644 --- a/src/vk_mem_alloc.h +++ b/src/vk_mem_alloc.h @@ -4243,28 +4243,6 @@ static inline uint32_t VmaCountBitsSet(uint32_t v) return c; } -// Aligns given value up to nearest multiply of align value. For example: VmaAlignUp(11, 8) = 16. -// Use types like uint32_t, uint64_t as T. -template -static inline T VmaAlignUp(T val, T align) -{ - return (val + align - 1) / align * align; -} -// Aligns given value down to nearest multiply of align value. For example: VmaAlignUp(11, 8) = 8. -// Use types like uint32_t, uint64_t as T. -template -static inline T VmaAlignDown(T val, T align) -{ - return val / align * align; -} - -// Division with mathematical rounding to nearest number. -template -static inline T VmaRoundDiv(T x, T y) -{ - return (x + (y / (T)2)) / y; -} - /* Returns true if given number is a power of two. T must be unsigned integer number or signed integer but always nonnegative. @@ -4276,6 +4254,30 @@ inline bool VmaIsPow2(T x) return (x & (x-1)) == 0; } +// Aligns given value up to nearest multiply of align value. For example: VmaAlignUp(11, 8) = 16. +// Use types like uint32_t, uint64_t as T. +template +static inline T VmaAlignUp(T val, T alignment) +{ + VMA_HEAVY_ASSERT(VmaIsPow2(alignment)); + return (val + alignment - 1) & ~(alignment - 1); +} +// Aligns given value down to nearest multiply of align value. For example: VmaAlignUp(11, 8) = 8. +// Use types like uint32_t, uint64_t as T. +template +static inline T VmaAlignDown(T val, T alignment) +{ + VMA_HEAVY_ASSERT(VmaIsPow2(alignment)); + return val & ~(alignment - 1); +} + +// Division with mathematical rounding to nearest number. +template +static inline T VmaRoundDiv(T x, T y) +{ + return (x + (y / (T)2)) / y; +} + // Returns smallest power of 2 greater or equal to v. static inline uint32_t VmaNextPow2(uint32_t v) {