math: add align_up/down
Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
parent
496dc9cb34
commit
406d14746f
@ -67,6 +67,34 @@ namespace stdex
|
|||||||
throw std::invalid_argument("add overflow");
|
throw std::invalid_argument("add overflow");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Rounds number down and aligns it to have given number of lower bits zero.
|
||||||
|
///
|
||||||
|
/// \param a Number to round down
|
||||||
|
/// \param n Number of lower bits to be zero after rounding
|
||||||
|
///
|
||||||
|
/// \return The biggest number lower or equal to a that has n lower bits zero
|
||||||
|
///
|
||||||
|
inline constexpr size_t align_down(size_t a, int n)
|
||||||
|
{
|
||||||
|
const size_t mask = SIZE_MAX << n;
|
||||||
|
return a & mask;
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Rounds number up and aligns it to have given number of lower bits zero. It throws on overflow.
|
||||||
|
///
|
||||||
|
/// \param a Number to round up
|
||||||
|
/// \param n Number of lower bits to be zero after rounding
|
||||||
|
///
|
||||||
|
/// \return The first number greater or equal to a that has n lower bits zero
|
||||||
|
///
|
||||||
|
inline constexpr size_t align_up(size_t a, int n)
|
||||||
|
{
|
||||||
|
const size_t mask = SIZE_MAX << n;
|
||||||
|
return add(a, ~mask) & mask;
|
||||||
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Bitwise rotates left
|
/// Bitwise rotates left
|
||||||
///
|
///
|
||||||
|
Loading…
x
Reference in New Issue
Block a user