00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "main.h"
00026
00027
00028
00029 template<typename Scalar> bool areApprox(const Scalar* a, const Scalar* b, int size)
00030 {
00031 for (int i=0; i<size; ++i)
00032 if (!ei_isApprox(a[i],b[i])) return false;
00033 return true;
00034 }
00035
00036 #define CHECK_CWISE(REFOP, POP) { \
00037 for (int i=0; i<PacketSize; ++i) \
00038 ref[i] = REFOP(data1[i], data1[i+PacketSize]); \
00039 ei_pstore(data2, POP(ei_pload(data1), ei_pload(data1+PacketSize))); \
00040 VERIFY(areApprox(ref, data2, PacketSize) && #POP); \
00041 }
00042
00043 #define REF_ADD(a,b) ((a)+(b))
00044 #define REF_SUB(a,b) ((a)-(b))
00045 #define REF_MUL(a,b) ((a)*(b))
00046 #define REF_DIV(a,b) ((a)/(b))
00047
00048 namespace std {
00049
00050 template<> const complex<float>& min(const complex<float>& a, const complex<float>& b)
00051 { return a.real() < b.real() ? a : b; }
00052
00053 template<> const complex<float>& max(const complex<float>& a, const complex<float>& b)
00054 { return a.real() < b.real() ? b : a; }
00055
00056 }
00057
00058 template<typename Scalar> void packetmath()
00059 {
00060 typedef typename ei_packet_traits<Scalar>::type Packet;
00061 const int PacketSize = ei_packet_traits<Scalar>::size;
00062
00063 const int size = PacketSize*4;
00064 EIGEN_ALIGN_128 Scalar data1[ei_packet_traits<Scalar>::size*4];
00065 EIGEN_ALIGN_128 Scalar data2[ei_packet_traits<Scalar>::size*4];
00066 EIGEN_ALIGN_128 Packet packets[PacketSize*2];
00067 EIGEN_ALIGN_128 Scalar ref[ei_packet_traits<Scalar>::size*4];
00068 for (int i=0; i<size; ++i)
00069 {
00070 data1[i] = ei_random<Scalar>();
00071 data2[i] = ei_random<Scalar>();
00072 }
00073
00074 ei_pstore(data2, ei_pload(data1));
00075 VERIFY(areApprox(data1, data2, PacketSize) && "aligned load/store");
00076
00077 for (int offset=0; offset<PacketSize; ++offset)
00078 {
00079 ei_pstore(data2, ei_ploadu(data1+offset));
00080 VERIFY(areApprox(data1+offset, data2, PacketSize) && "ei_ploadu");
00081 }
00082
00083 for (int offset=0; offset<PacketSize; ++offset)
00084 {
00085 ei_pstoreu(data2+offset, ei_pload(data1));
00086 VERIFY(areApprox(data1, data2+offset, PacketSize) && "ei_pstoreu");
00087 }
00088
00089 for (int offset=0; offset<PacketSize; ++offset)
00090 {
00091 packets[0] = ei_pload(data1);
00092 packets[1] = ei_pload(data1+PacketSize);
00093 if (offset==0) ei_palign<0>(packets[0], packets[1]);
00094 else if (offset==1) ei_palign<1>(packets[0], packets[1]);
00095 else if (offset==2) ei_palign<2>(packets[0], packets[1]);
00096 else if (offset==3) ei_palign<3>(packets[0], packets[1]);
00097 ei_pstore(data2, packets[0]);
00098
00099 for (int i=0; i<PacketSize; ++i)
00100 ref[i] = data1[i+offset];
00101
00102 typedef Matrix<Scalar, PacketSize, 1> Vector;
00103 VERIFY(areApprox(ref, data2, PacketSize) && "ei_palign");
00104 }
00105
00106 CHECK_CWISE(REF_ADD, ei_padd);
00107 CHECK_CWISE(REF_SUB, ei_psub);
00108 CHECK_CWISE(REF_MUL, ei_pmul);
00109 #ifndef EIGEN_VECTORIZE_ALTIVEC
00110 if (!ei_is_same_type<Scalar,int>::ret)
00111 CHECK_CWISE(REF_DIV, ei_pdiv);
00112 #endif
00113 CHECK_CWISE(std::min, ei_pmin);
00114 CHECK_CWISE(std::max, ei_pmax);
00115
00116 for (int i=0; i<PacketSize; ++i)
00117 ref[i] = data1[0];
00118 ei_pstore(data2, ei_pset1(data1[0]));
00119 VERIFY(areApprox(ref, data2, PacketSize) && "ei_pset1");
00120
00121 VERIFY(ei_isApprox(data1[0], ei_pfirst(ei_pload(data1))) && "ei_pfirst");
00122
00123 ref[0] = 0;
00124 for (int i=0; i<PacketSize; ++i)
00125 ref[0] += data1[i];
00126 VERIFY(ei_isApprox(ref[0], ei_predux(ei_pload(data1))) && "ei_predux");
00127
00128 for (int j=0; j<PacketSize; ++j)
00129 {
00130 ref[j] = 0;
00131 for (int i=0; i<PacketSize; ++i)
00132 ref[j] += data1[i+j*PacketSize];
00133 packets[j] = ei_pload(data1+j*PacketSize);
00134 }
00135 ei_pstore(data2, ei_preduxp(packets));
00136 VERIFY(areApprox(ref, data2, PacketSize) && "ei_preduxp");
00137 }
00138
00139 void test_eigen2_packetmath()
00140 {
00141 for(int i = 0; i < g_repeat; i++) {
00142 CALL_SUBTEST_1( packetmath<float>() );
00143 CALL_SUBTEST_2( packetmath<double>() );
00144 CALL_SUBTEST_3( packetmath<int>() );
00145 CALL_SUBTEST_4( packetmath<std::complex<float> >() );
00146 }
00147 }