opBinary failes with templates

Peter Alexander peter.alexander.au at gmail.com
Thu Jul 28 15:10:59 PDT 2011


On 27/07/11 10:39 PM, KennyTM~ wrote:
> Actually you shouldn't need to write a vector struct if "+" is all it
> required, as D supports array operation already:
>
>
>
> void main() {
> float[3] x = [1, 3, 5], y = [5, -6, 2];
> x[] = x[] + y[];
> assert(x == [6, -3, 7]);
> }
>
>
> It is also more efficient than the for-loop since this could use SIMD
> operations in some cases.

For 3 floats, it will not be more efficient.

In my experiments, manually writing:

x[0] += y[0];
x[1] += y[1];
x[2] += y[2];

Is about 6x faster than calling

x[] = x[] + y[];
// or x[] += y[]; same result

You only get the advantage of the SIMD arrays ops in large arrays. The 
overhead of the function call, loading the scalars into and out of 
vector registers, and looping through the array is way more than the 
work that needs to be done. DMD really shouldn't call those SIMD array 
ops for small statically sized arrays.


More information about the Digitalmars-d mailing list