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.

345 lines
12 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 (!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. typedef typename internal::packet_traits<Scalar>::type Packet;
  87. const int PacketSize = internal::packet_traits<Scalar>::size;
  88. typedef typename NumTraits<Scalar>::Real RealScalar;
  89. const int size = PacketSize*4;
  90. EIGEN_ALIGN16 Scalar data1[internal::packet_traits<Scalar>::size*4];
  91. EIGEN_ALIGN16 Scalar data2[internal::packet_traits<Scalar>::size*4];
  92. EIGEN_ALIGN16 Packet packets[PacketSize*2];
  93. EIGEN_ALIGN16 Scalar ref[internal::packet_traits<Scalar>::size*4];
  94. RealScalar refvalue = 0;
  95. for (int i=0; i<size; ++i)
  96. {
  97. data1[i] = internal::random<Scalar>()/RealScalar(PacketSize);
  98. data2[i] = internal::random<Scalar>()/RealScalar(PacketSize);
  99. refvalue = (std::max)(refvalue,internal::abs(data1[i]));
  100. }
  101. internal::pstore(data2, internal::pload<Packet>(data1));
  102. VERIFY(areApprox(data1, data2, PacketSize) && "aligned load/store");
  103. for (int offset=0; offset<PacketSize; ++offset)
  104. {
  105. internal::pstore(data2, internal::ploadu<Packet>(data1+offset));
  106. VERIFY(areApprox(data1+offset, data2, PacketSize) && "internal::ploadu");
  107. }
  108. for (int offset=0; offset<PacketSize; ++offset)
  109. {
  110. internal::pstoreu(data2+offset, internal::pload<Packet>(data1));
  111. VERIFY(areApprox(data1, data2+offset, PacketSize) && "internal::pstoreu");
  112. }
  113. for (int offset=0; offset<PacketSize; ++offset)
  114. {
  115. packets[0] = internal::pload<Packet>(data1);
  116. packets[1] = internal::pload<Packet>(data1+PacketSize);
  117. if (offset==0) internal::palign<0>(packets[0], packets[1]);
  118. else if (offset==1) internal::palign<1>(packets[0], packets[1]);
  119. else if (offset==2) internal::palign<2>(packets[0], packets[1]);
  120. else if (offset==3) internal::palign<3>(packets[0], packets[1]);
  121. internal::pstore(data2, packets[0]);
  122. for (int i=0; i<PacketSize; ++i)
  123. ref[i] = data1[i+offset];
  124. typedef Matrix<Scalar, PacketSize, 1> Vector;
  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(internal::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. typedef typename internal::packet_traits<Scalar>::type Packet;
  179. const int PacketSize = internal::packet_traits<Scalar>::size;
  180. const int size = PacketSize*4;
  181. EIGEN_ALIGN16 Scalar data1[internal::packet_traits<Scalar>::size*4];
  182. EIGEN_ALIGN16 Scalar data2[internal::packet_traits<Scalar>::size*4];
  183. EIGEN_ALIGN16 Scalar ref[internal::packet_traits<Scalar>::size*4];
  184. for (int i=0; i<size; ++i)
  185. {
  186. data1[i] = internal::random<Scalar>(-1e3,1e3);
  187. data2[i] = internal::random<Scalar>(-1e3,1e3);
  188. }
  189. CHECK_CWISE1_IF(internal::packet_traits<Scalar>::HasSin, internal::sin, internal::psin);
  190. CHECK_CWISE1_IF(internal::packet_traits<Scalar>::HasCos, internal::cos, internal::pcos);
  191. CHECK_CWISE1_IF(internal::packet_traits<Scalar>::HasTan, internal::tan, internal::ptan);
  192. for (int i=0; i<size; ++i)
  193. {
  194. data1[i] = internal::random<Scalar>(-1,1);
  195. data2[i] = internal::random<Scalar>(-1,1);
  196. }
  197. CHECK_CWISE1_IF(internal::packet_traits<Scalar>::HasASin, internal::asin, internal::pasin);
  198. CHECK_CWISE1_IF(internal::packet_traits<Scalar>::HasACos, internal::acos, internal::pacos);
  199. for (int i=0; i<size; ++i)
  200. {
  201. data1[i] = internal::random<Scalar>(-87,88);
  202. data2[i] = internal::random<Scalar>(-87,88);
  203. }
  204. CHECK_CWISE1_IF(internal::packet_traits<Scalar>::HasExp, internal::exp, internal::pexp);
  205. for (int i=0; i<size; ++i)
  206. {
  207. data1[i] = internal::random<Scalar>(0,1e6);
  208. data2[i] = internal::random<Scalar>(0,1e6);
  209. }
  210. CHECK_CWISE1_IF(internal::packet_traits<Scalar>::HasLog, internal::log, internal::plog);
  211. CHECK_CWISE1_IF(internal::packet_traits<Scalar>::HasSqrt, internal::sqrt, internal::psqrt);
  212. ref[0] = data1[0];
  213. for (int i=0; i<PacketSize; ++i)
  214. ref[0] = (std::min)(ref[0],data1[i]);
  215. VERIFY(internal::isApprox(ref[0], internal::predux_min(internal::pload<Packet>(data1))) && "internal::predux_min");
  216. CHECK_CWISE2((std::min), internal::pmin);
  217. CHECK_CWISE2((std::max), internal::pmax);
  218. CHECK_CWISE1(internal::abs, internal::pabs);
  219. ref[0] = data1[0];
  220. for (int i=0; i<PacketSize; ++i)
  221. ref[0] = (std::max)(ref[0],data1[i]);
  222. VERIFY(internal::isApprox(ref[0], internal::predux_max(internal::pload<Packet>(data1))) && "internal::predux_max");
  223. for (int i=0; i<PacketSize; ++i)
  224. ref[i] = data1[0]+Scalar(i);
  225. internal::pstore(data2, internal::plset(data1[0]));
  226. VERIFY(areApprox(ref, data2, PacketSize) && "internal::plset");
  227. }
  228. template<typename Scalar,bool ConjLhs,bool ConjRhs> void test_conj_helper(Scalar* data1, Scalar* data2, Scalar* ref, Scalar* pval)
  229. {
  230. typedef typename internal::packet_traits<Scalar>::type Packet;
  231. const int PacketSize = internal::packet_traits<Scalar>::size;
  232. internal::conj_if<ConjLhs> cj0;
  233. internal::conj_if<ConjRhs> cj1;
  234. internal::conj_helper<Scalar,Scalar,ConjLhs,ConjRhs> cj;
  235. internal::conj_helper<Packet,Packet,ConjLhs,ConjRhs> pcj;
  236. for(int i=0;i<PacketSize;++i)
  237. {
  238. ref[i] = cj0(data1[i]) * cj1(data2[i]);
  239. VERIFY(internal::isApprox(ref[i], cj.pmul(data1[i],data2[i])) && "conj_helper pmul");
  240. }
  241. internal::pstore(pval,pcj.pmul(internal::pload<Packet>(data1),internal::pload<Packet>(data2)));
  242. VERIFY(areApprox(ref, pval, PacketSize) && "conj_helper pmul");
  243. for(int i=0;i<PacketSize;++i)
  244. {
  245. Scalar tmp = ref[i];
  246. ref[i] += cj0(data1[i]) * cj1(data2[i]);
  247. VERIFY(internal::isApprox(ref[i], cj.pmadd(data1[i],data2[i],tmp)) && "conj_helper pmadd");
  248. }
  249. internal::pstore(pval,pcj.pmadd(internal::pload<Packet>(data1),internal::pload<Packet>(data2),internal::pload<Packet>(pval)));
  250. VERIFY(areApprox(ref, pval, PacketSize) && "conj_helper pmadd");
  251. }
  252. template<typename Scalar> void packetmath_complex()
  253. {
  254. typedef typename internal::packet_traits<Scalar>::type Packet;
  255. const int PacketSize = internal::packet_traits<Scalar>::size;
  256. const int size = PacketSize*4;
  257. EIGEN_ALIGN16 Scalar data1[PacketSize*4];
  258. EIGEN_ALIGN16 Scalar data2[PacketSize*4];
  259. EIGEN_ALIGN16 Scalar ref[PacketSize*4];
  260. EIGEN_ALIGN16 Scalar pval[PacketSize*4];
  261. for (int i=0; i<size; ++i)
  262. {
  263. data1[i] = internal::random<Scalar>() * Scalar(1e2);
  264. data2[i] = internal::random<Scalar>() * Scalar(1e2);
  265. }
  266. test_conj_helper<Scalar,false,false> (data1,data2,ref,pval);
  267. test_conj_helper<Scalar,false,true> (data1,data2,ref,pval);
  268. test_conj_helper<Scalar,true,false> (data1,data2,ref,pval);
  269. test_conj_helper<Scalar,true,true> (data1,data2,ref,pval);
  270. {
  271. for(int i=0;i<PacketSize;++i)
  272. ref[i] = Scalar(std::imag(data1[i]),std::real(data1[i]));
  273. internal::pstore(pval,internal::pcplxflip(internal::pload<Packet>(data1)));
  274. VERIFY(areApprox(ref, pval, PacketSize) && "pcplxflip");
  275. }
  276. }
  277. void test_packetmath()
  278. {
  279. for(int i = 0; i < g_repeat; i++) {
  280. CALL_SUBTEST_1( packetmath<float>() );
  281. CALL_SUBTEST_2( packetmath<double>() );
  282. CALL_SUBTEST_3( packetmath<int>() );
  283. CALL_SUBTEST_1( packetmath<std::complex<float> >() );
  284. CALL_SUBTEST_2( packetmath<std::complex<double> >() );
  285. CALL_SUBTEST_1( packetmath_real<float>() );
  286. CALL_SUBTEST_2( packetmath_real<double>() );
  287. CALL_SUBTEST_1( packetmath_complex<std::complex<float> >() );
  288. CALL_SUBTEST_2( packetmath_complex<std::complex<double> >() );
  289. }
  290. }