diff options
author | Treeston <treeston.mmoc@gmail.com> | 2019-08-12 14:19:22 +0200 |
---|---|---|
committer | Treeston <treeston.mmoc@gmail.com> | 2019-08-12 14:19:22 +0200 |
commit | 31b5632c0006cf74f83d5cece5e0e40579d27501 (patch) | |
tree | 4b078666f2d055364c59f3c4d89d024bb3862e5c /src | |
parent | 1c2b1cbba82fcd523ccd0cceafe532f35d71dcc1 (diff) |
Common/Utilities: Explicit tail recursion to keep the compilers happy.
Diffstat (limited to 'src')
-rw-r--r-- | src/common/Utilities/advstd.h | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/src/common/Utilities/advstd.h b/src/common/Utilities/advstd.h index 86edcd4445d..187df3c15be 100644 --- a/src/common/Utilities/advstd.h +++ b/src/common/Utilities/advstd.h @@ -112,11 +112,13 @@ namespace advstd template <typename T1, typename T2> constexpr std::enable_if_t<advstd::is_unsigned_v<T1> && advstd::is_unsigned_v<T2>, std::common_type_t<T1, T2>> gcd(T1 m, T2 n) { - if (m < n) - return gcd(n, m); - if (!n) - return m; - return gcd(n, m%n); + while (n) + { + std::swap(m,n); + if (m <= n) + n %= m; + } + return m; } // C++17 std::lcm |