Work around conservative optimization

Johan Engelen j at j.nl
Sat Jun 2 18:32:37 UTC 2018


On Saturday, 2 June 2018 at 11:44:40 UTC, Johan Engelen wrote:
>
> ```
> void store32_le_optim(ref ubyte[4] dest, uint val)
> {
>     import core.stdc.string;
>     memcpy(&dest, &val, val.sizeof);
> }
> ```

This only works on little endian machines of course, but the 
proper version is easy:

```
void store32_le_optim(ref ubyte[4] dest, uint val)
{
     import core.stdc.string;
     ubyte[4] temp;
     temp[0]=cast(ubyte)val;
     temp[1]=cast(ubyte)(val>>8);
     temp[2]=cast(ubyte)(val>>16);
     temp[3]=cast(ubyte)(val>>24);
     memcpy(&dest, &temp, temp.sizeof);
}
```

See in action for Little Endian and Big Endian: 
https://godbolt.org/g/QqcCpi

-Johan



More information about the digitalmars-d-ldc mailing list