Skip to content

Commit 21689e0

Browse files
committed
Add 64 bit impl
1 parent 42d0183 commit 21689e0

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

include/boost/int128/numeric.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,40 @@ constexpr TargetType saturate_cast(const int128_t value) noexcept
251251
}
252252
}
253253

254+
namespace detail {
255+
256+
constexpr std::uint64_t gcd64(std::uint64_t x, std::uint64_t y) noexcept
257+
{
258+
if (x == 0)
259+
{
260+
return y;
261+
}
262+
if (y == 0)
263+
{
264+
return x;
265+
}
266+
267+
const auto s {impl::countr_impl(x | y)};
268+
x >>= impl::countr_impl(x);
269+
270+
do
271+
{
272+
y >>= impl::countr_impl(y);
273+
if (x > y)
274+
{
275+
const auto temp {x};
276+
x = y;
277+
y = temp;
278+
}
279+
280+
y -= x;
281+
} while (y);
282+
283+
return x << s;
284+
}
285+
286+
} // namespace detail
287+
254288
constexpr uint128_t gcd(uint128_t a, uint128_t b) noexcept
255289
{
256290
// Base case

0 commit comments

Comments
 (0)