b3QuadWord.h 6.6 KB

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