1// This file is part of Eigen, a lightweight C++ template library 2// for linear algebra. 3// 4// Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr> 5// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com> 6// 7// This Source Code Form is subject to the terms of the Mozilla 8// Public License v. 2.0. If a copy of the MPL was not distributed 9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 10 11#include "main.h" 12 13// using namespace Eigen; 14 15namespace Eigen { 16namespace internal { 17template<typename T> T negate(const T& x) { return -x; } 18} 19} 20 21template<typename Scalar> bool isApproxAbs(const Scalar& a, const Scalar& b, const typename NumTraits<Scalar>::Real& refvalue) 22{ 23 return internal::isMuchSmallerThan(a-b, refvalue); 24} 25 26template<typename Scalar> bool areApproxAbs(const Scalar* a, const Scalar* b, int size, const typename NumTraits<Scalar>::Real& refvalue) 27{ 28 for (int i=0; i<size; ++i) 29 { 30 if (!isApproxAbs(a[i],b[i],refvalue)) 31 { 32 std::cout << "[" << Map<const Matrix<Scalar,1,Dynamic> >(a,size) << "]" << " != " << Map<const Matrix<Scalar,1,Dynamic> >(b,size) << "\n"; 33 return false; 34 } 35 } 36 return true; 37} 38 39template<typename Scalar> bool areApprox(const Scalar* a, const Scalar* b, int size) 40{ 41 for (int i=0; i<size; ++i) 42 { 43 if (!internal::isApprox(a[i],b[i])) 44 { 45 std::cout << "[" << Map<const Matrix<Scalar,1,Dynamic> >(a,size) << "]" << " != " << Map<const Matrix<Scalar,1,Dynamic> >(b,size) << "\n"; 46 return false; 47 } 48 } 49 return true; 50} 51 52 53#define CHECK_CWISE2(REFOP, POP) { \ 54 for (int i=0; i<PacketSize; ++i) \ 55 ref[i] = REFOP(data1[i], data1[i+PacketSize]); \ 56 internal::pstore(data2, POP(internal::pload<Packet>(data1), internal::pload<Packet>(data1+PacketSize))); \ 57 VERIFY(areApprox(ref, data2, PacketSize) && #POP); \ 58} 59 60#define CHECK_CWISE1(REFOP, POP) { \ 61 for (int i=0; i<PacketSize; ++i) \ 62 ref[i] = REFOP(data1[i]); \ 63 internal::pstore(data2, POP(internal::pload<Packet>(data1))); \ 64 VERIFY(areApprox(ref, data2, PacketSize) && #POP); \ 65} 66 67template<bool Cond,typename Packet> 68struct packet_helper 69{ 70 template<typename T> 71 inline Packet load(const T* from) const { return internal::pload<Packet>(from); } 72 73 template<typename T> 74 inline void store(T* to, const Packet& x) const { internal::pstore(to,x); } 75}; 76 77template<typename Packet> 78struct packet_helper<false,Packet> 79{ 80 template<typename T> 81 inline T load(const T* from) const { return *from; } 82 83 template<typename T> 84 inline void store(T* to, const T& x) const { *to = x; } 85}; 86 87#define CHECK_CWISE1_IF(COND, REFOP, POP) if(COND) { \ 88 packet_helper<COND,Packet> h; \ 89 for (int i=0; i<PacketSize; ++i) \ 90 ref[i] = REFOP(data1[i]); \ 91 h.store(data2, POP(h.load(data1))); \ 92 VERIFY(areApprox(ref, data2, PacketSize) && #POP); \ 93} 94 95#define REF_ADD(a,b) ((a)+(b)) 96#define REF_SUB(a,b) ((a)-(b)) 97#define REF_MUL(a,b) ((a)*(b)) 98#define REF_DIV(a,b) ((a)/(b)) 99 100template<typename Scalar> void packetmath() 101{ 102 typedef typename internal::packet_traits<Scalar>::type Packet; 103 const int PacketSize = internal::packet_traits<Scalar>::size; 104 typedef typename NumTraits<Scalar>::Real RealScalar; 105 106 const int size = PacketSize*4; 107 EIGEN_ALIGN16 Scalar data1[internal::packet_traits<Scalar>::size*4]; 108 EIGEN_ALIGN16 Scalar data2[internal::packet_traits<Scalar>::size*4]; 109 EIGEN_ALIGN16 Packet packets[PacketSize*2]; 110 EIGEN_ALIGN16 Scalar ref[internal::packet_traits<Scalar>::size*4]; 111 RealScalar refvalue = 0; 112 for (int i=0; i<size; ++i) 113 { 114 data1[i] = internal::random<Scalar>()/RealScalar(PacketSize); 115 data2[i] = internal::random<Scalar>()/RealScalar(PacketSize); 116 refvalue = (std::max)(refvalue,internal::abs(data1[i])); 117 } 118 119 internal::pstore(data2, internal::pload<Packet>(data1)); 120 VERIFY(areApprox(data1, data2, PacketSize) && "aligned load/store"); 121 122 for (int offset=0; offset<PacketSize; ++offset) 123 { 124 internal::pstore(data2, internal::ploadu<Packet>(data1+offset)); 125 VERIFY(areApprox(data1+offset, data2, PacketSize) && "internal::ploadu"); 126 } 127 128 for (int offset=0; offset<PacketSize; ++offset) 129 { 130 internal::pstoreu(data2+offset, internal::pload<Packet>(data1)); 131 VERIFY(areApprox(data1, data2+offset, PacketSize) && "internal::pstoreu"); 132 } 133 134 for (int offset=0; offset<PacketSize; ++offset) 135 { 136 packets[0] = internal::pload<Packet>(data1); 137 packets[1] = internal::pload<Packet>(data1+PacketSize); 138 if (offset==0) internal::palign<0>(packets[0], packets[1]); 139 else if (offset==1) internal::palign<1>(packets[0], packets[1]); 140 else if (offset==2) internal::palign<2>(packets[0], packets[1]); 141 else if (offset==3) internal::palign<3>(packets[0], packets[1]); 142 internal::pstore(data2, packets[0]); 143 144 for (int i=0; i<PacketSize; ++i) 145 ref[i] = data1[i+offset]; 146 147 typedef Matrix<Scalar, PacketSize, 1> Vector; 148 VERIFY(areApprox(ref, data2, PacketSize) && "internal::palign"); 149 } 150 151 CHECK_CWISE2(REF_ADD, internal::padd); 152 CHECK_CWISE2(REF_SUB, internal::psub); 153 CHECK_CWISE2(REF_MUL, internal::pmul); 154 #ifndef EIGEN_VECTORIZE_ALTIVEC 155 if (!internal::is_same<Scalar,int>::value) 156 CHECK_CWISE2(REF_DIV, internal::pdiv); 157 #endif 158 CHECK_CWISE1(internal::negate, internal::pnegate); 159 CHECK_CWISE1(internal::conj, internal::pconj); 160 161 for(int offset=0;offset<3;++offset) 162 { 163 for (int i=0; i<PacketSize; ++i) 164 ref[i] = data1[offset]; 165 internal::pstore(data2, internal::pset1<Packet>(data1[offset])); 166 VERIFY(areApprox(ref, data2, PacketSize) && "internal::pset1"); 167 } 168 169 VERIFY(internal::isApprox(data1[0], internal::pfirst(internal::pload<Packet>(data1))) && "internal::pfirst"); 170 171 if(PacketSize>1) 172 { 173 for(int offset=0;offset<4;++offset) 174 { 175 for(int i=0;i<PacketSize/2;++i) 176 ref[2*i+0] = ref[2*i+1] = data1[offset+i]; 177 internal::pstore(data2,internal::ploaddup<Packet>(data1+offset)); 178 VERIFY(areApprox(ref, data2, PacketSize) && "ploaddup"); 179 } 180 } 181 182 ref[0] = 0; 183 for (int i=0; i<PacketSize; ++i) 184 ref[0] += data1[i]; 185 VERIFY(isApproxAbs(ref[0], internal::predux(internal::pload<Packet>(data1)), refvalue) && "internal::predux"); 186 187 ref[0] = 1; 188 for (int i=0; i<PacketSize; ++i) 189 ref[0] *= data1[i]; 190 VERIFY(internal::isApprox(ref[0], internal::predux_mul(internal::pload<Packet>(data1))) && "internal::predux_mul"); 191 192 for (int j=0; j<PacketSize; ++j) 193 { 194 ref[j] = 0; 195 for (int i=0; i<PacketSize; ++i) 196 ref[j] += data1[i+j*PacketSize]; 197 packets[j] = internal::pload<Packet>(data1+j*PacketSize); 198 } 199 internal::pstore(data2, internal::preduxp(packets)); 200 VERIFY(areApproxAbs(ref, data2, PacketSize, refvalue) && "internal::preduxp"); 201 202 for (int i=0; i<PacketSize; ++i) 203 ref[i] = data1[PacketSize-i-1]; 204 internal::pstore(data2, internal::preverse(internal::pload<Packet>(data1))); 205 VERIFY(areApprox(ref, data2, PacketSize) && "internal::preverse"); 206} 207 208template<typename Scalar> void packetmath_real() 209{ 210 typedef typename internal::packet_traits<Scalar>::type Packet; 211 const int PacketSize = internal::packet_traits<Scalar>::size; 212 213 const int size = PacketSize*4; 214 EIGEN_ALIGN16 Scalar data1[internal::packet_traits<Scalar>::size*4]; 215 EIGEN_ALIGN16 Scalar data2[internal::packet_traits<Scalar>::size*4]; 216 EIGEN_ALIGN16 Scalar ref[internal::packet_traits<Scalar>::size*4]; 217 218 for (int i=0; i<size; ++i) 219 { 220 data1[i] = internal::random<Scalar>(-1e3,1e3); 221 data2[i] = internal::random<Scalar>(-1e3,1e3); 222 } 223 CHECK_CWISE1_IF(internal::packet_traits<Scalar>::HasSin, internal::sin, internal::psin); 224 CHECK_CWISE1_IF(internal::packet_traits<Scalar>::HasCos, internal::cos, internal::pcos); 225 CHECK_CWISE1_IF(internal::packet_traits<Scalar>::HasTan, internal::tan, internal::ptan); 226 227 for (int i=0; i<size; ++i) 228 { 229 data1[i] = internal::random<Scalar>(-1,1); 230 data2[i] = internal::random<Scalar>(-1,1); 231 } 232 CHECK_CWISE1_IF(internal::packet_traits<Scalar>::HasASin, internal::asin, internal::pasin); 233 CHECK_CWISE1_IF(internal::packet_traits<Scalar>::HasACos, internal::acos, internal::pacos); 234 235 for (int i=0; i<size; ++i) 236 { 237 data1[i] = internal::random<Scalar>(-87,88); 238 data2[i] = internal::random<Scalar>(-87,88); 239 } 240 CHECK_CWISE1_IF(internal::packet_traits<Scalar>::HasExp, internal::exp, internal::pexp); 241 242 for (int i=0; i<size; ++i) 243 { 244 data1[i] = internal::random<Scalar>(0,1e6); 245 data2[i] = internal::random<Scalar>(0,1e6); 246 } 247 CHECK_CWISE1_IF(internal::packet_traits<Scalar>::HasLog, internal::log, internal::plog); 248 CHECK_CWISE1_IF(internal::packet_traits<Scalar>::HasSqrt, internal::sqrt, internal::psqrt); 249 250 ref[0] = data1[0]; 251 for (int i=0; i<PacketSize; ++i) 252 ref[0] = (std::min)(ref[0],data1[i]); 253 VERIFY(internal::isApprox(ref[0], internal::predux_min(internal::pload<Packet>(data1))) && "internal::predux_min"); 254 255 CHECK_CWISE2((std::min), internal::pmin); 256 CHECK_CWISE2((std::max), internal::pmax); 257 CHECK_CWISE1(internal::abs, internal::pabs); 258 259 ref[0] = data1[0]; 260 for (int i=0; i<PacketSize; ++i) 261 ref[0] = (std::max)(ref[0],data1[i]); 262 VERIFY(internal::isApprox(ref[0], internal::predux_max(internal::pload<Packet>(data1))) && "internal::predux_max"); 263 264 for (int i=0; i<PacketSize; ++i) 265 ref[i] = data1[0]+Scalar(i); 266 internal::pstore(data2, internal::plset(data1[0])); 267 VERIFY(areApprox(ref, data2, PacketSize) && "internal::plset"); 268} 269 270template<typename Scalar,bool ConjLhs,bool ConjRhs> void test_conj_helper(Scalar* data1, Scalar* data2, Scalar* ref, Scalar* pval) 271{ 272 typedef typename internal::packet_traits<Scalar>::type Packet; 273 const int PacketSize = internal::packet_traits<Scalar>::size; 274 275 internal::conj_if<ConjLhs> cj0; 276 internal::conj_if<ConjRhs> cj1; 277 internal::conj_helper<Scalar,Scalar,ConjLhs,ConjRhs> cj; 278 internal::conj_helper<Packet,Packet,ConjLhs,ConjRhs> pcj; 279 280 for(int i=0;i<PacketSize;++i) 281 { 282 ref[i] = cj0(data1[i]) * cj1(data2[i]); 283 VERIFY(internal::isApprox(ref[i], cj.pmul(data1[i],data2[i])) && "conj_helper pmul"); 284 } 285 internal::pstore(pval,pcj.pmul(internal::pload<Packet>(data1),internal::pload<Packet>(data2))); 286 VERIFY(areApprox(ref, pval, PacketSize) && "conj_helper pmul"); 287 288 for(int i=0;i<PacketSize;++i) 289 { 290 Scalar tmp = ref[i]; 291 ref[i] += cj0(data1[i]) * cj1(data2[i]); 292 VERIFY(internal::isApprox(ref[i], cj.pmadd(data1[i],data2[i],tmp)) && "conj_helper pmadd"); 293 } 294 internal::pstore(pval,pcj.pmadd(internal::pload<Packet>(data1),internal::pload<Packet>(data2),internal::pload<Packet>(pval))); 295 VERIFY(areApprox(ref, pval, PacketSize) && "conj_helper pmadd"); 296} 297 298template<typename Scalar> void packetmath_complex() 299{ 300 typedef typename internal::packet_traits<Scalar>::type Packet; 301 const int PacketSize = internal::packet_traits<Scalar>::size; 302 303 const int size = PacketSize*4; 304 EIGEN_ALIGN16 Scalar data1[PacketSize*4]; 305 EIGEN_ALIGN16 Scalar data2[PacketSize*4]; 306 EIGEN_ALIGN16 Scalar ref[PacketSize*4]; 307 EIGEN_ALIGN16 Scalar pval[PacketSize*4]; 308 309 for (int i=0; i<size; ++i) 310 { 311 data1[i] = internal::random<Scalar>() * Scalar(1e2); 312 data2[i] = internal::random<Scalar>() * Scalar(1e2); 313 } 314 315 test_conj_helper<Scalar,false,false> (data1,data2,ref,pval); 316 test_conj_helper<Scalar,false,true> (data1,data2,ref,pval); 317 test_conj_helper<Scalar,true,false> (data1,data2,ref,pval); 318 test_conj_helper<Scalar,true,true> (data1,data2,ref,pval); 319 320 { 321 for(int i=0;i<PacketSize;++i) 322 ref[i] = Scalar(std::imag(data1[i]),std::real(data1[i])); 323 internal::pstore(pval,internal::pcplxflip(internal::pload<Packet>(data1))); 324 VERIFY(areApprox(ref, pval, PacketSize) && "pcplxflip"); 325 } 326 327 328} 329 330void test_packetmath() 331{ 332 for(int i = 0; i < g_repeat; i++) { 333 CALL_SUBTEST_1( packetmath<float>() ); 334 CALL_SUBTEST_2( packetmath<double>() ); 335 CALL_SUBTEST_3( packetmath<int>() ); 336 CALL_SUBTEST_1( packetmath<std::complex<float> >() ); 337 CALL_SUBTEST_2( packetmath<std::complex<double> >() ); 338 339 CALL_SUBTEST_1( packetmath_real<float>() ); 340 CALL_SUBTEST_2( packetmath_real<double>() ); 341 342 CALL_SUBTEST_1( packetmath_complex<std::complex<float> >() ); 343 CALL_SUBTEST_2( packetmath_complex<std::complex<double> >() ); 344 } 345} 346