Work around conservative optimization

Johan Engelen j at j.nl
Sat Jun 2 11:44:40 UTC 2018


On Saturday, 2 June 2018 at 10:40:43 UTC, Kagamin wrote:
> uint load32_le(in ref ubyte[4] s)
> {
> 	return s[0] | (s[1]<<8) | (s[2]<<16) | (s[3]<<24);
> }
>
> void store32_le(ref ubyte[4] dest, uint val)
> {
> 	dest[0]=cast(byte)val;
> 	dest[1]=cast(byte)(val>>8);
> 	dest[2]=cast(byte)(val>>16);
> 	dest[3]=cast(byte)(val>>24);
> }
>
> The first function is optimized to one load, but the second 
> remains as 4 stores. Is there a code pattern that gets around 
> this?

```
void store32_le_optim(ref ubyte[4] dest, uint val)
{
     import core.stdc.string;
     memcpy(&dest, &val, val.sizeof);
}
```

LLVM is not yet smart enough to optimize adjacent stores, but it 
does assume it is valid to use knowledge of standard memcpy 
semantics.

-Johan



More information about the digitalmars-d-ldc mailing list