btVector3.h 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  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_VECTOR3_H
  13. #define BT_VECTOR3_H
  14. //#include <stdint.h>
  15. #include "btScalar.h"
  16. #include "btMinMax.h"
  17. #include "btAlignedAllocator.h"
  18. #ifdef BT_USE_DOUBLE_PRECISION
  19. #define btVector3Data btVector3DoubleData
  20. #define btVector3DataName "btVector3DoubleData"
  21. #else
  22. #define btVector3Data btVector3FloatData
  23. #define btVector3DataName "btVector3FloatData"
  24. #endif //BT_USE_DOUBLE_PRECISION
  25. #if defined BT_USE_SSE
  26. //typedef uint32_t __m128i __attribute__ ((vector_size(16)));
  27. #ifdef _MSC_VER
  28. #pragma warning(disable: 4556) // value of intrinsic immediate argument '4294967239' is out of range '0 - 255'
  29. #endif
  30. #define BT_SHUFFLE(x,y,z,w) ((w)<<6 | (z)<<4 | (y)<<2 | (x))
  31. //#define bt_pshufd_ps( _a, _mask ) (__m128) _mm_shuffle_epi32((__m128i)(_a), (_mask) )
  32. #define bt_pshufd_ps( _a, _mask ) _mm_shuffle_ps((_a), (_a), (_mask) )
  33. #define bt_splat3_ps( _a, _i ) bt_pshufd_ps((_a), BT_SHUFFLE(_i,_i,_i, 3) )
  34. #define bt_splat_ps( _a, _i ) bt_pshufd_ps((_a), BT_SHUFFLE(_i,_i,_i,_i) )
  35. #define btv3AbsiMask (_mm_set_epi32(0x00000000, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF))
  36. #define btvAbsMask (_mm_set_epi32( 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF))
  37. #define btvFFF0Mask (_mm_set_epi32(0x00000000, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF))
  38. #define btv3AbsfMask btCastiTo128f(btv3AbsiMask)
  39. #define btvFFF0fMask btCastiTo128f(btvFFF0Mask)
  40. #define btvxyzMaskf btvFFF0fMask
  41. #define btvAbsfMask btCastiTo128f(btvAbsMask)
  42. //there is an issue with XCode 3.2 (LCx errors)
  43. #define btvMzeroMask (_mm_set_ps(-0.0f, -0.0f, -0.0f, -0.0f))
  44. #define v1110 (_mm_set_ps(0.0f, 1.0f, 1.0f, 1.0f))
  45. #define vHalf (_mm_set_ps(0.5f, 0.5f, 0.5f, 0.5f))
  46. #define v1_5 (_mm_set_ps(1.5f, 1.5f, 1.5f, 1.5f))
  47. //const __m128 ATTRIBUTE_ALIGNED16(btvMzeroMask) = {-0.0f, -0.0f, -0.0f, -0.0f};
  48. //const __m128 ATTRIBUTE_ALIGNED16(v1110) = {1.0f, 1.0f, 1.0f, 0.0f};
  49. //const __m128 ATTRIBUTE_ALIGNED16(vHalf) = {0.5f, 0.5f, 0.5f, 0.5f};
  50. //const __m128 ATTRIBUTE_ALIGNED16(v1_5) = {1.5f, 1.5f, 1.5f, 1.5f};
  51. #endif
  52. #ifdef BT_USE_NEON
  53. const float32x4_t ATTRIBUTE_ALIGNED16(btvMzeroMask) = (float32x4_t){-0.0f, -0.0f, -0.0f, -0.0f};
  54. const int32x4_t ATTRIBUTE_ALIGNED16(btvFFF0Mask) = (int32x4_t){static_cast<int32_t>(0xFFFFFFFF),
  55. static_cast<int32_t>(0xFFFFFFFF), static_cast<int32_t>(0xFFFFFFFF), 0x0};
  56. const int32x4_t ATTRIBUTE_ALIGNED16(btvAbsMask) = (int32x4_t){0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF};
  57. const int32x4_t ATTRIBUTE_ALIGNED16(btv3AbsMask) = (int32x4_t){0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x0};
  58. #endif
  59. /**@brief btVector3 can be used to represent 3D points and vectors.
  60. * It has an un-used w component to suit 16-byte alignment when btVector3 is stored in containers. This extra component can be used by derived classes (Quaternion?) or by user
  61. * Ideally, this class should be replaced by a platform optimized SIMD version that keeps the data in registers
  62. */
  63. ATTRIBUTE_ALIGNED16(class) btVector3
  64. {
  65. public:
  66. BT_DECLARE_ALIGNED_ALLOCATOR();
  67. #if defined (__SPU__) && defined (__CELLOS_LV2__)
  68. btScalar m_floats[4];
  69. public:
  70. SIMD_FORCE_INLINE const vec_float4& get128() const
  71. {
  72. return *((const vec_float4*)&m_floats[0]);
  73. }
  74. public:
  75. #else //__CELLOS_LV2__ __SPU__
  76. #if defined (BT_USE_SSE) || defined(BT_USE_NEON) // _WIN32 || ARM
  77. union {
  78. btSimdFloat4 mVec128;
  79. btScalar m_floats[4];
  80. };
  81. SIMD_FORCE_INLINE btSimdFloat4 get128() const
  82. {
  83. return mVec128;
  84. }
  85. SIMD_FORCE_INLINE void set128(btSimdFloat4 v128)
  86. {
  87. mVec128 = v128;
  88. }
  89. #else
  90. btScalar m_floats[4];
  91. #endif
  92. #endif //__CELLOS_LV2__ __SPU__
  93. public:
  94. /**@brief No initialization constructor */
  95. SIMD_FORCE_INLINE btVector3()
  96. {
  97. }
  98. /**@brief Constructor from scalars
  99. * @param x X value
  100. * @param y Y value
  101. * @param z Z value
  102. */
  103. SIMD_FORCE_INLINE btVector3(const btScalar& _x, const btScalar& _y, const btScalar& _z)
  104. {
  105. m_floats[0] = _x;
  106. m_floats[1] = _y;
  107. m_floats[2] = _z;
  108. m_floats[3] = btScalar(0.f);
  109. }
  110. #if (defined (BT_USE_SSE_IN_API) && defined (BT_USE_SSE) )|| defined (BT_USE_NEON)
  111. // Set Vector
  112. SIMD_FORCE_INLINE btVector3( btSimdFloat4 v)
  113. {
  114. mVec128 = v;
  115. }
  116. // Copy constructor
  117. SIMD_FORCE_INLINE btVector3(const btVector3& rhs)
  118. {
  119. mVec128 = rhs.mVec128;
  120. }
  121. // Assignment Operator
  122. SIMD_FORCE_INLINE btVector3&
  123. operator=(const btVector3& v)
  124. {
  125. mVec128 = v.mVec128;
  126. return *this;
  127. }
  128. #endif // #if defined (BT_USE_SSE_IN_API) || defined (BT_USE_NEON)
  129. /**@brief Add a vector to this one
  130. * @param The vector to add to this one */
  131. SIMD_FORCE_INLINE btVector3& operator+=(const btVector3& v)
  132. {
  133. #if defined(BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  134. mVec128 = _mm_add_ps(mVec128, v.mVec128);
  135. #elif defined(BT_USE_NEON)
  136. mVec128 = vaddq_f32(mVec128, v.mVec128);
  137. #else
  138. m_floats[0] += v.m_floats[0];
  139. m_floats[1] += v.m_floats[1];
  140. m_floats[2] += v.m_floats[2];
  141. #endif
  142. return *this;
  143. }
  144. /**@brief Subtract a vector from this one
  145. * @param The vector to subtract */
  146. SIMD_FORCE_INLINE btVector3& operator-=(const btVector3& v)
  147. {
  148. #if defined(BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  149. mVec128 = _mm_sub_ps(mVec128, v.mVec128);
  150. #elif defined(BT_USE_NEON)
  151. mVec128 = vsubq_f32(mVec128, v.mVec128);
  152. #else
  153. m_floats[0] -= v.m_floats[0];
  154. m_floats[1] -= v.m_floats[1];
  155. m_floats[2] -= v.m_floats[2];
  156. #endif
  157. return *this;
  158. }
  159. /**@brief Scale the vector
  160. * @param s Scale factor */
  161. SIMD_FORCE_INLINE btVector3& operator*=(const btScalar& s)
  162. {
  163. #if defined(BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  164. __m128 vs = _mm_load_ss(&s); // (S 0 0 0)
  165. vs = bt_pshufd_ps(vs, 0x80); // (S S S 0.0)
  166. mVec128 = _mm_mul_ps(mVec128, vs);
  167. #elif defined(BT_USE_NEON)
  168. mVec128 = vmulq_n_f32(mVec128, s);
  169. #else
  170. m_floats[0] *= s;
  171. m_floats[1] *= s;
  172. m_floats[2] *= s;
  173. #endif
  174. return *this;
  175. }
  176. /**@brief Inversely scale the vector
  177. * @param s Scale factor to divide by */
  178. SIMD_FORCE_INLINE btVector3& operator/=(const btScalar& s)
  179. {
  180. btFullAssert(s != btScalar(0.0));
  181. #if 0 //defined(BT_USE_SSE_IN_API)
  182. // this code is not faster !
  183. __m128 vs = _mm_load_ss(&s);
  184. vs = _mm_div_ss(v1110, vs);
  185. vs = bt_pshufd_ps(vs, 0x00); // (S S S S)
  186. mVec128 = _mm_mul_ps(mVec128, vs);
  187. return *this;
  188. #else
  189. return *this *= btScalar(1.0) / s;
  190. #endif
  191. }
  192. /**@brief Return the dot product
  193. * @param v The other vector in the dot product */
  194. SIMD_FORCE_INLINE btScalar dot(const btVector3& v) const
  195. {
  196. #if defined BT_USE_SIMD_VECTOR3 && defined (BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  197. __m128 vd = _mm_mul_ps(mVec128, v.mVec128);
  198. __m128 z = _mm_movehl_ps(vd, vd);
  199. __m128 y = _mm_shuffle_ps(vd, vd, 0x55);
  200. vd = _mm_add_ss(vd, y);
  201. vd = _mm_add_ss(vd, z);
  202. return _mm_cvtss_f32(vd);
  203. #elif defined(BT_USE_NEON)
  204. float32x4_t vd = vmulq_f32(mVec128, v.mVec128);
  205. float32x2_t x = vpadd_f32(vget_low_f32(vd), vget_low_f32(vd));
  206. x = vadd_f32(x, vget_high_f32(vd));
  207. return vget_lane_f32(x, 0);
  208. #else
  209. return m_floats[0] * v.m_floats[0] +
  210. m_floats[1] * v.m_floats[1] +
  211. m_floats[2] * v.m_floats[2];
  212. #endif
  213. }
  214. /**@brief Return the length of the vector squared */
  215. SIMD_FORCE_INLINE btScalar length2() const
  216. {
  217. return dot(*this);
  218. }
  219. /**@brief Return the length of the vector */
  220. SIMD_FORCE_INLINE btScalar length() const
  221. {
  222. return btSqrt(length2());
  223. }
  224. /**@brief Return the norm (length) of the vector */
  225. SIMD_FORCE_INLINE btScalar norm() const
  226. {
  227. return length();
  228. }
  229. /**@brief Return the distance squared between the ends of this and another vector
  230. * This is symantically treating the vector like a point */
  231. SIMD_FORCE_INLINE btScalar distance2(const btVector3& v) const;
  232. /**@brief Return the distance between the ends of this and another vector
  233. * This is symantically treating the vector like a point */
  234. SIMD_FORCE_INLINE btScalar distance(const btVector3& v) const;
  235. SIMD_FORCE_INLINE btVector3& safeNormalize()
  236. {
  237. btVector3 absVec = this->absolute();
  238. int maxIndex = absVec.maxAxis();
  239. if (absVec[maxIndex]>0)
  240. {
  241. *this /= absVec[maxIndex];
  242. return *this /= length();
  243. }
  244. setValue(1,0,0);
  245. return *this;
  246. }
  247. /**@brief Normalize this vector
  248. * x^2 + y^2 + z^2 = 1 */
  249. SIMD_FORCE_INLINE btVector3& normalize()
  250. {
  251. btAssert(length() != btScalar(0));
  252. #if defined(BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  253. // dot product first
  254. __m128 vd = _mm_mul_ps(mVec128, mVec128);
  255. __m128 z = _mm_movehl_ps(vd, vd);
  256. __m128 y = _mm_shuffle_ps(vd, vd, 0x55);
  257. vd = _mm_add_ss(vd, y);
  258. vd = _mm_add_ss(vd, z);
  259. #if 0
  260. vd = _mm_sqrt_ss(vd);
  261. vd = _mm_div_ss(v1110, vd);
  262. vd = bt_splat_ps(vd, 0x80);
  263. mVec128 = _mm_mul_ps(mVec128, vd);
  264. #else
  265. // NR step 1/sqrt(x) - vd is x, y is output
  266. y = _mm_rsqrt_ss(vd); // estimate
  267. // one step NR
  268. z = v1_5;
  269. vd = _mm_mul_ss(vd, vHalf); // vd * 0.5
  270. //x2 = vd;
  271. vd = _mm_mul_ss(vd, y); // vd * 0.5 * y0
  272. vd = _mm_mul_ss(vd, y); // vd * 0.5 * y0 * y0
  273. z = _mm_sub_ss(z, vd); // 1.5 - vd * 0.5 * y0 * y0
  274. y = _mm_mul_ss(y, z); // y0 * (1.5 - vd * 0.5 * y0 * y0)
  275. y = bt_splat_ps(y, 0x80);
  276. mVec128 = _mm_mul_ps(mVec128, y);
  277. #endif
  278. return *this;
  279. #else
  280. return *this /= length();
  281. #endif
  282. }
  283. /**@brief Return a normalized version of this vector */
  284. SIMD_FORCE_INLINE btVector3 normalized() const;
  285. /**@brief Return a rotated version of this vector
  286. * @param wAxis The axis to rotate about
  287. * @param angle The angle to rotate by */
  288. SIMD_FORCE_INLINE btVector3 rotate( const btVector3& wAxis, const btScalar angle ) const;
  289. /**@brief Return the angle between this and another vector
  290. * @param v The other vector */
  291. SIMD_FORCE_INLINE btScalar angle(const btVector3& v) const
  292. {
  293. btScalar s = btSqrt(length2() * v.length2());
  294. btFullAssert(s != btScalar(0.0));
  295. return btAcos(dot(v) / s);
  296. }
  297. /**@brief Return a vector will the absolute values of each element */
  298. SIMD_FORCE_INLINE btVector3 absolute() const
  299. {
  300. #if defined BT_USE_SIMD_VECTOR3 && defined (BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  301. return btVector3(_mm_and_ps(mVec128, btv3AbsfMask));
  302. #elif defined(BT_USE_NEON)
  303. return btVector3(vabsq_f32(mVec128));
  304. #else
  305. return btVector3(
  306. btFabs(m_floats[0]),
  307. btFabs(m_floats[1]),
  308. btFabs(m_floats[2]));
  309. #endif
  310. }
  311. /**@brief Return the cross product between this and another vector
  312. * @param v The other vector */
  313. SIMD_FORCE_INLINE btVector3 cross(const btVector3& v) const
  314. {
  315. #if defined(BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  316. __m128 T, V;
  317. T = bt_pshufd_ps(mVec128, BT_SHUFFLE(1, 2, 0, 3)); // (Y Z X 0)
  318. V = bt_pshufd_ps(v.mVec128, BT_SHUFFLE(1, 2, 0, 3)); // (Y Z X 0)
  319. V = _mm_mul_ps(V, mVec128);
  320. T = _mm_mul_ps(T, v.mVec128);
  321. V = _mm_sub_ps(V, T);
  322. V = bt_pshufd_ps(V, BT_SHUFFLE(1, 2, 0, 3));
  323. return btVector3(V);
  324. #elif defined(BT_USE_NEON)
  325. float32x4_t T, V;
  326. // form (Y, Z, X, _) of mVec128 and v.mVec128
  327. float32x2_t Tlow = vget_low_f32(mVec128);
  328. float32x2_t Vlow = vget_low_f32(v.mVec128);
  329. T = vcombine_f32(vext_f32(Tlow, vget_high_f32(mVec128), 1), Tlow);
  330. V = vcombine_f32(vext_f32(Vlow, vget_high_f32(v.mVec128), 1), Vlow);
  331. V = vmulq_f32(V, mVec128);
  332. T = vmulq_f32(T, v.mVec128);
  333. V = vsubq_f32(V, T);
  334. Vlow = vget_low_f32(V);
  335. // form (Y, Z, X, _);
  336. V = vcombine_f32(vext_f32(Vlow, vget_high_f32(V), 1), Vlow);
  337. V = (float32x4_t)vandq_s32((int32x4_t)V, btvFFF0Mask);
  338. return btVector3(V);
  339. #else
  340. return btVector3(
  341. m_floats[1] * v.m_floats[2] - m_floats[2] * v.m_floats[1],
  342. m_floats[2] * v.m_floats[0] - m_floats[0] * v.m_floats[2],
  343. m_floats[0] * v.m_floats[1] - m_floats[1] * v.m_floats[0]);
  344. #endif
  345. }
  346. SIMD_FORCE_INLINE btScalar triple(const btVector3& v1, const btVector3& v2) const
  347. {
  348. #if defined BT_USE_SIMD_VECTOR3 && defined (BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  349. // cross:
  350. __m128 T = _mm_shuffle_ps(v1.mVec128, v1.mVec128, BT_SHUFFLE(1, 2, 0, 3)); // (Y Z X 0)
  351. __m128 V = _mm_shuffle_ps(v2.mVec128, v2.mVec128, BT_SHUFFLE(1, 2, 0, 3)); // (Y Z X 0)
  352. V = _mm_mul_ps(V, v1.mVec128);
  353. T = _mm_mul_ps(T, v2.mVec128);
  354. V = _mm_sub_ps(V, T);
  355. V = _mm_shuffle_ps(V, V, BT_SHUFFLE(1, 2, 0, 3));
  356. // dot:
  357. V = _mm_mul_ps(V, mVec128);
  358. __m128 z = _mm_movehl_ps(V, V);
  359. __m128 y = _mm_shuffle_ps(V, V, 0x55);
  360. V = _mm_add_ss(V, y);
  361. V = _mm_add_ss(V, z);
  362. return _mm_cvtss_f32(V);
  363. #elif defined(BT_USE_NEON)
  364. // cross:
  365. float32x4_t T, V;
  366. // form (Y, Z, X, _) of mVec128 and v.mVec128
  367. float32x2_t Tlow = vget_low_f32(v1.mVec128);
  368. float32x2_t Vlow = vget_low_f32(v2.mVec128);
  369. T = vcombine_f32(vext_f32(Tlow, vget_high_f32(v1.mVec128), 1), Tlow);
  370. V = vcombine_f32(vext_f32(Vlow, vget_high_f32(v2.mVec128), 1), Vlow);
  371. V = vmulq_f32(V, v1.mVec128);
  372. T = vmulq_f32(T, v2.mVec128);
  373. V = vsubq_f32(V, T);
  374. Vlow = vget_low_f32(V);
  375. // form (Y, Z, X, _);
  376. V = vcombine_f32(vext_f32(Vlow, vget_high_f32(V), 1), Vlow);
  377. // dot:
  378. V = vmulq_f32(mVec128, V);
  379. float32x2_t x = vpadd_f32(vget_low_f32(V), vget_low_f32(V));
  380. x = vadd_f32(x, vget_high_f32(V));
  381. return vget_lane_f32(x, 0);
  382. #else
  383. return
  384. m_floats[0] * (v1.m_floats[1] * v2.m_floats[2] - v1.m_floats[2] * v2.m_floats[1]) +
  385. m_floats[1] * (v1.m_floats[2] * v2.m_floats[0] - v1.m_floats[0] * v2.m_floats[2]) +
  386. m_floats[2] * (v1.m_floats[0] * v2.m_floats[1] - v1.m_floats[1] * v2.m_floats[0]);
  387. #endif
  388. }
  389. /**@brief Return the axis with the smallest value
  390. * Note return values are 0,1,2 for x, y, or z */
  391. SIMD_FORCE_INLINE int minAxis() const
  392. {
  393. return m_floats[0] < m_floats[1] ? (m_floats[0] <m_floats[2] ? 0 : 2) : (m_floats[1] <m_floats[2] ? 1 : 2);
  394. }
  395. /**@brief Return the axis with the largest value
  396. * Note return values are 0,1,2 for x, y, or z */
  397. SIMD_FORCE_INLINE int maxAxis() const
  398. {
  399. return m_floats[0] < m_floats[1] ? (m_floats[1] <m_floats[2] ? 2 : 1) : (m_floats[0] <m_floats[2] ? 2 : 0);
  400. }
  401. SIMD_FORCE_INLINE int furthestAxis() const
  402. {
  403. return absolute().minAxis();
  404. }
  405. SIMD_FORCE_INLINE int closestAxis() const
  406. {
  407. return absolute().maxAxis();
  408. }
  409. SIMD_FORCE_INLINE void setInterpolate3(const btVector3& v0, const btVector3& v1, btScalar rt)
  410. {
  411. #if defined(BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  412. __m128 vrt = _mm_load_ss(&rt); // (rt 0 0 0)
  413. btScalar s = btScalar(1.0) - rt;
  414. __m128 vs = _mm_load_ss(&s); // (S 0 0 0)
  415. vs = bt_pshufd_ps(vs, 0x80); // (S S S 0.0)
  416. __m128 r0 = _mm_mul_ps(v0.mVec128, vs);
  417. vrt = bt_pshufd_ps(vrt, 0x80); // (rt rt rt 0.0)
  418. __m128 r1 = _mm_mul_ps(v1.mVec128, vrt);
  419. __m128 tmp3 = _mm_add_ps(r0,r1);
  420. mVec128 = tmp3;
  421. #elif defined(BT_USE_NEON)
  422. mVec128 = vsubq_f32(v1.mVec128, v0.mVec128);
  423. mVec128 = vmulq_n_f32(mVec128, rt);
  424. mVec128 = vaddq_f32(mVec128, v0.mVec128);
  425. #else
  426. btScalar s = btScalar(1.0) - rt;
  427. m_floats[0] = s * v0.m_floats[0] + rt * v1.m_floats[0];
  428. m_floats[1] = s * v0.m_floats[1] + rt * v1.m_floats[1];
  429. m_floats[2] = s * v0.m_floats[2] + rt * v1.m_floats[2];
  430. //don't do the unused w component
  431. // m_co[3] = s * v0[3] + rt * v1[3];
  432. #endif
  433. }
  434. /**@brief Return the linear interpolation between this and another vector
  435. * @param v The other vector
  436. * @param t The ration of this to v (t = 0 => return this, t=1 => return other) */
  437. SIMD_FORCE_INLINE btVector3 lerp(const btVector3& v, const btScalar& t) const
  438. {
  439. #if defined(BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  440. __m128 vt = _mm_load_ss(&t); // (t 0 0 0)
  441. vt = bt_pshufd_ps(vt, 0x80); // (rt rt rt 0.0)
  442. __m128 vl = _mm_sub_ps(v.mVec128, mVec128);
  443. vl = _mm_mul_ps(vl, vt);
  444. vl = _mm_add_ps(vl, mVec128);
  445. return btVector3(vl);
  446. #elif defined(BT_USE_NEON)
  447. float32x4_t vl = vsubq_f32(v.mVec128, mVec128);
  448. vl = vmulq_n_f32(vl, t);
  449. vl = vaddq_f32(vl, mVec128);
  450. return btVector3(vl);
  451. #else
  452. return
  453. btVector3( m_floats[0] + (v.m_floats[0] - m_floats[0]) * t,
  454. m_floats[1] + (v.m_floats[1] - m_floats[1]) * t,
  455. m_floats[2] + (v.m_floats[2] - m_floats[2]) * t);
  456. #endif
  457. }
  458. /**@brief Elementwise multiply this vector by the other
  459. * @param v The other vector */
  460. SIMD_FORCE_INLINE btVector3& operator*=(const btVector3& v)
  461. {
  462. #if defined(BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  463. mVec128 = _mm_mul_ps(mVec128, v.mVec128);
  464. #elif defined(BT_USE_NEON)
  465. mVec128 = vmulq_f32(mVec128, v.mVec128);
  466. #else
  467. m_floats[0] *= v.m_floats[0];
  468. m_floats[1] *= v.m_floats[1];
  469. m_floats[2] *= v.m_floats[2];
  470. #endif
  471. return *this;
  472. }
  473. /**@brief Return the x value */
  474. SIMD_FORCE_INLINE const btScalar& getX() const { return m_floats[0]; }
  475. /**@brief Return the y value */
  476. SIMD_FORCE_INLINE const btScalar& getY() const { return m_floats[1]; }
  477. /**@brief Return the z value */
  478. SIMD_FORCE_INLINE const btScalar& getZ() const { return m_floats[2]; }
  479. /**@brief Set the x value */
  480. SIMD_FORCE_INLINE void setX(btScalar _x) { m_floats[0] = _x;};
  481. /**@brief Set the y value */
  482. SIMD_FORCE_INLINE void setY(btScalar _y) { m_floats[1] = _y;};
  483. /**@brief Set the z value */
  484. SIMD_FORCE_INLINE void setZ(btScalar _z) { m_floats[2] = _z;};
  485. /**@brief Set the w value */
  486. SIMD_FORCE_INLINE void setW(btScalar _w) { m_floats[3] = _w;};
  487. /**@brief Return the x value */
  488. SIMD_FORCE_INLINE const btScalar& x() const { return m_floats[0]; }
  489. /**@brief Return the y value */
  490. SIMD_FORCE_INLINE const btScalar& y() const { return m_floats[1]; }
  491. /**@brief Return the z value */
  492. SIMD_FORCE_INLINE const btScalar& z() const { return m_floats[2]; }
  493. /**@brief Return the w value */
  494. SIMD_FORCE_INLINE const btScalar& w() const { return m_floats[3]; }
  495. //SIMD_FORCE_INLINE btScalar& operator[](int i) { return (&m_floats[0])[i]; }
  496. //SIMD_FORCE_INLINE const btScalar& operator[](int i) const { return (&m_floats[0])[i]; }
  497. ///operator btScalar*() replaces operator[], using implicit conversion. We added operator != and operator == to avoid pointer comparisons.
  498. SIMD_FORCE_INLINE operator btScalar *() { return &m_floats[0]; }
  499. SIMD_FORCE_INLINE operator const btScalar *() const { return &m_floats[0]; }
  500. SIMD_FORCE_INLINE bool operator==(const btVector3& other) const
  501. {
  502. #if defined(BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  503. return (0xf == _mm_movemask_ps((__m128)_mm_cmpeq_ps(mVec128, other.mVec128)));
  504. #else
  505. return ((m_floats[3]==other.m_floats[3]) &&
  506. (m_floats[2]==other.m_floats[2]) &&
  507. (m_floats[1]==other.m_floats[1]) &&
  508. (m_floats[0]==other.m_floats[0]));
  509. #endif
  510. }
  511. SIMD_FORCE_INLINE bool operator!=(const btVector3& other) const
  512. {
  513. return !(*this == other);
  514. }
  515. /**@brief Set each element to the max of the current values and the values of another btVector3
  516. * @param other The other btVector3 to compare with
  517. */
  518. SIMD_FORCE_INLINE void setMax(const btVector3& other)
  519. {
  520. #if defined(BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  521. mVec128 = _mm_max_ps(mVec128, other.mVec128);
  522. #elif defined(BT_USE_NEON)
  523. mVec128 = vmaxq_f32(mVec128, other.mVec128);
  524. #else
  525. btSetMax(m_floats[0], other.m_floats[0]);
  526. btSetMax(m_floats[1], other.m_floats[1]);
  527. btSetMax(m_floats[2], other.m_floats[2]);
  528. btSetMax(m_floats[3], other.w());
  529. #endif
  530. }
  531. /**@brief Set each element to the min of the current values and the values of another btVector3
  532. * @param other The other btVector3 to compare with
  533. */
  534. SIMD_FORCE_INLINE void setMin(const btVector3& other)
  535. {
  536. #if defined(BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  537. mVec128 = _mm_min_ps(mVec128, other.mVec128);
  538. #elif defined(BT_USE_NEON)
  539. mVec128 = vminq_f32(mVec128, other.mVec128);
  540. #else
  541. btSetMin(m_floats[0], other.m_floats[0]);
  542. btSetMin(m_floats[1], other.m_floats[1]);
  543. btSetMin(m_floats[2], other.m_floats[2]);
  544. btSetMin(m_floats[3], other.w());
  545. #endif
  546. }
  547. SIMD_FORCE_INLINE void setValue(const btScalar& _x, const btScalar& _y, const btScalar& _z)
  548. {
  549. m_floats[0]=_x;
  550. m_floats[1]=_y;
  551. m_floats[2]=_z;
  552. m_floats[3] = btScalar(0.f);
  553. }
  554. void getSkewSymmetricMatrix(btVector3* v0,btVector3* v1,btVector3* v2) const
  555. {
  556. #if defined BT_USE_SIMD_VECTOR3 && defined (BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  557. __m128 V = _mm_and_ps(mVec128, btvFFF0fMask);
  558. __m128 V0 = _mm_xor_ps(btvMzeroMask, V);
  559. __m128 V2 = _mm_movelh_ps(V0, V);
  560. __m128 V1 = _mm_shuffle_ps(V, V0, 0xCE);
  561. V0 = _mm_shuffle_ps(V0, V, 0xDB);
  562. V2 = _mm_shuffle_ps(V2, V, 0xF9);
  563. v0->mVec128 = V0;
  564. v1->mVec128 = V1;
  565. v2->mVec128 = V2;
  566. #else
  567. v0->setValue(0. ,-z() ,y());
  568. v1->setValue(z() ,0. ,-x());
  569. v2->setValue(-y() ,x() ,0.);
  570. #endif
  571. }
  572. void setZero()
  573. {
  574. #if defined(BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  575. mVec128 = (__m128)_mm_xor_ps(mVec128, mVec128);
  576. #elif defined(BT_USE_NEON)
  577. int32x4_t vi = vdupq_n_s32(0);
  578. mVec128 = vreinterpretq_f32_s32(vi);
  579. #else
  580. setValue(btScalar(0.),btScalar(0.),btScalar(0.));
  581. #endif
  582. }
  583. SIMD_FORCE_INLINE bool isZero() const
  584. {
  585. return m_floats[0] == btScalar(0) && m_floats[1] == btScalar(0) && m_floats[2] == btScalar(0);
  586. }
  587. SIMD_FORCE_INLINE bool fuzzyZero() const
  588. {
  589. return length2() < SIMD_EPSILON;
  590. }
  591. SIMD_FORCE_INLINE void serialize(struct btVector3Data& dataOut) const;
  592. SIMD_FORCE_INLINE void deSerialize(const struct btVector3Data& dataIn);
  593. SIMD_FORCE_INLINE void serializeFloat(struct btVector3FloatData& dataOut) const;
  594. SIMD_FORCE_INLINE void deSerializeFloat(const struct btVector3FloatData& dataIn);
  595. SIMD_FORCE_INLINE void serializeDouble(struct btVector3DoubleData& dataOut) const;
  596. SIMD_FORCE_INLINE void deSerializeDouble(const struct btVector3DoubleData& dataIn);
  597. /**@brief returns index of maximum dot product between this and vectors in array[]
  598. * @param array The other vectors
  599. * @param array_count The number of other vectors
  600. * @param dotOut The maximum dot product */
  601. SIMD_FORCE_INLINE long maxDot( const btVector3 *array, long array_count, btScalar &dotOut ) const;
  602. /**@brief returns index of minimum dot product between this and vectors in array[]
  603. * @param array The other vectors
  604. * @param array_count The number of other vectors
  605. * @param dotOut The minimum dot product */
  606. SIMD_FORCE_INLINE long minDot( const btVector3 *array, long array_count, btScalar &dotOut ) const;
  607. /* create a vector as btVector3( this->dot( btVector3 v0 ), this->dot( btVector3 v1), this->dot( btVector3 v2 )) */
  608. SIMD_FORCE_INLINE btVector3 dot3( const btVector3 &v0, const btVector3 &v1, const btVector3 &v2 ) const
  609. {
  610. #if defined BT_USE_SIMD_VECTOR3 && defined (BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  611. __m128 a0 = _mm_mul_ps( v0.mVec128, this->mVec128 );
  612. __m128 a1 = _mm_mul_ps( v1.mVec128, this->mVec128 );
  613. __m128 a2 = _mm_mul_ps( v2.mVec128, this->mVec128 );
  614. __m128 b0 = _mm_unpacklo_ps( a0, a1 );
  615. __m128 b1 = _mm_unpackhi_ps( a0, a1 );
  616. __m128 b2 = _mm_unpacklo_ps( a2, _mm_setzero_ps() );
  617. __m128 r = _mm_movelh_ps( b0, b2 );
  618. r = _mm_add_ps( r, _mm_movehl_ps( b2, b0 ));
  619. a2 = _mm_and_ps( a2, btvxyzMaskf);
  620. r = _mm_add_ps( r, btCastdTo128f (_mm_move_sd( btCastfTo128d(a2), btCastfTo128d(b1) )));
  621. return btVector3(r);
  622. #elif defined(BT_USE_NEON)
  623. static const uint32x4_t xyzMask = (const uint32x4_t){ static_cast<uint32_t>(-1), static_cast<uint32_t>(-1), static_cast<uint32_t>(-1), 0 };
  624. float32x4_t a0 = vmulq_f32( v0.mVec128, this->mVec128);
  625. float32x4_t a1 = vmulq_f32( v1.mVec128, this->mVec128);
  626. float32x4_t a2 = vmulq_f32( v2.mVec128, this->mVec128);
  627. float32x2x2_t zLo = vtrn_f32( vget_high_f32(a0), vget_high_f32(a1));
  628. a2 = (float32x4_t) vandq_u32((uint32x4_t) a2, xyzMask );
  629. float32x2_t b0 = vadd_f32( vpadd_f32( vget_low_f32(a0), vget_low_f32(a1)), zLo.val[0] );
  630. float32x2_t b1 = vpadd_f32( vpadd_f32( vget_low_f32(a2), vget_high_f32(a2)), vdup_n_f32(0.0f));
  631. return btVector3( vcombine_f32(b0, b1) );
  632. #else
  633. return btVector3( dot(v0), dot(v1), dot(v2));
  634. #endif
  635. }
  636. };
  637. /**@brief Return the sum of two vectors (Point symantics)*/
  638. SIMD_FORCE_INLINE btVector3
  639. operator+(const btVector3& v1, const btVector3& v2)
  640. {
  641. #if defined(BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  642. return btVector3(_mm_add_ps(v1.mVec128, v2.mVec128));
  643. #elif defined(BT_USE_NEON)
  644. return btVector3(vaddq_f32(v1.mVec128, v2.mVec128));
  645. #else
  646. return btVector3(
  647. v1.m_floats[0] + v2.m_floats[0],
  648. v1.m_floats[1] + v2.m_floats[1],
  649. v1.m_floats[2] + v2.m_floats[2]);
  650. #endif
  651. }
  652. /**@brief Return the elementwise product of two vectors */
  653. SIMD_FORCE_INLINE btVector3
  654. operator*(const btVector3& v1, const btVector3& v2)
  655. {
  656. #if defined(BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  657. return btVector3(_mm_mul_ps(v1.mVec128, v2.mVec128));
  658. #elif defined(BT_USE_NEON)
  659. return btVector3(vmulq_f32(v1.mVec128, v2.mVec128));
  660. #else
  661. return btVector3(
  662. v1.m_floats[0] * v2.m_floats[0],
  663. v1.m_floats[1] * v2.m_floats[1],
  664. v1.m_floats[2] * v2.m_floats[2]);
  665. #endif
  666. }
  667. /**@brief Return the difference between two vectors */
  668. SIMD_FORCE_INLINE btVector3
  669. operator-(const btVector3& v1, const btVector3& v2)
  670. {
  671. #if defined BT_USE_SIMD_VECTOR3 && (defined(BT_USE_SSE_IN_API) && defined(BT_USE_SSE))
  672. // without _mm_and_ps this code causes slowdown in Concave moving
  673. __m128 r = _mm_sub_ps(v1.mVec128, v2.mVec128);
  674. return btVector3(_mm_and_ps(r, btvFFF0fMask));
  675. #elif defined(BT_USE_NEON)
  676. float32x4_t r = vsubq_f32(v1.mVec128, v2.mVec128);
  677. return btVector3((float32x4_t)vandq_s32((int32x4_t)r, btvFFF0Mask));
  678. #else
  679. return btVector3(
  680. v1.m_floats[0] - v2.m_floats[0],
  681. v1.m_floats[1] - v2.m_floats[1],
  682. v1.m_floats[2] - v2.m_floats[2]);
  683. #endif
  684. }
  685. /**@brief Return the negative of the vector */
  686. SIMD_FORCE_INLINE btVector3
  687. operator-(const btVector3& v)
  688. {
  689. #if defined BT_USE_SIMD_VECTOR3 && (defined(BT_USE_SSE_IN_API) && defined (BT_USE_SSE))
  690. __m128 r = _mm_xor_ps(v.mVec128, btvMzeroMask);
  691. return btVector3(_mm_and_ps(r, btvFFF0fMask));
  692. #elif defined(BT_USE_NEON)
  693. return btVector3((btSimdFloat4)veorq_s32((int32x4_t)v.mVec128, (int32x4_t)btvMzeroMask));
  694. #else
  695. return btVector3(-v.m_floats[0], -v.m_floats[1], -v.m_floats[2]);
  696. #endif
  697. }
  698. /**@brief Return the vector scaled by s */
  699. SIMD_FORCE_INLINE btVector3
  700. operator*(const btVector3& v, const btScalar& s)
  701. {
  702. #if defined(BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  703. __m128 vs = _mm_load_ss(&s); // (S 0 0 0)
  704. vs = bt_pshufd_ps(vs, 0x80); // (S S S 0.0)
  705. return btVector3(_mm_mul_ps(v.mVec128, vs));
  706. #elif defined(BT_USE_NEON)
  707. float32x4_t r = vmulq_n_f32(v.mVec128, s);
  708. return btVector3((float32x4_t)vandq_s32((int32x4_t)r, btvFFF0Mask));
  709. #else
  710. return btVector3(v.m_floats[0] * s, v.m_floats[1] * s, v.m_floats[2] * s);
  711. #endif
  712. }
  713. /**@brief Return the vector scaled by s */
  714. SIMD_FORCE_INLINE btVector3
  715. operator*(const btScalar& s, const btVector3& v)
  716. {
  717. return v * s;
  718. }
  719. /**@brief Return the vector inversely scaled by s */
  720. SIMD_FORCE_INLINE btVector3
  721. operator/(const btVector3& v, const btScalar& s)
  722. {
  723. btFullAssert(s != btScalar(0.0));
  724. #if 0 //defined(BT_USE_SSE_IN_API)
  725. // this code is not faster !
  726. __m128 vs = _mm_load_ss(&s);
  727. vs = _mm_div_ss(v1110, vs);
  728. vs = bt_pshufd_ps(vs, 0x00); // (S S S S)
  729. return btVector3(_mm_mul_ps(v.mVec128, vs));
  730. #else
  731. return v * (btScalar(1.0) / s);
  732. #endif
  733. }
  734. /**@brief Return the vector inversely scaled by s */
  735. SIMD_FORCE_INLINE btVector3
  736. operator/(const btVector3& v1, const btVector3& v2)
  737. {
  738. #if defined BT_USE_SIMD_VECTOR3 && (defined(BT_USE_SSE_IN_API)&& defined (BT_USE_SSE))
  739. __m128 vec = _mm_div_ps(v1.mVec128, v2.mVec128);
  740. vec = _mm_and_ps(vec, btvFFF0fMask);
  741. return btVector3(vec);
  742. #elif defined(BT_USE_NEON)
  743. float32x4_t x, y, v, m;
  744. x = v1.mVec128;
  745. y = v2.mVec128;
  746. v = vrecpeq_f32(y); // v ~ 1/y
  747. m = vrecpsq_f32(y, v); // m = (2-v*y)
  748. v = vmulq_f32(v, m); // vv = v*m ~~ 1/y
  749. m = vrecpsq_f32(y, v); // mm = (2-vv*y)
  750. v = vmulq_f32(v, x); // x*vv
  751. v = vmulq_f32(v, m); // (x*vv)*(2-vv*y) = x*(vv(2-vv*y)) ~~~ x/y
  752. return btVector3(v);
  753. #else
  754. return btVector3(
  755. v1.m_floats[0] / v2.m_floats[0],
  756. v1.m_floats[1] / v2.m_floats[1],
  757. v1.m_floats[2] / v2.m_floats[2]);
  758. #endif
  759. }
  760. /**@brief Return the dot product between two vectors */
  761. SIMD_FORCE_INLINE btScalar
  762. btDot(const btVector3& v1, const btVector3& v2)
  763. {
  764. return v1.dot(v2);
  765. }
  766. /**@brief Return the distance squared between two vectors */
  767. SIMD_FORCE_INLINE btScalar
  768. btDistance2(const btVector3& v1, const btVector3& v2)
  769. {
  770. return v1.distance2(v2);
  771. }
  772. /**@brief Return the distance between two vectors */
  773. SIMD_FORCE_INLINE btScalar
  774. btDistance(const btVector3& v1, const btVector3& v2)
  775. {
  776. return v1.distance(v2);
  777. }
  778. /**@brief Return the angle between two vectors */
  779. SIMD_FORCE_INLINE btScalar
  780. btAngle(const btVector3& v1, const btVector3& v2)
  781. {
  782. return v1.angle(v2);
  783. }
  784. /**@brief Return the cross product of two vectors */
  785. SIMD_FORCE_INLINE btVector3
  786. btCross(const btVector3& v1, const btVector3& v2)
  787. {
  788. return v1.cross(v2);
  789. }
  790. SIMD_FORCE_INLINE btScalar
  791. btTriple(const btVector3& v1, const btVector3& v2, const btVector3& v3)
  792. {
  793. return v1.triple(v2, v3);
  794. }
  795. /**@brief Return the linear interpolation between two vectors
  796. * @param v1 One vector
  797. * @param v2 The other vector
  798. * @param t The ration of this to v (t = 0 => return v1, t=1 => return v2) */
  799. SIMD_FORCE_INLINE btVector3
  800. lerp(const btVector3& v1, const btVector3& v2, const btScalar& t)
  801. {
  802. return v1.lerp(v2, t);
  803. }
  804. SIMD_FORCE_INLINE btScalar btVector3::distance2(const btVector3& v) const
  805. {
  806. return (v - *this).length2();
  807. }
  808. SIMD_FORCE_INLINE btScalar btVector3::distance(const btVector3& v) const
  809. {
  810. return (v - *this).length();
  811. }
  812. SIMD_FORCE_INLINE btVector3 btVector3::normalized() const
  813. {
  814. btVector3 norm = *this;
  815. return norm.normalize();
  816. }
  817. SIMD_FORCE_INLINE btVector3 btVector3::rotate( const btVector3& wAxis, const btScalar _angle ) const
  818. {
  819. // wAxis must be a unit lenght vector
  820. #if defined BT_USE_SIMD_VECTOR3 && defined (BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  821. __m128 O = _mm_mul_ps(wAxis.mVec128, mVec128);
  822. btScalar ssin = btSin( _angle );
  823. __m128 C = wAxis.cross( mVec128 ).mVec128;
  824. O = _mm_and_ps(O, btvFFF0fMask);
  825. btScalar scos = btCos( _angle );
  826. __m128 vsin = _mm_load_ss(&ssin); // (S 0 0 0)
  827. __m128 vcos = _mm_load_ss(&scos); // (S 0 0 0)
  828. __m128 Y = bt_pshufd_ps(O, 0xC9); // (Y Z X 0)
  829. __m128 Z = bt_pshufd_ps(O, 0xD2); // (Z X Y 0)
  830. O = _mm_add_ps(O, Y);
  831. vsin = bt_pshufd_ps(vsin, 0x80); // (S S S 0)
  832. O = _mm_add_ps(O, Z);
  833. vcos = bt_pshufd_ps(vcos, 0x80); // (S S S 0)
  834. vsin = vsin * C;
  835. O = O * wAxis.mVec128;
  836. __m128 X = mVec128 - O;
  837. O = O + vsin;
  838. vcos = vcos * X;
  839. O = O + vcos;
  840. return btVector3(O);
  841. #else
  842. btVector3 o = wAxis * wAxis.dot( *this );
  843. btVector3 _x = *this - o;
  844. btVector3 _y;
  845. _y = wAxis.cross( *this );
  846. return ( o + _x * btCos( _angle ) + _y * btSin( _angle ) );
  847. #endif
  848. }
  849. SIMD_FORCE_INLINE long btVector3::maxDot( const btVector3 *array, long array_count, btScalar &dotOut ) const
  850. {
  851. #if (defined BT_USE_SSE && defined BT_USE_SIMD_VECTOR3 && defined BT_USE_SSE_IN_API) || defined (BT_USE_NEON)
  852. #if defined _WIN32 || defined (BT_USE_SSE)
  853. const long scalar_cutoff = 10;
  854. long _maxdot_large( const float *array, const float *vec, unsigned long array_count, float *dotOut );
  855. #elif defined BT_USE_NEON
  856. const long scalar_cutoff = 4;
  857. extern long (*_maxdot_large)( const float *array, const float *vec, unsigned long array_count, float *dotOut );
  858. #endif
  859. if( array_count < scalar_cutoff )
  860. #endif
  861. {
  862. btScalar maxDot = -SIMD_INFINITY;
  863. int i = 0;
  864. int ptIndex = -1;
  865. for( i = 0; i < array_count; i++ )
  866. {
  867. btScalar dot = array[i].dot(*this);
  868. if( dot > maxDot )
  869. {
  870. maxDot = dot;
  871. ptIndex = i;
  872. }
  873. }
  874. dotOut = maxDot;
  875. return ptIndex;
  876. }
  877. #if (defined BT_USE_SSE && defined BT_USE_SIMD_VECTOR3 && defined BT_USE_SSE_IN_API) || defined (BT_USE_NEON)
  878. return _maxdot_large( (float*) array, (float*) &m_floats[0], array_count, &dotOut );
  879. #endif
  880. }
  881. SIMD_FORCE_INLINE long btVector3::minDot( const btVector3 *array, long array_count, btScalar &dotOut ) const
  882. {
  883. #if (defined BT_USE_SSE && defined BT_USE_SIMD_VECTOR3 && defined BT_USE_SSE_IN_API) || defined (BT_USE_NEON)
  884. #if defined BT_USE_SSE
  885. const long scalar_cutoff = 10;
  886. long _mindot_large( const float *array, const float *vec, unsigned long array_count, float *dotOut );
  887. #elif defined BT_USE_NEON
  888. const long scalar_cutoff = 4;
  889. extern long (*_mindot_large)( const float *array, const float *vec, unsigned long array_count, float *dotOut );
  890. #else
  891. #error unhandled arch!
  892. #endif
  893. if( array_count < scalar_cutoff )
  894. #endif
  895. {
  896. btScalar minDot = SIMD_INFINITY;
  897. int i = 0;
  898. int ptIndex = -1;
  899. for( i = 0; i < array_count; i++ )
  900. {
  901. btScalar dot = array[i].dot(*this);
  902. if( dot < minDot )
  903. {
  904. minDot = dot;
  905. ptIndex = i;
  906. }
  907. }
  908. dotOut = minDot;
  909. return ptIndex;
  910. }
  911. #if (defined BT_USE_SSE && defined BT_USE_SIMD_VECTOR3 && defined BT_USE_SSE_IN_API) || defined (BT_USE_NEON)
  912. return _mindot_large( (float*) array, (float*) &m_floats[0], array_count, &dotOut );
  913. #endif//BT_USE_SIMD_VECTOR3
  914. }
  915. class btVector4 : public btVector3
  916. {
  917. public:
  918. SIMD_FORCE_INLINE btVector4() {}
  919. SIMD_FORCE_INLINE btVector4(const btScalar& _x, const btScalar& _y, const btScalar& _z,const btScalar& _w)
  920. : btVector3(_x,_y,_z)
  921. {
  922. m_floats[3] = _w;
  923. }
  924. #if (defined (BT_USE_SSE_IN_API)&& defined (BT_USE_SSE)) || defined (BT_USE_NEON)
  925. SIMD_FORCE_INLINE btVector4(const btSimdFloat4 vec)
  926. {
  927. mVec128 = vec;
  928. }
  929. SIMD_FORCE_INLINE btVector4(const btVector3& rhs)
  930. {
  931. mVec128 = rhs.mVec128;
  932. }
  933. SIMD_FORCE_INLINE btVector4&
  934. operator=(const btVector4& v)
  935. {
  936. mVec128 = v.mVec128;
  937. return *this;
  938. }
  939. #endif // #if defined (BT_USE_SSE_IN_API) || defined (BT_USE_NEON)
  940. SIMD_FORCE_INLINE btVector4 absolute4() const
  941. {
  942. #if defined BT_USE_SIMD_VECTOR3 && defined(BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  943. return btVector4(_mm_and_ps(mVec128, btvAbsfMask));
  944. #elif defined(BT_USE_NEON)
  945. return btVector4(vabsq_f32(mVec128));
  946. #else
  947. return btVector4(
  948. btFabs(m_floats[0]),
  949. btFabs(m_floats[1]),
  950. btFabs(m_floats[2]),
  951. btFabs(m_floats[3]));
  952. #endif
  953. }
  954. btScalar getW() const { return m_floats[3];}
  955. SIMD_FORCE_INLINE int maxAxis4() const
  956. {
  957. int maxIndex = -1;
  958. btScalar maxVal = btScalar(-BT_LARGE_FLOAT);
  959. if (m_floats[0] > maxVal)
  960. {
  961. maxIndex = 0;
  962. maxVal = m_floats[0];
  963. }
  964. if (m_floats[1] > maxVal)
  965. {
  966. maxIndex = 1;
  967. maxVal = m_floats[1];
  968. }
  969. if (m_floats[2] > maxVal)
  970. {
  971. maxIndex = 2;
  972. maxVal =m_floats[2];
  973. }
  974. if (m_floats[3] > maxVal)
  975. {
  976. maxIndex = 3;
  977. maxVal = m_floats[3];
  978. }
  979. return maxIndex;
  980. }
  981. SIMD_FORCE_INLINE int minAxis4() const
  982. {
  983. int minIndex = -1;
  984. btScalar minVal = btScalar(BT_LARGE_FLOAT);
  985. if (m_floats[0] < minVal)
  986. {
  987. minIndex = 0;
  988. minVal = m_floats[0];
  989. }
  990. if (m_floats[1] < minVal)
  991. {
  992. minIndex = 1;
  993. minVal = m_floats[1];
  994. }
  995. if (m_floats[2] < minVal)
  996. {
  997. minIndex = 2;
  998. minVal =m_floats[2];
  999. }
  1000. if (m_floats[3] < minVal)
  1001. {
  1002. minIndex = 3;
  1003. minVal = m_floats[3];
  1004. }
  1005. return minIndex;
  1006. }
  1007. SIMD_FORCE_INLINE int closestAxis4() const
  1008. {
  1009. return absolute4().maxAxis4();
  1010. }
  1011. /**@brief Set x,y,z and zero w
  1012. * @param x Value of x
  1013. * @param y Value of y
  1014. * @param z Value of z
  1015. */
  1016. /* void getValue(btScalar *m) const
  1017. {
  1018. m[0] = m_floats[0];
  1019. m[1] = m_floats[1];
  1020. m[2] =m_floats[2];
  1021. }
  1022. */
  1023. /**@brief Set the values
  1024. * @param x Value of x
  1025. * @param y Value of y
  1026. * @param z Value of z
  1027. * @param w Value of w
  1028. */
  1029. SIMD_FORCE_INLINE void setValue(const btScalar& _x, const btScalar& _y, const btScalar& _z,const btScalar& _w)
  1030. {
  1031. m_floats[0]=_x;
  1032. m_floats[1]=_y;
  1033. m_floats[2]=_z;
  1034. m_floats[3]=_w;
  1035. }
  1036. };
  1037. ///btSwapVector3Endian swaps vector endianness, useful for network and cross-platform serialization
  1038. SIMD_FORCE_INLINE void btSwapScalarEndian(const btScalar& sourceVal, btScalar& destVal)
  1039. {
  1040. #ifdef BT_USE_DOUBLE_PRECISION
  1041. unsigned char* dest = (unsigned char*) &destVal;
  1042. unsigned char* src = (unsigned char*) &sourceVal;
  1043. dest[0] = src[7];
  1044. dest[1] = src[6];
  1045. dest[2] = src[5];
  1046. dest[3] = src[4];
  1047. dest[4] = src[3];
  1048. dest[5] = src[2];
  1049. dest[6] = src[1];
  1050. dest[7] = src[0];
  1051. #else
  1052. unsigned char* dest = (unsigned char*) &destVal;
  1053. unsigned char* src = (unsigned char*) &sourceVal;
  1054. dest[0] = src[3];
  1055. dest[1] = src[2];
  1056. dest[2] = src[1];
  1057. dest[3] = src[0];
  1058. #endif //BT_USE_DOUBLE_PRECISION
  1059. }
  1060. ///btSwapVector3Endian swaps vector endianness, useful for network and cross-platform serialization
  1061. SIMD_FORCE_INLINE void btSwapVector3Endian(const btVector3& sourceVec, btVector3& destVec)
  1062. {
  1063. for (int i=0;i<4;i++)
  1064. {
  1065. btSwapScalarEndian(sourceVec[i],destVec[i]);
  1066. }
  1067. }
  1068. ///btUnSwapVector3Endian swaps vector endianness, useful for network and cross-platform serialization
  1069. SIMD_FORCE_INLINE void btUnSwapVector3Endian(btVector3& vector)
  1070. {
  1071. btVector3 swappedVec;
  1072. for (int i=0;i<4;i++)
  1073. {
  1074. btSwapScalarEndian(vector[i],swappedVec[i]);
  1075. }
  1076. vector = swappedVec;
  1077. }
  1078. template <class T>
  1079. SIMD_FORCE_INLINE void btPlaneSpace1 (const T& n, T& p, T& q)
  1080. {
  1081. if (btFabs(n[2]) > SIMDSQRT12) {
  1082. // choose p in y-z plane
  1083. btScalar a = n[1]*n[1] + n[2]*n[2];
  1084. btScalar k = btRecipSqrt (a);
  1085. p[0] = 0;
  1086. p[1] = -n[2]*k;
  1087. p[2] = n[1]*k;
  1088. // set q = n x p
  1089. q[0] = a*k;
  1090. q[1] = -n[0]*p[2];
  1091. q[2] = n[0]*p[1];
  1092. }
  1093. else {
  1094. // choose p in x-y plane
  1095. btScalar a = n[0]*n[0] + n[1]*n[1];
  1096. btScalar k = btRecipSqrt (a);
  1097. p[0] = -n[1]*k;
  1098. p[1] = n[0]*k;
  1099. p[2] = 0;
  1100. // set q = n x p
  1101. q[0] = -n[2]*p[1];
  1102. q[1] = n[2]*p[0];
  1103. q[2] = a*k;
  1104. }
  1105. }
  1106. struct btVector3FloatData
  1107. {
  1108. float m_floats[4];
  1109. };
  1110. struct btVector3DoubleData
  1111. {
  1112. double m_floats[4];
  1113. };
  1114. SIMD_FORCE_INLINE void btVector3::serializeFloat(struct btVector3FloatData& dataOut) const
  1115. {
  1116. ///could also do a memcpy, check if it is worth it
  1117. for (int i=0;i<4;i++)
  1118. dataOut.m_floats[i] = float(m_floats[i]);
  1119. }
  1120. SIMD_FORCE_INLINE void btVector3::deSerializeFloat(const struct btVector3FloatData& dataIn)
  1121. {
  1122. for (int i=0;i<4;i++)
  1123. m_floats[i] = btScalar(dataIn.m_floats[i]);
  1124. }
  1125. SIMD_FORCE_INLINE void btVector3::serializeDouble(struct btVector3DoubleData& dataOut) const
  1126. {
  1127. ///could also do a memcpy, check if it is worth it
  1128. for (int i=0;i<4;i++)
  1129. dataOut.m_floats[i] = double(m_floats[i]);
  1130. }
  1131. SIMD_FORCE_INLINE void btVector3::deSerializeDouble(const struct btVector3DoubleData& dataIn)
  1132. {
  1133. for (int i=0;i<4;i++)
  1134. m_floats[i] = btScalar(dataIn.m_floats[i]);
  1135. }
  1136. SIMD_FORCE_INLINE void btVector3::serialize(struct btVector3Data& dataOut) const
  1137. {
  1138. ///could also do a memcpy, check if it is worth it
  1139. for (int i=0;i<4;i++)
  1140. dataOut.m_floats[i] = m_floats[i];
  1141. }
  1142. SIMD_FORCE_INLINE void btVector3::deSerialize(const struct btVector3Data& dataIn)
  1143. {
  1144. for (int i=0;i<4;i++)
  1145. m_floats[i] = dataIn.m_floats[i];
  1146. }
  1147. #endif //BT_VECTOR3_H