Skip to content

Optimise the GCD implementations. #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 3, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ extern crate std;
extern crate num_traits as traits;

use core::ops::Add;
use core::mem;

use traits::{Num, Signed};

Expand Down Expand Up @@ -275,16 +274,20 @@ macro_rules! impl_integer_for_isize {
n = n.abs();

// divide n and m by 2 until odd
// m inside loop
n >>= n.trailing_zeros();

while m != 0 {
m >>= m.trailing_zeros();
if n > m { mem::swap(&mut n, &mut m) }
m -= n;
m >>= m.trailing_zeros();

loop {
if m > n {
m -= n;
if m == 0 { return n << shift; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case is impossible, since m > n.

m >>= m.trailing_zeros();
} else {
n -= m;
if n == 0 { return m << shift; }
n >>= n.trailing_zeros();
}
}

n << shift
}

/// Calculates the Lowest Common Multiple (LCM) of the number and
Expand Down Expand Up @@ -537,16 +540,20 @@ macro_rules! impl_integer_for_usize {
let shift = (m | n).trailing_zeros();

// divide n and m by 2 until odd
// m inside loop
n >>= n.trailing_zeros();

while m != 0 {
m >>= m.trailing_zeros();
if n > m { mem::swap(&mut n, &mut m) }
m -= n;
m >>= m.trailing_zeros();

loop {
if m > n {
m -= n;
if m == 0 { return n << shift; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again impossible with m > n.

m >>= m.trailing_zeros();
} else {
n -= m;
if n == 0 { return m << shift; }
n >>= n.trailing_zeros();
}
}

n << shift
}

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