Proposal: Operator overloading without temporaries
Sean Kelly
sean at f4.ca
Tue Mar 28 09:31:31 PST 2006
Norbert Nemec wrote:
> Don Clugston wrote:
>> Obviously not with real matrices (C*D is not a pointwise operation), but
>> point taken. (BTW, the temporaries are still there, they're just in
>> registers this time (A[i]*B[i], C[i]*D[i]). The proposal does get rid of
>> all unnecessary temporaries, the problem is that there's no vectorisation).
>
> Point accepted. For matrices, the issues are much more complicated, but
> still there is quite a bit of optimization possible when vectorization
> is taken into accound. p.e. the expression A*B+C can be done very
> efficiently when done in one shot. (There even are BLAS routines for
> this kind of combined operations, which are very common in many fields
> of application.)
It almost seems like this could be handled via a special opIndex
function: opIndexCalc or some such. If the method exists for all
involved types, then:
A = B + C
could be translated to:
for( size_t i = 0; i < A.length; ++i )
A[i] = B[i] + C[i];
where the subscripting calls opIndexCalc instead of the standard
opIndex. But this leaves out array length checking, and simply throwing
an IndexOutOfBounds exception if something goes wrong would leave A
corrupted. So perhaps some checking would also be required to see if
opIndexCalc should be called? The only catch is that this would likely
need to occur at run-time:
if( A.matches( B ) && A.matches( C ) )
for( size_t i = 0; i < A.length; ++i )
A[i] = B[i] + C[i];
else
A = B + C; // standard method using temporaries
I don't have enough experience to know what might work here, but it
would be great if an alternative to expression templates could be devised.
Sean
More information about the Digitalmars-d
mailing list