btScalar.h 24 KB

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