From 1d577c8267569030d3b1fb0f53cfbfa9b7618b2c Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Tue, 25 Feb 2020 19:26:45 +0000 Subject: [PATCH] Add utility functions for integer division towards +/- infinity --- src/core/math_func.hpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/core/math_func.hpp b/src/core/math_func.hpp index 5910a401d8..013f03fbeb 100644 --- a/src/core/math_func.hpp +++ b/src/core/math_func.hpp @@ -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 +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 +static inline T DivTowardsPositiveInf(T a, T b) +{ + return (a / b) + (a % b > 0 ? 1 : 0); +} + uint32 IntSqrt(uint32 num); #endif /* MATH_FUNC_HPP */