"kwikius" <an**@servocomm .freeserve.co.u kwrote in message

news:48******** **@mk-nntp-2.news.uk.tisca li.com...

<...>

The following is some quick attempt at a language extension to express sum

and dot on a vector FWIW:

<snip pseudo stuff>

Below is a version which is standard C++... no parallel, N in app can be

easily modified for diff size arrays.

Its interesting to see how the asm changes as N gets bigger:

Asm produced by VC8 with optimisation at end, shows how near it is to being

able to parallelise.

regards

Andy Little

//sfinae

template <bool C, typename T = void>

struct where_{

typedef T type;

};

template <typename T>

struct where_<false,T> {};

// fixed size vect

template <int N>

struct vect

{

double elements[N];

vect(double (& ar)[N]){

for (int i = 0; i < N; ++i){

elements[i] = ar[i];

}

}

vect(){}

static const int size = N;

double const & operator [] (int n) const

{

return elements[n];

}

double & operator [] (int n)

{

return elements[n];

}

vect & operator = (vect const & in)

{

for ( int i = 0; i < N ; ++i){

elements[i] = in[i];

}

}

};

template <typename Vect,int Offset>

struct vect_view{

Vect const & v;

vect_view(Vect const & in) : v(in){}

double const & operator [] (int n)const

{

return v[n + Offset];

}

static const int size = Vect::size - Offset;

vect_view & operator = (vect_view const & in)

{

for (int i = 0; i < N ; ++i){

elements[i] = in[i];

}

}

};

template <int N, typename Where = void>

struct sum_impl;

template <int N>

struct sum_impl<N, typename where_<(N >= 2) && ((N %2) ==0) >::type >

{

template <typename T>

double operator()(T const & in)const

{

vect<N/2out;

for (int i = 0; i < N /2 ; ++i){

out[i] = in[i*2] + in[i*2 +1];

}

sum_impl<N/2s;

return s(out);

}

};

template <int N>

struct sum_impl<N, typename where_<(N 2) && ((N %2) == 1)>::type>

{

template <typename T>

double operator()(T const & in)const

{

sum_impl<N-1s;

return in[0] + s(vect_view<T,1 >(in) );

}

};

template <int N>

struct sum_impl<N, typename where_<(N ==1)>::type>

{

template <typename T>

double operator()(T const & in)const

{

return in[0];

}

};

template <typename V>

double sum( V const & v)

{

sum_impl<V::siz es;

return s(v);

}

template <typename V>

double dot( V const & lhs, V const & rhs)

{

vect<V::sizev;

for (int i = 0; i < V::size;++i){

v[i] = lhs[i] * rhs[i];

}

return sum(v);

}

///-----------------------------

#include <iostream>

int main()

{

static const int n = 4;

double ar[n] ;

std::cout << "input " << n << " doubles\n";

for ( int i = 0; i < n; ++i){

std::cin >ar[i];

}

std::cout << "thankyou\n ";

vect<nvv(ar);

double r = dot(vv,vv);

std::cout << r <<'\n';

}

// asm for n = 4 in VC8

; 136 : double r = dot(vv,vv);

fld QWORD PTR _ar$[esp+72]

fmul ST(0), ST(0)

; 137 : std::cout << r <<'\n';

mov ecx, DWORD PTR

__imp_?cout@std @@3V?$basic_ost ream@DU?$char_t raits@D@std@@@1 @A

fld QWORD PTR _ar$[esp+80]

fmul ST(0), ST(0)

fld QWORD PTR _ar$[esp+88]

fmul ST(0), ST(0)

fld QWORD PTR _ar$[esp+96]

fmul ST(0), ST(0)

fxch ST(2)

faddp ST(3), ST(0)

faddp ST(1), ST(0)

faddp ST(1), ST(0)

fstp QWORD PTR [esp]

call DWORD PTR

__imp_??6?$basi c_ostream@DU?$c har_traits@D@st d@@@std@@QAEAAV 01@N@Z