Geometry_SSE.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2009 Rohit Garg <rpg.314@gmail.com>
  5. // Copyright (C) 2009-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
  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. #ifndef EIGEN_GEOMETRY_SSE_H
  11. #define EIGEN_GEOMETRY_SSE_H
  12. namespace Eigen {
  13. namespace internal {
  14. template<class Derived, class OtherDerived>
  15. struct quat_product<Architecture::SSE, Derived, OtherDerived, float>
  16. {
  17. enum {
  18. AAlignment = traits<Derived>::Alignment,
  19. BAlignment = traits<OtherDerived>::Alignment,
  20. ResAlignment = traits<Quaternion<float> >::Alignment
  21. };
  22. static inline Quaternion<float> run(const QuaternionBase<Derived>& _a, const QuaternionBase<OtherDerived>& _b)
  23. {
  24. Quaternion<float> res;
  25. const __m128 mask = _mm_setr_ps(0.f,0.f,0.f,-0.f);
  26. __m128 a = _a.coeffs().template packet<AAlignment>(0);
  27. __m128 b = _b.coeffs().template packet<BAlignment>(0);
  28. __m128 s1 = _mm_mul_ps(vec4f_swizzle1(a,1,2,0,2),vec4f_swizzle1(b,2,0,1,2));
  29. __m128 s2 = _mm_mul_ps(vec4f_swizzle1(a,3,3,3,1),vec4f_swizzle1(b,0,1,2,1));
  30. pstoret<float,Packet4f,ResAlignment>(
  31. &res.x(),
  32. _mm_add_ps(_mm_sub_ps(_mm_mul_ps(a,vec4f_swizzle1(b,3,3,3,3)),
  33. _mm_mul_ps(vec4f_swizzle1(a,2,0,1,0),
  34. vec4f_swizzle1(b,1,2,0,0))),
  35. _mm_xor_ps(mask,_mm_add_ps(s1,s2))));
  36. return res;
  37. }
  38. };
  39. template<class Derived>
  40. struct quat_conj<Architecture::SSE, Derived, float>
  41. {
  42. enum {
  43. ResAlignment = traits<Quaternion<float> >::Alignment
  44. };
  45. static inline Quaternion<float> run(const QuaternionBase<Derived>& q)
  46. {
  47. Quaternion<float> res;
  48. const __m128 mask = _mm_setr_ps(-0.f,-0.f,-0.f,0.f);
  49. pstoret<float,Packet4f,ResAlignment>(&res.x(), _mm_xor_ps(mask, q.coeffs().template packet<traits<Derived>::Alignment>(0)));
  50. return res;
  51. }
  52. };
  53. template<typename VectorLhs,typename VectorRhs>
  54. struct cross3_impl<Architecture::SSE,VectorLhs,VectorRhs,float,true>
  55. {
  56. enum {
  57. ResAlignment = traits<typename plain_matrix_type<VectorLhs>::type>::Alignment
  58. };
  59. static inline typename plain_matrix_type<VectorLhs>::type
  60. run(const VectorLhs& lhs, const VectorRhs& rhs)
  61. {
  62. __m128 a = lhs.template packet<traits<VectorLhs>::Alignment>(0);
  63. __m128 b = rhs.template packet<traits<VectorRhs>::Alignment>(0);
  64. __m128 mul1=_mm_mul_ps(vec4f_swizzle1(a,1,2,0,3),vec4f_swizzle1(b,2,0,1,3));
  65. __m128 mul2=_mm_mul_ps(vec4f_swizzle1(a,2,0,1,3),vec4f_swizzle1(b,1,2,0,3));
  66. typename plain_matrix_type<VectorLhs>::type res;
  67. pstoret<float,Packet4f,ResAlignment>(&res.x(),_mm_sub_ps(mul1,mul2));
  68. return res;
  69. }
  70. };
  71. template<class Derived, class OtherDerived>
  72. struct quat_product<Architecture::SSE, Derived, OtherDerived, double>
  73. {
  74. enum {
  75. BAlignment = traits<OtherDerived>::Alignment,
  76. ResAlignment = traits<Quaternion<double> >::Alignment
  77. };
  78. static inline Quaternion<double> run(const QuaternionBase<Derived>& _a, const QuaternionBase<OtherDerived>& _b)
  79. {
  80. const Packet2d mask = _mm_castsi128_pd(_mm_set_epi32(0x0,0x0,0x80000000,0x0));
  81. Quaternion<double> res;
  82. const double* a = _a.coeffs().data();
  83. Packet2d b_xy = _b.coeffs().template packet<BAlignment>(0);
  84. Packet2d b_zw = _b.coeffs().template packet<BAlignment>(2);
  85. Packet2d a_xx = pset1<Packet2d>(a[0]);
  86. Packet2d a_yy = pset1<Packet2d>(a[1]);
  87. Packet2d a_zz = pset1<Packet2d>(a[2]);
  88. Packet2d a_ww = pset1<Packet2d>(a[3]);
  89. // two temporaries:
  90. Packet2d t1, t2;
  91. /*
  92. * t1 = ww*xy + yy*zw
  93. * t2 = zz*xy - xx*zw
  94. * res.xy = t1 +/- swap(t2)
  95. */
  96. t1 = padd(pmul(a_ww, b_xy), pmul(a_yy, b_zw));
  97. t2 = psub(pmul(a_zz, b_xy), pmul(a_xx, b_zw));
  98. #ifdef EIGEN_VECTORIZE_SSE3
  99. EIGEN_UNUSED_VARIABLE(mask)
  100. pstoret<double,Packet2d,ResAlignment>(&res.x(), _mm_addsub_pd(t1, preverse(t2)));
  101. #else
  102. pstoret<double,Packet2d,ResAlignment>(&res.x(), padd(t1, pxor(mask,preverse(t2))));
  103. #endif
  104. /*
  105. * t1 = ww*zw - yy*xy
  106. * t2 = zz*zw + xx*xy
  107. * res.zw = t1 -/+ swap(t2) = swap( swap(t1) +/- t2)
  108. */
  109. t1 = psub(pmul(a_ww, b_zw), pmul(a_yy, b_xy));
  110. t2 = padd(pmul(a_zz, b_zw), pmul(a_xx, b_xy));
  111. #ifdef EIGEN_VECTORIZE_SSE3
  112. EIGEN_UNUSED_VARIABLE(mask)
  113. pstoret<double,Packet2d,ResAlignment>(&res.z(), preverse(_mm_addsub_pd(preverse(t1), t2)));
  114. #else
  115. pstoret<double,Packet2d,ResAlignment>(&res.z(), psub(t1, pxor(mask,preverse(t2))));
  116. #endif
  117. return res;
  118. }
  119. };
  120. template<class Derived>
  121. struct quat_conj<Architecture::SSE, Derived, double>
  122. {
  123. enum {
  124. ResAlignment = traits<Quaternion<double> >::Alignment
  125. };
  126. static inline Quaternion<double> run(const QuaternionBase<Derived>& q)
  127. {
  128. Quaternion<double> res;
  129. const __m128d mask0 = _mm_setr_pd(-0.,-0.);
  130. const __m128d mask2 = _mm_setr_pd(-0.,0.);
  131. pstoret<double,Packet2d,ResAlignment>(&res.x(), _mm_xor_pd(mask0, q.coeffs().template packet<traits<Derived>::Alignment>(0)));
  132. pstoret<double,Packet2d,ResAlignment>(&res.z(), _mm_xor_pd(mask2, q.coeffs().template packet<traits<Derived>::Alignment>(2)));
  133. return res;
  134. }
  135. };
  136. } // end namespace internal
  137. } // end namespace Eigen
  138. #endif // EIGEN_GEOMETRY_SSE_H