refactor(*): add check and constraint diagnostics for illegal alignment

This commit is contained in:
2022-04-27 22:50:56 +08:00
parent 897ee4f283
commit 494928aa6b
5 changed files with 18 additions and 4 deletions

View File

@ -2,6 +2,7 @@
#include "CoreTypes.h"
#include "TypeTraits/TypeTraits.h"
#include "Miscellaneous/AssertionMacros.h"
NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN(Redcraft)
@ -9,27 +10,33 @@ NAMESPACE_MODULE_BEGIN(Utility)
NAMESPACE_BEGIN(Memory)
FORCEINLINE constexpr bool IsValidAlignment(size_t Alignment) { return !(Alignment & (Alignment - 1)); }
template <typename T> requires TIsIntegral<T>::Value || TIsPointer<T>::Value
FORCEINLINE constexpr T Align(T InValue, size_t Alignment)
{
checkf(IsValidAlignment(Alignment), TEXT("The alignment value must be an integer power of 2."));
return (T)(((uint64)(InValue) + static_cast<uint64>(Alignment) - 1) & ~(static_cast<uint64>(Alignment) - 1));
}
template <typename T> requires TIsIntegral<T>::Value || TIsPointer<T>::Value
FORCEINLINE constexpr T AlignDown(T InValue, size_t Alignment)
{
checkf(IsValidAlignment(Alignment), TEXT("The alignment value must be an integer power of 2."));
return (T)((uint64)(InValue) & ~(static_cast<uint64>(Alignment) - 1));
}
template <typename T> requires TIsIntegral<T>::Value || TIsPointer<T>::Value
FORCEINLINE constexpr T AlignArbitrary(T InValue, size_t Alignment)
{
checkf(IsValidAlignment(Alignment), TEXT("The alignment value must be an integer power of 2."));
return (T)((((uint64)(InValue) + static_cast<uint64>(Alignment) - 1) / static_cast<uint64>(Alignment)) * static_cast<uint64>(Alignment));
}
template <typename T> requires TIsIntegral<T>::Value || TIsPointer<T>::Value
FORCEINLINE constexpr bool IsAligned(T InValue, size_t Alignment)
{
checkf(IsValidAlignment(Alignment), TEXT("The alignment value must be an integer power of 2."));
return !((uint64)(InValue) & (static_cast<uint64>(Alignment) - 1));
}