Array operations -- not actually so difficult

Don Clugston dac at nospam.com.au
Sat Dec 16 01:34:53 PST 2006


Bill Baxter wrote:
> Don Clugston wrote:
>> Array operations are one of the very few 2.0 features that haven't 
>> made it into 1.0. My experiments into expression templates suggest 
>> that it might not be very complicated after all.
>>
>> Consider something like a dot product,
>>
>> real a[], b[], c[];
>> real d = dot(a+b, c);
>>
>> For performance, it would be better if the compiler actually *didn't* 
>> evaluate a+b. Instead, that should compile to:
>>
>> real sum = 0;
>> for (int i=0; i<c.length; ++i) {
>>      sum+=(a[i]+b[i])*c[i];
>> }
>> d=sum;
>>
>> In fact, we could create a fantastic library implementation of array 
>> operations with a tiny bit of syntactic sugar:
>> instead of generating an error message for array operations, look for 
>> an opXXX function, and treat it the same way the implicit array 
>> properties work.
> 
> How does that result in the above code for dot product?
> Looks to me like if all you have is calling opXXX functions the above 
> dot would still become two loops with a tmp for the a+b.

I've posted an update to my expression templates code in 
dsource/mathextra/Blade.
It prints near-optimal x87 asm code for any combination of +,-,+=,-=, *, 
*= and dot product on double[] vectors.
The code is still less than 300 lines, so a comprehensive expression 
template system would probably not be too horrible.



More information about the Digitalmars-d mailing list