btScalar.h 24 KB

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