btQuadWord.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. Copyright (c) 2003-2006 Gino van den Bergen / Erwin Coumans http://continuousphysics.com/Bullet/
  3. This software is provided 'as-is', without any express or implied warranty.
  4. In no event will the authors be held liable for any damages arising from the use of this software.
  5. Permission is granted to anyone to use this software for any purpose,
  6. including commercial applications, and to alter it and redistribute it freely,
  7. subject to the following restrictions:
  8. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  9. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  10. 3. This notice may not be removed or altered from any source distribution.
  11. */
  12. #ifndef BT_SIMD_QUADWORD_H
  13. #define BT_SIMD_QUADWORD_H
  14. #include "btScalar.h"
  15. #include "btMinMax.h"
  16. #if defined(__CELLOS_LV2) && defined(__SPU__)
  17. #include <altivec.h>
  18. #endif
  19. /**@brief The btQuadWord class is base class for btVector3 and btQuaternion.
  20. * Some issues under PS3 Linux with IBM 2.1 SDK, gcc compiler prevent from using aligned quadword.
  21. */
  22. #ifndef USE_LIBSPE2
  23. ATTRIBUTE_ALIGNED16(class)
  24. btQuadWord
  25. #else
  26. class btQuadWord
  27. #endif
  28. {
  29. protected:
  30. #if defined(__SPU__) && defined(__CELLOS_LV2__)
  31. union {
  32. vec_float4 mVec128;
  33. btScalar m_floats[4];
  34. };
  35. public:
  36. vec_float4 get128() const
  37. {
  38. return mVec128;
  39. }
  40. protected:
  41. #else //__CELLOS_LV2__ __SPU__
  42. #if defined(BT_USE_SSE) || defined(BT_USE_NEON)
  43. union {
  44. btSimdFloat4 mVec128;
  45. btScalar m_floats[4];
  46. };
  47. public:
  48. SIMD_FORCE_INLINE btSimdFloat4 get128() const
  49. {
  50. return mVec128;
  51. }
  52. SIMD_FORCE_INLINE void set128(btSimdFloat4 v128)
  53. {
  54. mVec128 = v128;
  55. }
  56. #else
  57. btScalar m_floats[4];
  58. #endif // BT_USE_SSE
  59. #endif //__CELLOS_LV2__ __SPU__
  60. public:
  61. #if (defined(BT_USE_SSE_IN_API) && defined(BT_USE_SSE)) || defined(BT_USE_NEON)
  62. // Set Vector
  63. SIMD_FORCE_INLINE btQuadWord(const btSimdFloat4 vec)
  64. {
  65. mVec128 = vec;
  66. }
  67. // Copy constructor
  68. SIMD_FORCE_INLINE btQuadWord(const btQuadWord& rhs)
  69. {
  70. mVec128 = rhs.mVec128;
  71. }
  72. // Assignment Operator
  73. SIMD_FORCE_INLINE btQuadWord&
  74. operator=(const btQuadWord& v)
  75. {
  76. mVec128 = v.mVec128;
  77. return *this;
  78. }
  79. #endif
  80. /**@brief Return the x value */
  81. SIMD_FORCE_INLINE const btScalar& getX() const { return m_floats[0]; }
  82. /**@brief Return the y value */
  83. SIMD_FORCE_INLINE const btScalar& getY() const { return m_floats[1]; }
  84. /**@brief Return the z value */
  85. SIMD_FORCE_INLINE const btScalar& getZ() const { return m_floats[2]; }
  86. /**@brief Set the x value */
  87. SIMD_FORCE_INLINE void setX(btScalar _x) { m_floats[0] = _x; };
  88. /**@brief Set the y value */
  89. SIMD_FORCE_INLINE void setY(btScalar _y) { m_floats[1] = _y; };
  90. /**@brief Set the z value */
  91. SIMD_FORCE_INLINE void setZ(btScalar _z) { m_floats[2] = _z; };
  92. /**@brief Set the w value */
  93. SIMD_FORCE_INLINE void setW(btScalar _w) { m_floats[3] = _w; };
  94. /**@brief Return the x value */
  95. SIMD_FORCE_INLINE const btScalar& x() const { return m_floats[0]; }
  96. /**@brief Return the y value */
  97. SIMD_FORCE_INLINE const btScalar& y() const { return m_floats[1]; }
  98. /**@brief Return the z value */
  99. SIMD_FORCE_INLINE const btScalar& z() const { return m_floats[2]; }
  100. /**@brief Return the w value */
  101. SIMD_FORCE_INLINE const btScalar& w() const { return m_floats[3]; }
  102. //SIMD_FORCE_INLINE btScalar& operator[](int i) { return (&m_floats[0])[i]; }
  103. //SIMD_FORCE_INLINE const btScalar& operator[](int i) const { return (&m_floats[0])[i]; }
  104. ///operator btScalar*() replaces operator[], using implicit conversion. We added operator != and operator == to avoid pointer comparisons.
  105. SIMD_FORCE_INLINE operator btScalar*() { return &m_floats[0]; }
  106. SIMD_FORCE_INLINE operator const btScalar*() const { return &m_floats[0]; }
  107. SIMD_FORCE_INLINE bool operator==(const btQuadWord& other) const
  108. {
  109. #ifdef BT_USE_SSE
  110. return (0xf == _mm_movemask_ps((__m128)_mm_cmpeq_ps(mVec128, other.mVec128)));
  111. #else
  112. return ((m_floats[3] == other.m_floats[3]) &&
  113. (m_floats[2] == other.m_floats[2]) &&
  114. (m_floats[1] == other.m_floats[1]) &&
  115. (m_floats[0] == other.m_floats[0]));
  116. #endif
  117. }
  118. SIMD_FORCE_INLINE bool operator!=(const btQuadWord& other) const
  119. {
  120. return !(*this == other);
  121. }
  122. /**@brief Set x,y,z and zero w
  123. * @param x Value of x
  124. * @param y Value of y
  125. * @param z Value of z
  126. */
  127. SIMD_FORCE_INLINE void setValue(const btScalar& _x, const btScalar& _y, const btScalar& _z)
  128. {
  129. m_floats[0] = _x;
  130. m_floats[1] = _y;
  131. m_floats[2] = _z;
  132. m_floats[3] = 0.f;
  133. }
  134. /* void getValue(btScalar *m) const
  135. {
  136. m[0] = m_floats[0];
  137. m[1] = m_floats[1];
  138. m[2] = m_floats[2];
  139. }
  140. */
  141. /**@brief Set the values
  142. * @param x Value of x
  143. * @param y Value of y
  144. * @param z Value of z
  145. * @param w Value of w
  146. */
  147. SIMD_FORCE_INLINE void setValue(const btScalar& _x, const btScalar& _y, const btScalar& _z, const btScalar& _w)
  148. {
  149. m_floats[0] = _x;
  150. m_floats[1] = _y;
  151. m_floats[2] = _z;
  152. m_floats[3] = _w;
  153. }
  154. /**@brief No initialization constructor */
  155. SIMD_FORCE_INLINE btQuadWord()
  156. // :m_floats[0](btScalar(0.)),m_floats[1](btScalar(0.)),m_floats[2](btScalar(0.)),m_floats[3](btScalar(0.))
  157. {
  158. }
  159. /**@brief Three argument constructor (zeros w)
  160. * @param x Value of x
  161. * @param y Value of y
  162. * @param z Value of z
  163. */
  164. SIMD_FORCE_INLINE btQuadWord(const btScalar& _x, const btScalar& _y, const btScalar& _z)
  165. {
  166. m_floats[0] = _x, m_floats[1] = _y, m_floats[2] = _z, m_floats[3] = 0.0f;
  167. }
  168. /**@brief Initializing constructor
  169. * @param x Value of x
  170. * @param y Value of y
  171. * @param z Value of z
  172. * @param w Value of w
  173. */
  174. SIMD_FORCE_INLINE btQuadWord(const btScalar& _x, const btScalar& _y, const btScalar& _z, const btScalar& _w)
  175. {
  176. m_floats[0] = _x, m_floats[1] = _y, m_floats[2] = _z, m_floats[3] = _w;
  177. }
  178. /**@brief Set each element to the max of the current values and the values of another btQuadWord
  179. * @param other The other btQuadWord to compare with
  180. */
  181. SIMD_FORCE_INLINE void setMax(const btQuadWord& other)
  182. {
  183. #ifdef BT_USE_SSE
  184. mVec128 = _mm_max_ps(mVec128, other.mVec128);
  185. #elif defined(BT_USE_NEON)
  186. mVec128 = vmaxq_f32(mVec128, other.mVec128);
  187. #else
  188. btSetMax(m_floats[0], other.m_floats[0]);
  189. btSetMax(m_floats[1], other.m_floats[1]);
  190. btSetMax(m_floats[2], other.m_floats[2]);
  191. btSetMax(m_floats[3], other.m_floats[3]);
  192. #endif
  193. }
  194. /**@brief Set each element to the min of the current values and the values of another btQuadWord
  195. * @param other The other btQuadWord to compare with
  196. */
  197. SIMD_FORCE_INLINE void setMin(const btQuadWord& other)
  198. {
  199. #ifdef BT_USE_SSE
  200. mVec128 = _mm_min_ps(mVec128, other.mVec128);
  201. #elif defined(BT_USE_NEON)
  202. mVec128 = vminq_f32(mVec128, other.mVec128);
  203. #else
  204. btSetMin(m_floats[0], other.m_floats[0]);
  205. btSetMin(m_floats[1], other.m_floats[1]);
  206. btSetMin(m_floats[2], other.m_floats[2]);
  207. btSetMin(m_floats[3], other.m_floats[3]);
  208. #endif
  209. }
  210. };
  211. #endif //BT_SIMD_QUADWORD_H