gcd_bitwise
Disclaimer: The code is not mine.
The code is part of the coreutils project. I have forked it for ease of use, for those who dont want to pull in big dependencies for calculating gcd.
Some notes: This code uses stein's algorithm, that replaces division with arithmetic shifts, comparisons, and subtraction, for optimization of performance. For more info on how efficient this algorithm is, please refer to this page.
Quick Start
use GcdBuilder;
The specific rust implementation from coreutils project showcases several performance optimisations:
-
Trial division by
2
is eschewed in favour of a single bitshift and the count trailing zeros primitiveu64::trailing_zeros
. This performs the equivalent of applying repeatedly identitygcd(2u, v) = gcd(u, v)
, in a much smaller amount of time. -
The loop is laid out so as to avoid repeated work; for instance, eliminating
2
as a factor ofnum2
was moved to the back of the loop, and after the exit condition, asnum2
is known to be odd upon entering the loop. -
The body of the loop is branch-free except for its exit condition
(num2 == 0)
, as the exchange ofnum1
andnum2
(ensuringnum1 <= num2
) compiles down to conditional moves. Hard-to-predict branches can have a large, negative impact on performance.