www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 17483] New: std.numeric.gcd cannot inline function

https://issues.dlang.org/show_bug.cgi?id=17483

          Issue ID: 17483
           Summary: std.numeric.gcd cannot inline function
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: stanislav.blinov gmail.com

Not only can it not be inlined, due to this function's implementation it turns
into two function calls when called with const/immutable arguments.

---
import std.traits : isIntegral, Unqual;

pragma(inline, true)
// pasted from std.numeric
T gcd(T)(T a, T b) if (isIntegral!T) {
    static if (is(T == const) || is(T == immutable)) {
        return gcd!(Unqual!T)(a, b);
    } else version(DigitalMars) {
        static if (T.min < 0) {
            assert(a >= 0 && b >= 0);
        }
        while (b) {
            immutable t = b;
            b = a % b;
            a = t;
        }
        return a;
    } else {
        if (a == 0)
            return b;
        if (b == 0)
            return a;

        import core.bitop : bsf;
        import std.algorithm.mutation : swap;

        immutable uint shift = bsf(a | b);
        a >>= a.bsf;

        do {
            b >>= b.bsf;
            if (a > b)
                swap(a, b);
            b -= a;
        } while (b);

        return a << shift;
    }
}

void foo() {
    size_t a, b;
    gcd(a, b);
}
---

--
Jun 08 2017