35 #include "../../Platforms/Basic/interface/DebugUtilities.hpp"
43 return val > 0 && (val & (val - 1)) == 0;
46 template <
typename T1,
typename T2>
47 inline typename std::conditional<
sizeof(
T1) >=
sizeof(T2),
T1, T2>::type
AlignUp(
T1 val, T2 alignment)
49 static_assert(std::is_unsigned<T1>::value == std::is_unsigned<T2>::value,
"both types must be signed or unsigned");
50 static_assert(!std::is_pointer<T1>::value && !std::is_pointer<T2>::value,
"types must not be pointers");
53 using T =
typename std::conditional<
sizeof(
T1) >=
sizeof(T2),
T1, T2>::type;
54 return (
static_cast<T
>(val) +
static_cast<T
>(alignment - 1)) & ~static_cast<T>(alignment - 1);
57 template <
typename PtrType,
typename AlignType>
58 inline PtrType*
AlignUp(PtrType* val, AlignType alignment)
60 return reinterpret_cast<PtrType*
>(
AlignUp(
reinterpret_cast<uintptr_t
>(val),
static_cast<uintptr_t
>(alignment)));
63 template <
typename T1,
typename T2>
64 inline typename std::conditional<
sizeof(
T1) >=
sizeof(T2),
T1, T2>::type
AlignDown(
T1 val, T2 alignment)
66 static_assert(std::is_unsigned<T1>::value == std::is_unsigned<T2>::value,
"both types must be signed or unsigned");
67 static_assert(!std::is_pointer<T1>::value && !std::is_pointer<T2>::value,
"types must not be pointers");
70 using T =
typename std::conditional<
sizeof(
T1) >=
sizeof(T2),
T1, T2>::type;
71 return static_cast<T
>(val) & ~
static_cast<T
>(alignment - 1);
74 template <
typename PtrType,
typename AlignType>
75 inline PtrType*
AlignDown(PtrType* val, AlignType alignment)
77 return reinterpret_cast<PtrType*
>(
AlignDown(
reinterpret_cast<uintptr_t
>(val),
static_cast<uintptr_t
>(alignment)));