[Issue 4125] std.numeric.gcd can use a binary GCD

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Jan 31 12:11:37 PST 2012


http://d.puremagic.com/issues/show_bug.cgi?id=4125


Marco Leise <Marco.Leise at gmx.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |Marco.Leise at gmx.de


--- Comment #1 from Marco Leise <Marco.Leise at gmx.de> 2012-01-31 12:11:35 PST ---
Replace uint with ulong for the longer version ;)
I use this and it is notably faster than what I used before.

uint gcd(uint u, uint v)
{
    int shift;
    if (u == 0 || v == 0) return u | v;
    for (shift = 0; ((u | v) & 1) == 0; ++shift) {
        u >>= 1;
        v >>= 1;
    }
    while ((u & 1) == 0) u >>= 1;
    do {
        while ((v & 1) == 0) v >>= 1;
        if (u < v) {
            v -= u;
        } else {
            uint diff = u - v;
            u = v;
            v = diff;
        }
        v >>= 1;
    } while (v != 0);
    return u << shift;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list