Work around conservative optimization

kinke noone at
Sun Jun 3 18:36:12 UTC 2018

On Sunday, 3 June 2018 at 16:51:13 UTC, David Nadlinger wrote:
> On 3 Jun 2018, at 12:54, kinke via digitalmars-d-ldc wrote:
>> [For endian-ness conversion of integers, we have 
>> ldc.intrinsics.llvm_bswap().]
> At the risk of pointing out the obvious, I usually find it 
> vastly preferable to just write the code in a way that's 
> independent of the target platform's endianness – like in 
> Johan's example – and let the optimizer deal with eliding the 
> explicit handling if possible. Even DMD's optimizer can 
> recognize those patterns just fine.

No need to reinvent the wheel, the Phobos solution is trivial 

void store32_le(ref ubyte[4] dest, uint val)
     import std.bitmanip;
     dest = nativeToLittleEndian(val);

There's no overhead for little-endian machines with `-O`, but a 
suboptimal non-inlined druntime call instead of the LLVM 
intrinsic directly in the other case.

More information about the digitalmars-d-ldc mailing list