Skip to content

Commit 3135a4f

Browse files
committed
fixup! Optimise GCD implementation.
Address review comments.
1 parent 4c7fc09 commit 3135a4f

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/lib.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -274,20 +274,19 @@ macro_rules! impl_integer_for_isize {
274274
n = n.abs();
275275

276276
// divide n and m by 2 until odd
277-
n >>= n.trailing_zeros();
278277
m >>= m.trailing_zeros();
278+
n >>= n.trailing_zeros();
279279

280-
loop {
280+
while m != n {
281281
if m > n {
282282
m -= n;
283-
if m == 0 { return n << shift; }
284283
m >>= m.trailing_zeros();
285284
} else {
286285
n -= m;
287-
if n == 0 { return m << shift; }
288286
n >>= n.trailing_zeros();
289287
}
290288
}
289+
m << shift
291290
}
292291

293292
/// Calculates the Lowest Common Multiple (LCM) of the number and
@@ -540,20 +539,19 @@ macro_rules! impl_integer_for_usize {
540539
let shift = (m | n).trailing_zeros();
541540

542541
// divide n and m by 2 until odd
543-
n >>= n.trailing_zeros();
544542
m >>= m.trailing_zeros();
543+
n >>= n.trailing_zeros();
545544

546-
loop {
545+
while m != n {
547546
if m > n {
548547
m -= n;
549-
if m == 0 { return n << shift; }
550548
m >>= m.trailing_zeros();
551549
} else {
552550
n -= m;
553-
if n == 0 { return m << shift; }
554551
n >>= n.trailing_zeros();
555552
}
556553
}
554+
m << shift
557555
}
558556

559557
/// Calculates the Lowest Common Multiple (LCM) of the number and `other`.

0 commit comments

Comments
 (0)