btScalar.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /*
  2. Copyright (c) 2003-2009 Erwin Coumans http://bullet.googlecode.com
  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_SCALAR_H
  13. #define BT_SCALAR_H
  14. #ifdef BT_MANAGED_CODE
  15. //Aligned data types not supported in managed code
  16. #pragma unmanaged
  17. #endif
  18. #include <math.h>
  19. #include <stdlib.h>//size_t for MSVC 6.0
  20. #include <float.h>
  21. /* SVN $Revision$ on $Date$ from http://bullet.googlecode.com*/
  22. #define BT_BULLET_VERSION 282
  23. inline int btGetVersion()
  24. {
  25. return BT_BULLET_VERSION;
  26. }
  27. #if defined(DEBUG) || defined (_DEBUG)
  28. #define BT_DEBUG
  29. #endif
  30. #ifdef _WIN32
  31. #if defined(__MINGW32__) || defined(__CYGWIN__) || (defined (_MSC_VER) && _MSC_VER < 1300)
  32. #define SIMD_FORCE_INLINE inline
  33. #define ATTRIBUTE_ALIGNED16(a) a
  34. #define ATTRIBUTE_ALIGNED64(a) a
  35. #define ATTRIBUTE_ALIGNED128(a) a
  36. #elif (_M_ARM)
  37. #define SIMD_FORCE_INLINE __forceinline
  38. #define ATTRIBUTE_ALIGNED16(a) __declspec() a
  39. #define ATTRIBUTE_ALIGNED64(a) __declspec() a
  40. #define ATTRIBUTE_ALIGNED128(a) __declspec () a
  41. #else
  42. //#define BT_HAS_ALIGNED_ALLOCATOR
  43. #pragma warning(disable : 4324) // disable padding warning
  44. // #pragma warning(disable:4530) // Disable the exception disable but used in MSCV Stl warning.
  45. // #pragma warning(disable:4996) //Turn off warnings about deprecated C routines
  46. // #pragma warning(disable:4786) // Disable the "debug name too long" warning
  47. #define SIMD_FORCE_INLINE __forceinline
  48. #define ATTRIBUTE_ALIGNED16(a) __declspec(align(16)) a
  49. #define ATTRIBUTE_ALIGNED64(a) __declspec(align(64)) a
  50. #define ATTRIBUTE_ALIGNED128(a) __declspec (align(128)) a
  51. #ifdef _XBOX
  52. #define BT_USE_VMX128
  53. #include <ppcintrinsics.h>
  54. #define BT_HAVE_NATIVE_FSEL
  55. #define btFsel(a,b,c) __fsel((a),(b),(c))
  56. #else
  57. #if (defined (_WIN32) && (_MSC_VER) && _MSC_VER >= 1400) && (!defined (BT_USE_DOUBLE_PRECISION))
  58. #if _MSC_VER>1400
  59. #define BT_USE_SIMD_VECTOR3
  60. #endif
  61. #define BT_USE_SSE
  62. #ifdef BT_USE_SSE
  63. //BT_USE_SSE_IN_API is disabled under Windows by default, because
  64. //it makes it harder to integrate Bullet into your application under Windows
  65. //(structured embedding Bullet structs/classes need to be 16-byte aligned)
  66. //with relatively little performance gain
  67. //If you are not embedded Bullet data in your classes, or make sure that you align those classes on 16-byte boundaries
  68. //you can manually enable this line or set it in the build system for a bit of performance gain (a few percent, dependent on usage)
  69. //#define BT_USE_SSE_IN_API
  70. #endif //BT_USE_SSE
  71. #include <emmintrin.h>
  72. #endif
  73. #endif//_XBOX
  74. #endif //__MINGW32__
  75. #ifdef BT_DEBUG
  76. #ifdef _MSC_VER
  77. #include <stdio.h>
  78. #define btAssert(x) { if(!(x)){printf("Assert "__FILE__ ":%u ("#x")\n", __LINE__);__debugbreak(); }}
  79. #else//_MSC_VER
  80. #include <assert.h>
  81. #define btAssert assert
  82. #endif//_MSC_VER
  83. #else
  84. #define btAssert(x)
  85. #endif
  86. //btFullAssert is optional, slows down a lot
  87. #define btFullAssert(x)
  88. #define btLikely(_c) _c
  89. #define btUnlikely(_c) _c
  90. #else
  91. #if defined (__CELLOS_LV2__)
  92. #define SIMD_FORCE_INLINE inline __attribute__((always_inline))
  93. #define ATTRIBUTE_ALIGNED16(a) a __attribute__ ((aligned (16)))
  94. #define ATTRIBUTE_ALIGNED64(a) a __attribute__ ((aligned (64)))
  95. #define ATTRIBUTE_ALIGNED128(a) a __attribute__ ((aligned (128)))
  96. #ifndef assert
  97. #include <assert.h>
  98. #endif
  99. #ifdef BT_DEBUG
  100. #ifdef __SPU__
  101. #include <spu_printf.h>
  102. #define printf spu_printf
  103. #define btAssert(x) {if(!(x)){printf("Assert "__FILE__ ":%u ("#x")\n", __LINE__);spu_hcmpeq(0,0);}}
  104. #else
  105. #define btAssert assert
  106. #endif
  107. #else
  108. #define btAssert(x)
  109. #endif
  110. //btFullAssert is optional, slows down a lot
  111. #define btFullAssert(x)
  112. #define btLikely(_c) _c
  113. #define btUnlikely(_c) _c
  114. #else
  115. #ifdef USE_LIBSPE2
  116. #define SIMD_FORCE_INLINE __inline
  117. #define ATTRIBUTE_ALIGNED16(a) a __attribute__ ((aligned (16)))
  118. #define ATTRIBUTE_ALIGNED64(a) a __attribute__ ((aligned (64)))
  119. #define ATTRIBUTE_ALIGNED128(a) a __attribute__ ((aligned (128)))
  120. #ifndef assert
  121. #include <assert.h>
  122. #endif
  123. #ifdef BT_DEBUG
  124. #define btAssert assert
  125. #else
  126. #define btAssert(x)
  127. #endif
  128. //btFullAssert is optional, slows down a lot
  129. #define btFullAssert(x)
  130. #define btLikely(_c) __builtin_expect((_c), 1)
  131. #define btUnlikely(_c) __builtin_expect((_c), 0)
  132. #else
  133. //non-windows systems
  134. #if (defined (__APPLE__) && (!defined (BT_USE_DOUBLE_PRECISION)))
  135. #if defined (__i386__) || defined (__x86_64__)
  136. #define BT_USE_SIMD_VECTOR3
  137. #define BT_USE_SSE
  138. //BT_USE_SSE_IN_API is enabled on Mac OSX by default, because memory is automatically aligned on 16-byte boundaries
  139. //if apps run into issues, we will disable the next line
  140. #define BT_USE_SSE_IN_API
  141. #ifdef BT_USE_SSE
  142. // include appropriate SSE level
  143. #if defined (__SSE4_1__)
  144. #include <smmintrin.h>
  145. #elif defined (__SSSE3__)
  146. #include <tmmintrin.h>
  147. #elif defined (__SSE3__)
  148. #include <pmmintrin.h>
  149. #else
  150. #include <emmintrin.h>
  151. #endif
  152. #endif //BT_USE_SSE
  153. #elif defined( __ARM_NEON__ ) && (!defined( __arm64__ ))
  154. #ifdef __clang__
  155. #define BT_USE_NEON 1
  156. #define BT_USE_SIMD_VECTOR3
  157. #if defined BT_USE_NEON && defined (__clang__)
  158. #include <arm_neon.h>
  159. #endif//BT_USE_NEON
  160. #endif //__clang__
  161. #endif//__arm__
  162. #define SIMD_FORCE_INLINE inline __attribute__ ((always_inline))
  163. ///@todo: check out alignment methods for other platforms/compilers
  164. #define ATTRIBUTE_ALIGNED16(a) a __attribute__ ((aligned (16)))
  165. #define ATTRIBUTE_ALIGNED64(a) a __attribute__ ((aligned (64)))
  166. #define ATTRIBUTE_ALIGNED128(a) a __attribute__ ((aligned (128)))
  167. #ifndef assert
  168. #include <assert.h>
  169. #endif
  170. #if defined(DEBUG) || defined (_DEBUG)
  171. #if defined (__i386__) || defined (__x86_64__)
  172. #include <stdio.h>
  173. #define btAssert(x)\
  174. {\
  175. if(!(x))\
  176. {\
  177. printf("Assert %s in line %d, file %s\n",#x, __LINE__, __FILE__);\
  178. asm volatile ("int3");\
  179. }\
  180. }
  181. #else//defined (__i386__) || defined (__x86_64__)
  182. #define btAssert assert
  183. #endif//defined (__i386__) || defined (__x86_64__)
  184. #else//defined(DEBUG) || defined (_DEBUG)
  185. #define btAssert(x)
  186. #endif//defined(DEBUG) || defined (_DEBUG)
  187. //btFullAssert is optional, slows down a lot
  188. #define btFullAssert(x)
  189. #define btLikely(_c) _c
  190. #define btUnlikely(_c) _c
  191. #else
  192. #define SIMD_FORCE_INLINE inline
  193. ///@todo: check out alignment methods for other platforms/compilers
  194. ///#define ATTRIBUTE_ALIGNED16(a) a __attribute__ ((aligned (16)))
  195. ///#define ATTRIBUTE_ALIGNED64(a) a __attribute__ ((aligned (64)))
  196. ///#define ATTRIBUTE_ALIGNED128(a) a __attribute__ ((aligned (128)))
  197. #define ATTRIBUTE_ALIGNED16(a) a
  198. #define ATTRIBUTE_ALIGNED64(a) a
  199. #define ATTRIBUTE_ALIGNED128(a) a
  200. #ifndef assert
  201. #include <assert.h>
  202. #endif
  203. #if defined(DEBUG) || defined (_DEBUG)
  204. #define btAssert assert
  205. #else
  206. #define btAssert(x)
  207. #endif
  208. //btFullAssert is optional, slows down a lot
  209. #define btFullAssert(x)
  210. #define btLikely(_c) _c
  211. #define btUnlikely(_c) _c
  212. #endif //__APPLE__
  213. #endif // LIBSPE2
  214. #endif //__CELLOS_LV2__
  215. #endif
  216. ///The btScalar type abstracts floating point numbers, to easily switch between double and single floating point precision.
  217. #if defined(BT_USE_DOUBLE_PRECISION)
  218. typedef double btScalar;
  219. //this number could be bigger in double precision
  220. #define BT_LARGE_FLOAT 1e30
  221. #else
  222. typedef float btScalar;
  223. //keep BT_LARGE_FLOAT*BT_LARGE_FLOAT < FLT_MAX
  224. #define BT_LARGE_FLOAT 1e18f
  225. #endif
  226. #ifdef BT_USE_SSE
  227. typedef __m128 btSimdFloat4;
  228. #endif//BT_USE_SSE
  229. #if defined (BT_USE_SSE)
  230. //#if defined BT_USE_SSE_IN_API && defined (BT_USE_SSE)
  231. #ifdef _WIN32
  232. #ifndef BT_NAN
  233. static int btNanMask = 0x7F800001;
  234. #define BT_NAN (*(float*)&btNanMask)
  235. #endif
  236. #ifndef BT_INFINITY
  237. static int btInfinityMask = 0x7F800000;
  238. #define BT_INFINITY (*(float*)&btInfinityMask)
  239. #endif
  240. //use this, in case there are clashes (such as xnamath.h)
  241. #ifndef BT_NO_SIMD_OPERATOR_OVERLOADS
  242. inline __m128 operator + (const __m128 A, const __m128 B)
  243. {
  244. return _mm_add_ps(A, B);
  245. }
  246. inline __m128 operator - (const __m128 A, const __m128 B)
  247. {
  248. return _mm_sub_ps(A, B);
  249. }
  250. inline __m128 operator * (const __m128 A, const __m128 B)
  251. {
  252. return _mm_mul_ps(A, B);
  253. }
  254. #endif //BT_NO_SIMD_OPERATOR_OVERLOADS
  255. #define btCastfTo128i(a) (_mm_castps_si128(a))
  256. #define btCastfTo128d(a) (_mm_castps_pd(a))
  257. #define btCastiTo128f(a) (_mm_castsi128_ps(a))
  258. #define btCastdTo128f(a) (_mm_castpd_ps(a))
  259. #define btCastdTo128i(a) (_mm_castpd_si128(a))
  260. #define btAssign128(r0,r1,r2,r3) _mm_setr_ps(r0,r1,r2,r3)
  261. #else//_WIN32
  262. #define btCastfTo128i(a) ((__m128i)(a))
  263. #define btCastfTo128d(a) ((__m128d)(a))
  264. #define btCastiTo128f(a) ((__m128) (a))
  265. #define btCastdTo128f(a) ((__m128) (a))
  266. #define btCastdTo128i(a) ((__m128i)(a))
  267. #define btAssign128(r0,r1,r2,r3) (__m128){r0,r1,r2,r3}
  268. #define BT_INFINITY INFINITY
  269. #define BT_NAN NAN
  270. #endif//_WIN32
  271. #else
  272. #ifdef BT_USE_NEON
  273. #include <arm_neon.h>
  274. typedef float32x4_t btSimdFloat4;
  275. #define BT_INFINITY INFINITY
  276. #define BT_NAN NAN
  277. #define btAssign128(r0,r1,r2,r3) (float32x4_t){r0,r1,r2,r3}
  278. #else//BT_USE_NEON
  279. #ifndef BT_INFINITY
  280. static int btInfinityMask = 0x7F800000;
  281. #define BT_INFINITY (*(float*)&btInfinityMask)
  282. #endif
  283. #endif//BT_USE_NEON
  284. #endif //BT_USE_SSE
  285. #ifdef BT_USE_NEON
  286. #include <arm_neon.h>
  287. typedef float32x4_t btSimdFloat4;
  288. #define BT_INFINITY INFINITY
  289. #define BT_NAN NAN
  290. #define btAssign128(r0,r1,r2,r3) (float32x4_t){r0,r1,r2,r3}
  291. #endif
  292. #define BT_DECLARE_ALIGNED_ALLOCATOR() \
  293. SIMD_FORCE_INLINE void* operator new(size_t sizeInBytes) { return btAlignedAlloc(sizeInBytes,16); } \
  294. SIMD_FORCE_INLINE void operator delete(void* ptr) { btAlignedFree(ptr); } \
  295. SIMD_FORCE_INLINE void* operator new(size_t, void* ptr) { return ptr; } \
  296. SIMD_FORCE_INLINE void operator delete(void*, void*) { } \
  297. SIMD_FORCE_INLINE void* operator new[](size_t sizeInBytes) { return btAlignedAlloc(sizeInBytes,16); } \
  298. SIMD_FORCE_INLINE void operator delete[](void* ptr) { btAlignedFree(ptr); } \
  299. SIMD_FORCE_INLINE void* operator new[](size_t, void* ptr) { return ptr; } \
  300. SIMD_FORCE_INLINE void operator delete[](void*, void*) { } \
  301. #if defined(BT_USE_DOUBLE_PRECISION) || defined(BT_FORCE_DOUBLE_FUNCTIONS)
  302. SIMD_FORCE_INLINE btScalar btSqrt(btScalar x) { return sqrt(x); }
  303. SIMD_FORCE_INLINE btScalar btFabs(btScalar x) { return fabs(x); }
  304. SIMD_FORCE_INLINE btScalar btCos(btScalar x) { return cos(x); }
  305. SIMD_FORCE_INLINE btScalar btSin(btScalar x) { return sin(x); }
  306. SIMD_FORCE_INLINE btScalar btTan(btScalar x) { return tan(x); }
  307. SIMD_FORCE_INLINE btScalar btAcos(btScalar x) { if (x<btScalar(-1)) x=btScalar(-1); if (x>btScalar(1)) x=btScalar(1); return acos(x); }
  308. SIMD_FORCE_INLINE btScalar btAsin(btScalar x) { if (x<btScalar(-1)) x=btScalar(-1); if (x>btScalar(1)) x=btScalar(1); return asin(x); }
  309. SIMD_FORCE_INLINE btScalar btAtan(btScalar x) { return atan(x); }
  310. SIMD_FORCE_INLINE btScalar btAtan2(btScalar x, btScalar y) { return atan2(x, y); }
  311. SIMD_FORCE_INLINE btScalar btExp(btScalar x) { return exp(x); }
  312. SIMD_FORCE_INLINE btScalar btLog(btScalar x) { return log(x); }
  313. SIMD_FORCE_INLINE btScalar btPow(btScalar x,btScalar y) { return pow(x,y); }
  314. SIMD_FORCE_INLINE btScalar btFmod(btScalar x,btScalar y) { return fmod(x,y); }
  315. #else
  316. SIMD_FORCE_INLINE btScalar btSqrt(btScalar y)
  317. {
  318. #ifdef USE_APPROXIMATION
  319. double x, z, tempf;
  320. unsigned long *tfptr = ((unsigned long *)&tempf) + 1;
  321. tempf = y;
  322. *tfptr = (0xbfcdd90a - *tfptr)>>1; /* estimate of 1/sqrt(y) */
  323. x = tempf;
  324. z = y*btScalar(0.5);
  325. x = (btScalar(1.5)*x)-(x*x)*(x*z); /* iteration formula */
  326. x = (btScalar(1.5)*x)-(x*x)*(x*z);
  327. x = (btScalar(1.5)*x)-(x*x)*(x*z);
  328. x = (btScalar(1.5)*x)-(x*x)*(x*z);
  329. x = (btScalar(1.5)*x)-(x*x)*(x*z);
  330. return x*y;
  331. #else
  332. return sqrtf(y);
  333. #endif
  334. }
  335. SIMD_FORCE_INLINE btScalar btFabs(btScalar x) { return fabsf(x); }
  336. SIMD_FORCE_INLINE btScalar btCos(btScalar x) { return cosf(x); }
  337. SIMD_FORCE_INLINE btScalar btSin(btScalar x) { return sinf(x); }
  338. SIMD_FORCE_INLINE btScalar btTan(btScalar x) { return tanf(x); }
  339. SIMD_FORCE_INLINE btScalar btAcos(btScalar x) {
  340. if (x<btScalar(-1))
  341. x=btScalar(-1);
  342. if (x>btScalar(1))
  343. x=btScalar(1);
  344. return acosf(x);
  345. }
  346. SIMD_FORCE_INLINE btScalar btAsin(btScalar x) {
  347. if (x<btScalar(-1))
  348. x=btScalar(-1);
  349. if (x>btScalar(1))
  350. x=btScalar(1);
  351. return asinf(x);
  352. }
  353. SIMD_FORCE_INLINE btScalar btAtan(btScalar x) { return atanf(x); }
  354. SIMD_FORCE_INLINE btScalar btAtan2(btScalar x, btScalar y) { return atan2f(x, y); }
  355. SIMD_FORCE_INLINE btScalar btExp(btScalar x) { return expf(x); }
  356. SIMD_FORCE_INLINE btScalar btLog(btScalar x) { return logf(x); }
  357. SIMD_FORCE_INLINE btScalar btPow(btScalar x,btScalar y) { return powf(x,y); }
  358. SIMD_FORCE_INLINE btScalar btFmod(btScalar x,btScalar y) { return fmodf(x,y); }
  359. #endif
  360. #define SIMD_PI btScalar(3.1415926535897932384626433832795029)
  361. #define SIMD_2_PI btScalar(2.0) * SIMD_PI
  362. #define SIMD_HALF_PI (SIMD_PI * btScalar(0.5))
  363. #define SIMD_RADS_PER_DEG (SIMD_2_PI / btScalar(360.0))
  364. #define SIMD_DEGS_PER_RAD (btScalar(360.0) / SIMD_2_PI)
  365. #define SIMDSQRT12 btScalar(0.7071067811865475244008443621048490)
  366. #define btRecipSqrt(x) ((btScalar)(btScalar(1.0)/btSqrt(btScalar(x)))) /* reciprocal square root */
  367. #define btRecip(x) (btScalar(1.0)/btScalar(x))
  368. #ifdef BT_USE_DOUBLE_PRECISION
  369. #define SIMD_EPSILON DBL_EPSILON
  370. #define SIMD_INFINITY DBL_MAX
  371. #else
  372. #define SIMD_EPSILON FLT_EPSILON
  373. #define SIMD_INFINITY FLT_MAX
  374. #endif
  375. SIMD_FORCE_INLINE btScalar btAtan2Fast(btScalar y, btScalar x)
  376. {
  377. btScalar coeff_1 = SIMD_PI / 4.0f;
  378. btScalar coeff_2 = 3.0f * coeff_1;
  379. btScalar abs_y = btFabs(y);
  380. btScalar angle;
  381. if (x >= 0.0f) {
  382. btScalar r = (x - abs_y) / (x + abs_y);
  383. angle = coeff_1 - coeff_1 * r;
  384. } else {
  385. btScalar r = (x + abs_y) / (abs_y - x);
  386. angle = coeff_2 - coeff_1 * r;
  387. }
  388. return (y < 0.0f) ? -angle : angle;
  389. }
  390. SIMD_FORCE_INLINE bool btFuzzyZero(btScalar x) { return btFabs(x) < SIMD_EPSILON; }
  391. SIMD_FORCE_INLINE bool btEqual(btScalar a, btScalar eps) {
  392. return (((a) <= eps) && !((a) < -eps));
  393. }
  394. SIMD_FORCE_INLINE bool btGreaterEqual (btScalar a, btScalar eps) {
  395. return (!((a) <= eps));
  396. }
  397. SIMD_FORCE_INLINE int btIsNegative(btScalar x) {
  398. return x < btScalar(0.0) ? 1 : 0;
  399. }
  400. SIMD_FORCE_INLINE btScalar btRadians(btScalar x) { return x * SIMD_RADS_PER_DEG; }
  401. SIMD_FORCE_INLINE btScalar btDegrees(btScalar x) { return x * SIMD_DEGS_PER_RAD; }
  402. #define BT_DECLARE_HANDLE(name) typedef struct name##__ { int unused; } *name
  403. #ifndef btFsel
  404. SIMD_FORCE_INLINE btScalar btFsel(btScalar a, btScalar b, btScalar c)
  405. {
  406. return a >= 0 ? b : c;
  407. }
  408. #endif
  409. #define btFsels(a,b,c) (btScalar)btFsel(a,b,c)
  410. SIMD_FORCE_INLINE bool btMachineIsLittleEndian()
  411. {
  412. long int i = 1;
  413. const char *p = (const char *) &i;
  414. if (p[0] == 1) // Lowest address contains the least significant byte
  415. return true;
  416. else
  417. return false;
  418. }
  419. ///btSelect avoids branches, which makes performance much better for consoles like Playstation 3 and XBox 360
  420. ///Thanks Phil Knight. See also http://www.cellperformance.com/articles/2006/04/more_techniques_for_eliminatin_1.html
  421. SIMD_FORCE_INLINE unsigned btSelect(unsigned condition, unsigned valueIfConditionNonZero, unsigned valueIfConditionZero)
  422. {
  423. // Set testNz to 0xFFFFFFFF if condition is nonzero, 0x00000000 if condition is zero
  424. // Rely on positive value or'ed with its negative having sign bit on
  425. // and zero value or'ed with its negative (which is still zero) having sign bit off
  426. // Use arithmetic shift right, shifting the sign bit through all 32 bits
  427. unsigned testNz = (unsigned)(((int)condition | -(int)condition) >> 31);
  428. unsigned testEqz = ~testNz;
  429. return ((valueIfConditionNonZero & testNz) | (valueIfConditionZero & testEqz));
  430. }
  431. SIMD_FORCE_INLINE int btSelect(unsigned condition, int valueIfConditionNonZero, int valueIfConditionZero)
  432. {
  433. unsigned testNz = (unsigned)(((int)condition | -(int)condition) >> 31);
  434. unsigned testEqz = ~testNz;
  435. return static_cast<int>((valueIfConditionNonZero & testNz) | (valueIfConditionZero & testEqz));
  436. }
  437. SIMD_FORCE_INLINE float btSelect(unsigned condition, float valueIfConditionNonZero, float valueIfConditionZero)
  438. {
  439. #ifdef BT_HAVE_NATIVE_FSEL
  440. return (float)btFsel((btScalar)condition - btScalar(1.0f), valueIfConditionNonZero, valueIfConditionZero);
  441. #else
  442. return (condition != 0) ? valueIfConditionNonZero : valueIfConditionZero;
  443. #endif
  444. }
  445. template<typename T> SIMD_FORCE_INLINE void btSwap(T& a, T& b)
  446. {
  447. T tmp = a;
  448. a = b;
  449. b = tmp;
  450. }
  451. //PCK: endian swapping functions
  452. SIMD_FORCE_INLINE unsigned btSwapEndian(unsigned val)
  453. {
  454. return (((val & 0xff000000) >> 24) | ((val & 0x00ff0000) >> 8) | ((val & 0x0000ff00) << 8) | ((val & 0x000000ff) << 24));
  455. }
  456. SIMD_FORCE_INLINE unsigned short btSwapEndian(unsigned short val)
  457. {
  458. return static_cast<unsigned short>(((val & 0xff00) >> 8) | ((val & 0x00ff) << 8));
  459. }
  460. SIMD_FORCE_INLINE unsigned btSwapEndian(int val)
  461. {
  462. return btSwapEndian((unsigned)val);
  463. }
  464. SIMD_FORCE_INLINE unsigned short btSwapEndian(short val)
  465. {
  466. return btSwapEndian((unsigned short) val);
  467. }
  468. ///btSwapFloat uses using char pointers to swap the endianness
  469. ////btSwapFloat/btSwapDouble will NOT return a float, because the machine might 'correct' invalid floating point values
  470. ///Not all values of sign/exponent/mantissa are valid floating point numbers according to IEEE 754.
  471. ///When a floating point unit is faced with an invalid value, it may actually change the value, or worse, throw an exception.
  472. ///In most systems, running user mode code, you wouldn't get an exception, but instead the hardware/os/runtime will 'fix' the number for you.
  473. ///so instead of returning a float/double, we return integer/long long integer
  474. SIMD_FORCE_INLINE unsigned int btSwapEndianFloat(float d)
  475. {
  476. unsigned int a = 0;
  477. unsigned char *dst = (unsigned char *)&a;
  478. unsigned char *src = (unsigned char *)&d;
  479. dst[0] = src[3];
  480. dst[1] = src[2];
  481. dst[2] = src[1];
  482. dst[3] = src[0];
  483. return a;
  484. }
  485. // unswap using char pointers
  486. SIMD_FORCE_INLINE float btUnswapEndianFloat(unsigned int a)
  487. {
  488. float d = 0.0f;
  489. unsigned char *src = (unsigned char *)&a;
  490. unsigned char *dst = (unsigned char *)&d;
  491. dst[0] = src[3];
  492. dst[1] = src[2];
  493. dst[2] = src[1];
  494. dst[3] = src[0];
  495. return d;
  496. }
  497. // swap using char pointers
  498. SIMD_FORCE_INLINE void btSwapEndianDouble(double d, unsigned char* dst)
  499. {
  500. unsigned char *src = (unsigned char *)&d;
  501. dst[0] = src[7];
  502. dst[1] = src[6];
  503. dst[2] = src[5];
  504. dst[3] = src[4];
  505. dst[4] = src[3];
  506. dst[5] = src[2];
  507. dst[6] = src[1];
  508. dst[7] = src[0];
  509. }
  510. // unswap using char pointers
  511. SIMD_FORCE_INLINE double btUnswapEndianDouble(const unsigned char *src)
  512. {
  513. double d = 0.0;
  514. unsigned char *dst = (unsigned char *)&d;
  515. dst[0] = src[7];
  516. dst[1] = src[6];
  517. dst[2] = src[5];
  518. dst[3] = src[4];
  519. dst[4] = src[3];
  520. dst[5] = src[2];
  521. dst[6] = src[1];
  522. dst[7] = src[0];
  523. return d;
  524. }
  525. template<typename T>
  526. SIMD_FORCE_INLINE void btSetZero(T* a, int n)
  527. {
  528. T* acurr = a;
  529. size_t ncurr = n;
  530. while (ncurr > 0)
  531. {
  532. *(acurr++) = 0;
  533. --ncurr;
  534. }
  535. }
  536. SIMD_FORCE_INLINE btScalar btLargeDot(const btScalar *a, const btScalar *b, int n)
  537. {
  538. btScalar p0,q0,m0,p1,q1,m1,sum;
  539. sum = 0;
  540. n -= 2;
  541. while (n >= 0) {
  542. p0 = a[0]; q0 = b[0];
  543. m0 = p0 * q0;
  544. p1 = a[1]; q1 = b[1];
  545. m1 = p1 * q1;
  546. sum += m0;
  547. sum += m1;
  548. a += 2;
  549. b += 2;
  550. n -= 2;
  551. }
  552. n += 2;
  553. while (n > 0) {
  554. sum += (*a) * (*b);
  555. a++;
  556. b++;
  557. n--;
  558. }
  559. return sum;
  560. }
  561. // returns normalized value in range [-SIMD_PI, SIMD_PI]
  562. SIMD_FORCE_INLINE btScalar btNormalizeAngle(btScalar angleInRadians)
  563. {
  564. angleInRadians = btFmod(angleInRadians, SIMD_2_PI);
  565. if(angleInRadians < -SIMD_PI)
  566. {
  567. return angleInRadians + SIMD_2_PI;
  568. }
  569. else if(angleInRadians > SIMD_PI)
  570. {
  571. return angleInRadians - SIMD_2_PI;
  572. }
  573. else
  574. {
  575. return angleInRadians;
  576. }
  577. }
  578. ///rudimentary class to provide type info
  579. struct btTypedObject
  580. {
  581. btTypedObject(int objectType)
  582. :m_objectType(objectType)
  583. {
  584. }
  585. int m_objectType;
  586. inline int getObjectType() const
  587. {
  588. return m_objectType;
  589. }
  590. };
  591. ///align a pointer to the provided alignment, upwards
  592. template <typename T>T* btAlignPointer(T* unalignedPtr, size_t alignment)
  593. {
  594. struct btConvertPointerSizeT
  595. {
  596. union
  597. {
  598. T* ptr;
  599. size_t integer;
  600. };
  601. };
  602. btConvertPointerSizeT converter;
  603. const size_t bit_mask = ~(alignment - 1);
  604. converter.ptr = unalignedPtr;
  605. converter.integer += alignment-1;
  606. converter.integer &= bit_mask;
  607. return converter.ptr;
  608. }
  609. #endif //BT_SCALAR_H