On 5 January 2012 23:40, Peter Alexander <peter.alexander...@gmail.com> wrote:
> On 5/01/12 10:17 PM, Iain Buclaw wrote:
>>
>> On 5 January 2012 22:11, Manu<turkey...@gmail.com>  wrote:
>>>
>>> So regarding my assumptions about translating the D front end expressions
>>> to
>>>
>>> GCC? Is that all simpler than I imagine?
>>> Do you think GDC generates optimal code comparable to C code?
>>>
>>> What about pure functions, can you make good on optimisations like
>>> caching
>>> results of pure functions, moving them outside loops, etc?
>>>
>>>
>>
>> I think you are confusing the pure with memoization.  I could be wrong
>> however... :)
>>
>
> I think Manu is right:
>
> void foo(int x)
> {
>    int[10] a;
>    foreach (ref e; a)
>        e = bar(x);
> }
>
> If bar is pure then you can safely transform this into:
>
> void foo(int x)
> {
>    int[10] a;
>    auto barx = bar(x);
>    foreach (ref e; a)
>        e = barx;
> }
>
> If bar is not pure then this transformation would be unsafe.

Yes, it will do something like that, though the loop will be unrolled
- and given that gdc supports vectorisation, I think that above
example will likely be vectorised too.  So off the top of my head:

void foo(int x)
{
   auto barx = bar(x);
   vector(4) vect = { barx, barx, barx, barx };
   *[&a] = vect;
   *[&a+16] = vect;
   a[9] = barx;
   a[10] = barx;
}



Regards
-- 
Iain Buclaw

*(p < e ? p++ : p) = (c & 0x0f) + '0';

Reply via email to