[Libreoffice-commits] core.git: include/tools
Mike Kaganski (via logerrit)
logerrit at kemper.freedesktop.org
Mon Feb 8 04:09:02 UTC 2021
include/tools/UnitConversion.hxx | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
New commits:
commit 70a1d5258648a442525bd0b365ce92763f2a79fb
Author: Mike Kaganski <mike.kaganski at collabora.com>
AuthorDate: Sun Feb 7 22:16:41 2021 +0100
Commit: Mike Kaganski <mike.kaganski at collabora.com>
CommitDate: Mon Feb 8 05:08:21 2021 +0100
Small refactor
Change-Id: I0493c505be1eb06b121535128ae819b294c643a3
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/110554
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski at collabora.com>
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
More information about the Libreoffice-commits
mailing list