Add NPOT AlignUp utility

All our normal alignment functions are designed to only handle power of 2 (`POT`) multiples as we only align or check alignment to `POT` multiples but there are cases where this is not possible and we deal with `NPOT` multiples which is why this function is required.
This commit is contained in:
PixelyIon 2022-06-09 21:55:12 +05:30
parent 662ea532d8
commit 7a0cfb484c
No known key found for this signature in database
GPG Key ID: 11BC6C3201BC2C05

View File

@ -70,7 +70,7 @@ namespace skyline::util {
/**
* @return The value aligned up to the next multiple
* @note The multiple needs to be a power of 2
* @note The multiple **must** be a power of 2
*/
template<typename TypeVal>
requires IsPointerOrUnsignedIntegral<TypeVal>
@ -79,9 +79,20 @@ namespace skyline::util {
return ValuePointer<TypeVal>((PointerValue(value) + multiple) & ~(multiple));
}
/**
* @return The value aligned up to the next multiple, the multiple is not restricted to being a power of two (NPOT)
* @note This will round away from zero for negative numbers
* @note This is costlier to compute than the power of 2 version, it should be preferred over this when possible
*/
template<typename TypeVal>
requires std::is_integral_v<TypeVal> || std::is_pointer_v<TypeVal>
constexpr TypeVal AlignUpNpot(TypeVal value, ssize_t multiple) {
return ValuePointer<TypeVal>(((PointerValue(value) + multiple - 1) / multiple) * multiple);
}
/**
* @return The value aligned down to the previous multiple
* @note The multiple needs to be a power of 2
* @note The multiple **must** be a power of 2
*/
template<typename TypeVal>
requires IsPointerOrUnsignedIntegral<TypeVal>