2022-03-18 12:17:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "CoreTypes.h"
|
|
|
|
#include "TypeTraits/TypeTraits.h"
|
|
|
|
|
|
|
|
NAMESPACE_REDCRAFT_BEGIN
|
|
|
|
NAMESPACE_MODULE_BEGIN(Redcraft)
|
|
|
|
NAMESPACE_MODULE_BEGIN(Utility)
|
|
|
|
|
|
|
|
NAMESPACE_BEGIN(Memory)
|
|
|
|
|
2022-04-10 12:14:46 +00:00
|
|
|
template <typename T> requires TIsIntegral<T>::Value || TIsPointer<T>::Value
|
2022-03-18 12:17:28 +00:00
|
|
|
FORCEINLINE constexpr T Align(T InValue, size_t Alignment)
|
|
|
|
{
|
2022-04-08 09:29:05 +00:00
|
|
|
return (T)(((uintptr)(InValue) + static_cast<uintptr>(Alignment) - 1) & ~(static_cast<uintptr>(Alignment) - 1));
|
2022-03-18 12:17:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-10 12:14:46 +00:00
|
|
|
template <typename T> requires TIsIntegral<T>::Value || TIsPointer<T>::Value
|
2022-03-18 12:17:28 +00:00
|
|
|
FORCEINLINE constexpr T AlignDown(T InValue, size_t Alignment)
|
|
|
|
{
|
2022-04-08 09:29:05 +00:00
|
|
|
return (T)((uintptr)(InValue) & ~(static_cast<uintptr>(Alignment) - 1));
|
2022-03-18 12:17:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-10 12:14:46 +00:00
|
|
|
template <typename T> requires TIsIntegral<T>::Value || TIsPointer<T>::Value
|
2022-03-18 12:17:28 +00:00
|
|
|
FORCEINLINE constexpr T AlignArbitrary(T InValue, size_t Alignment)
|
|
|
|
{
|
2022-04-08 09:29:05 +00:00
|
|
|
return (T)((((uintptr)(InValue) + static_cast<uintptr>(Alignment) - 1) / static_cast<uintptr>(Alignment)) * static_cast<uintptr>(Alignment));
|
2022-03-18 12:17:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-10 12:14:46 +00:00
|
|
|
template <typename T> requires TIsIntegral<T>::Value || TIsPointer<T>::Value
|
2022-03-18 12:17:28 +00:00
|
|
|
FORCEINLINE constexpr bool IsAligned(T InValue, size_t Alignment)
|
|
|
|
{
|
2022-04-08 09:29:05 +00:00
|
|
|
return !((uintptr)(InValue) & (static_cast<uintptr>(Alignment) - 1));
|
2022-03-18 12:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NAMESPACE_END(Memory)
|
|
|
|
|
|
|
|
NAMESPACE_MODULE_END(Utility)
|
|
|
|
NAMESPACE_MODULE_END(Redcraft)
|
|
|
|
NAMESPACE_REDCRAFT_END
|