From a757f41ead414ed2dbc21747b68ab7d2ca2b910b Mon Sep 17 00:00:00 2001 From: krcroft Date: Sat, 29 Feb 2020 09:45:31 -0800 Subject: [PATCH] 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. --- include/support.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/include/support.h b/include/support.h index e75fc514..cfa8cf23 100644 --- a/include/support.h +++ b/include/support.h @@ -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::value>::type, + typename T2, class = typename std::enable_if::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.