btScalar.h 22 KB

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