Add utility functions for integer division towards +/- infinity

This commit is contained in:
Jonathan G Rennison
2020-02-25 19:26:45 +00:00
parent de2c2f7cea
commit 1d577c8267

View File

@@ -408,6 +408,30 @@ static inline int DivAwayFromZero(int a, uint b)
}
}
/**
* Computes a / b rounded towards negative infinity for signed a and unsigned b.
* @param a Numerator
* @param b Denominator
* @return Quotient, rounded towards negative infinity
*/
template <typename T>
static inline T DivTowardsNegativeInf(T a, T b)
{
return (a / b) - (a % b < 0 ? 1 : 0);
}
/**
* Computes a / b rounded towards positive infinity for signed a and unsigned b.
* @param a Numerator
* @param b Denominator
* @return Quotient, rounded towards positive infinity
*/
template <typename T>
static inline T DivTowardsPositiveInf(T a, T b)
{
return (a / b) + (a % b > 0 ? 1 : 0);
}
uint32 IntSqrt(uint32 num);
#endif /* MATH_FUNC_HPP */