btQuadWord.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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) btQuadWord
  24. #else
  25. class btQuadWord
  26. #endif
  27. {
  28. protected:
  29. #if defined (__SPU__) && defined (__CELLOS_LV2__)
  30. union {
  31. vec_float4 mVec128;
  32. btScalar m_floats[4];
  33. };
  34. public:
  35. vec_float4 get128() const
  36. {
  37. return mVec128;
  38. }
  39. protected:
  40. #else //__CELLOS_LV2__ __SPU__
  41. #if defined(BT_USE_SSE) || defined(BT_USE_NEON)
  42. union {
  43. btSimdFloat4 mVec128;
  44. btScalar m_floats[4];
  45. };
  46. public:
  47. SIMD_FORCE_INLINE btSimdFloat4 get128() const
  48. {
  49. return mVec128;
  50. }
  51. SIMD_FORCE_INLINE void set128(btSimdFloat4 v128)
  52. {
  53. mVec128 = v128;
  54. }
  55. #else
  56. btScalar m_floats[4];
  57. #endif // BT_USE_SSE
  58. #endif //__CELLOS_LV2__ __SPU__
  59. public:
  60. #if defined(BT_USE_SSE) || defined(BT_USE_NEON)
  61. // Set Vector
  62. SIMD_FORCE_INLINE btQuadWord(const btSimdFloat4 vec)
  63. {
  64. mVec128 = vec;
  65. }
  66. // Copy constructor
  67. SIMD_FORCE_INLINE btQuadWord(const btQuadWord& rhs)
  68. {
  69. mVec128 = rhs.mVec128;
  70. }
  71. // Assignment Operator
  72. SIMD_FORCE_INLINE btQuadWord&
  73. operator=(const btQuadWord& v)
  74. {
  75. mVec128 = v.mVec128;
  76. return *this;
  77. }
  78. #endif
  79. /**@brief Return the x value */
  80. SIMD_FORCE_INLINE const btScalar& getX() const { return m_floats[0]; }
  81. /**@brief Return the y value */
  82. SIMD_FORCE_INLINE const btScalar& getY() const { return m_floats[1]; }
  83. /**@brief Return the z value */
  84. SIMD_FORCE_INLINE const btScalar& getZ() const { return m_floats[2]; }
  85. /**@brief Set the x value */
  86. SIMD_FORCE_INLINE void setX(btScalar _x) { m_floats[0] = _x;};
  87. /**@brief Set the y value */
  88. SIMD_FORCE_INLINE void setY(btScalar _y) { m_floats[1] = _y;};
  89. /**@brief Set the z value */
  90. SIMD_FORCE_INLINE void setZ(btScalar _z) { m_floats[2] = _z;};
  91. /**@brief Set the w value */
  92. SIMD_FORCE_INLINE void setW(btScalar _w) { m_floats[3] = _w;};
  93. /**@brief Return the x value */
  94. SIMD_FORCE_INLINE const btScalar& x() const { return m_floats[0]; }
  95. /**@brief Return the y value */
  96. SIMD_FORCE_INLINE const btScalar& y() const { return m_floats[1]; }
  97. /**@brief Return the z value */
  98. SIMD_FORCE_INLINE const btScalar& z() const { return m_floats[2]; }
  99. /**@brief Return the w value */
  100. SIMD_FORCE_INLINE const btScalar& w() const { return m_floats[3]; }
  101. //SIMD_FORCE_INLINE btScalar& operator[](int i) { return (&m_floats[0])[i]; }
  102. //SIMD_FORCE_INLINE const btScalar& operator[](int i) const { return (&m_floats[0])[i]; }
  103. ///operator btScalar*() replaces operator[], using implicit conversion. We added operator != and operator == to avoid pointer comparisons.
  104. SIMD_FORCE_INLINE operator btScalar *() { return &m_floats[0]; }
  105. SIMD_FORCE_INLINE operator const btScalar *() const { return &m_floats[0]; }
  106. SIMD_FORCE_INLINE bool operator==(const btQuadWord& other) const
  107. {
  108. #ifdef BT_USE_SSE
  109. return (0xf == _mm_movemask_ps((__m128)_mm_cmpeq_ps(mVec128, other.mVec128)));
  110. #else
  111. return ((m_floats[3]==other.m_floats[3]) &&
  112. (m_floats[2]==other.m_floats[2]) &&
  113. (m_floats[1]==other.m_floats[1]) &&
  114. (m_floats[0]==other.m_floats[0]));
  115. #endif
  116. }
  117. SIMD_FORCE_INLINE bool operator!=(const btQuadWord& other) const
  118. {
  119. return !(*this == other);
  120. }
  121. /**@brief Set x,y,z and zero w
  122. * @param x Value of x
  123. * @param y Value of y
  124. * @param z Value of z
  125. */
  126. SIMD_FORCE_INLINE void setValue(const btScalar& _x, const btScalar& _y, const btScalar& _z)
  127. {
  128. m_floats[0]=_x;
  129. m_floats[1]=_y;
  130. m_floats[2]=_z;
  131. m_floats[3] = 0.f;
  132. }
  133. /* void getValue(btScalar *m) const
  134. {
  135. m[0] = m_floats[0];
  136. m[1] = m_floats[1];
  137. m[2] = m_floats[2];
  138. }
  139. */
  140. /**@brief Set the values
  141. * @param x Value of x
  142. * @param y Value of y
  143. * @param z Value of z
  144. * @param w Value of w
  145. */
  146. SIMD_FORCE_INLINE void setValue(const btScalar& _x, const btScalar& _y, const btScalar& _z,const btScalar& _w)
  147. {
  148. m_floats[0]=_x;
  149. m_floats[1]=_y;
  150. m_floats[2]=_z;
  151. m_floats[3]=_w;
  152. }
  153. /**@brief No initialization constructor */
  154. SIMD_FORCE_INLINE btQuadWord()
  155. // :m_floats[0](btScalar(0.)),m_floats[1](btScalar(0.)),m_floats[2](btScalar(0.)),m_floats[3](btScalar(0.))
  156. {
  157. }
  158. /**@brief Three argument constructor (zeros w)
  159. * @param x Value of x
  160. * @param y Value of y
  161. * @param z Value of z
  162. */
  163. SIMD_FORCE_INLINE btQuadWord(const btScalar& _x, const btScalar& _y, const btScalar& _z)
  164. {
  165. m_floats[0] = _x, m_floats[1] = _y, m_floats[2] = _z, m_floats[3] = 0.0f;
  166. }
  167. /**@brief Initializing constructor
  168. * @param x Value of x
  169. * @param y Value of y
  170. * @param z Value of z
  171. * @param w Value of w
  172. */
  173. SIMD_FORCE_INLINE btQuadWord(const btScalar& _x, const btScalar& _y, const btScalar& _z,const btScalar& _w)
  174. {
  175. m_floats[0] = _x, m_floats[1] = _y, m_floats[2] = _z, m_floats[3] = _w;
  176. }
  177. /**@brief Set each element to the max of the current values and the values of another btQuadWord
  178. * @param other The other btQuadWord to compare with
  179. */
  180. SIMD_FORCE_INLINE void setMax(const btQuadWord& other)
  181. {
  182. #ifdef BT_USE_SSE
  183. mVec128 = _mm_max_ps(mVec128, other.mVec128);
  184. #elif defined(BT_USE_NEON)
  185. mVec128 = vmaxq_f32(mVec128, other.mVec128);
  186. #else
  187. btSetMax(m_floats[0], other.m_floats[0]);
  188. btSetMax(m_floats[1], other.m_floats[1]);
  189. btSetMax(m_floats[2], other.m_floats[2]);
  190. btSetMax(m_floats[3], other.m_floats[3]);
  191. #endif
  192. }
  193. /**@brief Set each element to the min of the current values and the values of another btQuadWord
  194. * @param other The other btQuadWord to compare with
  195. */
  196. SIMD_FORCE_INLINE void setMin(const btQuadWord& other)
  197. {
  198. #ifdef BT_USE_SSE
  199. mVec128 = _mm_min_ps(mVec128, other.mVec128);
  200. #elif defined(BT_USE_NEON)
  201. mVec128 = vminq_f32(mVec128, other.mVec128);
  202. #else
  203. btSetMin(m_floats[0], other.m_floats[0]);
  204. btSetMin(m_floats[1], other.m_floats[1]);
  205. btSetMin(m_floats[2], other.m_floats[2]);
  206. btSetMin(m_floats[3], other.m_floats[3]);
  207. #endif
  208. }
  209. };
  210. #endif //BT_SIMD_QUADWORD_H