diff options
author | frosch <frosch@openttd.org> | 2010-04-18 14:56:05 +0000 |
---|---|---|
committer | frosch <frosch@openttd.org> | 2010-04-18 14:56:05 +0000 |
commit | 2e90f7f8b975d380c3d544995ef6db9d6c8a86d8 (patch) | |
tree | 90fb0852bd31229b016ca998a8e6c0ce44d699dd /src/core | |
parent | 2330851d1dc0650335bc73ec4cfd010f08c55742 (diff) | |
download | openttd-2e90f7f8b975d380c3d544995ef6db9d6c8a86d8.tar.xz |
(svn r19670) -Codechange: Add CeilDiv() and RoundDiv() to simplify integer divisions with rounding.
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/math_func.hpp | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/core/math_func.hpp b/src/core/math_func.hpp index d57a3a511..8f31dd273 100644 --- a/src/core/math_func.hpp +++ b/src/core/math_func.hpp @@ -318,4 +318,26 @@ static FORCEINLINE uint ToPercent16(uint i) int LeastCommonMultiple(int a, int b); int GreatestCommonDivisor(int a, int b); +/** + * Computes ceil(a / b) for non-negative a and b. + * @param a Numerator + * @param b Denominator + * @return Quotient, rounded up + */ +static FORCEINLINE uint CeilDiv(uint a, uint b) +{ + return (a + b - 1) / b; +} + +/** + * Computes round(a / b) for non-negative a and b. + * @param a Numerator + * @param b Denominator + * @return Quotient, rounded to nearest + */ +static FORCEINLINE uint RoundDiv(uint a, uint b) +{ + return (a + b / 2) / b; +} + #endif /* MATH_FUNC_HPP */ |