2022-03-18 12:17:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "CoreTypes.h"
|
|
|
|
#include "TypeTraits/TypeTraits.h"
|
2022-04-27 14:50:56 +00:00
|
|
|
#include "Miscellaneous/AssertionMacros.h"
|
2022-03-18 12:17:28 +00:00
|
|
|
|
|
|
|
NAMESPACE_REDCRAFT_BEGIN
|
|
|
|
NAMESPACE_MODULE_BEGIN(Redcraft)
|
|
|
|
NAMESPACE_MODULE_BEGIN(Utility)
|
|
|
|
|
|
|
|
NAMESPACE_BEGIN(Memory)
|
|
|
|
|
2022-04-27 14:50:56 +00:00
|
|
|
FORCEINLINE constexpr bool IsValidAlignment(size_t Alignment) { return !(Alignment & (Alignment - 1)); }
|
|
|
|
|
2022-05-15 14:56:53 +00:00
|
|
|
template <typename T> requires CIntegral<T> || CPointer<T>
|
2022-03-18 12:17:28 +00:00
|
|
|
FORCEINLINE constexpr T Align(T InValue, size_t Alignment)
|
|
|
|
{
|
2022-04-27 14:50:56 +00:00
|
|
|
checkf(IsValidAlignment(Alignment), TEXT("The alignment value must be an integer power of 2."));
|
2022-04-24 14:16:56 +00:00
|
|
|
return (T)(((uint64)(InValue) + static_cast<uint64>(Alignment) - 1) & ~(static_cast<uint64>(Alignment) - 1));
|
2022-03-18 12:17:28 +00:00
|
|
|
}
|
|
|
|
|
2022-05-15 14:56:53 +00:00
|
|
|
template <typename T> requires CIntegral<T> || CPointer<T>
|
2022-03-18 12:17:28 +00:00
|
|
|
FORCEINLINE constexpr T AlignDown(T InValue, size_t Alignment)
|
|
|
|
{
|
2022-04-27 14:50:56 +00:00
|
|
|
checkf(IsValidAlignment(Alignment), TEXT("The alignment value must be an integer power of 2."));
|
2022-04-24 14:16:56 +00:00
|
|
|
return (T)((uint64)(InValue) & ~(static_cast<uint64>(Alignment) - 1));
|
2022-03-18 12:17:28 +00:00
|
|
|
}
|
|
|
|
|
2022-05-15 14:56:53 +00:00
|
|
|
template <typename T> requires CIntegral<T> || CPointer<T>
|
2022-03-18 12:17:28 +00:00
|
|
|
FORCEINLINE constexpr T AlignArbitrary(T InValue, size_t Alignment)
|
|
|
|
{
|
2022-04-27 14:50:56 +00:00
|
|
|
checkf(IsValidAlignment(Alignment), TEXT("The alignment value must be an integer power of 2."));
|
2022-04-24 14:16:56 +00:00
|
|
|
return (T)((((uint64)(InValue) + static_cast<uint64>(Alignment) - 1) / static_cast<uint64>(Alignment)) * static_cast<uint64>(Alignment));
|
2022-03-18 12:17:28 +00:00
|
|
|
}
|
|
|
|
|
2022-05-15 14:56:53 +00:00
|
|
|
template <typename T> requires CIntegral<T> || CPointer<T>
|
2022-03-18 12:17:28 +00:00
|
|
|
FORCEINLINE constexpr bool IsAligned(T InValue, size_t Alignment)
|
|
|
|
{
|
2022-04-27 14:50:56 +00:00
|
|
|
checkf(IsValidAlignment(Alignment), TEXT("The alignment value must be an integer power of 2."));
|
2022-04-24 14:16:56 +00:00
|
|
|
return !((uint64)(InValue) & (static_cast<uint64>(Alignment) - 1));
|
2022-03-18 12:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NAMESPACE_END(Memory)
|
|
|
|
|
|
|
|
NAMESPACE_MODULE_END(Utility)
|
|
|
|
NAMESPACE_MODULE_END(Redcraft)
|
|
|
|
NAMESPACE_REDCRAFT_END
|