(svn r19671) -Fix (r19670): RoundDiv() needs to deal with signed numerators.

This commit is contained in:
frosch
2010-04-18 17:13:01 +00:00
parent 2e90f7f8b9
commit af7051178d
3 changed files with 13 additions and 7 deletions

View File

@@ -330,14 +330,20 @@ static FORCEINLINE uint CeilDiv(uint a, uint b)
}
/**
* Computes round(a / b) for non-negative a and b.
* Computes round(a / b) for signed a and unsigned b.
* @param a Numerator
* @param b Denominator
* @return Quotient, rounded to nearest
*/
static FORCEINLINE uint RoundDiv(uint a, uint b)
static FORCEINLINE int RoundDivSU(int a, uint b)
{
return (a + b / 2) / b;
if (a > 0) {
/* 0.5 is rounded to 1 */
return (a + (int)b / 2) / (int)b;
} else {
/* -0.5 is rounded to 0 */
return (a - ((int)b - 1) / 2) / (int)b;
}
}
#endif /* MATH_FUNC_HPP */