You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

365 lines
13 KiB

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