On Saturday, 21 December 2013 at 23:52:05 UTC, Ali Çehreli wrote:
On 12/21/2013 03:13 PM, John Colvin wrote:

> Ideally the compiler will optimise your version to be fast,
but you may
> find you get better performance by doing the bit
manipulations eplicitly:

Assuming that the program needs to support only big endian and little endian systems (i.e. excluding systems where no D compiler exists :)), the following is less wordy and should be equally fast:

import std.bitmanip;
import std.system;

ulong ubytesToUlong(ubyte[] block, size_t n = 0)
in
{
    assert (n >= 0);
    assert (n + 8 <= block.length);
}
body
{
    ulong value = *cast(ulong*)(block.ptr + n);

    if (std.system.endian == Endian.littleEndian) {
        return *cast(ulong*)(value.nativeToBigEndian.ptr);

    } else {
        return value;
    }
}

Ali

Nevermind equally fast, that will be much faster. -10 brain points for me tonight...

Reply via email to