Question

If I call the function

/** Check if all Elements, possibly recursively, of $(D x) are zero. */
bool allZero(T)(in T x) @safe pure nothrow {
    import std.range: isIterable;
    static if (isIterable!T) {
        foreach (ref elt; x) {
            if (!elt.allZero) { return false; }
        }
        return true;
    } else {
        return x == 0;
    }
}

with a static array will D unroll the foreach for me automatically in release mode?

If not could

/** Static Iota. */
import std.typetuple: TypeTuple;
template siota(size_t from, size_t to) { alias siotaImpl!(to-1, from) siota; }
private template siotaImpl(size_t to, size_t now) {
    static if (now >= to) { alias TypeTuple!(now) siotaImpl; }
    else                  { alias TypeTuple!(now, siotaImpl!(to, now+1)) siotaImpl; }
}

be used to achieve unrolling instead of foreach?

Further is there a flag to DMD that generates assembly code so that I myself, in the future, can investigate the code generated by DMD?

Update: Here's my solution so far.

/** Check if all Elements, possibly recursively, of $(D x) are zero. */
bool allZero(T, bool useStatic = true)(in T x) @safe pure nothrow { // TODO: Extend to support struct's and classes's'
    static        if (useStatic && isStaticArray!T) {
        foreach (ix; siota!(0, x.length)) {
            if (!x[ix].allZero) { return false; } // make use of siota?
        }
        return true;
    } else static if (isIterable!T) {
        foreach (ref elt; x) {
            if (!elt.allZero) { return false; }
        }
        return true;
    } else {
        return x == 0;
    }
}

Does it look ok?

Was it helpful?

Solution

with a static array will D unroll the foreach for me automatically?

No, the language does not guarantee it. Some implementations (compilers) might unroll the loop as an optimization.

If not could my implementation of static iota (siota) be used to achieve this?

Yes, using foreach over a tuple generates code for every "iteration", effectively unrolling the loop.

Further is there a flag to DMD that generates assembly code so that I myself, in the future, can investigate the code generated by DMD?

No, DMD cannot emit assembly listings. You can use a disassembler (such as obj2asm or IDA), or a different compiler instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top