1
0
Fork 0

Add an integer division and ceiling helper function

Many parts of the code (especially audio) deal with discrete integer
(and always-positive) values that need to be scaled up or down by
some divisor. This functions provides a fast and an easy way to
divide and round up, while avoiding the CPU burden and excessive
wordiness associated with casting to and from floating point and
back to integer again.
This commit is contained in:
krcroft 2020-02-29 09:45:31 -08:00 committed by Patryk Obara
parent 619b21425a
commit a757f41ead

View file

@ -38,6 +38,15 @@
// Works with both \ and / directory delimeters.
std::string get_basename(const std::string& filename);
// Unsigned integer division with ceiling
// There is no risk of signed types being used here because the template is unsigned
template<typename T1, class = typename std::enable_if<std::is_unsigned<T1>::value>::type,
typename T2, class = typename std::enable_if<std::is_unsigned<T2>::value>::type>
inline const T1 ceil_divide(const T1 x, const T2 y) noexcept {
return (x != 0) ? 1 + ((x - 1) / y) : 0;
// https://stackoverflow.com/a/2745086
}
// Include a message in assert, similar to static_assert:
#define assertm(exp, msg) assert(((void)msg, exp))
// Use (void) to silent unused warnings.