[*] Fixup AuRoundUpPow2 to not round up when the value is already a pow of 2

[+] AuIsPow2
[+] AuNextPow2
This commit is contained in:
Reece Wilson 2024-01-07 03:40:04 +00:00
parent fa951eac4b
commit 6d7d14a3e2

View File

@ -42,10 +42,33 @@ constexpr const T AuPageRound(const T value, const T pageSize)
// TODO: out of order
#include "auBitsUtils.hpp"
template <class T>
const bool AuIsPow2(const T value)
{
return (value) && (value & (value - 1) == 0);
}
template <class T>
const T AuNextPow2(const T value)
{
if constexpr (AuIsSame_v<T, AuUInt16> ||
AuIsSame_v<T, AuUInt8>)
{
return AuNextPow2<AuUInt32>(value);
}
else
{
AuUInt8 ret;
AuBitScanReverse(ret, value);
return T(1) << (T(ret) + T(1));
}
}
template <class T>
const T AuRoundUpPow2(const T value)
{
if constexpr (AuIsSame_v<T, AuUInt16>)
if constexpr (AuIsSame_v<T, AuUInt16> ||
AuIsSame_v<T, AuUInt8>)
{
return AuRoundUpPow2<AuUInt32>(value);
}
@ -53,14 +76,15 @@ const T AuRoundUpPow2(const T value)
{
AuUInt8 ret;
AuBitScanReverse(ret, value);
return T(1) << (ret + T(1));
return T(1) << (T(ret) + T(T(1) << T(ret) == value ? 0 : 1));
}
}
template <class T>
const T AuRoundDownPow2(const T value)
{
if constexpr (AuIsSame_v<T, AuUInt16>)
if constexpr (AuIsSame_v<T, AuUInt16> ||
AuIsSame_v<T, AuUInt8>)
{
return AuRoundDownPow2<AuUInt32>(value);
}
@ -68,6 +92,6 @@ const T AuRoundDownPow2(const T value)
{
AuUInt8 ret;
AuBitScanReverse(ret, value);
return T(1) << ret;
return T(1) << T(ret);
}
}