btScalar.h 24 KB

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