eigen2_packetmath.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra. Eigen itself is part of the KDE project.
00003 //
00004 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #include "main.h"
00026 
00027 // using namespace Eigen;
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 }


libicr
Author(s): Robert Krug
autogenerated on Mon Jan 6 2014 11:32:38