ubytes to ulong problem

Ali Çehreli acehreli at yahoo.com
Sat Dec 21 15:52:05 PST 2013


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



More information about the Digitalmars-d-learn mailing list