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


bearophile_h...@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_h...@eml.cc


--- Comment #6 from bearophile_h...@eml.cc 2010-06-04 15:19:11 PDT ---
A partially artificial test case. Faster and better versions are quite
possible, but I expect dmd to be able to run this quickly.


import std.stdio: writeln;

ubyte[1 << NPOW] setBits(int NPOW)() {
    nothrow pure uint setBits8(uint n) {
        uint result;
        foreach (i; 0 .. 8)
            if (n & (1 << i))
                result++;
        return result;
    }

    nothrow pure uint setBits16(uint n) {
        enum uint FIRST_UBYTE =  0b0000_0000_1111_1111;
        enum uint SECOND_UBYTE = 0b1111_1111_0000_0000;
        return setBits8(n & FIRST_UBYTE) + setBits8((n & SECOND_UBYTE) >> 8);
    }

    typeof(return) result;
    foreach (i; 1 .. result.length)
        result[i] = cast(typeof(result[0]))setBits16(i);
    return result;
}

enum nbits = setBits!16(); // currently 12 is about the max

void main() {
    writeln(nbits);
}

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

Reply via email to