diff options
author | Mike Kaganski <mike.kaganski@collabora.com> | 2021-02-07 22:16:41 +0100 |
---|---|---|
committer | Mike Kaganski <mike.kaganski@collabora.com> | 2021-02-08 05:08:21 +0100 |
commit | 70a1d5258648a442525bd0b365ce92763f2a79fb (patch) | |
tree | 5801d7a57dcc5da743035372649dbce9a44e22c5 | |
parent | 307328d4d1b6c9aab2cb714cca6e87a022c22240 (diff) |
Small refactor
Change-Id: I0493c505be1eb06b121535128ae819b294c643a3
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/110554
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
-rw-r--r-- | include/tools/UnitConversion.hxx | 7 |
1 files changed, 3 insertions, 4 deletions
diff --git a/include/tools/UnitConversion.hxx b/include/tools/UnitConversion.hxx index 7aac3f36ebd0..802a372a546d 100644 --- a/include/tools/UnitConversion.hxx +++ b/include/tools/UnitConversion.hxx @@ -23,15 +23,14 @@ template <typename I> constexpr bool isBetween(I n, sal_Int64 min, sal_Int64 max return n <= sal_uInt64(max); } -constexpr int actualMul(int m, int d) { return (m % d == 0) ? m / d : (d % m == 0) ? 1 : m; } -constexpr int actualDiv(int m, int d) { return (m % d == 0) ? 1 : (d % m == 0) ? d / m : d; } +constexpr int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } // Ensure correct rounding for both positive and negative integers template <int mul, int div, typename I, std::enable_if_t<std::is_integral_v<I>, int> = 0> constexpr sal_Int64 MulDiv(I n) { static_assert(mul > 0 && div > 0); - constexpr int m = actualMul(mul, div), d = actualDiv(mul, div); + constexpr int m = mul / gcd(mul, div), d = div / gcd(mul, div); assert(isBetween(n, (SAL_MIN_INT64 + d / 2) / m, (SAL_MAX_INT64 - d / 2) / m)); return (n >= 0 ? (sal_Int64(n) * m + d / 2) : (sal_Int64(n) * m - d / 2)) / d; } @@ -45,7 +44,7 @@ constexpr double MulDiv(F f) template <int mul, int div, typename I, std::enable_if_t<std::is_integral_v<I>, int> = 0> constexpr sal_Int64 sanitizeMulDiv(I n) { - constexpr int m = actualMul(mul, div), d = actualDiv(mul, div); + constexpr int m = mul / gcd(mul, div), d = div / gcd(mul, div); if constexpr (m > d) if (!isBetween(n, SAL_MIN_INT64 / m * d + d / 2, SAL_MAX_INT64 / m * d - d / 2)) return n > 0 ? SAL_MAX_INT64 : SAL_MIN_INT64; // saturate |